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 |
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 | void do_unique(L &l, typename L::size_type expected) |
23 | { |
24 | typename L::size_type old_size = std::distance(l.begin(), l.end()); |
25 | #if TEST_STD_VER > 17 |
26 | ASSERT_SAME_TYPE(decltype(l.unique()), typename L::size_type); |
27 | assert(l.unique() == expected); |
28 | #else |
29 | ASSERT_SAME_TYPE(decltype(l.unique()), void); |
30 | l.unique(); |
31 | #endif |
32 | assert(old_size - std::distance(l.begin(), l.end()) == expected); |
33 | } |
34 | |
35 | int main(int, char**) |
36 | { |
37 | { |
38 | typedef int T; |
39 | typedef std::forward_list<T> C; |
40 | const T t1[] = {0, 5, 5, 0, 0, 0, 5}; |
41 | const T t2[] = {0, 5, 0, 5}; |
42 | C c1(std::begin(arr: t1), std::end(arr: t1)); |
43 | C c2(std::begin(arr: t2), std::end(arr: t2)); |
44 | do_unique(l&: c1, expected: 3); |
45 | assert(c1 == c2); |
46 | } |
47 | { |
48 | typedef int T; |
49 | typedef std::forward_list<T> C; |
50 | const T t1[] = {0, 0, 0, 0}; |
51 | const T t2[] = {0}; |
52 | C c1(std::begin(arr: t1), std::end(arr: t1)); |
53 | C c2(std::begin(arr: t2), std::end(arr: t2)); |
54 | do_unique(l&: c1, expected: 3); |
55 | assert(c1 == c2); |
56 | } |
57 | { |
58 | typedef int T; |
59 | typedef std::forward_list<T> C; |
60 | const T t1[] = {5, 5, 5}; |
61 | const T t2[] = {5}; |
62 | C c1(std::begin(arr: t1), std::end(arr: t1)); |
63 | C c2(std::begin(arr: t2), std::end(arr: t2)); |
64 | do_unique(l&: c1, expected: 2); |
65 | assert(c1 == c2); |
66 | } |
67 | { |
68 | typedef int T; |
69 | typedef std::forward_list<T> C; |
70 | C c1; |
71 | C c2; |
72 | do_unique(l&: c1, expected: 0); |
73 | assert(c1 == c2); |
74 | } |
75 | { |
76 | typedef int T; |
77 | typedef std::forward_list<T> C; |
78 | const T t1[] = {5, 5, 5, 0}; |
79 | const T t2[] = {5, 0}; |
80 | C c1(std::begin(arr: t1), std::end(arr: t1)); |
81 | C c2(std::begin(arr: t2), std::end(arr: t2)); |
82 | do_unique(l&: c1, expected: 2); |
83 | assert(c1 == c2); |
84 | } |
85 | #if TEST_STD_VER >= 11 |
86 | { |
87 | typedef int T; |
88 | typedef std::forward_list<T, min_allocator<T>> C; |
89 | const T t1[] = {0, 5, 5, 0, 0, 0, 5}; |
90 | const T t2[] = {0, 5, 0, 5}; |
91 | C c1(std::begin(t1), std::end(t1)); |
92 | C c2(std::begin(t2), std::end(t2)); |
93 | do_unique(c1, 3); |
94 | assert(c1 == c2); |
95 | } |
96 | { |
97 | typedef int T; |
98 | typedef std::forward_list<T, min_allocator<T>> C; |
99 | const T t1[] = {0, 0, 0, 0}; |
100 | const T t2[] = {0}; |
101 | C c1(std::begin(t1), std::end(t1)); |
102 | C c2(std::begin(t2), std::end(t2)); |
103 | do_unique(c1, 3); |
104 | assert(c1 == c2); |
105 | } |
106 | { |
107 | typedef int T; |
108 | typedef std::forward_list<T, min_allocator<T>> C; |
109 | const T t1[] = {5, 5, 5}; |
110 | const T t2[] = {5}; |
111 | C c1(std::begin(t1), std::end(t1)); |
112 | C c2(std::begin(t2), std::end(t2)); |
113 | do_unique(c1, 2); |
114 | assert(c1 == c2); |
115 | } |
116 | { |
117 | typedef int T; |
118 | typedef std::forward_list<T, min_allocator<T>> C; |
119 | C c1; |
120 | C c2; |
121 | do_unique(c1, 0); |
122 | assert(c1 == c2); |
123 | } |
124 | { |
125 | typedef int T; |
126 | typedef std::forward_list<T, min_allocator<T>> C; |
127 | const T t1[] = {5, 5, 5, 0}; |
128 | const T t2[] = {5, 0}; |
129 | C c1(std::begin(t1), std::end(t1)); |
130 | C c2(std::begin(t2), std::end(t2)); |
131 | do_unique(c1, 2); |
132 | assert(c1 == c2); |
133 | } |
134 | #endif |
135 | |
136 | return 0; |
137 | } |
138 | |