1 // Copyright (C) 2020-2023 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 } }
25 #include <testsuite_hooks.h>
26 #include <testsuite_iterators.h>
28 namespace ranges
= std::ranges
;
29 namespace views
= std::ranges::views
;
34 int x
[] = {1,2,3,4,5};
35 auto v
= views::all(x
);
37 static_assert(ranges::view
<decltype(v
)>);
38 static_assert(ranges::random_access_range
<decltype(v
)>);
40 VERIFY( ranges::size(v
) == 5 );
41 VERIFY( ranges::size(x
| views::all
) == 5 );
42 VERIFY( ranges::size(v
| views::all
| views::all
) == 5 );
43 VERIFY( ranges::size(v
| (views::all
| views::all
)) == 5 );
46 VERIFY( ranges::equal(v
, (int[]){5,4,3,2,1}) );
54 for (auto&& i
: ranges::ref_view
{x
})
56 VERIFY( ranges::equal(x
, (int[]){1,2,3,4,5}) );
62 std::array ints
{0,1,2,3,4,5};
63 auto even
= [] (int i
) { return i
%2==0; };
64 auto odd
= [] (int i
) { return i
%2==1; };
65 auto square
= [] (int i
) { return i
*i
; };
69 | (views::filter(even
)
70 | (views::filter(odd
) | views::all
)))
71 | views::transform(square
)))
79 auto odd
= [] (int i
) { return i
%2==1; };
80 auto square
= [] (int i
) { return i
*i
; };
81 auto increment
= [] (int i
) { return i
+1; };
82 auto small
= [] (int i
) { return i
<30; };
83 auto non_negative
= [] (int i
) { return i
>=0; };
84 auto negative
= [] (int i
) { return i
<0; };
85 return ranges::equal(views::iota(-5)
86 | views::drop_while(negative
)
87 | views::take_while(non_negative
)
88 | views::transform(increment
)
92 | views::transform(square
),
94 | views::drop_while(negative
)
97 | views::transform(square
)
98 | views::take_while(small
)
99 | views::take_while(small
));
102 static_assert(std::is_empty_v
<decltype(views::common
109 // Adding empty range adaptor closure objects to a pipeline used to not
110 // increase the size of the pipeline, but now that our range adaptor closure
111 // objects derive from a common empty base class, [[no_unique_address]] can no
112 // longer make two empty adjacent range adaptor closure objects occupy the same
113 // data member address.
114 static_assert(sizeof(decltype(views::take(5) | views::drop(5)))
115 == sizeof(decltype(views::take(5)
124 template<auto all
= views::all
>
128 // Verify SFINAE behavior.
129 static_assert(!requires
{ all(); });
130 static_assert(!requires
{ all(0, 0); });
131 static_assert(!requires
{ all(0); });
132 static_assert(!requires
{ 0 | all
; });
141 static_assert(noexcept(views::all(x
)));
143 // Using owning_view:
144 static_assert(noexcept(views::all(std::array
<int, 3>{})));
145 struct A
{ A(); A(const A
&); };
146 static_assert(!std::is_nothrow_move_constructible_v
<std::array
<A
, 3>>);
147 static_assert(!noexcept(views::all(std::array
<A
, 3>{})));
154 struct view_t
: ranges::empty_view
<int> { // move-only view
155 view_t(const view_t
&) = delete;
156 view_t(view_t
&&) = default;
157 view_t
& operator=(const view_t
&) = delete;
158 view_t
& operator=(view_t
&&) = default;
160 static_assert(std::movable
<view_t
> && !std::copyable
<view_t
>);
161 static_assert(!ranges::viewable_range
<view_t
&>);
167 #ifdef _GLIBCXX_DEBUG
168 using std::_GLIBCXX_STD_C::vector
;
173 // Verify P2415R2 "What is a view?" changes.
174 // In particular, rvalue non-view non-borrowed ranges are now viewable.
175 static_assert(ranges::viewable_range
<vector
<int>&&>);
176 static_assert(!ranges::viewable_range
<const vector
<int>&&>);
178 static_assert(ranges::viewable_range
<std::initializer_list
<int>&>);
179 static_assert(ranges::viewable_range
<const std::initializer_list
<int>&>);
180 static_assert(!ranges::viewable_range
<std::initializer_list
<int>&&>);
181 static_assert(!ranges::viewable_range
<const std::initializer_list
<int>&&>);
183 using type
= views::all_t
<vector
<int>&&>;
184 using type
= ranges::owning_view
<vector
<int>>;
186 std::same_as
<type
> auto v
= vector
<int>{{1,2,3}} | views::all
;
188 VERIFY( ranges::equal(v
, (int[]){1,2,3}) );
189 VERIFY( ranges::size(v
) == 3 );
190 VERIFY( !ranges::empty(v
) );
191 VERIFY( ranges::data(v
) == &v
[0] );
193 const auto w
= std::move(v
);
194 VERIFY( ranges::equal(w
, (int[]){1,2,3}) );
195 VERIFY( ranges::size(w
) == 3 );
196 VERIFY( !ranges::empty(w
) );
197 VERIFY( ranges::data(w
) == &w
[0] );
207 static_assert(test03());
208 static_assert(test04());
212 static_assert(test08());