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