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 
12 // Became constexpr in C++20
13 // template <InputIterator InIter,
14 //           OutputIterator<auto, const InIter::value_type&> OutIter,
15 //           Callable<auto, const InIter::value_type&, const InIter::value_type&> BinaryOperation>
16 //   requires Constructible<InIter::value_type, InIter::reference>
17 //         && OutputIterator<OutIter, BinaryOperation::result_type>
18 //         && MoveAssignable<InIter::value_type>
19 //         && CopyConstructible<BinaryOperation>
20 //   OutIter
21 //   adjacent_difference(InIter first, InIter last, OutIter result, BinaryOperation binary_op);
22 
23 #include <numeric>
24 #include <functional>
25 #include <string>
26 #include <cassert>
27 
28 #include "test_macros.h"
29 #include "test_iterators.h"
30 
31 #if TEST_STD_VER > 17
32 struct rvalue_subtractable
33 {
34     bool correctOperatorUsed = false;
35 
36     // make sure the predicate is passed an rvalue and an lvalue (so check that the first argument was moved)
37     constexpr rvalue_subtractable operator()(rvalue_subtractable const&, rvalue_subtractable&& r) {
38         r.correctOperatorUsed = true;
39         return std::move(r);
40     }
41 };
42 
43 constexpr rvalue_subtractable operator-(rvalue_subtractable const&, rvalue_subtractable& rhs)
44 {
45     rhs.correctOperatorUsed = false;
46     return rhs;
47 }
48 
49 constexpr rvalue_subtractable operator-(rvalue_subtractable const&, rvalue_subtractable&& rhs)
50 {
51     rhs.correctOperatorUsed = true;
52     return std::move(rhs);
53 }
54 
55 constexpr void
56 test_use_move()
57 {
58     const std::size_t size = 100;
59     rvalue_subtractable arr[size];
60     rvalue_subtractable res1[size];
61     rvalue_subtractable res2[size];
62     std::adjacent_difference(arr, arr + size, res1);
63     std::adjacent_difference(arr, arr + size, res2, /*predicate=*/rvalue_subtractable());
64     // start at 1 because the first element is not moved
65     for (unsigned i = 1; i < size; ++i) assert(res1[i].correctOperatorUsed);
66     for (unsigned i = 1; i < size; ++i) assert(res2[i].correctOperatorUsed);
67 }
68 #endif // TEST_STD_VER > 17
69 
70 // C++20 can use string in constexpr evaluation, but both libc++ and MSVC
71 // don't have the support yet. In these cases omit the constexpr test.
72 // FIXME Remove constexpr string workaround introduced in D90569
73 #if TEST_STD_VER > 17 && \
74 	(!defined(__cpp_lib_constexpr_string) || __cpp_lib_constexpr_string < 201907L)
75 void
76 #else
77 TEST_CONSTEXPR_CXX20 void
78 #endif
79 test_string()
80 {
81     std::string sa[] = {"a", "b", "c"};
82     std::string sr[] = {"a", "ba", "cb"};
83     std::string output[3];
84     std::adjacent_difference(sa, sa + 3, output, std::plus<std::string>());
85     for (unsigned i = 0; i < 3; ++i) assert(output[i] == sr[i]);
86 }
87 
88 template <class InIter, class OutIter>
89 TEST_CONSTEXPR_CXX20 void
90 test()
91 {
92     int ia[] = {15, 10, 6, 3, 1};
93     int ir[] = {15, 25, 16, 9, 4};
94     const unsigned s = sizeof(ia) / sizeof(ia[0]);
95     int ib[s] = {0};
96     OutIter r = std::adjacent_difference(InIter(ia), InIter(ia+s), OutIter(ib),
97                                          std::plus<int>());
98     assert(base(r) == ib + s);
99     for (unsigned i = 0; i < s; ++i)
100         assert(ib[i] == ir[i]);
101 }
102 
103 #if TEST_STD_VER >= 11
104 
105 class Y;
106 
107 class X
108 {
109     int i_;
110 
111     TEST_CONSTEXPR_CXX20 X& operator=(const X&);
112 public:
113     TEST_CONSTEXPR_CXX20 explicit X(int i) : i_(i) {}
114     TEST_CONSTEXPR_CXX20 X(const X& x) : i_(x.i_) {}
115     TEST_CONSTEXPR_CXX20 X& operator=(X&& x)
116     {
117         i_ = x.i_;
118         x.i_ = -1;
119         return *this;
120     }
121 
122     TEST_CONSTEXPR_CXX20 friend X operator-(const X& x, const X& y) {return X(x.i_ - y.i_);}
123 
124     friend class Y;
125 };
126 
127 class Y
128 {
129     int i_;
130 
131     TEST_CONSTEXPR_CXX20 Y& operator=(const Y&);
132 public:
133     TEST_CONSTEXPR_CXX20 explicit Y(int i) : i_(i) {}
134     TEST_CONSTEXPR_CXX20 Y(const Y& y) : i_(y.i_) {}
135     TEST_CONSTEXPR_CXX20 void operator=(const X& x) {i_ = x.i_;}
136 };
137 
138 #endif
139 
140 
141 TEST_CONSTEXPR_CXX20 bool
142 test()
143 {
144     test<cpp17_input_iterator<const int*>, output_iterator<int*> >();
145     test<cpp17_input_iterator<const int*>, forward_iterator<int*> >();
146     test<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
147     test<cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
148     test<cpp17_input_iterator<const int*>, int*>();
149 
150     test<forward_iterator<const int*>, output_iterator<int*> >();
151     test<forward_iterator<const int*>, forward_iterator<int*> >();
152     test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
153     test<forward_iterator<const int*>, random_access_iterator<int*> >();
154     test<forward_iterator<const int*>, int*>();
155 
156     test<bidirectional_iterator<const int*>, output_iterator<int*> >();
157     test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
158     test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
159     test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
160     test<bidirectional_iterator<const int*>, int*>();
161 
162     test<random_access_iterator<const int*>, output_iterator<int*> >();
163     test<random_access_iterator<const int*>, forward_iterator<int*> >();
164     test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
165     test<random_access_iterator<const int*>, random_access_iterator<int*> >();
166     test<random_access_iterator<const int*>, int*>();
167 
168     test<const int*, output_iterator<int*> >();
169     test<const int*, forward_iterator<int*> >();
170     test<const int*, bidirectional_iterator<int*> >();
171     test<const int*, random_access_iterator<int*> >();
172     test<const int*, int*>();
173 
174 #if TEST_STD_VER >= 11
175     X x[3] = {X(1), X(2), X(3)};
176     Y y[3] = {Y(1), Y(2), Y(3)};
177     std::adjacent_difference(x, x+3, y, std::minus<X>());
178 #endif
179 
180 #if TEST_STD_VER > 17
181     test_use_move();
182 #endif // TEST_STD_VER > 17
183     // C++20 can use string in constexpr evaluation, but both libc++ and MSVC
184     // don't have the support yet. In these cases omit the constexpr test.
185     // FIXME Remove constexpr string workaround introduced in D90569
186 #if TEST_STD_VER > 17 && \
187 	(!defined(__cpp_lib_constexpr_string) || __cpp_lib_constexpr_string < 201907L)
188 	if (!std::is_constant_evaluated())
189 #endif
190     test_string();
191 
192     return true;
193 }
194 
195 int main(int, char**)
196 {
197     test();
198 #if TEST_STD_VER > 17
199     static_assert(test());
200 #endif
201     return 0;
202 }
203