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