| 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_range; |
| 25 | using __gnu_test::input_iterator_wrapper; |
| 26 | using __gnu_test::output_iterator_wrapper; |
| 27 | using __gnu_test::forward_iterator_wrapper; |
| 28 | |
| 29 | namespace ranges = std::ranges; |
| 30 | |
| 31 | template<template<typename> typename source_wrapper, |
| 32 | template<typename> typename dest_wrapper> |
| 33 | void |
| 34 | test01() |
| 35 | { |
| 36 | int x[6] = {0, 0, 0, 1, 1, 1}; |
| 37 | int y[2]; |
| 38 | const int z[2] = {0, 1}; |
| 39 | |
| 40 | test_range<int, source_wrapper> rx(x); |
| 41 | test_range<int, dest_wrapper> ry(y); |
| 42 | auto [in,out] = ranges::unique_copy(rx, ry.begin()); |
| 43 | VERIFY( in == ranges::end(rx) && out == ranges::end(ry) ); |
| 44 | VERIFY( ranges::equal(y, z) ); |
| 45 | } |
| 46 | |
| 47 | template<template<typename> typename source_wrapper, |
| 48 | template<typename> typename dest_wrapper> |
| 49 | void |
| 50 | test02() |
| 51 | { |
| 52 | int x[6] = {0, 0, 0, 1, 1, 1}; |
| 53 | int y[2] = {0, 0}; |
| 54 | const int z[2] = {0, 0}; |
| 55 | |
| 56 | test_range<int, source_wrapper> rx(x, x); |
| 57 | test_range<int, dest_wrapper> ry(y, y); |
| 58 | auto [in, out] = ranges::unique_copy(rx.begin(), rx.end(), ry.begin()); |
| 59 | VERIFY( in.ptr == x && out.ptr == y ); |
| 60 | VERIFY( ranges::equal(y, z) ); |
| 61 | } |
| 62 | |
| 63 | template<template<typename> typename source_wrapper, |
| 64 | template<typename> typename dest_wrapper> |
| 65 | void |
| 66 | test03() |
| 67 | { |
| 68 | struct X { int i; }; |
| 69 | X x[6] = { {1}, {2}, {2}, {4}, {4}, {6} }; |
| 70 | X y[4] = { {1}, {2}, {4}, {6} }; |
| 71 | const X z[4] = { {1}, {2}, {4}, {6} }; |
| 72 | |
| 73 | test_range<X, source_wrapper> rx(x); |
| 74 | test_range<X, dest_wrapper> ry(y); |
| 75 | auto [in, out] |
| 76 | = ranges::unique_copy(rx, ry.begin(), ranges::equal_to{}, &X::i); |
| 77 | VERIFY( in == ranges::end(rx) && out == ranges::end(ry) ); |
| 78 | VERIFY( ranges::equal(y, z, {}, &X::i, &X::i) ); |
| 79 | } |
| 80 | |
| 81 | constexpr bool |
| 82 | test04() |
| 83 | { |
| 84 | struct X { int i; }; |
| 85 | X x[7] = { {1}, {2}, {2}, {2}, {4}, {4}, {6} }; |
| 86 | X y[4] = { {1}, {2}, {4}, {6} }; |
| 87 | const X z[4] = { {1}, {2}, {4}, {6} }; |
| 88 | |
| 89 | auto [in, out] |
| 90 | = ranges::unique_copy(x, x+7, y, ranges::equal_to{}, &X::i); |
| 91 | return (in == ranges::end(x) |
| 92 | && out == ranges::end(y) |
| 93 | && ranges::equal(y, z, {}, &X::i, &X::i)); |
| 94 | } |
| 95 | |
| 96 | int |
| 97 | main() |
| 98 | { |
| 99 | test01<input_iterator_wrapper, output_iterator_wrapper>(); |
| 100 | test01<input_iterator_wrapper, forward_iterator_wrapper>(); |
| 101 | test01<forward_iterator_wrapper, output_iterator_wrapper>(); |
| 102 | |
| 103 | test02<input_iterator_wrapper, output_iterator_wrapper>(); |
| 104 | test02<input_iterator_wrapper, forward_iterator_wrapper>(); |
| 105 | test02<forward_iterator_wrapper, output_iterator_wrapper>(); |
| 106 | |
| 107 | test03<input_iterator_wrapper, output_iterator_wrapper>(); |
| 108 | test03<input_iterator_wrapper, forward_iterator_wrapper>(); |
| 109 | test03<forward_iterator_wrapper, output_iterator_wrapper>(); |
| 110 | |
| 111 | static_assert(test04()); |
| 112 | } |