]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/25_algorithms/replace_copy_if/constrained.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / replace_copy_if / constrained.cc
1 // Copyright (C) 2020-2023 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 friend constexpr bool
38 operator==(const X& a, const X& b)
39 {
40 return a.i == b.i;
41 }
42 };
43
44 void
45 test01()
46 {
47 auto is_negative_p = [] (int a) { return a < 0; };
48 auto is_two_p = [] (int a) { return a == 2; };
49
50 {
51 const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
52 X y[5];
53 X z[5] = { {9}, {9}, {6}, {8}, {10} };
54 auto [in, out] = ranges::replace_copy_if(x, x+5, y,
55 is_two_p, X{9}, &X::i);
56 VERIFY( in == x+5 && out == y+5 );
57 VERIFY( ranges::equal(y, z) );
58 }
59
60 {
61 const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
62 X y[5];
63 X z[5] = { {2}, {2}, {6}, {8}, {10} };
64 auto [in, out] = ranges::replace_copy_if(x, x+5, y,
65 is_negative_p, X{9}, &X::i);
66 VERIFY( in == x+5 && out == y+5 );
67 VERIFY( ranges::equal(x, x+5, y, y+5) && ranges::equal(y, z) );
68 }
69
70 {
71 X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
72 X y[6];
73 X z[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
74 test_container<X, forward_iterator_wrapper> cx(x), cy(y), cz(z);
75 auto [in, out] = ranges::replace_copy_if(cx, cy.begin(),
76 is_two_p, X{7}, &X::i);
77 VERIFY( in == cx.end() && out == cy.end() );
78 VERIFY( ranges::equal(cy, cz) );
79 }
80
81 {
82 X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
83 X y[6];
84 const X z[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
85 test_range<X, input_iterator_wrapper> cx(x);
86 test_range<X, output_iterator_wrapper> cy(y);
87 auto [in, out] = ranges::replace_copy_if(cx, cy.begin(),
88 is_two_p, X{7}, &X::i);
89 VERIFY( in == cx.end() && out == cy.end() );
90 VERIFY( ranges::equal(y, z) );
91 }
92 }
93
94 struct Y { int i; int j; };
95
96 constexpr bool
97 test02()
98 {
99 bool ok = true;
100 Y x[] = { {3,2}, {2,4}, {3,6} };
101 Y y[] = { {4,5}, {2,4}, {4,5} };
102 Y z[] = { {4,5}, {2,4}, {4,5} };
103 auto [in, out]
104 = ranges::replace_copy_if(x, y,
105 [] (int a) { return a%2 == 1; }, Y{4,5}, &Y::i);
106 ok &= in == x+3;
107 ok &= out == y+3;
108 ok &= ranges::equal(y, z, {}, &Y::i, &Y::i);
109 ok &= ranges::equal(y, z, {}, &Y::j, &Y::j);
110 return ok;
111 }
112
113 int
114 main()
115 {
116 test01();
117 static_assert(test02());
118 }