xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/iter_alloc.pass.cpp (revision 6e1dcc9335116f650d68cdbed12bbb34a99b2d9b)
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 #include <string>
16 #include <iterator>
17 #include <cassert>
18 #include <cstddef>
19 
20 #include "test_macros.h"
21 #include "test_allocator.h"
22 #include "test_iterators.h"
23 #include "min_allocator.h"
24 
25 template <class Alloc, class It>
26 TEST_CONSTEXPR_CXX20 void test(It first, It last) {
27   typedef typename std::iterator_traits<It>::value_type charT;
28   typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
29   S s2(first, last);
30   LIBCPP_ASSERT(s2.__invariants());
31   assert(s2.size() == static_cast<std::size_t>(std::distance(first, last)));
32   unsigned i = 0;
33   for (It it = first; it != last;) {
34     assert(s2[i] == *it);
35     ++it;
36     ++i;
37   }
38   assert(s2.get_allocator() == Alloc());
39   assert(s2.capacity() >= s2.size());
40 }
41 
42 template <class Alloc, class It>
43 TEST_CONSTEXPR_CXX20 void test(It first, It last, const Alloc& a) {
44   typedef typename std::iterator_traits<It>::value_type charT;
45   typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
46   S s2(first, last, a);
47   LIBCPP_ASSERT(s2.__invariants());
48   assert(s2.size() == static_cast<std::size_t>(std::distance(first, last)));
49   unsigned i = 0;
50   for (It it = first; it != last;) {
51     assert(s2[i] == *it);
52     ++it;
53     ++i;
54   }
55   assert(s2.get_allocator() == a);
56   assert(s2.capacity() >= s2.size());
57 }
58 
59 template <class Alloc>
60 TEST_CONSTEXPR_CXX20 void test_string(const Alloc& a) {
61   const char* s = "12345678901234567890123456789012345678901234567890";
62 
63   test<Alloc>(s, s);
64   test<Alloc>(s, s, Alloc(a));
65 
66   test<Alloc>(s, s + 1);
67   test<Alloc>(s, s + 1, Alloc(a));
68 
69   test<Alloc>(s, s + 10);
70   test<Alloc>(s, s + 10, Alloc(a));
71 
72   test<Alloc>(s, s + 50);
73   test<Alloc>(s, s + 50, Alloc(a));
74 
75   test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s));
76   test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), Alloc(a));
77 
78   test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 1));
79   test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 1), Alloc(a));
80 
81   test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 10));
82   test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 10), Alloc(a));
83 
84   test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 50));
85   test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 50), Alloc(a));
86 }
87 
88 TEST_CONSTEXPR_CXX20 bool test() {
89   test_string(test_allocator<char>());
90   test_string(test_allocator<char>(2));
91 #if TEST_STD_VER >= 11
92   test_string(min_allocator<char>());
93 #endif
94   {
95     static_assert((!std::is_constructible<std::string, std::string, std::string>::value), "");
96     static_assert((!std::is_constructible<std::string, std::string, std::string, std::allocator<char> >::value), "");
97   }
98 
99   return true;
100 }
101 
102 int main(int, char**) {
103   test();
104 #if TEST_STD_VER > 17
105   static_assert(test());
106 #endif
107 
108   return 0;
109 }
110