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