| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <forward_list> |
| 10 | |
| 11 | // void unique(); // C++17 and before |
| 12 | // size_type unique(); // C++20 and after; constexpr since C++26 |
| 13 | |
| 14 | #include <forward_list> |
| 15 | #include <iterator> |
| 16 | #include <cassert> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | #include "min_allocator.h" |
| 20 | |
| 21 | template <class L> |
| 22 | TEST_CONSTEXPR_CXX26 void do_unique(L& l, typename L::size_type expected) { |
| 23 | typename L::size_type old_size = std::distance(l.begin(), l.end()); |
| 24 | #if TEST_STD_VER > 17 |
| 25 | ASSERT_SAME_TYPE(decltype(l.unique()), typename L::size_type); |
| 26 | assert(l.unique() == expected); |
| 27 | #else |
| 28 | ASSERT_SAME_TYPE(decltype(l.unique()), void); |
| 29 | l.unique(); |
| 30 | #endif |
| 31 | assert(old_size - std::distance(l.begin(), l.end()) == expected); |
| 32 | } |
| 33 | |
| 34 | TEST_CONSTEXPR_CXX26 bool test() { |
| 35 | { |
| 36 | typedef int T; |
| 37 | typedef std::forward_list<T> C; |
| 38 | const T t1[] = {0, 5, 5, 0, 0, 0, 5}; |
| 39 | const T t2[] = {0, 5, 0, 5}; |
| 40 | C c1(std::begin(arr: t1), std::end(arr: t1)); |
| 41 | C c2(std::begin(arr: t2), std::end(arr: t2)); |
| 42 | do_unique(c1, 3); |
| 43 | assert(c1 == c2); |
| 44 | } |
| 45 | { |
| 46 | typedef int T; |
| 47 | typedef std::forward_list<T> C; |
| 48 | const T t1[] = {0, 0, 0, 0}; |
| 49 | const T t2[] = {0}; |
| 50 | C c1(std::begin(arr: t1), std::end(arr: t1)); |
| 51 | C c2(std::begin(arr: t2), std::end(arr: t2)); |
| 52 | do_unique(c1, 3); |
| 53 | assert(c1 == c2); |
| 54 | } |
| 55 | { |
| 56 | typedef int T; |
| 57 | typedef std::forward_list<T> C; |
| 58 | const T t1[] = {5, 5, 5}; |
| 59 | const T t2[] = {5}; |
| 60 | C c1(std::begin(arr: t1), std::end(arr: t1)); |
| 61 | C c2(std::begin(arr: t2), std::end(arr: t2)); |
| 62 | do_unique(c1, 2); |
| 63 | assert(c1 == c2); |
| 64 | } |
| 65 | { |
| 66 | typedef int T; |
| 67 | typedef std::forward_list<T> C; |
| 68 | C c1; |
| 69 | C c2; |
| 70 | do_unique(c1, 0); |
| 71 | assert(c1 == c2); |
| 72 | } |
| 73 | { |
| 74 | typedef int T; |
| 75 | typedef std::forward_list<T> C; |
| 76 | const T t1[] = {5, 5, 5, 0}; |
| 77 | const T t2[] = {5, 0}; |
| 78 | C c1(std::begin(arr: t1), std::end(arr: t1)); |
| 79 | C c2(std::begin(arr: t2), std::end(arr: t2)); |
| 80 | do_unique(c1, 2); |
| 81 | assert(c1 == c2); |
| 82 | } |
| 83 | #if TEST_STD_VER >= 11 |
| 84 | { |
| 85 | typedef int T; |
| 86 | typedef std::forward_list<T, min_allocator<T>> C; |
| 87 | const T t1[] = {0, 5, 5, 0, 0, 0, 5}; |
| 88 | const T t2[] = {0, 5, 0, 5}; |
| 89 | C c1(std::begin(t1), std::end(t1)); |
| 90 | C c2(std::begin(t2), std::end(t2)); |
| 91 | do_unique(c1, 3); |
| 92 | assert(c1 == c2); |
| 93 | } |
| 94 | { |
| 95 | typedef int T; |
| 96 | typedef std::forward_list<T, min_allocator<T>> C; |
| 97 | const T t1[] = {0, 0, 0, 0}; |
| 98 | const T t2[] = {0}; |
| 99 | C c1(std::begin(t1), std::end(t1)); |
| 100 | C c2(std::begin(t2), std::end(t2)); |
| 101 | do_unique(c1, 3); |
| 102 | assert(c1 == c2); |
| 103 | } |
| 104 | { |
| 105 | typedef int T; |
| 106 | typedef std::forward_list<T, min_allocator<T>> C; |
| 107 | const T t1[] = {5, 5, 5}; |
| 108 | const T t2[] = {5}; |
| 109 | C c1(std::begin(t1), std::end(t1)); |
| 110 | C c2(std::begin(t2), std::end(t2)); |
| 111 | do_unique(c1, 2); |
| 112 | assert(c1 == c2); |
| 113 | } |
| 114 | { |
| 115 | typedef int T; |
| 116 | typedef std::forward_list<T, min_allocator<T>> C; |
| 117 | C c1; |
| 118 | C c2; |
| 119 | do_unique(c1, 0); |
| 120 | assert(c1 == c2); |
| 121 | } |
| 122 | { |
| 123 | typedef int T; |
| 124 | typedef std::forward_list<T, min_allocator<T>> C; |
| 125 | const T t1[] = {5, 5, 5, 0}; |
| 126 | const T t2[] = {5, 0}; |
| 127 | C c1(std::begin(t1), std::end(t1)); |
| 128 | C c2(std::begin(t2), std::end(t2)); |
| 129 | do_unique(c1, 2); |
| 130 | assert(c1 == c2); |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | int main(int, char**) { |
| 138 | assert(test()); |
| 139 | #if TEST_STD_VER >= 26 |
| 140 | static_assert(test()); |
| 141 | #endif |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |