Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.cons / iter_alloc.pass.cpp
blob2ef73e5ec8c1adeaabfa6695ed37a464c0ba976d
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 //===----------------------------------------------------------------------===//
9 // <string>
11 // template<class InputIterator>
12 // basic_string(InputIterator begin, InputIterator end,
13 // const Allocator& a = Allocator()); // constexpr since C++20
15 #include <string>
16 #include <iterator>
17 #include <cassert>
18 #include <cstddef>
20 #include "test_macros.h"
21 #include "test_allocator.h"
22 #include "test_iterators.h"
23 #include "min_allocator.h"
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;
38 assert(s2.get_allocator() == Alloc());
39 assert(s2.capacity() >= s2.size());
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;
55 assert(s2.get_allocator() == a);
56 assert(s2.capacity() >= s2.size());
59 template <class Alloc>
60 TEST_CONSTEXPR_CXX20 void test_string(const Alloc& a) {
61 const char* s = "12345678901234567890123456789012345678901234567890";
63 test<Alloc>(s, s);
64 test<Alloc>(s, s, Alloc(a));
66 test<Alloc>(s, s + 1);
67 test<Alloc>(s, s + 1, Alloc(a));
69 test<Alloc>(s, s + 10);
70 test<Alloc>(s, s + 10, Alloc(a));
72 test<Alloc>(s, s + 50);
73 test<Alloc>(s, s + 50, Alloc(a));
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));
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));
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));
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));
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
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), "");
99 return true;
102 int main(int, char**) {
103 test();
104 #if TEST_STD_VER > 17
105 static_assert(test());
106 #endif
108 return 0;