| 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 <list> |
| 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 |
|
| 31 |
|
| 32 |
namespace ranges = std::ranges; |
| 33 |
|
| 34 |
struct X |
| 35 |
{ |
| 36 |
int i; |
| 37 |
}; |
| 38 |
|
| 39 |
void |
| 40 |
test01() |
| 41 |
{ |
| 42 |
{ |
| 43 |
X x[6] = { {2}, {2}, {6}, {8}, {2}, {11} }; |
| 44 |
const int y[5] = { {2}, {6}, {8}, {2}, {11} }; |
| 45 |
test_container<X, forward_iterator_wrapper> cx(x); |
| 46 |
auto res = ranges::unique(cx, {}, &X::i); |
| 47 |
VERIFY( res.end() == cx.end() ); |
| 48 |
VERIFY( ranges::equal(cx.begin(), res.begin(), y, y+5, {}, &X::i) ); |
| 49 |
} |
| 50 |
|
| 51 |
{ |
| 52 |
X x[6] = { {2}, {2}, {6}, {8}, {2}, {11} }; |
| 53 |
const int y[5] = { {2}, {6}, {8}, {2}, {11} }; |
| 54 |
test_range<X, forward_iterator_wrapper> rx(x); |
| 55 |
auto res = ranges::unique(rx, {}, &X::i); |
| 56 |
VERIFY( res.end() == rx.end() ); |
| 57 |
VERIFY( ranges::equal(rx.begin(), res.begin(), y, y+5, {}, &X::i) ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
constexpr bool |
| 62 |
test02() |
| 63 |
{ |
| 64 |
int x[2] = {2, 2}; |
| 65 |
const int y[1] = {2}; |
| 66 |
auto res = ranges::unique(x); |
| 67 |
return ranges::equal(x, res.begin(), y, y+1, ranges::equal_to{}); |
| 68 |
} |
| 69 |
|
| 70 |
/* The following is adapted from 25_algorithms/unique/2.cc. */ |
| 71 |
|
| 72 |
namespace two_dot_cc |
| 73 |
{ |
| 74 |
const int T1[] = {1, 4, 4, 6, 1, 2, 2, 3, 1, 6, 6, 6, 5, 7, 5, 4, 4}; |
| 75 |
const int T2[] = {1, 1, 1, 2, 2, 1, 1, 7, 6, 6, 7, 8, 8, 8, 8, 9, 9}; |
| 76 |
const int N = sizeof(T1) / sizeof(int); |
| 77 |
|
| 78 |
const int A1[] = {1, 4, 6, 1, 2, 3, 1, 6, 5, 7, 5, 4}; |
| 79 |
const int A2[] = {1, 4, 4, 6, 6, 6, 6, 7}; |
| 80 |
const int A3[] = {1, 1, 1}; |
| 81 |
|
| 82 |
const int B1[] = {1, 2, 1, 7, 6, 7, 8, 9}; |
| 83 |
const int B2[] = {1, 1, 1, 2, 2, 7, 7, 8, 8, 8, 8, 9, 9}; |
| 84 |
const int B3[] = {9, 9, 8, 8, 8, 8, 7, 6, 6, 1, 1, 1, 1, 1}; |
| 85 |
|
| 86 |
void test01() |
| 87 |
{ |
| 88 |
using namespace std; |
| 89 |
|
| 90 |
list<int>::iterator pos; |
| 91 |
|
| 92 |
list<int> coll(T1, T1 + N); |
| 93 |
pos = ranges::unique(coll.begin(), coll.end()).begin(); |
| 94 |
VERIFY( equal(coll.begin(), pos, A1) ); |
| 95 |
|
| 96 |
list<int> coll2(T2, T2 + N); |
| 97 |
pos = ranges::unique(coll2.begin(), coll2.end()).begin(); |
| 98 |
VERIFY( equal(coll2.begin(), pos, B1) ); |
| 99 |
} |
| 100 |
|
| 101 |
void test02() |
| 102 |
{ |
| 103 |
using namespace std; |
| 104 |
|
| 105 |
list<int>::iterator pos; |
| 106 |
|
| 107 |
list<int> coll(T1, T1 + N); |
| 108 |
pos = ranges::unique(coll.begin(), coll.end(), greater<int>()).begin(); |
| 109 |
VERIFY( equal(coll.begin(), pos, A2) ); |
| 110 |
|
| 111 |
list<int> coll2(T2, T2 + N); |
| 112 |
pos = ranges::unique(coll2.begin(), coll2.end(), greater<int>()).begin(); |
| 113 |
VERIFY( equal(coll2.begin(), pos, B2) ); |
| 114 |
} |
| 115 |
|
| 116 |
void test03() |
| 117 |
{ |
| 118 |
using namespace std; |
| 119 |
|
| 120 |
list<int>::iterator pos; |
| 121 |
|
| 122 |
list<int> coll(T1, T1 + N); |
| 123 |
pos = ranges::unique(coll.begin(), coll.end(), less<int>()).begin(); |
| 124 |
VERIFY( equal(coll.begin(), pos, A3) ); |
| 125 |
|
| 126 |
list<int> coll2(T2, T2 + N); |
| 127 |
reverse(coll2.begin(), coll2.end()); |
| 128 |
pos = ranges::unique(coll2.begin(), coll2.end(), less<int>()).begin(); |
| 129 |
VERIFY( equal(coll2.begin(), pos, B3) ); |
| 130 |
} |
| 131 |
} // namespace two_dot_cc |
| 132 |
|
| 133 |
int main() |
| 134 |
{ |
| 135 |
test01(); |
| 136 |
static_assert(test02()); |
| 137 |
|
| 138 |
two_dot_cc::test01(); |
| 139 |
two_dot_cc::test02(); |
| 140 |
two_dot_cc::test03(); |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |