| 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 | // { dg-require-effective-target hosted } |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <functional> |
| 23 | #include <testsuite_hooks.h> |
| 24 | #include <testsuite_iterators.h> |
| 25 | |
| 26 | using __gnu_test::test_container; |
| 27 | using __gnu_test::test_range; |
| 28 | using __gnu_test::input_iterator_wrapper; |
| 29 | using __gnu_test::forward_iterator_wrapper; |
| 30 | using __gnu_test::bidirectional_iterator_wrapper; |
| 31 | |
| 32 | namespace ranges = std::ranges; |
| 33 | |
| 34 | struct X { int i; }; |
| 35 | |
| 36 | void |
| 37 | test01() |
| 38 | { |
| 39 | int x[] = { {2}, {2}, {6}, {8}, {10}, {11}, {11} }; |
| 40 | int y[] = { {2}, {2}, {6}, {8}, {10}, {11}, {11} }; |
| 41 | X z[] = { {2}, {6}, {8}, {10}, {2}, {2} }; |
| 42 | int w[] = { {1}, {1}, {1}, {1}, {1} }; |
| 43 | |
| 44 | VERIFY( ranges::equal(w, w+4, w+1, w+5) ); |
| 45 | VERIFY( ranges::equal(w, w+5, w, w+5, ranges::greater{}, |
| 46 | [] (int a) { return a+1; }) ); |
| 47 | |
| 48 | test_container<int, forward_iterator_wrapper> cx(x), cy(y); |
| 49 | test_container<X, forward_iterator_wrapper> cz(z); |
| 50 | VERIFY( ranges::equal(cx, cy) ); |
| 51 | VERIFY( !ranges::equal(cx, cy, {}, [] (int a) { return a+1; }) ); |
| 52 | VERIFY( !ranges::equal(cx, cz, {}, {}, &X::i) ); |
| 53 | |
| 54 | test_range<int, input_iterator_wrapper> rx(x), ry(y); |
| 55 | test_range<X, input_iterator_wrapper> rz(z); |
| 56 | VERIFY( ranges::equal(rx, ry) ); |
| 57 | |
| 58 | rx.bounds.first = x; |
| 59 | ry.bounds.first = y; |
| 60 | VERIFY( !ranges::equal(rx, ry, {}, {}, [] (int a) { return a+1; }) ); |
| 61 | |
| 62 | rx.bounds.first = x; |
| 63 | rz.bounds.first = z; |
| 64 | VERIFY( !ranges::equal(rx, rz, {}, {}, &X::i) ); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | test02() |
| 69 | { |
| 70 | static constexpr X x[] = { {2}, {2}, {6}, {8}, {10}, {11} }; |
| 71 | static constexpr X y[] = { {2}, {6}, {8}, {10}, {11}, {2} }; |
| 72 | static constexpr int z[] = { {2}, {6}, {8}, {10}, {2}, {2} }; |
| 73 | static constexpr int w[] = { {2}, {6}, {8}, {10}, {2}, {2} }; |
| 74 | |
| 75 | static_assert(ranges::equal(z, w)); |
| 76 | static_assert(!ranges::equal(z, z+5, w+1, w+6)); |
| 77 | static_assert(!ranges::equal(z, z, {}, {}, [] (int a) { return a+1; })); |
| 78 | static_assert(!ranges::equal(x, y, {}, &X::i, &X::i)); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | test03() |
| 83 | { |
| 84 | std::vector<int> x = { {2}, {2}, {6}, {8}, {10}, {11} }; |
| 85 | std::vector<int> y = { {2}, {2}, {6}, {8}, {10}, {11} }; |
| 86 | std::vector<int> z = { {2}, {2}, {6}, {8}, {10}, {12} }; |
| 87 | VERIFY( ranges::equal(x, y) ); |
| 88 | VERIFY( !ranges::equal(x, z) ); |
| 89 | } |
| 90 | |
| 91 | int |
| 92 | main() |
| 93 | { |
| 94 | test01(); |
| 95 | test02(); |
| 96 | test03(); |
| 97 | } |