| 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::output_iterator_wrapper; |
| 28 | using __gnu_test::forward_iterator_wrapper; |
| 29 | |
| 30 | namespace ranges = std::ranges; |
| 31 | |
| 32 | struct X |
| 33 | { |
| 34 | int i; |
| 35 | |
| 36 | friend constexpr bool |
| 37 | operator==(const X& a, const X& b) |
| 38 | { |
| 39 | return a.i == b.i; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | void |
| 44 | test01() |
| 45 | { |
| 46 | auto is_negative_p = [] (int a) { return a < 0; }; |
| 47 | auto is_two_p = [] (int a) { return a == 2; }; |
| 48 | |
| 49 | { |
| 50 | const X x[6] = { {2}, {2}, {6}, {8}, {2}, {11} }; |
| 51 | X y[2]; |
| 52 | X z[2] = { {6}, {8} }; |
| 53 | auto [in, out] = ranges::remove_copy_if(x, x+5, y, is_two_p, &X::i); |
| 54 | VERIFY( in == x+5 && out == y+2 ); |
| 55 | VERIFY( ranges::equal(y, z) ); |
| 56 | } |
| 57 | |
| 58 | { |
| 59 | const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} }; |
| 60 | X y[5]; |
| 61 | X z[5] = { {2}, {2}, {6}, {8}, {10} }; |
| 62 | auto [in, out] = ranges::remove_copy_if(x, x+5, y, is_negative_p, &X::i); |
| 63 | VERIFY( in == x+5 && out == y+5 ); |
| 64 | VERIFY( ranges::equal(x, x+5, y, y+5) && ranges::equal(y, z) ); |
| 65 | } |
| 66 | |
| 67 | { |
| 68 | X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} }; |
| 69 | X y[4]; |
| 70 | X z[4] = { {6}, {8}, {10}, {11} }; |
| 71 | test_container<X, forward_iterator_wrapper> cx(x), cy(y), cz(z); |
| 72 | auto [in, out] = ranges::remove_copy_if(cx, cy.begin(), is_two_p, &X::i); |
| 73 | VERIFY( in == cx.end() && out == cy.end() ); |
| 74 | VERIFY( ranges::equal(cy, cz) ); |
| 75 | } |
| 76 | |
| 77 | { |
| 78 | X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} }; |
| 79 | X y[4]; |
| 80 | const X z[4] = { {6}, {8}, {10}, {11} }; |
| 81 | test_range<X, input_iterator_wrapper> cx(x); |
| 82 | test_range<X, output_iterator_wrapper> cy(y); |
| 83 | auto [in, out] = ranges::remove_copy_if(cx, cy.begin(), is_two_p, &X::i); |
| 84 | VERIFY( in == cx.end() && out == cy.end() ); |
| 85 | VERIFY( ranges::equal(y, z) ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | struct Y { int i; int j; }; |
| 90 | |
| 91 | constexpr bool |
| 92 | test02() |
| 93 | { |
| 94 | bool ok = true; |
| 95 | Y x[3] = { {3,2}, {2,4}, {3,6} }; |
| 96 | Y y[1]; |
| 97 | Y z[1] = { {2,4} }; |
| 98 | auto [in, out] |
| 99 | = ranges::remove_copy_if(x, y, [] (int a) { return a%2 == 1; }, &Y::i); |
| 100 | ok &= in == x+3; |
| 101 | ok &= out == y+1; |
| 102 | ok &= ranges::equal(y, z, {}, &Y::i, &Y::i); |
| 103 | ok &= ranges::equal(y, z, {}, &Y::j, &Y::j); |
| 104 | return ok; |
| 105 | } |
| 106 | |
| 107 | int |
| 108 | main() |
| 109 | { |
| 110 | test01(); |
| 111 | static_assert(test02()); |
| 112 | } |