1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <algorithm>
11
12 // template<ForwardIterator Iter>
13 // requires OutputIterator<Iter, Iter::reference>
14 // && EqualityComparable<Iter::value_type>
15 // constexpr Iter // constexpr after C++17
16 // unique(Iter first, Iter last);
17
18 #include <algorithm>
19 #include <cassert>
20 #include <memory>
21
22 #include "test_macros.h"
23 #include "test_iterators.h"
24
25 #if TEST_STD_VER > 17
test_constexpr()26 TEST_CONSTEXPR bool test_constexpr() {
27 int ia[] = {0, 1, 1, 3, 4};
28 const int expected[] = {0, 1, 3, 4};
29 const size_t N = 4;
30
31 auto it = std::unique(std::begin(ia), std::end(ia));
32 return it == (std::begin(ia) + N)
33 && std::equal(std::begin(ia), it, std::begin(expected), std::end(expected))
34 ;
35 }
36 #endif
37
38 template <class Iter>
39 void
test()40 test()
41 {
42 int ia[] = {0};
43 const unsigned sa = sizeof(ia)/sizeof(ia[0]);
44 Iter r = std::unique(Iter(ia), Iter(ia+sa));
45 assert(base(r) == ia + sa);
46 assert(ia[0] == 0);
47
48 int ib[] = {0, 1};
49 const unsigned sb = sizeof(ib)/sizeof(ib[0]);
50 r = std::unique(Iter(ib), Iter(ib+sb));
51 assert(base(r) == ib + sb);
52 assert(ib[0] == 0);
53 assert(ib[1] == 1);
54
55 int ic[] = {0, 0};
56 const unsigned sc = sizeof(ic)/sizeof(ic[0]);
57 r = std::unique(Iter(ic), Iter(ic+sc));
58 assert(base(r) == ic + 1);
59 assert(ic[0] == 0);
60
61 int id[] = {0, 0, 1};
62 const unsigned sd = sizeof(id)/sizeof(id[0]);
63 r = std::unique(Iter(id), Iter(id+sd));
64 assert(base(r) == id + 2);
65 assert(id[0] == 0);
66 assert(id[1] == 1);
67
68 int ie[] = {0, 0, 1, 0};
69 const unsigned se = sizeof(ie)/sizeof(ie[0]);
70 r = std::unique(Iter(ie), Iter(ie+se));
71 assert(base(r) == ie + 3);
72 assert(ie[0] == 0);
73 assert(ie[1] == 1);
74 assert(ie[2] == 0);
75
76 int ig[] = {0, 0, 1, 1};
77 const unsigned sg = sizeof(ig)/sizeof(ig[0]);
78 r = std::unique(Iter(ig), Iter(ig+sg));
79 assert(base(r) == ig + 2);
80 assert(ig[0] == 0);
81 assert(ig[1] == 1);
82
83 int ih[] = {0, 1, 1};
84 const unsigned sh = sizeof(ih)/sizeof(ih[0]);
85 r = std::unique(Iter(ih), Iter(ih+sh));
86 assert(base(r) == ih + 2);
87 assert(ih[0] == 0);
88 assert(ih[1] == 1);
89
90 int ii[] = {0, 1, 1, 1, 2, 2, 2};
91 const unsigned si = sizeof(ii)/sizeof(ii[0]);
92 r = std::unique(Iter(ii), Iter(ii+si));
93 assert(base(r) == ii + 3);
94 assert(ii[0] == 0);
95 assert(ii[1] == 1);
96 assert(ii[2] == 2);
97 }
98
99 #if TEST_STD_VER >= 11
100
101 struct do_nothing
102 {
operator ()do_nothing103 void operator()(void*) const {}
104 };
105
106 typedef std::unique_ptr<int, do_nothing> Ptr;
107
108 template <class Iter>
109 void
test1()110 test1()
111 {
112 int one = 1;
113 int two = 2;
114 Ptr ia[1];
115 const unsigned sa = sizeof(ia)/sizeof(ia[0]);
116 Iter r = std::unique(Iter(ia), Iter(ia+sa));
117 assert(base(r) == ia + sa);
118 assert(ia[0] == 0);
119
120 Ptr ib[2];
121 ib[1].reset(&one);
122 const unsigned sb = sizeof(ib)/sizeof(ib[0]);
123 r = std::unique(Iter(ib), Iter(ib+sb));
124 assert(base(r) == ib + sb);
125 assert(ib[0] == 0);
126 assert(*ib[1] == 1);
127
128 Ptr ic[2];
129 const unsigned sc = sizeof(ic)/sizeof(ic[0]);
130 r = std::unique(Iter(ic), Iter(ic+sc));
131 assert(base(r) == ic + 1);
132 assert(ic[0] == 0);
133
134 Ptr id[3];
135 id[2].reset(&one);
136 const unsigned sd = sizeof(id)/sizeof(id[0]);
137 r = std::unique(Iter(id), Iter(id+sd));
138 assert(base(r) == id + 2);
139 assert(id[0] == 0);
140 assert(*id[1] == 1);
141
142 Ptr ie[4];
143 ie[2].reset(&one);
144 const unsigned se = sizeof(ie)/sizeof(ie[0]);
145 r = std::unique(Iter(ie), Iter(ie+se));
146 assert(base(r) == ie + 3);
147 assert(ie[0] == 0);
148 assert(*ie[1] == 1);
149 assert(ie[2] == 0);
150
151 Ptr ig[4];
152 ig[2].reset(&one);
153 ig[3].reset(&one);
154 const unsigned sg = sizeof(ig)/sizeof(ig[0]);
155 r = std::unique(Iter(ig), Iter(ig+sg));
156 assert(base(r) == ig + 2);
157 assert(ig[0] == 0);
158 assert(*ig[1] == 1);
159
160 Ptr ih[3];
161 ih[1].reset(&one);
162 ih[2].reset(&one);
163 const unsigned sh = sizeof(ih)/sizeof(ih[0]);
164 r = std::unique(Iter(ih), Iter(ih+sh));
165 assert(base(r) == ih + 2);
166 assert(ih[0] == 0);
167 assert(*ih[1] == 1);
168
169 Ptr ii[7];
170 ii[1].reset(&one);
171 ii[2].reset(&one);
172 ii[3].reset(&one);
173 ii[4].reset(&two);
174 ii[5].reset(&two);
175 ii[6].reset(&two);
176 const unsigned si = sizeof(ii)/sizeof(ii[0]);
177 r = std::unique(Iter(ii), Iter(ii+si));
178 assert(base(r) == ii + 3);
179 assert(ii[0] == 0);
180 assert(*ii[1] == 1);
181 assert(*ii[2] == 2);
182 }
183 #endif // TEST_STD_VER >= 11
184
main()185 int main()
186 {
187 test<forward_iterator<int*> >();
188 test<bidirectional_iterator<int*> >();
189 test<random_access_iterator<int*> >();
190 test<int*>();
191
192 #if TEST_STD_VER >= 11
193 test1<forward_iterator<Ptr*> >();
194 test1<bidirectional_iterator<Ptr*> >();
195 test1<random_access_iterator<Ptr*> >();
196 test1<Ptr*>();
197 #endif
198
199 #if TEST_STD_VER > 17
200 static_assert(test_constexpr());
201 #endif
202 }
203