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 // <algorithm>
10
11 // template<InputIterator InIter, class OutIter>
12 // requires OutputIterator<OutIter, RvalueOf<InIter::value_type>::type>
13 // && EqualityComparable<InIter::value_type>
14 // && HasAssign<InIter::value_type, InIter::reference>
15 // && Constructible<InIter::value_type, InIter::reference>
16 // constexpr OutIter // constexpr after C++17
17 // unique_copy(InIter first, InIter last, OutIter result);
18
19 #include <algorithm>
20 #include <cassert>
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, 2, 2, 4};
28 int ib[] = {0, 0, 0, 0, 0};
29 const int expected[] = {0, 1, 2, 4};
30
31 auto it = std::unique_copy(std::begin(ia), std::end(ia), std::begin(ib));
32 return it == (std::begin(ib) + std::size(expected))
33 && *it == 0 // don't overwrite final value in output
34 && std::equal(std::begin(ib), it, std::begin(expected), std::end(expected))
35 ;
36 }
37 #endif
38
39 template <class InIter, class OutIter>
40 void
test()41 test()
42 {
43 const int ia[] = {0};
44 const unsigned sa = sizeof(ia)/sizeof(ia[0]);
45 int ja[sa] = {-1};
46 OutIter r = std::unique_copy(InIter(ia), InIter(ia+sa), OutIter(ja));
47 assert(base(r) == ja + sa);
48 assert(ja[0] == 0);
49
50 const int ib[] = {0, 1};
51 const unsigned sb = sizeof(ib)/sizeof(ib[0]);
52 int jb[sb] = {-1};
53 r = std::unique_copy(InIter(ib), InIter(ib+sb), OutIter(jb));
54 assert(base(r) == jb + sb);
55 assert(jb[0] == 0);
56 assert(jb[1] == 1);
57
58 const int ic[] = {0, 0};
59 const unsigned sc = sizeof(ic)/sizeof(ic[0]);
60 int jc[sc] = {-1};
61 r = std::unique_copy(InIter(ic), InIter(ic+sc), OutIter(jc));
62 assert(base(r) == jc + 1);
63 assert(jc[0] == 0);
64
65 const int id[] = {0, 0, 1};
66 const unsigned sd = sizeof(id)/sizeof(id[0]);
67 int jd[sd] = {-1};
68 r = std::unique_copy(InIter(id), InIter(id+sd), OutIter(jd));
69 assert(base(r) == jd + 2);
70 assert(jd[0] == 0);
71 assert(jd[1] == 1);
72
73 const int ie[] = {0, 0, 1, 0};
74 const unsigned se = sizeof(ie)/sizeof(ie[0]);
75 int je[se] = {-1};
76 r = std::unique_copy(InIter(ie), InIter(ie+se), OutIter(je));
77 assert(base(r) == je + 3);
78 assert(je[0] == 0);
79 assert(je[1] == 1);
80 assert(je[2] == 0);
81
82 const int ig[] = {0, 0, 1, 1};
83 const unsigned sg = sizeof(ig)/sizeof(ig[0]);
84 int jg[sg] = {-1};
85 r = std::unique_copy(InIter(ig), InIter(ig+sg), OutIter(jg));
86 assert(base(r) == jg + 2);
87 assert(jg[0] == 0);
88 assert(jg[1] == 1);
89
90 const int ih[] = {0, 1, 1};
91 const unsigned sh = sizeof(ih)/sizeof(ih[0]);
92 int jh[sh] = {-1};
93 r = std::unique_copy(InIter(ih), InIter(ih+sh), OutIter(jh));
94 assert(base(r) == jh + 2);
95 assert(jh[0] == 0);
96 assert(jh[1] == 1);
97
98 const int ii[] = {0, 1, 1, 1, 2, 2, 2};
99 const unsigned si = sizeof(ii)/sizeof(ii[0]);
100 int ji[si] = {-1};
101 r = std::unique_copy(InIter(ii), InIter(ii+si), OutIter(ji));
102 assert(base(r) == ji + 3);
103 assert(ji[0] == 0);
104 assert(ji[1] == 1);
105 assert(ji[2] == 2);
106 }
107
main(int,char **)108 int main(int, char**)
109 {
110 test<input_iterator<const int*>, output_iterator<int*> >();
111 test<input_iterator<const int*>, forward_iterator<int*> >();
112 test<input_iterator<const int*>, bidirectional_iterator<int*> >();
113 test<input_iterator<const int*>, random_access_iterator<int*> >();
114 test<input_iterator<const int*>, int*>();
115
116 test<forward_iterator<const int*>, output_iterator<int*> >();
117 test<forward_iterator<const int*>, forward_iterator<int*> >();
118 test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
119 test<forward_iterator<const int*>, random_access_iterator<int*> >();
120 test<forward_iterator<const int*>, int*>();
121
122 test<bidirectional_iterator<const int*>, output_iterator<int*> >();
123 test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
124 test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
125 test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
126 test<bidirectional_iterator<const int*>, int*>();
127
128 test<random_access_iterator<const int*>, output_iterator<int*> >();
129 test<random_access_iterator<const int*>, forward_iterator<int*> >();
130 test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
131 test<random_access_iterator<const int*>, random_access_iterator<int*> >();
132 test<random_access_iterator<const int*>, int*>();
133
134 test<const int*, output_iterator<int*> >();
135 test<const int*, forward_iterator<int*> >();
136 test<const int*, bidirectional_iterator<int*> >();
137 test<const int*, random_access_iterator<int*> >();
138 test<const int*, int*>();
139
140 #if TEST_STD_VER > 17
141 static_assert(test_constexpr());
142 #endif
143
144 return 0;
145 }
146