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