]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/std/ranges/adaptors/split.cc
1 // Copyright (C) 2020-2022 Free Software Foundation, Inc.
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)
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 } }
24 #include <string_view>
25 #include <testsuite_hooks.h>
26 #include <testsuite_iterators.h>
28 using __gnu_test::test_range
;
29 using __gnu_test::forward_iterator_wrapper
;
30 using __gnu_test::input_iterator_wrapper
;
32 namespace ranges
= std::ranges
;
33 namespace views
= std::ranges::views
;
35 using namespace std::literals
;
40 auto from_chars
= [] (auto v
) {
41 return std::stoi(std::string(v
.data(), v
.data() + v
.size()));
43 auto ints
= "1.2.3.4"sv
45 | views::transform(from_chars
);
46 VERIFY( ranges::equal(ints
, (int[]){1,2,3,4}) );
52 // PR libstdc++/101214
53 auto v
= views::iota(0) | views::take(5) | views::split(0);
54 static_assert(!ranges::common_range
<decltype(v
)>);
55 static_assert(std::default_initializable
<decltype(v
.end())>);
56 static_assert(std::sentinel_for
<decltype(v
.end()), decltype(v
.begin())>);
59 // The following testcases are adapted from lazy_split.cc.
60 namespace from_lazy_split_cc
65 auto x
= "the quick brown fox"sv
;
66 auto p
= std::string
{" "};
67 auto v
= x
| views::split(views::all(p
)); // views::all is needed here after P2281.
69 VERIFY( ranges::equal(*i
++, "the"sv
) );
70 VERIFY( ranges::equal(*i
++, "quick"sv
) );
71 VERIFY( ranges::equal(*i
++, "brown"sv
) );
72 VERIFY( ranges::equal(*i
++, "fox"sv
) );
73 VERIFY( i
== v
.end() );
79 auto x
= "the quick brown fox"sv
;
80 auto v
= x
| views::split(' ');
82 VERIFY( ranges::equal(*i
++, "the"sv
) );
83 VERIFY( ranges::equal(*i
++, "quick"sv
) );
84 VERIFY( ranges::equal(*i
++, "brown"sv
) );
85 VERIFY( ranges::equal(*i
++, "fox"sv
) );
86 VERIFY( i
== v
.end() );
92 char x
[] = "the quick brown fox";
93 test_range
<char, forward_iterator_wrapper
> rx(x
, x
+sizeof(x
)-1);
94 auto v
= rx
| views::split(' ');
96 VERIFY( ranges::equal(*i
++, "the"sv
) );
97 VERIFY( ranges::equal(*i
++, "quick"sv
) );
98 VERIFY( ranges::equal(*i
++, "brown"sv
) );
99 VERIFY( ranges::equal(*i
++, "fox"sv
) );
100 VERIFY( i
== v
.end() );
106 auto x
= "the quick brown fox"sv
;
107 std::initializer_list
<char> p
= {' ', ' '};
108 static_assert(!ranges::view
<decltype(p
)>);
109 static_assert(std::same_as
<decltype(p
| views::all
),
110 ranges::ref_view
<decltype(p
)>>);
111 auto v
= x
| views::split(views::all(p
)); // views::all is needed here after P2281.
113 VERIFY( ranges::equal(*i
++, "the"sv
) );
114 VERIFY( ranges::equal(*i
++, "quick"sv
) );
115 VERIFY( ranges::equal(*i
++, "brown"sv
) );
116 VERIFY( ranges::equal(*i
++, "fox"sv
) );
117 VERIFY( i
== v
.end() );
123 auto as_string
= [](ranges::view
auto rng
) {
124 auto in
= rng
| views::common
;
125 return std::string(in
.begin(), in
.end());
128 = "Now is the time for all good men to come to the aid of their county.";
130 = str
| views::split(' ') | views::transform(as_string
) | views::common
;
131 std::vector
<std::string
> words(rng
.begin(), rng
.end());
132 auto not_space_p
= [](char c
) { return c
!= ' '; };
133 VERIFY( ranges::equal(words
| views::join
,
134 str
| views::filter(not_space_p
)) );
137 template<auto split
= views::split
>
141 // Verify SFINAE behavior.
143 static_assert(!requires
{ split(); });
144 static_assert(!requires
{ split(s
, p
, 0); });
145 static_assert(!requires
{ split(p
)(); });
146 static_assert(!requires
{ s
| split
; });
148 static_assert(!requires
{ s
| split(p
); });
149 static_assert(!requires
{ split(p
)(s
); });
150 static_assert(!requires
{ s
| (split(p
) | views::all
); });
151 static_assert(!requires
{ (split(p
) | views::all
)(s
); });
153 static_assert(requires
{ s
| split(views::all(p
)); });
154 static_assert(requires
{ split(views::all(p
))(s
); });
155 static_assert(requires
{ s
| (split(views::all(p
)) | views::all
); });
156 static_assert(requires
{ (split(views::all(p
)) | views::all
)(s
); });
158 auto adapt
= split(p
);
159 static_assert(requires
{ s
| adapt
; });
160 static_assert(requires
{ adapt(s
); });
162 auto adapt2
= split(p
) | views::all
;
163 static_assert(requires
{ s
| adapt2
; });
164 static_assert(requires
{ adapt2(s
); });
171 auto to_string
= [] (auto r
) {
172 return std::string(r
.begin(), ranges::next(r
.begin(), r
.end()));
174 auto v
= "xxyx"sv
| views::split("xy"sv
) | views::transform(to_string
);
175 VERIFY( ranges::equal(v
, (std::string_view
[]){"x", "x"}) );
182 auto v
= views::split("text"sv
, "text"sv
);
184 VERIFY( ranges::empty(*i
++) );
185 VERIFY( ranges::empty(*i
++) );
186 VERIFY( i
== v
.end() );
188 static_assert(ranges::distance(views::split(" text "sv
, ' ')) == 3);
189 static_assert(ranges::distance(views::split(" t e x t "sv
, ' ')) == 6);
190 static_assert(ranges::distance(views::split(" text "sv
, " "sv
)) == 3);
191 static_assert(ranges::distance(views::split(" text "sv
, " "sv
)) == 4);
192 static_assert(ranges::distance(views::split(" text "sv
, " "sv
)) == 4);
193 static_assert(ranges::distance(views::split("t"sv
, 't')) == 2);
194 static_assert(ranges::distance(views::split("text"sv
, ""sv
)) == 4);
196 } // namespace from_lazy_split_cc
204 from_lazy_split_cc::test01();
205 from_lazy_split_cc::test02();
206 from_lazy_split_cc::test03();
207 from_lazy_split_cc::test04();
208 from_lazy_split_cc::test05();
209 from_lazy_split_cc::test06();
210 from_lazy_split_cc::test10();
211 from_lazy_split_cc::test11();