Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.cons / substr.pass.cpp
blob693edab76b17b0ce471fe9501f94b1d8a1585f39
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 // basic_string(const basic_string<charT,traits,Allocator>& str,
12 // size_type pos, size_type n,
13 // const Allocator& a = Allocator()); // constexpr since C++20
15 // basic_string(const basic_string<charT,traits,Allocator>& str,
16 // size_type pos,
17 // const Allocator& a = Allocator()); // constexpr since C++20
19 #include <string>
20 #include <stdexcept>
21 #include <algorithm>
22 #include <vector>
23 #include <scoped_allocator>
24 #include <cassert>
26 #include "test_macros.h"
27 #include "test_allocator.h"
28 #include "min_allocator.h"
30 template <class S>
31 TEST_CONSTEXPR_CXX20 void test(S str, unsigned pos) {
32 typedef typename S::traits_type T;
33 typedef typename S::allocator_type A;
35 if (pos <= str.size()) {
36 S s2(str, pos);
37 LIBCPP_ASSERT(s2.__invariants());
38 typename S::size_type rlen = str.size() - pos;
39 assert(s2.size() == rlen);
40 assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
41 assert(s2.get_allocator() == A());
42 assert(s2.capacity() >= s2.size());
44 #ifndef TEST_HAS_NO_EXCEPTIONS
45 else if (!TEST_IS_CONSTANT_EVALUATED) {
46 try {
47 S s2(str, pos);
48 assert(false);
49 } catch (std::out_of_range&) {
50 assert(pos > str.size());
53 #endif
56 template <class S>
57 TEST_CONSTEXPR_CXX20 void test(S str, unsigned pos, unsigned n) {
58 typedef typename S::traits_type T;
59 typedef typename S::allocator_type A;
60 if (pos <= str.size()) {
61 S s2(str, pos, n);
62 LIBCPP_ASSERT(s2.__invariants());
63 typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n);
64 assert(s2.size() == rlen);
65 assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
66 assert(s2.get_allocator() == A());
67 assert(s2.capacity() >= s2.size());
69 #ifndef TEST_HAS_NO_EXCEPTIONS
70 else if (!TEST_IS_CONSTANT_EVALUATED) {
71 try {
72 S s2(str, pos, n);
73 assert(false);
74 } catch (std::out_of_range&) {
75 assert(pos > str.size());
78 #endif
81 template <class S>
82 TEST_CONSTEXPR_CXX20 void test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a) {
83 typedef typename S::traits_type T;
85 if (pos <= str.size()) {
86 S s2(str, pos, n, a);
87 LIBCPP_ASSERT(s2.__invariants());
88 typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n);
89 assert(s2.size() == rlen);
90 assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
91 assert(s2.get_allocator() == a);
92 assert(s2.capacity() >= s2.size());
94 #ifndef TEST_HAS_NO_EXCEPTIONS
95 else if (!TEST_IS_CONSTANT_EVALUATED) {
96 try {
97 S s2(str, pos, n, a);
98 assert(false);
99 } catch (std::out_of_range&) {
100 assert(pos > str.size());
103 #endif
106 void test_lwg2583() {
107 #if TEST_STD_VER >= 11 && !defined(TEST_HAS_NO_EXCEPTIONS)
108 typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > StringA;
109 std::vector<StringA, std::scoped_allocator_adaptor<test_allocator<StringA> > > vs;
110 StringA s{"1234"};
111 vs.emplace_back(s, 2);
113 try {
114 vs.emplace_back(s, 5);
115 } catch (const std::out_of_range&) {
116 return;
118 assert(false);
119 #endif
122 template <class Alloc>
123 TEST_CONSTEXPR_CXX20 void test_string(const Alloc& a1, const Alloc& a2) {
124 using S = std::basic_string<char, std::char_traits<char>, Alloc>;
126 test(S(Alloc(a1)), 0);
127 test(S(Alloc(a1)), 1);
128 test(S("1", Alloc(a1)), 0);
129 test(S("1", Alloc(a1)), 1);
130 test(S("1", Alloc(a1)), 2);
131 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc(a1)), 0);
132 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc(a1)), 5);
133 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc(a1)), 50);
134 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc(a1)), 500);
136 test(S(Alloc(a1)), 0, 0);
137 test(S(Alloc(a1)), 0, 1);
138 test(S(Alloc(a1)), 1, 0);
139 test(S(Alloc(a1)), 1, 1);
140 test(S(Alloc(a1)), 1, 2);
141 test(S("1", Alloc(a1)), 0, 0);
142 test(S("1", Alloc(a1)), 0, 1);
143 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc(a1)), 50, 0);
144 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc(a1)), 50, 1);
145 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc(a1)), 50, 10);
146 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc(a1)), 50, 100);
148 test(S(Alloc(a1)), 0, 0, Alloc(a2));
149 test(S(Alloc(a1)), 0, 1, Alloc(a2));
150 test(S(Alloc(a1)), 1, 0, Alloc(a2));
151 test(S(Alloc(a1)), 1, 1, Alloc(a2));
152 test(S(Alloc(a1)), 1, 2, Alloc(a2));
153 test(S("1", Alloc(a1)), 0, 0, Alloc(a2));
154 test(S("1", Alloc(a1)), 0, 1, Alloc(a2));
155 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc(a1)), 50, 0, Alloc(a2));
156 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc(a1)), 50, 1, Alloc(a2));
157 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc(a1)), 50, 10, Alloc(a2));
158 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc(a1)), 50, 100, Alloc(a2));
161 TEST_CONSTEXPR_CXX20 bool test() {
162 test_string(std::allocator<char>(), std::allocator<char>());
163 test_string(test_allocator<char>(), test_allocator<char>());
164 test_string(test_allocator<char>(3), test_allocator<char>(5));
165 #if TEST_STD_VER >= 11
166 test_string(min_allocator<char>(), min_allocator<char>());
167 #endif
169 return true;
172 int main(int, char**) {
173 test();
174 #if TEST_STD_VER > 17
175 static_assert(test());
176 #endif
177 test_lwg2583();
179 return 0;