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