T_size_size.pass.cpp (6278B)
1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <string> 11 12 // template<class _Tp> 13 // basic_string(const _Tp& __t, size_type __pos, size_type __n, 14 // const allocator_type& __a = allocator_type()); 15 // 16 // Mostly we're testing string_view here 17 18 #include <string> 19 #include <string_view> 20 #include <stdexcept> 21 #include <algorithm> 22 #include <cassert> 23 24 #include "test_macros.h" 25 #include "test_allocator.h" 26 #include "min_allocator.h" 27 28 template <class S, class SV> 29 void 30 test(SV sv, std::size_t pos, std::size_t n) 31 { 32 typedef typename S::traits_type T; 33 typedef typename S::allocator_type A; 34 typedef typename S::size_type Size; 35 if (pos <= sv.size()) 36 { 37 S s2(sv, static_cast<Size>(pos), static_cast<Size>(n)); 38 LIBCPP_ASSERT(s2.__invariants()); 39 assert(pos <= sv.size()); 40 std::size_t rlen = std::min(sv.size() - pos, n); 41 assert(s2.size() == rlen); 42 assert(T::compare(s2.data(), sv.data() + pos, rlen) == 0); 43 assert(s2.get_allocator() == A()); 44 assert(s2.capacity() >= s2.size()); 45 } 46 #ifndef TEST_HAS_NO_EXCEPTIONS 47 else 48 { 49 try 50 { 51 S s2(sv, static_cast<Size>(pos), static_cast<Size>(n)); 52 assert(false); 53 } 54 catch (std::out_of_range&) 55 { 56 assert(pos > sv.size()); 57 } 58 } 59 #endif 60 } 61 62 template <class S, class SV> 63 void 64 test(SV sv, std::size_t pos, std::size_t n, const typename S::allocator_type& a) 65 { 66 typedef typename S::traits_type T; 67 typedef typename S::size_type Size; 68 if (pos <= sv.size()) 69 { 70 S s2(sv, static_cast<Size>(pos), static_cast<Size>(n), a); 71 LIBCPP_ASSERT(s2.__invariants()); 72 assert(pos <= sv.size()); 73 std::size_t rlen = std::min(sv.size() - pos, n); 74 assert(s2.size() == rlen); 75 assert(T::compare(s2.data(), sv.data() + pos, rlen) == 0); 76 assert(s2.get_allocator() == a); 77 assert(s2.capacity() >= s2.size()); 78 } 79 #ifndef TEST_HAS_NO_EXCEPTIONS 80 else 81 { 82 try 83 { 84 S s2(sv, static_cast<Size>(pos), static_cast<Size>(n), a); 85 assert(false); 86 } 87 catch (std::out_of_range&) 88 { 89 assert(pos > sv.size()); 90 } 91 } 92 #endif 93 } 94 95 int main() 96 { 97 98 { 99 typedef test_allocator<char> A; 100 typedef std::basic_string_view<char, std::char_traits<char> > SV; 101 typedef std::basic_string <char, std::char_traits<char>, A> S; 102 103 test<S,SV>(SV(), 0, 0); 104 test<S,SV>(SV(), 0, 1); 105 test<S,SV>(SV(), 1, 0); 106 test<S,SV>(SV(), 1, 1); 107 test<S,SV>(SV(), 1, 2); 108 test<S,SV>(SV("1"), 0, 0); 109 test<S,SV>(SV("1"), 0, 1); 110 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 0); 111 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 1); 112 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 10); 113 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 100); 114 115 test<S,SV>(SV(), 0, 0, A(4)); 116 test<S,SV>(SV(), 0, 1, A(4)); 117 test<S,SV>(SV(), 1, 0, A(4)); 118 test<S,SV>(SV(), 1, 1, A(4)); 119 test<S,SV>(SV(), 1, 2, A(4)); 120 test<S,SV>(SV("1"), 0, 0, A(6)); 121 test<S,SV>(SV("1"), 0, 1, A(6)); 122 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 0, A(8)); 123 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 1, A(8)); 124 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 10, A(8)); 125 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 100, A(8)); 126 } 127 128 #if TEST_STD_VER >= 11 129 { 130 typedef min_allocator<char> A; 131 typedef std::basic_string_view<char, std::char_traits<char> > SV; 132 typedef std::basic_string <char, std::char_traits<char>, A> S; 133 134 test<S,SV>(SV(), 0, 0); 135 test<S,SV>(SV(), 0, 1); 136 test<S,SV>(SV(), 1, 0); 137 test<S,SV>(SV(), 1, 1); 138 test<S,SV>(SV(), 1, 2); 139 test<S,SV>(SV("1"), 0, 0); 140 test<S,SV>(SV("1"), 0, 1); 141 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 0); 142 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 1); 143 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 10); 144 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 100); 145 146 test<S,SV>(SV(), 0, 0, A()); 147 test<S,SV>(SV(), 0, 1, A()); 148 test<S,SV>(SV(), 1, 0, A()); 149 test<S,SV>(SV(), 1, 1, A()); 150 test<S,SV>(SV(), 1, 2, A()); 151 test<S,SV>(SV("1"), 0, 0, A()); 152 test<S,SV>(SV("1"), 0, 1, A()); 153 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 0, A()); 154 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 1, A()); 155 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 10, A()); 156 test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 100, A()); 157 } 158 #endif 159 { 160 typedef std::string S; 161 typedef std::string_view SV; 162 S s = "ABCD"; 163 SV sv = "EFGH"; 164 char arr[] = "IJKL"; 165 166 S s1("CDEF", 4); // calls ctor(const char *, len) 167 assert(s1 == "CDEF"); 168 169 S s2("QRST", 0, 3); // calls ctor(string("QRST", pos, len) 170 assert(s2 == "QRS"); 171 172 S s3(sv, 0, std::string::npos); // calls ctor(T, pos, npos) 173 assert(s3 == sv); 174 175 S s4(sv, 0, 3); // calls ctor(T, pos, len) 176 assert(s4 == "EFG"); 177 178 S s5(arr, 0, 2); // calls ctor(const char *, len) 179 assert(s5 == "IJ"); 180 181 S s6(arr, 0); // calls ctor(const char *, len) 182 assert(s6 == ""); 183 184 S s7(s.data(), 2); // calls ctor(const char *, len) 185 assert(s7 == "AB"); 186 } 187 }