]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/std/ranges/adaptors/filter.cc
libstdc++: Add ranges_size_t and rename all_view (LWG 3335)
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / ranges / adaptors / filter.cc
1 // Copyright (C) 2020 Free Software Foundation, Inc.
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 <testsuite_hooks.h>
24 #include <testsuite_iterators.h>
25
26 using __gnu_test::test_range;
27 using __gnu_test::bidirectional_iterator_wrapper;
28
29 namespace ranges = std::ranges;
30 namespace views = std::ranges::views;
31
32 void
33 test01()
34 {
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 );
51 }
52
53 void
54 test02()
55 {
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}) );
65 }
66
67 struct X
68 {
69 int i, j;
70 };
71
72 void
73 test03()
74 {
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; });
78 int sum = 0;
79 for (auto i = v.begin(); i != v.end(); ++i)
80 sum += i->j;
81 VERIFY( sum == 14 );
82 }
83
84 void
85 test04()
86 {
87 auto yes = [] (int) { return true; };
88 VERIFY( ranges::equal(views::iota(0) | views::filter(yes) | views::take(1),
89 (int[]){0}) );
90 }
91
92 int
93 main()
94 {
95 test01();
96 test02();
97 test03();
98 test04();
99 }