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