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 // <numeric>
10 // UNSUPPORTED: clang-8
11 // UNSUPPORTED: gcc-9
12
13 // Became constexpr in C++20
14 // template <InputIterator InIter,
15 // OutputIterator<auto, const InIter::value_type&> OutIter>
16 // requires HasMinus<InIter::value_type, InIter::value_type>
17 // && Constructible<InIter::value_type, InIter::reference>
18 // && OutputIterator<OutIter,
19 // HasMinus<InIter::value_type, InIter::value_type>::result_type>
20 // && MoveAssignable<InIter::value_type>
21 // OutIter
22 // adjacent_difference(InIter first, InIter last, OutIter result);
23
24 #include <numeric>
25 #include <cassert>
26
27 #include "test_macros.h"
28 #include "test_iterators.h"
29
30 template <class InIter, class OutIter>
31 TEST_CONSTEXPR_CXX20 void
test()32 test()
33 {
34 int ia[] = {15, 10, 6, 3, 1};
35 int ir[] = {15, -5, -4, -3, -2};
36 const unsigned s = sizeof(ia) / sizeof(ia[0]);
37 int ib[s] = {0};
38 OutIter r = std::adjacent_difference(InIter(ia), InIter(ia+s), OutIter(ib));
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 TEST_CONSTEXPR_CXX20 X& operator=(const X&);
53 public:
X(int i)54 TEST_CONSTEXPR_CXX20 explicit X(int i) : i_(i) {}
X(const X & x)55 TEST_CONSTEXPR_CXX20 X(const X& x) : i_(x.i_) {}
operator =(X && x)56 TEST_CONSTEXPR_CXX20 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 TEST_CONSTEXPR_CXX20 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 TEST_CONSTEXPR_CXX20 Y& operator=(const Y&);
73 public:
Y(int i)74 TEST_CONSTEXPR_CXX20 explicit Y(int i) : i_(i) {}
Y(const Y & y)75 TEST_CONSTEXPR_CXX20 Y(const Y& y) : i_(y.i_) {}
operator =(const X & x)76 TEST_CONSTEXPR_CXX20 void operator=(const X& x) {i_ = x.i_;}
77 };
78
79 #endif
80
81 TEST_CONSTEXPR_CXX20 bool
test()82 test()
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);
118 #endif
119
120 return true;
121 }
122
main(int,char **)123 int main(int, char**)
124 {
125 test();
126 #if TEST_STD_VER > 17
127 static_assert(test());
128 #endif
129 return 0;
130 }
131