| 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 | { |
| 46 | X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} }; |
| 47 | X y[6] = { {2}, {2}, {6}, {9}, {10}, {11} }; |
| 48 | auto res = ranges::replace(x, x+5, 8, X{9}, &X::i); |
| 49 | VERIFY( res == x+5 ); |
| 50 | VERIFY( ranges::equal(x, y) ); |
| 51 | } |
| 52 | |
| 53 | { |
| 54 | X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} }; |
| 55 | X y[6] = { {2}, {2}, {6}, {8}, {10}, {11} }; |
| 56 | auto res = ranges::replace(x, x+5, 7, X{9}, &X::i); |
| 57 | VERIFY( res == x+5 ); |
| 58 | VERIFY( ranges::equal(x, y) ); |
| 59 | } |
| 60 | |
| 61 | { |
| 62 | X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} }; |
| 63 | X y[6] = { {7}, {7}, {6}, {8}, {10}, {11} }; |
| 64 | test_container<X, forward_iterator_wrapper> cx(x), cy(y); |
| 65 | auto res = ranges::replace(cx, 2, X{7}, &X::i); |
| 66 | VERIFY( res == cx.end() ); |
| 67 | VERIFY( ranges::equal(cx, cy) ); |
| 68 | } |
| 69 | |
| 70 | { |
| 71 | int x[6] = { {2}, {2}, {6}, {8}, {10}, {2} }; |
| 72 | int y[6] = { {7}, {7}, {6}, {8}, {10}, {7} }; |
| 73 | test_range<int, input_iterator_wrapper> rx(x), ry(y); |
| 74 | auto res = ranges::replace(rx, 2, 7); |
| 75 | VERIFY( res == rx.end() ); |
| 76 | |
| 77 | rx.bounds.first = x; |
| 78 | ry.bounds.first = y; |
| 79 | VERIFY( ranges::equal(rx, ry) ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | struct Y { int i; int j; }; |
| 84 | |
| 85 | constexpr bool |
| 86 | test02() |
| 87 | { |
| 88 | bool ok = true; |
| 89 | Y x[] = { {3,2}, {2,4}, {3,6} }; |
| 90 | Y y[] = { {4,5}, {2,4}, {4,5} }; |
| 91 | auto res = ranges::replace(x, 3, Y{4,5}, &Y::i); |
| 92 | ok &= res == x+3; |
| 93 | ok &= ranges::equal(x, y, {}, &Y::i, &Y::i); |
| 94 | ok &= ranges::equal(x, y, {}, &Y::j, &Y::j); |
| 95 | return ok; |
| 96 | } |
| 97 | |
| 98 | int |
| 99 | main() |
| 100 | { |
| 101 | test01(); |
| 102 | static_assert(test02()); |
| 103 | } |