Debian Codegraph @

libstdc++-v3/testsuite/std/ranges/adaptors/lazy_split.cc

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++20 } }
19
20 #include <algorithm>
21 #include <ranges>
22 #include <string>
23 #include <string_view>
24 #include <utility>
25 #include <vector>
26 #include <testsuite_hooks.h>
27 #include <testsuite_iterators.h>
28
29 using __gnu_test::test_range;
30 using __gnu_test::forward_iterator_wrapper;
31 using __gnu_test::input_iterator_wrapper;
32
33 namespace ranges = std::ranges;
34 namespace views = std::ranges::views;
35
36 using namespace std::literals;
37
38 void
39 test01()
40 {
41 auto x = "the quick brown fox"sv;
42 auto p = std::string{" "};
43 auto v = x | views::lazy_split(views::all(p)); // views::all is needed here after P2281.
44 auto i = v.begin();
45 VERIFY( ranges::equal(*i++, "the"sv) );
46 VERIFY( ranges::equal(*i++, "quick"sv) );
47 VERIFY( ranges::equal(*i++, "brown"sv) );
48 VERIFY( ranges::equal(*i++, "fox"sv) );
49 VERIFY( i == v.end() );
50 }
51
52 void
53 test02()
54 {
55 auto x = "the quick brown fox"sv;
56 auto v = x | views::lazy_split(' ');
57 auto i = v.begin();
58 VERIFY( ranges::equal(*i++, "the"sv) );
59 VERIFY( ranges::equal(*i++, "quick"sv) );
60 VERIFY( ranges::equal(*i++, "brown"sv) );
61 VERIFY( ranges::equal(*i++, "fox"sv) );
62 VERIFY( i == v.end() );
63 }
64
65 void
66 test03()
67 {
68 char x[] = "the quick brown fox";
69 test_range<char, forward_iterator_wrapper> rx(x, x+sizeof(x)-1);
70 auto v = rx | views::lazy_split(' ');
71 auto i = v.begin();
72 VERIFY( ranges::equal(*i++, "the"sv) );
73 VERIFY( ranges::equal(*i++, "quick"sv) );
74 VERIFY( ranges::equal(*i++, "brown"sv) );
75 VERIFY( ranges::equal(*i++, "fox"sv) );
76 VERIFY( i == v.end() );
77 }
78
79 void
80 test04()
81 {
82 auto x = "the quick brown fox"sv;
83 std::initializer_list<char> p = {' ', ' '};
84 static_assert(!ranges::view<decltype(p)>);
85 static_assert(std::same_as<decltype(p | views::all),
86 ranges::ref_view<decltype(p)>>);
87 auto v = x | views::lazy_split(views::all(p)); // views::all is needed here after P2281.
88 auto i = v.begin();
89 VERIFY( ranges::equal(*i++, "the"sv) );
90 VERIFY( ranges::equal(*i++, "quick"sv) );
91 VERIFY( ranges::equal(*i++, "brown"sv) );
92 VERIFY( ranges::equal(*i++, "fox"sv) );
93 VERIFY( i == v.end() );
94 }
95
96 void
97 test05()
98 {
99 auto as_string = [](ranges::view auto rng) {
100 auto in = rng | views::common;
101 return std::string(in.begin(), in.end());
102 };
103 std::string str
104 = "Now is the time for all good men to come to the aid of their county.";
105 auto rng
106 = str | views::lazy_split(' ') | views::transform(as_string) | views::common;
107 std::vector<std::string> words(rng.begin(), rng.end());
108 auto not_space_p = [](char c) { return c != ' '; };
109 VERIFY( ranges::equal(words | views::join,
110 str | views::filter(not_space_p)) );
111 }
112
113 void
114 test06()
115 {
116 std::string str = "hello world";
117 auto v = str | views::transform(std::identity{}) | views::lazy_split(' ');
118
119 // Verify that _Iterator<false> is implicitly convertible to _Iterator<true>.
120 static_assert(!std::same_as<decltype(ranges::begin(v)),
121 decltype(ranges::cbegin(v))>);
122 auto b = ranges::cbegin(v);
123 b = ranges::begin(v);
124 }
125
126 void
127 test07()
128 {
129 char str[] = "banana split";
130 auto split = str | views::lazy_split(' ');
131 auto val = *split.begin();
132 auto b = val.begin();
133 auto b2 = b++;
134 static_assert( noexcept(iter_move(b)) );
135 static_assert( noexcept(iter_swap(b, b2)) );
136 }
137
138 void
139 test08()
140 {
141 char x[] = "the quick brown fox";
142 test_range<char, input_iterator_wrapper> rx(x, x+sizeof(x)-1);
143 auto v = rx | views::lazy_split(' ');
144 auto i = v.begin();
145 VERIFY( ranges::equal(*i, "the"sv) );
146 ++i;
147 VERIFY( ranges::equal(*i, "quick"sv) );
148 ++i;
149 VERIFY( ranges::equal(*i, "brown"sv) );
150 ++i;
151 VERIFY( ranges::equal(*i, "fox"sv) );
152 ++i;
153 VERIFY( i == v.end() );
154 }
155
156 template<auto lazy_split = views::lazy_split>
157 void
158 test09()
159 {
160 // Verify SFINAE behavior.
161 std::string s, p;
162 static_assert(!requires { lazy_split(); });
163 static_assert(!requires { lazy_split(s, p, 0); });
164 static_assert(!requires { lazy_split(p)(); });
165 static_assert(!requires { s | lazy_split; });
166
167 // Test the case where the closure object is used as an rvalue and therefore
168 // the copy of p is forwarded as an rvalue.
169 // This used to be invalid, but is now well-formed after P2415R2 relaxed
170 // the requirements of viewable_range to admit rvalue non-view non-borrowed
171 // ranges such as std::string&&.
172 static_assert(requires { s | lazy_split(p); });
173 static_assert(requires { lazy_split(p)(s); });
174 static_assert(requires { s | (lazy_split(p) | views::all); });
175 static_assert(requires { (lazy_split(p) | views::all)(s); });
176
177 static_assert(requires { s | lazy_split(views::all(p)); });
178 static_assert(requires { lazy_split(views::all(p))(s); });
179 static_assert(requires { s | (lazy_split(views::all(p)) | views::all); });
180 static_assert(requires { (lazy_split(views::all(p)) | views::all)(s); });
181
182 auto adapt = lazy_split(p);
183 static_assert(requires { s | adapt; });
184 static_assert(requires { adapt(s); });
185
186 auto adapt2 = lazy_split(p) | views::all;
187 static_assert(requires { s | adapt2; });
188 static_assert(requires { adapt2(s); });
189 }
190
191 void
192 test10()
193 {
194 // LWG 3505
195 auto to_string = [] (auto r) {
196 return std::string(r.begin(), ranges::next(r.begin(), r.end()));
197 };
198 auto v = "xxyx"sv | views::lazy_split("xy"sv) | views::transform(to_string);
199 VERIFY( ranges::equal(v, (std::string_view[]){"x", "x"}) );
200 }
201
202 void
203 test11()
204 {
205 // LWG 3478
206 auto v = views::lazy_split("text"sv, "text"sv);
207 auto i = v.begin();
208 VERIFY( ranges::empty(*i++) );
209 VERIFY( ranges::empty(*i++) );
210 VERIFY( i == v.end() );
211
212 static_assert(ranges::distance(views::lazy_split(" text "sv, ' ')) == 3);
213 static_assert(ranges::distance(views::lazy_split(" t e x t "sv, ' ')) == 6);
214 static_assert(ranges::distance(views::lazy_split(" text "sv, " "sv)) == 3);
215 static_assert(ranges::distance(views::lazy_split(" text "sv, " "sv)) == 4);
216 static_assert(ranges::distance(views::lazy_split(" text "sv, " "sv)) == 4);
217 static_assert(ranges::distance(views::lazy_split("t"sv, 't')) == 2);
218 static_assert(ranges::distance(views::lazy_split("text"sv, ""sv)) == 4);
219 }
220
221 constexpr bool
222 test12()
223 {
224 // LWG 3904
225 auto r = views::single(0) | views::lazy_split(0);
226 auto i = r.begin();
227 ++i;
228 VERIFY( i != r.end() );
229 decltype(std::as_const(r).begin()) j = i;
230 VERIFY( j != r.end() );
231
232 return true;
233 }
234
235 void
236 test13()
237 {
238 // PR libstdc++/119962 - __maybe_present_t misses initialization
239 constexpr decltype(views::lazy_split(views::single(0), 0).begin()) it;
240 }
241
242 void
243 test14()
244 {
245 // LWG 3599
246 auto p = views::iota(0) | views::take(1) | views::reverse;
247 auto r = views::single(42) | views::lazy_split(p);
248 auto f = r.front();
249 }
250
251 int
252 main()
253 {
254 test01();
255 test02();
256 test03();
257 test04();
258 test05();
259 test06();
260 test07();
261 test08();
262 test09();
263 test10();
264 test11();
265 static_assert(test12());
266 test13();
267 test14();
268 }