libstdc++: Implement LWG 3904 change to lazy_split_view's iterator
[official-gcc.git] / libstdc++-v3 / testsuite / std / ranges / adaptors / lazy_split.cc
blob4e5c0dc3ed5b22f3779e021f863e617b9cecec80
1 // Copyright (C) 2020-2023 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.
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.
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/>.
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
21 #include <algorithm>
22 #include <ranges>
23 #include <string>
24 #include <string_view>
25 #include <utility>
26 #include <vector>
27 #include <testsuite_hooks.h>
28 #include <testsuite_iterators.h>
30 using __gnu_test::test_range;
31 using __gnu_test::forward_iterator_wrapper;
32 using __gnu_test::input_iterator_wrapper;
34 namespace ranges = std::ranges;
35 namespace views = std::ranges::views;
37 using namespace std::literals;
39 void
40 test01()
42 auto x = "the quick brown fox"sv;
43 auto p = std::string{" "};
44 auto v = x | views::lazy_split(views::all(p)); // views::all is needed here after P2281.
45 auto i = v.begin();
46 VERIFY( ranges::equal(*i++, "the"sv) );
47 VERIFY( ranges::equal(*i++, "quick"sv) );
48 VERIFY( ranges::equal(*i++, "brown"sv) );
49 VERIFY( ranges::equal(*i++, "fox"sv) );
50 VERIFY( i == v.end() );
53 void
54 test02()
56 auto x = "the quick brown fox"sv;
57 auto v = x | views::lazy_split(' ');
58 auto i = v.begin();
59 VERIFY( ranges::equal(*i++, "the"sv) );
60 VERIFY( ranges::equal(*i++, "quick"sv) );
61 VERIFY( ranges::equal(*i++, "brown"sv) );
62 VERIFY( ranges::equal(*i++, "fox"sv) );
63 VERIFY( i == v.end() );
66 void
67 test03()
69 char x[] = "the quick brown fox";
70 test_range<char, forward_iterator_wrapper> rx(x, x+sizeof(x)-1);
71 auto v = rx | views::lazy_split(' ');
72 auto i = v.begin();
73 VERIFY( ranges::equal(*i++, "the"sv) );
74 VERIFY( ranges::equal(*i++, "quick"sv) );
75 VERIFY( ranges::equal(*i++, "brown"sv) );
76 VERIFY( ranges::equal(*i++, "fox"sv) );
77 VERIFY( i == v.end() );
80 void
81 test04()
83 auto x = "the quick brown fox"sv;
84 std::initializer_list<char> p = {' ', ' '};
85 static_assert(!ranges::view<decltype(p)>);
86 static_assert(std::same_as<decltype(p | views::all),
87 ranges::ref_view<decltype(p)>>);
88 auto v = x | views::lazy_split(views::all(p)); // views::all is needed here after P2281.
89 auto i = v.begin();
90 VERIFY( ranges::equal(*i++, "the"sv) );
91 VERIFY( ranges::equal(*i++, "quick"sv) );
92 VERIFY( ranges::equal(*i++, "brown"sv) );
93 VERIFY( ranges::equal(*i++, "fox"sv) );
94 VERIFY( i == v.end() );
97 void
98 test05()
100 auto as_string = [](ranges::view auto rng) {
101 auto in = rng | views::common;
102 return std::string(in.begin(), in.end());
104 std::string str
105 = "Now is the time for all good men to come to the aid of their county.";
106 auto rng
107 = str | views::lazy_split(' ') | views::transform(as_string) | views::common;
108 std::vector<std::string> words(rng.begin(), rng.end());
109 auto not_space_p = [](char c) { return c != ' '; };
110 VERIFY( ranges::equal(words | views::join,
111 str | views::filter(not_space_p)) );
114 void
115 test06()
117 std::string str = "hello world";
118 auto v = str | views::transform(std::identity{}) | views::lazy_split(' ');
120 // Verify that _Iterator<false> is implicitly convertible to _Iterator<true>.
121 static_assert(!std::same_as<decltype(ranges::begin(v)),
122 decltype(ranges::cbegin(v))>);
123 auto b = ranges::cbegin(v);
124 b = ranges::begin(v);
127 void
128 test07()
130 char str[] = "banana split";
131 auto split = str | views::lazy_split(' ');
132 auto val = *split.begin();
133 auto b = val.begin();
134 auto b2 = b++;
135 static_assert( noexcept(iter_move(b)) );
136 static_assert( noexcept(iter_swap(b, b2)) );
139 void
140 test08()
142 char x[] = "the quick brown fox";
143 test_range<char, input_iterator_wrapper> rx(x, x+sizeof(x)-1);
144 auto v = rx | views::lazy_split(' ');
145 auto i = v.begin();
146 VERIFY( ranges::equal(*i, "the"sv) );
147 ++i;
148 VERIFY( ranges::equal(*i, "quick"sv) );
149 ++i;
150 VERIFY( ranges::equal(*i, "brown"sv) );
151 ++i;
152 VERIFY( ranges::equal(*i, "fox"sv) );
153 ++i;
154 VERIFY( i == v.end() );
157 template<auto lazy_split = views::lazy_split>
158 void
159 test09()
161 // Verify SFINAE behavior.
162 std::string s, p;
163 static_assert(!requires { lazy_split(); });
164 static_assert(!requires { lazy_split(s, p, 0); });
165 static_assert(!requires { lazy_split(p)(); });
166 static_assert(!requires { s | lazy_split; });
168 // Test the case where the closure object is used as an rvalue and therefore
169 // the copy of p is forwarded as an rvalue.
170 // This used to be invalid, but is now well-formed after P2415R2 relaxed
171 // the requirements of viewable_range to admit rvalue non-view non-borrowed
172 // ranges such as std::string&&.
173 static_assert(requires { s | lazy_split(p); });
174 static_assert(requires { lazy_split(p)(s); });
175 static_assert(requires { s | (lazy_split(p) | views::all); });
176 static_assert(requires { (lazy_split(p) | views::all)(s); });
178 static_assert(requires { s | lazy_split(views::all(p)); });
179 static_assert(requires { lazy_split(views::all(p))(s); });
180 static_assert(requires { s | (lazy_split(views::all(p)) | views::all); });
181 static_assert(requires { (lazy_split(views::all(p)) | views::all)(s); });
183 auto adapt = lazy_split(p);
184 static_assert(requires { s | adapt; });
185 static_assert(requires { adapt(s); });
187 auto adapt2 = lazy_split(p) | views::all;
188 static_assert(requires { s | adapt2; });
189 static_assert(requires { adapt2(s); });
192 void
193 test10()
195 // LWG 3505
196 auto to_string = [] (auto r) {
197 return std::string(r.begin(), ranges::next(r.begin(), r.end()));
199 auto v = "xxyx"sv | views::lazy_split("xy"sv) | views::transform(to_string);
200 VERIFY( ranges::equal(v, (std::string_view[]){"x", "x"}) );
203 void
204 test11()
206 // LWG 3478
207 auto v = views::lazy_split("text"sv, "text"sv);
208 auto i = v.begin();
209 VERIFY( ranges::empty(*i++) );
210 VERIFY( ranges::empty(*i++) );
211 VERIFY( i == v.end() );
213 static_assert(ranges::distance(views::lazy_split(" text "sv, ' ')) == 3);
214 static_assert(ranges::distance(views::lazy_split(" t e x t "sv, ' ')) == 6);
215 static_assert(ranges::distance(views::lazy_split(" text "sv, " "sv)) == 3);
216 static_assert(ranges::distance(views::lazy_split(" text "sv, " "sv)) == 4);
217 static_assert(ranges::distance(views::lazy_split(" text "sv, " "sv)) == 4);
218 static_assert(ranges::distance(views::lazy_split("t"sv, 't')) == 2);
219 static_assert(ranges::distance(views::lazy_split("text"sv, ""sv)) == 4);
222 constexpr bool
223 test12()
225 // LWG 3904
226 auto r = views::single(0) | views::lazy_split(0);
227 auto i = r.begin();
228 ++i;
229 VERIFY( i != r.end() );
230 decltype(std::as_const(r).begin()) j = i;
231 VERIFY( j != r.end() );
233 return true;
237 main()
239 test01();
240 test02();
241 test03();
242 test04();
243 test05();
244 test06();
245 test07();
246 test08();
247 test09();
248 test10();
249 test11();
250 static_assert(test12());