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 // <numeric>
11
12 // template <InputIterator InIter,
13 // OutputIterator<auto, const InIter::value_type&> OutIter,
14 // Callable<auto, const InIter::value_type&, const InIter::value_type&> BinaryOperation>
15 // requires Constructible<InIter::value_type, InIter::reference>
16 // && OutputIterator<OutIter, BinaryOperation::result_type>
17 // && MoveAssignable<InIter::value_type>
18 // && CopyConstructible<BinaryOperation>
19 // OutIter
20 // adjacent_difference(InIter first, InIter last, OutIter result, BinaryOperation binary_op);
21
22 #include <numeric>
23 #include <functional>
24 #include <cassert>
25
26 #include "test_macros.h"
27 #include "test_iterators.h"
28
29 template <class InIter, class OutIter>
30 void
test()31 test()
32 {
33 int ia[] = {15, 10, 6, 3, 1};
34 int ir[] = {15, 25, 16, 9, 4};
35 const unsigned s = sizeof(ia) / sizeof(ia[0]);
36 int ib[s] = {0};
37 OutIter r = std::adjacent_difference(InIter(ia), InIter(ia+s), OutIter(ib),
38 std::plus<int>());
39 assert(base(r) == ib + s);
40 for (unsigned i = 0; i < s; ++i)
41 assert(ib[i] == ir[i]);
42 }
43
44 #if TEST_STD_VER >= 11
45
46 class Y;
47
48 class X
49 {
50 int i_;
51
52 X& operator=(const X&);
53 public:
X(int i)54 explicit X(int i) : i_(i) {}
X(const X & x)55 X(const X& x) : i_(x.i_) {}
operator =(X && x)56 X& operator=(X&& x)
57 {
58 i_ = x.i_;
59 x.i_ = -1;
60 return *this;
61 }
62
operator -(const X & x,const X & y)63 friend X operator-(const X& x, const X& y) {return X(x.i_ - y.i_);}
64
65 friend class Y;
66 };
67
68 class Y
69 {
70 int i_;
71
72 Y& operator=(const Y&);
73 public:
Y(int i)74 explicit Y(int i) : i_(i) {}
Y(const Y & y)75 Y(const Y& y) : i_(y.i_) {}
operator =(const X & x)76 void operator=(const X& x) {i_ = x.i_;}
77 };
78
79 #endif
80
81
main()82 int main()
83 {
84 test<input_iterator<const int*>, output_iterator<int*> >();
85 test<input_iterator<const int*>, forward_iterator<int*> >();
86 test<input_iterator<const int*>, bidirectional_iterator<int*> >();
87 test<input_iterator<const int*>, random_access_iterator<int*> >();
88 test<input_iterator<const int*>, int*>();
89
90 test<forward_iterator<const int*>, output_iterator<int*> >();
91 test<forward_iterator<const int*>, forward_iterator<int*> >();
92 test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
93 test<forward_iterator<const int*>, random_access_iterator<int*> >();
94 test<forward_iterator<const int*>, int*>();
95
96 test<bidirectional_iterator<const int*>, output_iterator<int*> >();
97 test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
98 test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
99 test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
100 test<bidirectional_iterator<const int*>, int*>();
101
102 test<random_access_iterator<const int*>, output_iterator<int*> >();
103 test<random_access_iterator<const int*>, forward_iterator<int*> >();
104 test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
105 test<random_access_iterator<const int*>, random_access_iterator<int*> >();
106 test<random_access_iterator<const int*>, int*>();
107
108 test<const int*, output_iterator<int*> >();
109 test<const int*, forward_iterator<int*> >();
110 test<const int*, bidirectional_iterator<int*> >();
111 test<const int*, random_access_iterator<int*> >();
112 test<const int*, int*>();
113
114 #if TEST_STD_VER >= 11
115 X x[3] = {X(1), X(2), X(3)};
116 Y y[3] = {Y(1), Y(2), Y(3)};
117 std::adjacent_difference(x, x+3, y, std::minus<X>());
118 #endif
119 }
120