1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // <string>
10
11 // template<class InputIterator>
12 // basic_string(InputIterator begin, InputIterator end,
13 // const Allocator& a = Allocator()); // constexpr since C++20
14
15
16 #include <string>
17 #include <iterator>
18 #include <cassert>
19 #include <cstddef>
20
21 #include "test_macros.h"
22 #include "test_allocator.h"
23 #include "test_iterators.h"
24 #include "min_allocator.h"
25
26 template <class It>
27 TEST_CONSTEXPR_CXX20 void
test(It first,It last)28 test(It first, It last)
29 {
30 typedef typename std::iterator_traits<It>::value_type charT;
31 typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
32 typedef typename S::allocator_type A;
33 S s2(first, last);
34 LIBCPP_ASSERT(s2.__invariants());
35 assert(s2.size() == static_cast<std::size_t>(std::distance(first, last)));
36 unsigned i = 0;
37 for (It it = first; it != last;) {
38 assert(s2[i] == *it);
39 ++it;
40 ++i;
41 }
42 assert(s2.get_allocator() == A());
43 assert(s2.capacity() >= s2.size());
44 }
45
46 template <class It, class A>
47 TEST_CONSTEXPR_CXX20 void
test(It first,It last,const A & a)48 test(It first, It last, const A& a)
49 {
50 typedef typename std::iterator_traits<It>::value_type charT;
51 typedef std::basic_string<charT, std::char_traits<charT>, A> S;
52 S s2(first, last, a);
53 LIBCPP_ASSERT(s2.__invariants());
54 assert(s2.size() == static_cast<std::size_t>(std::distance(first, last)));
55 unsigned i = 0;
56 for (It it = first; it != last;) {
57 assert(s2[i] == *it);
58 ++it;
59 ++i;
60 }
61 assert(s2.get_allocator() == a);
62 assert(s2.capacity() >= s2.size());
63 }
64
test()65 TEST_CONSTEXPR_CXX20 bool test() {
66 {
67 typedef test_allocator<char> A;
68 const char* s = "12345678901234567890123456789012345678901234567890";
69
70 test(s, s);
71 test(s, s, A(2));
72
73 test(s, s+1);
74 test(s, s+1, A(2));
75
76 test(s, s+10);
77 test(s, s+10, A(2));
78
79 test(s, s+50);
80 test(s, s+50, A(2));
81
82 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s));
83 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), A(2));
84
85 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1));
86 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), A(2));
87
88 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10));
89 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), A(2));
90
91 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+50));
92 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+50), A(2));
93 }
94 #if TEST_STD_VER >= 11
95 {
96 typedef min_allocator<char> A;
97 const char* s = "12345678901234567890123456789012345678901234567890";
98
99 test(s, s);
100 test(s, s, A());
101
102 test(s, s+1);
103 test(s, s+1, A());
104
105 test(s, s+10);
106 test(s, s+10, A());
107
108 test(s, s+50);
109 test(s, s+50, A());
110
111 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s));
112 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), A());
113
114 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1));
115 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), A());
116
117 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10));
118 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), A());
119
120 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+50));
121 test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+50), A());
122 }
123 #endif
124 {
125 static_assert((!std::is_constructible<std::string, std::string,
126 std::string>::value),
127 "");
128 static_assert(
129 (!std::is_constructible<std::string, std::string, std::string,
130 std::allocator<char> >::value),
131 "");
132 }
133
134 return true;
135 }
136
main(int,char **)137 int main(int, char**)
138 {
139 test();
140 #if TEST_STD_VER > 17
141 static_assert(test());
142 #endif
143
144 return 0;
145 }
146