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 InputIterator> 13 // basic_string(InputIterator begin, InputIterator end, 14 // const Allocator& a = Allocator()); 15 16 // template<class InputIterator, 17 // class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>> 18 // basic_string(InputIterator, InputIterator, Allocator = Allocator()) 19 // -> basic_string<typename iterator_traits<InputIterator>::value_type, 20 // char_traits<typename iterator_traits<InputIterator>::value_type>, 21 // Allocator>; 22 // 23 // The deduction guide shall not participate in overload resolution if InputIterator 24 // is a type that does not qualify as an input iterator, or if Allocator is a type 25 // that does not qualify as an allocator. 26 27 28 #include <string> 29 #include <iterator> 30 #include <cassert> 31 #include <cstddef> 32 33 #include "test_macros.h" 34 #include "test_allocator.h" 35 #include "../input_iterator.h" 36 #include "min_allocator.h" 37 38 template <class It> 39 void 40 test(It first, It last) 41 { 42 typedef typename std::iterator_traits<It>::value_type charT; 43 typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S; 44 typedef typename S::allocator_type A; 45 S s2(first, last); 46 LIBCPP_ASSERT(s2.__invariants()); 47 assert(s2.size() == static_cast<std::size_t>(std::distance(first, last))); 48 unsigned i = 0; 49 for (It it = first; it != last; ++it, ++i) 50 assert(s2[i] == *it); 51 assert(s2.get_allocator() == A()); 52 assert(s2.capacity() >= s2.size()); 53 } 54 55 template <class It, class A> 56 void 57 test(It first, It last, const A& a) 58 { 59 typedef typename std::iterator_traits<It>::value_type charT; 60 typedef std::basic_string<charT, std::char_traits<charT>, A> S; 61 S s2(first, last, a); 62 LIBCPP_ASSERT(s2.__invariants()); 63 assert(s2.size() == static_cast<std::size_t>(std::distance(first, last))); 64 unsigned i = 0; 65 for (It it = first; it != last; ++it, ++i) 66 assert(s2[i] == *it); 67 assert(s2.get_allocator() == a); 68 assert(s2.capacity() >= s2.size()); 69 } 70 71 int main() 72 { 73 { 74 typedef test_allocator<char> A; 75 const char* s = "12345678901234567890123456789012345678901234567890"; 76 77 test(s, s); 78 test(s, s, A(2)); 79 80 test(s, s+1); 81 test(s, s+1, A(2)); 82 83 test(s, s+10); 84 test(s, s+10, A(2)); 85 86 test(s, s+50); 87 test(s, s+50, A(2)); 88 89 test(input_iterator<const char*>(s), input_iterator<const char*>(s)); 90 test(input_iterator<const char*>(s), input_iterator<const char*>(s), A(2)); 91 92 test(input_iterator<const char*>(s), input_iterator<const char*>(s+1)); 93 test(input_iterator<const char*>(s), input_iterator<const char*>(s+1), A(2)); 94 95 test(input_iterator<const char*>(s), input_iterator<const char*>(s+10)); 96 test(input_iterator<const char*>(s), input_iterator<const char*>(s+10), A(2)); 97 98 test(input_iterator<const char*>(s), input_iterator<const char*>(s+50)); 99 test(input_iterator<const char*>(s), input_iterator<const char*>(s+50), A(2)); 100 } 101 #if TEST_STD_VER >= 11 102 { 103 typedef min_allocator<char> A; 104 const char* s = "12345678901234567890123456789012345678901234567890"; 105 106 test(s, s); 107 test(s, s, A()); 108 109 test(s, s+1); 110 test(s, s+1, A()); 111 112 test(s, s+10); 113 test(s, s+10, A()); 114 115 test(s, s+50); 116 test(s, s+50, A()); 117 118 test(input_iterator<const char*>(s), input_iterator<const char*>(s)); 119 test(input_iterator<const char*>(s), input_iterator<const char*>(s), A()); 120 121 test(input_iterator<const char*>(s), input_iterator<const char*>(s+1)); 122 test(input_iterator<const char*>(s), input_iterator<const char*>(s+1), A()); 123 124 test(input_iterator<const char*>(s), input_iterator<const char*>(s+10)); 125 test(input_iterator<const char*>(s), input_iterator<const char*>(s+10), A()); 126 127 test(input_iterator<const char*>(s), input_iterator<const char*>(s+50)); 128 test(input_iterator<const char*>(s), input_iterator<const char*>(s+50), A()); 129 } 130 #endif 131 132 // Test deduction guides 133 #if TEST_STD_VER > 14 134 { 135 const char* s = "12345678901234"; 136 std::basic_string s1{s, s+10, std::allocator<char>{}}; 137 using S = decltype(s1); // what type did we get? 138 static_assert(std::is_same_v<S::value_type, char>, ""); 139 static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, ""); 140 static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, ""); 141 assert(s1.size() == 10); 142 assert(s1.compare(0, s1.size(), s, s1.size()) == 0); 143 } 144 { 145 const wchar_t* s = L"12345678901234"; 146 std::basic_string s1{s, s+10, test_allocator<wchar_t>{}}; 147 using S = decltype(s1); // what type did we get? 148 static_assert(std::is_same_v<S::value_type, wchar_t>, ""); 149 static_assert(std::is_same_v<S::traits_type, std::char_traits<wchar_t>>, ""); 150 static_assert(std::is_same_v<S::allocator_type, test_allocator<wchar_t>>, ""); 151 assert(s1.size() == 10); 152 assert(s1.compare(0, s1.size(), s, s1.size()) == 0); 153 } 154 { 155 const char16_t* s = u"12345678901234"; 156 std::basic_string s1{s, s+10, min_allocator<char16_t>{}}; 157 using S = decltype(s1); // what type did we get? 158 static_assert(std::is_same_v<S::value_type, char16_t>, ""); 159 static_assert(std::is_same_v<S::traits_type, std::char_traits<char16_t>>, ""); 160 static_assert(std::is_same_v<S::allocator_type, min_allocator<char16_t>>, ""); 161 assert(s1.size() == 10); 162 assert(s1.compare(0, s1.size(), s, s1.size()) == 0); 163 } 164 { 165 const char32_t* s = U"12345678901234"; 166 std::basic_string s1{s, s+10, explicit_allocator<char32_t>{}}; 167 using S = decltype(s1); // what type did we get? 168 static_assert(std::is_same_v<S::value_type, char32_t>, ""); 169 static_assert(std::is_same_v<S::traits_type, std::char_traits<char32_t>>, ""); 170 static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, ""); 171 assert(s1.size() == 10); 172 assert(s1.compare(0, s1.size(), s, s1.size()) == 0); 173 } 174 #endif 175 } 176