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 // basic_string(const basic_string<charT,traits,Allocator>& str, 13 // size_type pos, size_type n, 14 // const Allocator& a = Allocator()); 15 // 16 // basic_string(const basic_string<charT,traits,Allocator>& str, 17 // size_type pos, 18 // const Allocator& a = Allocator()); 19 20 #include <string> 21 #include <stdexcept> 22 #include <algorithm> 23 #include <vector> 24 #include <scoped_allocator> 25 #include <cassert> 26 27 #include "test_macros.h" 28 #include "test_allocator.h" 29 #include "min_allocator.h" 30 31 template <class S> 32 void 33 test(S str, unsigned pos) 34 { 35 typedef typename S::traits_type T; 36 typedef typename S::allocator_type A; 37 38 if (pos <= str.size()) 39 { 40 S s2(str, pos); 41 LIBCPP_ASSERT(s2.__invariants()); 42 typename S::size_type rlen = str.size() - pos; 43 assert(s2.size() == rlen); 44 assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); 45 assert(s2.get_allocator() == A()); 46 assert(s2.capacity() >= s2.size()); 47 } 48 #ifndef TEST_HAS_NO_EXCEPTIONS 49 else 50 { 51 try 52 { 53 S s2(str, pos); 54 assert(false); 55 } 56 catch (std::out_of_range&) 57 { 58 assert(pos > str.size()); 59 } 60 } 61 #endif 62 } 63 64 template <class S> 65 void 66 test(S str, unsigned pos, unsigned n) 67 { 68 typedef typename S::traits_type T; 69 typedef typename S::allocator_type A; 70 if (pos <= str.size()) 71 { 72 S s2(str, pos, n); 73 LIBCPP_ASSERT(s2.__invariants()); 74 typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n); 75 assert(s2.size() == rlen); 76 assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); 77 assert(s2.get_allocator() == A()); 78 assert(s2.capacity() >= s2.size()); 79 } 80 #ifndef TEST_HAS_NO_EXCEPTIONS 81 else 82 { 83 try 84 { 85 S s2(str, pos, n); 86 assert(false); 87 } 88 catch (std::out_of_range&) 89 { 90 assert(pos > str.size()); 91 } 92 } 93 #endif 94 } 95 96 template <class S> 97 void 98 test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a) 99 { 100 typedef typename S::traits_type T; 101 typedef typename S::allocator_type A; 102 103 if (pos <= str.size()) 104 { 105 S s2(str, pos, n, a); 106 LIBCPP_ASSERT(s2.__invariants()); 107 typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n); 108 assert(s2.size() == rlen); 109 assert(T::compare(s2.data(), str.data() + pos, rlen) == 0); 110 assert(s2.get_allocator() == a); 111 assert(s2.capacity() >= s2.size()); 112 } 113 #ifndef TEST_HAS_NO_EXCEPTIONS 114 else 115 { 116 try 117 { 118 S s2(str, pos, n, a); 119 assert(false); 120 } 121 catch (std::out_of_range&) 122 { 123 assert(pos > str.size()); 124 } 125 } 126 #endif 127 } 128 129 #if TEST_STD_VER >= 11 130 #ifndef TEST_HAS_NO_EXCEPTIONS 131 void test2583() 132 { // LWG #2583 133 typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > StringA; 134 std::vector<StringA, std::scoped_allocator_adaptor<test_allocator<StringA>>> vs; 135 StringA s{"1234"}; 136 vs.emplace_back(s, 2); 137 138 try { vs.emplace_back(s, 5); } 139 catch (const std::out_of_range&) { return; } 140 assert(false); 141 } 142 #endif 143 #endif 144 145 int main() 146 { 147 { 148 typedef test_allocator<char> A; 149 typedef std::basic_string<char, std::char_traits<char>, A> S; 150 151 test(S(A(3)), 0); 152 test(S(A(3)), 1); 153 test(S("1", A(5)), 0); 154 test(S("1", A(5)), 1); 155 test(S("1", A(5)), 2); 156 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0); 157 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5); 158 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50); 159 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500); 160 161 test(S(A(3)), 0, 0); 162 test(S(A(3)), 0, 1); 163 test(S(A(3)), 1, 0); 164 test(S(A(3)), 1, 1); 165 test(S(A(3)), 1, 2); 166 test(S("1", A(5)), 0, 0); 167 test(S("1", A(5)), 0, 1); 168 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0); 169 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1); 170 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10); 171 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100); 172 173 test(S(A(3)), 0, 0, A(4)); 174 test(S(A(3)), 0, 1, A(4)); 175 test(S(A(3)), 1, 0, A(4)); 176 test(S(A(3)), 1, 1, A(4)); 177 test(S(A(3)), 1, 2, A(4)); 178 test(S("1", A(5)), 0, 0, A(6)); 179 test(S("1", A(5)), 0, 1, A(6)); 180 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8)); 181 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8)); 182 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8)); 183 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8)); 184 } 185 #if TEST_STD_VER >= 11 186 { 187 typedef min_allocator<char> A; 188 typedef std::basic_string<char, std::char_traits<char>, A> S; 189 190 test(S(A()), 0); 191 test(S(A()), 1); 192 test(S("1", A()), 0); 193 test(S("1", A()), 1); 194 test(S("1", A()), 2); 195 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0); 196 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5); 197 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50); 198 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500); 199 200 test(S(A()), 0, 0); 201 test(S(A()), 0, 1); 202 test(S(A()), 1, 0); 203 test(S(A()), 1, 1); 204 test(S(A()), 1, 2); 205 test(S("1", A()), 0, 0); 206 test(S("1", A()), 0, 1); 207 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0); 208 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1); 209 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10); 210 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100); 211 212 test(S(A()), 0, 0, A()); 213 test(S(A()), 0, 1, A()); 214 test(S(A()), 1, 0, A()); 215 test(S(A()), 1, 1, A()); 216 test(S(A()), 1, 2, A()); 217 test(S("1", A()), 0, 0, A()); 218 test(S("1", A()), 0, 1, A()); 219 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A()); 220 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A()); 221 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A()); 222 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A()); 223 } 224 225 #ifndef TEST_HAS_NO_EXCEPTIONS 226 test2583(); 227 #endif 228 #endif 229 } 230