]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/std/ranges/adaptors/join.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / ranges / adaptors / join.cc
CommitLineData
7adcbafe 1// Copyright (C) 2020-2022 Free Software Foundation, Inc.
cba9ef06
PP
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-options "-std=gnu++2a" }
19// { dg-do run { target c++2a } }
20
21#include <algorithm>
237dde3d 22#include <array>
cba9ef06
PP
23#include <ranges>
24#include <string>
25#include <string_view>
26#include <vector>
27#include <testsuite_hooks.h>
28#include <testsuite_iterators.h>
29
30namespace ranges = std::ranges;
31namespace views = std::ranges::views;
32
33void
34test01()
35{
36 using namespace std::literals;
37 std::string_view cs[] = {"the", "quick", "brown", "fox"};
38 auto v = cs | views::join;
39 VERIFY( ranges::equal(v, "thequickbrownfox"sv) );
40 using R = decltype(v);
41 static_assert(ranges::bidirectional_range<R>);
42 static_assert(ranges::bidirectional_range<const R>);
43 static_assert(ranges::common_range<R>);
44 static_assert(ranges::common_range<const R>);
45}
46
47void
48test02()
49{
50 auto v = (views::iota(0,4)
51 | views::transform([] (int i) { return views::iota(0,i); })
52 | views::join);
53 VERIFY( ranges::equal(v, (int[]){0,0,1,0,1,2}) );
54 using R = decltype(v);
55 static_assert(ranges::input_range<R>);
56 static_assert(!ranges::range<const R>);
57 static_assert(!ranges::forward_range<R>);
58 static_assert(!ranges::common_range<const R>);
59}
60
61void
62test03()
63{
64 auto v = (views::iota(0,4)
65 | views::transform([] (int i) { return views::iota(0,i); })
66 | views::filter([] (auto) { return true; })
67 | views::join);
68 VERIFY( ranges::equal(v, (int[]){0,0,1,0,1,2}) );
69 using R = decltype(v);
70 static_assert(ranges::input_range<R>);
71 static_assert(!ranges::range<const R>);
72 static_assert(!ranges::forward_range<R>);
73 static_assert(!ranges::common_range<const R>);
74}
75
76void
77test04()
78{
79 auto v = (views::iota(0,4)
80 | views::transform([] (int i) { return views::iota(0,i); }));
81 auto v2 = ranges::ref_view{v};
82 VERIFY( ranges::equal(v2 | views::join, (int[]){0,0,1,0,1,2}) );
83 using R = decltype(v2);
84 static_assert(ranges::random_access_range<R>);
85 static_assert(ranges::range<const R>);
86 static_assert(ranges::common_range<const R>);
87 static_assert(ranges::random_access_range<ranges::range_reference_t<R>>);
88 static_assert(!std::is_reference_v<ranges::range_reference_t<R>>);
89}
90
91void
92test05()
93{
94 using namespace std::literals;
95 std::vector<std::string> x = {"the", " ", "quick", " ", "brown", " ", "fox"};
adbd2c71 96 auto v = x | views::join | views::lazy_split(' ');
cba9ef06
PP
97 auto i = v.begin();
98 VERIFY( ranges::equal(*i++, "the"sv) );
99 VERIFY( ranges::equal(*i++, "quick"sv) );
100 VERIFY( ranges::equal(*i++, "brown"sv) );
101 VERIFY( ranges::equal(*i++, "fox"sv) );
102 VERIFY( i == v.end() );
103}
104
6aa2ca21
PP
105void
106test06()
107{
108 std::vector<std::string> x = {""};
109 auto i = std::counted_iterator(x.begin(), 1);
110 auto r = ranges::subrange{i, std::default_sentinel};
111 auto v = r | views::transform(std::identity{}) | views::join;
112
113 // Verify that _Iterator<false> is implicitly convertible to _Iterator<true>.
114 static_assert(!std::same_as<decltype(ranges::begin(v)),
115 decltype(ranges::cbegin(v))>);
116 auto a = ranges::cbegin(v);
117 a = ranges::begin(v);
118
119 // Verify that _Sentinel<false> is implicitly convertible to _Sentinel<true>.
120 static_assert(!ranges::common_range<decltype(v)>);
121 static_assert(!std::same_as<decltype(ranges::end(v)),
122 decltype(ranges::cend(v))>);
123 auto b = ranges::cend(v);
124 b = ranges::end(v);
125}
126
9065c4ad
JW
127void
128test07()
129{
130 // LWG 3474. Nesting join_views is broken because of CTAD
131 std::vector<std::vector<std::vector<int>>> nested_vectors = {
132 {{1, 2, 3}, {4, 5}, {6}},
133 {{7}, {8, 9}, {10, 11, 12}},
134 {{13}}
135 };
136 auto joined = nested_vectors | std::views::join | std::views::join;
137
138 using V = decltype(joined);
139 static_assert( std::same_as<std::ranges::range_value_t<V>, int> );
140}
141
d4a788c7
PP
142void
143test08()
144{
145 // LWG 3500. join_view::iterator::operator->() is bogus
146 struct X { int a; };
147 ranges::single_view<ranges::single_view<X>> s{std::in_place, std::in_place, 5};
148 auto v = s | views::join;
149 auto i = v.begin();
150 VERIFY( i->a == 5 );
151}
152
a25321ca
PP
153template<auto join = views::join>
154void
155test09()
156{
157 // Verify SFINAE behavior.
158 static_assert(!requires { join(); });
159 static_assert(!requires { join(0, 0); });
160 static_assert(!requires { join(0); });
161 static_assert(!requires { 0 | join; });
162}
163
85ef4b8d
PP
164void
165test10()
166{
167 // PR libstdc++/100290
168 auto v = views::single(0)
169 | views::transform([](const auto& s) { return views::single(s); })
170 | views::join;
171 VERIFY( ranges::next(v.begin()) == v.end() );
172}
173
237dde3d
PP
174void
175test11()
176{
177 // Verify P2328 changes.
178 int r[] = {1, 2, 3};
179 auto v = r
180 | views::transform([] (int n) { return std::vector{{n, -n}}; })
181 | views::join;
182 VERIFY( ranges::equal(v, (int[]){1, -1, 2, -2, 3, -3}) );
183
184 struct S {
185 S() = default;
186 S(const S&) = delete;
187 S(S&&) = delete;
188 };
189 auto w = r
190 | views::transform([] (int) { return std::array<S, 2>{}; })
191 | views::join;
192 for (auto& i : w)
193 ;
194}
195
2c564e81
JW
196void
197test12()
198{
199 // PR libstdc++/101263
200 constexpr auto b = [] {
201 auto r = std::views::iota(0, 5)
202 | std::views::lazy_split(0)
203 | std::views::join;
204 return r.begin() != r.end();
205 }();
206}
207
cba9ef06
PP
208int
209main()
210{
211 test01();
212 test02();
213 test03();
214 test04();
215 test05();
6aa2ca21 216 test06();
9065c4ad 217 test07();
d4a788c7 218 test08();
a25321ca 219 test09();
85ef4b8d 220 test10();
237dde3d 221 test11();
2c564e81 222 test12();
cba9ef06 223}