]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/25_algorithms/remove_if/constrained.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / remove_if / constrained.cc
1 // Copyright (C) 2020-2022 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 <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
24
25 using __gnu_test::test_container;
26 using __gnu_test::test_range;
27 using __gnu_test::input_iterator_wrapper;
28 using __gnu_test::output_iterator_wrapper;
29 using __gnu_test::forward_iterator_wrapper;
30
31 namespace ranges = std::ranges;
32
33 struct X
34 {
35 int i;
36 };
37
38 void
39 test01()
40 {
41 int x[5] = { 1, 2, 3, 4, 5 };
42 const int y[4] = { 1, 2, 4, 5 };
43 auto res = ranges::remove_if(x, [] (int a) { return a == 3; });
44 VERIFY( res.begin() == x+4 && res.end() == x+5 );
45 VERIFY( ranges::equal(x, x+4, y, y+4) );
46 }
47
48 void
49 test02()
50 {
51 int x[1];
52 test_container<int, forward_iterator_wrapper> c(x, x);
53 auto res = ranges::remove_if(c, [] (int a) { return a == 1; });
54 VERIFY( res.begin().ptr == x && res.end().ptr == x );
55 }
56
57 void
58 test03()
59 {
60 int x[1] = {1};
61 test_container<int, forward_iterator_wrapper> c(x);
62 auto res = ranges::remove_if(c, [] (int a) { return a == 0; });
63 VERIFY( res.begin().ptr == x+1 && res.end().ptr == x+1 );
64 res = ranges::remove_if(c, [] (int a) { return a == 1; });
65 VERIFY( res.begin().ptr == x && res.end().ptr == x+1 );
66 }
67
68 void
69 test04()
70 {
71 X x[8] = { {0}, {1}, {0}, {1}, {0}, {0}, {1}, {1} };
72 const int y[4] = { 0, 0, 0, 0 };
73 test_range<X, forward_iterator_wrapper> c(x);
74 auto res = ranges::remove_if(c, [] (int a) { return a == 1; }, &X::i);
75 VERIFY( res.begin().ptr == x+4 && res.end().ptr == x+8 );
76 VERIFY( ranges::equal(x, x+4, y, y+4, {}, &X::i) );
77 }
78
79 constexpr bool
80 test05()
81 {
82 int x[6] = { 3, 2, 3, 3, 5, 3 };
83 const int y[2] = { 2, 5 };
84 auto res = ranges::remove_if(x, [] (int a) { return a == 3; });
85 return ranges::equal(x, res.begin(), y, y+2);
86 }
87
88
89 int
90 main()
91 {
92 test01();
93 test02();
94 test03();
95 test04();
96 static_assert(test05());
97 }