1 // Copyright (C) 2020 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 } }
23 #include <testsuite_hooks.h>
24 #include <testsuite_iterators.h>
26 using __gnu_test::test_range
;
27 using __gnu_test::bidirectional_iterator_wrapper
;
29 namespace ranges
= std::ranges
;
30 namespace views
= std::ranges::views
;
35 int x
[] = {1,2,3,4,5,6};
36 auto is_odd
= [] (int i
) { return i
%2==1; };
37 auto v
= x
| views::filter(is_odd
);
38 using R
= decltype(v
);
39 static_assert(std::same_as
<int&, decltype(*v
.begin())>);
40 static_assert(ranges::view
<R
>);
41 static_assert(ranges::input_range
<R
>);
42 static_assert(ranges::common_range
<R
>);
43 static_assert(!ranges::sized_range
<R
>);
44 static_assert(ranges::bidirectional_range
<R
>);
45 static_assert(!ranges::random_access_range
<R
>);
46 static_assert(ranges::range
<views::all_t
<R
>>);
47 VERIFY( ranges::equal(v
, (int[]){1,3,5}) );
48 VERIFY( ranges::equal(v
| views::reverse
, (int[]){5,3,1}) );
49 VERIFY( v
.pred()(3) == true );
50 VERIFY( v
.pred()(4) == false );
56 int x
[] = {1,2,3,4,5,6};
57 auto f
= [flag
=false] (int) mutable { return flag
= !flag
; };
58 auto v
= views::filter(f
)(x
);
59 using R
= decltype(v
);
60 static_assert(std::same_as
<int&, decltype(*v
.begin())>);
61 static_assert(ranges::range
<R
>);
62 static_assert(std::copyable
<R
>);
63 static_assert(!ranges::view
<const R
>);
64 VERIFY( ranges::equal(v
, (int[]){1,3,5}) );
75 X x
[] = {{1,3}, {2,5}, {3,7}, {4,9}};
76 test_range
<X
, bidirectional_iterator_wrapper
> rx(x
);
77 auto v
= rx
| views::filter([] (auto&& p
) { return p
.i
%2==0; });
79 for (auto i
= v
.begin(); i
!= v
.end(); ++i
)
87 auto yes
= [] (int) { return true; };
88 VERIFY( ranges::equal(views::iota(0) | views::filter(yes
) | views::take(1),