| 1 | // Copyright (C) 2020-2025 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-do run { target c++20 } } |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <testsuite_hooks.h> |
| 22 | #include <testsuite_iterators.h> |
| 23 | |
| 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; |
| 28 | |
| 29 | namespace ranges = std::ranges; |
| 30 | |
| 31 | struct X |
| 32 | { |
| 33 | int i; |
| 34 | |
| 35 | friend constexpr bool |
| 36 | operator==(const X& a, const X& b) |
| 37 | { |
| 38 | return a.i == b.i; |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | void |
| 43 | test01() |
| 44 | { |
| 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; }; |
| 48 | { |
| 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); |
| 52 | VERIFY( res == x+5 ); |
| 53 | VERIFY( ranges::equal(x, y) ); |
| 54 | } |
| 55 | |
| 56 | { |
| 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); |
| 60 | VERIFY( res == x+5 ); |
| 61 | VERIFY( ranges::equal(x, y) ); |
| 62 | } |
| 63 | |
| 64 | { |
| 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) ); |
| 71 | } |
| 72 | |
| 73 | { |
| 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() ); |
| 79 | |
| 80 | rx.bounds.first = x; |
| 81 | ry.bounds.first = y; |
| 82 | VERIFY( ranges::equal(rx, ry) ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | struct Y { int i; int j; }; |
| 87 | |
| 88 | constexpr bool |
| 89 | test02() |
| 90 | { |
| 91 | bool ok = true; |
| 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; }, |
| 95 | Y{4,5}, &Y::i); |
| 96 | ok &= res == x+3; |
| 97 | ok &= ranges::equal(x, y, {}, &Y::i, &Y::i); |
| 98 | ok &= ranges::equal(x, y, {}, &Y::j, &Y::j); |
| 99 | return ok; |
| 100 | } |
| 101 | |
| 102 | int |
| 103 | main() |
| 104 | { |
| 105 | test01(); |
| 106 | static_assert(test02()); |
| 107 | } |
| 108 |