1 // Copyright (C) 2020-2025 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-do run { target c++20 } }
21 #include <testsuite_hooks.h>
22 #include <testsuite_iterators.h>
24 using __gnu_test::test_container
;
25 using __gnu_test::test_range
;
26 using __gnu_test::input_iterator_wrapper
;
27 using __gnu_test::forward_iterator_wrapper
;
29 namespace ranges
= std::ranges
;
36 operator==(const X
& a
, const X
& b
)
45 auto is_even_p
= [] (int a
) { return a
%2 == 0; };
46 auto is_negative_p
= [] (int a
) { return a
< 0; };
47 auto is_two_p
= [] (int a
) { return a
== 2; };
49 X x
[6] = { {1}, {2}, {6}, {8}, {10}, {11} };
50 X y
[6] = { {1}, {9}, {9}, {9}, {9}, {11} };
51 auto res
= ranges::replace_if(x
, x
+5, is_even_p
, X
{9}, &X::i
);
53 VERIFY( ranges::equal(x
, y
) );
57 X x
[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
58 X y
[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
59 auto res
= ranges::replace_if(x
, x
+5, is_negative_p
, X
{9}, &X::i
);
61 VERIFY( ranges::equal(x
, y
) );
65 X x
[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
66 X y
[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
67 test_container
<X
, forward_iterator_wrapper
> cx(x
), cy(y
);
68 auto res
= ranges::replace_if(cx
, is_two_p
, X
{7}, &X::i
);
69 VERIFY( res
== cx
.end() );
70 VERIFY( ranges::equal(cx
, cy
) );
74 int x
[6] = { {2}, {2}, {6}, {8}, {10}, {2} };
75 int y
[6] = { {7}, {7}, {6}, {8}, {10}, {7} };
76 test_range
<int, input_iterator_wrapper
> rx(x
), ry(y
);
77 auto res
= ranges::replace_if(rx
, is_two_p
, 7);
78 VERIFY( res
== rx
.end() );
82 VERIFY( ranges::equal(rx
, ry
) );
86 struct Y
{ int i
; int j
; };
92 Y x
[] = { {3,2}, {2,4}, {3,6} };
93 Y y
[] = { {4,5}, {2,4}, {4,5} };
94 auto res
= ranges::replace_if(x
, [] (int a
) { return a
%2 == 1; },
97 ok
&= ranges::equal(x
, y
, {}, &Y::i
, &Y::i
);
98 ok
&= ranges::equal(x
, y
, {}, &Y::j
, &Y::j
);
106 static_assert(test02());