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