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