15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
95a83710eSEric Fiselier // <numeric>
105a83710eSEric Fiselier 
1167c88e47SMark de Wever // Became constexpr in C++20
125a83710eSEric Fiselier // template <InputIterator InIter,
135a83710eSEric Fiselier //           OutputIterator<auto, const InIter::value_type&> OutIter,
145a83710eSEric Fiselier //           Callable<auto, const InIter::value_type&, const InIter::value_type&> BinaryOperation>
155a83710eSEric Fiselier //   requires Constructible<InIter::value_type, InIter::reference>
165a83710eSEric Fiselier //         && OutputIterator<OutIter, BinaryOperation::result_type>
175a83710eSEric Fiselier //         && MoveAssignable<InIter::value_type>
185a83710eSEric Fiselier //         && CopyConstructible<BinaryOperation>
195a83710eSEric Fiselier //   OutIter
205a83710eSEric Fiselier //   adjacent_difference(InIter first, InIter last, OutIter result, BinaryOperation binary_op);
215a83710eSEric Fiselier 
225a83710eSEric Fiselier #include <numeric>
235a83710eSEric Fiselier #include <functional>
24b2943765Szoecarver #include <string>
255a83710eSEric Fiselier #include <cassert>
265a83710eSEric Fiselier 
274f73dbf4SEric Fiselier #include "test_macros.h"
285a83710eSEric Fiselier #include "test_iterators.h"
295a83710eSEric Fiselier 
30b2943765Szoecarver #if TEST_STD_VER > 17
31b2943765Szoecarver struct rvalue_subtractable
32b2943765Szoecarver {
33b2943765Szoecarver     bool correctOperatorUsed = false;
34b2943765Szoecarver 
35b2943765Szoecarver     // make sure the predicate is passed an rvalue and an lvalue (so check that the first argument was moved)
operator ()rvalue_subtractable3667c88e47SMark de Wever     constexpr rvalue_subtractable operator()(rvalue_subtractable const&, rvalue_subtractable&& r) {
37b2943765Szoecarver         r.correctOperatorUsed = true;
38b2943765Szoecarver         return std::move(r);
39b2943765Szoecarver     }
40b2943765Szoecarver };
41b2943765Szoecarver 
operator -(rvalue_subtractable const &,rvalue_subtractable & rhs)4267c88e47SMark de Wever constexpr rvalue_subtractable operator-(rvalue_subtractable const&, rvalue_subtractable& rhs)
43b2943765Szoecarver {
44b2943765Szoecarver     rhs.correctOperatorUsed = false;
45b2943765Szoecarver     return rhs;
46b2943765Szoecarver }
47b2943765Szoecarver 
operator -(rvalue_subtractable const &,rvalue_subtractable && rhs)4867c88e47SMark de Wever constexpr rvalue_subtractable operator-(rvalue_subtractable const&, rvalue_subtractable&& rhs)
49b2943765Szoecarver {
50b2943765Szoecarver     rhs.correctOperatorUsed = true;
51b2943765Szoecarver     return std::move(rhs);
52b2943765Szoecarver }
53b2943765Szoecarver 
5467c88e47SMark de Wever constexpr void
test_use_move()55b2943765Szoecarver test_use_move()
56b2943765Szoecarver {
57b2943765Szoecarver     const std::size_t size = 100;
58b2943765Szoecarver     rvalue_subtractable arr[size];
59b2943765Szoecarver     rvalue_subtractable res1[size];
60b2943765Szoecarver     rvalue_subtractable res2[size];
61b2943765Szoecarver     std::adjacent_difference(arr, arr + size, res1);
62b2943765Szoecarver     std::adjacent_difference(arr, arr + size, res2, /*predicate=*/rvalue_subtractable());
63b2943765Szoecarver     // start at 1 because the first element is not moved
64b2943765Szoecarver     for (unsigned i = 1; i < size; ++i) assert(res1[i].correctOperatorUsed);
65b2943765Szoecarver     for (unsigned i = 1; i < size; ++i) assert(res2[i].correctOperatorUsed);
66b2943765Szoecarver }
67b2943765Szoecarver #endif // TEST_STD_VER > 17
68b2943765Szoecarver 
6967c88e47SMark de Wever // C++20 can use string in constexpr evaluation, but both libc++ and MSVC
7067c88e47SMark de Wever // don't have the support yet. In these cases omit the constexpr test.
7167c88e47SMark de Wever // FIXME Remove constexpr string workaround introduced in D90569
7267c88e47SMark de Wever #if TEST_STD_VER > 17 && \
7367c88e47SMark de Wever 	(!defined(__cpp_lib_constexpr_string) || __cpp_lib_constexpr_string < 201907L)
74b2943765Szoecarver void
7567c88e47SMark de Wever #else
7667c88e47SMark de Wever TEST_CONSTEXPR_CXX20 void
7767c88e47SMark de Wever #endif
test_string()78b2943765Szoecarver test_string()
79b2943765Szoecarver {
80b2943765Szoecarver     std::string sa[] = {"a", "b", "c"};
81b2943765Szoecarver     std::string sr[] = {"a", "ba", "cb"};
82b2943765Szoecarver     std::string output[3];
83b2943765Szoecarver     std::adjacent_difference(sa, sa + 3, output, std::plus<std::string>());
84b2943765Szoecarver     for (unsigned i = 0; i < 3; ++i) assert(output[i] == sr[i]);
85b2943765Szoecarver }
86b2943765Szoecarver 
875a83710eSEric Fiselier template <class InIter, class OutIter>
8867c88e47SMark de Wever TEST_CONSTEXPR_CXX20 void
test()895a83710eSEric Fiselier test()
905a83710eSEric Fiselier {
915a83710eSEric Fiselier     int ia[] = {15, 10, 6, 3, 1};
925a83710eSEric Fiselier     int ir[] = {15, 25, 16, 9, 4};
935a83710eSEric Fiselier     const unsigned s = sizeof(ia) / sizeof(ia[0]);
945a83710eSEric Fiselier     int ib[s] = {0};
955a83710eSEric Fiselier     OutIter r = std::adjacent_difference(InIter(ia), InIter(ia+s), OutIter(ib),
965a83710eSEric Fiselier                                          std::plus<int>());
975a83710eSEric Fiselier     assert(base(r) == ib + s);
985a83710eSEric Fiselier     for (unsigned i = 0; i < s; ++i)
995a83710eSEric Fiselier         assert(ib[i] == ir[i]);
1005a83710eSEric Fiselier }
1015a83710eSEric Fiselier 
1024f73dbf4SEric Fiselier #if TEST_STD_VER >= 11
1035a83710eSEric Fiselier 
1045a83710eSEric Fiselier class Y;
1055a83710eSEric Fiselier 
1065a83710eSEric Fiselier class X
1075a83710eSEric Fiselier {
1085a83710eSEric Fiselier     int i_;
1095a83710eSEric Fiselier 
11067c88e47SMark de Wever     TEST_CONSTEXPR_CXX20 X& operator=(const X&);
1115a83710eSEric Fiselier public:
X(int i)11267c88e47SMark de Wever     TEST_CONSTEXPR_CXX20 explicit X(int i) : i_(i) {}
X(const X & x)11367c88e47SMark de Wever     TEST_CONSTEXPR_CXX20 X(const X& x) : i_(x.i_) {}
operator =(X && x)11467c88e47SMark de Wever     TEST_CONSTEXPR_CXX20 X& operator=(X&& x)
1155a83710eSEric Fiselier     {
1165a83710eSEric Fiselier         i_ = x.i_;
1175a83710eSEric Fiselier         x.i_ = -1;
1185a83710eSEric Fiselier         return *this;
1195a83710eSEric Fiselier     }
1205a83710eSEric Fiselier 
operator -(const X & x,const X & y)12167c88e47SMark de Wever     TEST_CONSTEXPR_CXX20 friend X operator-(const X& x, const X& y) {return X(x.i_ - y.i_);}
1225a83710eSEric Fiselier 
1235a83710eSEric Fiselier     friend class Y;
1245a83710eSEric Fiselier };
1255a83710eSEric Fiselier 
1265a83710eSEric Fiselier class Y
1275a83710eSEric Fiselier {
1285a83710eSEric Fiselier     int i_;
1295a83710eSEric Fiselier 
13067c88e47SMark de Wever     TEST_CONSTEXPR_CXX20 Y& operator=(const Y&);
1315a83710eSEric Fiselier public:
Y(int i)13267c88e47SMark de Wever     TEST_CONSTEXPR_CXX20 explicit Y(int i) : i_(i) {}
Y(const Y & y)13367c88e47SMark de Wever     TEST_CONSTEXPR_CXX20 Y(const Y& y) : i_(y.i_) {}
operator =(const X & x)13467c88e47SMark de Wever     TEST_CONSTEXPR_CXX20 void operator=(const X& x) {i_ = x.i_;}
1355a83710eSEric Fiselier };
1365a83710eSEric Fiselier 
1375a83710eSEric Fiselier #endif
1385a83710eSEric Fiselier 
1395a83710eSEric Fiselier 
14067c88e47SMark de Wever TEST_CONSTEXPR_CXX20 bool
test()14167c88e47SMark de Wever test()
1425a83710eSEric Fiselier {
143*5e97d37bSMark de Wever     test<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
144773ae441SChristopher Di Bella     test<cpp17_input_iterator<const int*>, forward_iterator<int*> >();
145773ae441SChristopher Di Bella     test<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
146773ae441SChristopher Di Bella     test<cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
147773ae441SChristopher Di Bella     test<cpp17_input_iterator<const int*>, int*>();
1485a83710eSEric Fiselier 
149*5e97d37bSMark de Wever     test<forward_iterator<const int*>, cpp17_output_iterator<int*> >();
1505a83710eSEric Fiselier     test<forward_iterator<const int*>, forward_iterator<int*> >();
1515a83710eSEric Fiselier     test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
1525a83710eSEric Fiselier     test<forward_iterator<const int*>, random_access_iterator<int*> >();
1535a83710eSEric Fiselier     test<forward_iterator<const int*>, int*>();
1545a83710eSEric Fiselier 
155*5e97d37bSMark de Wever     test<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
1565a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
1575a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
1585a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
1595a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, int*>();
1605a83710eSEric Fiselier 
161*5e97d37bSMark de Wever     test<random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
1625a83710eSEric Fiselier     test<random_access_iterator<const int*>, forward_iterator<int*> >();
1635a83710eSEric Fiselier     test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
1645a83710eSEric Fiselier     test<random_access_iterator<const int*>, random_access_iterator<int*> >();
1655a83710eSEric Fiselier     test<random_access_iterator<const int*>, int*>();
1665a83710eSEric Fiselier 
167*5e97d37bSMark de Wever     test<const int*, cpp17_output_iterator<int*> >();
1685a83710eSEric Fiselier     test<const int*, forward_iterator<int*> >();
1695a83710eSEric Fiselier     test<const int*, bidirectional_iterator<int*> >();
1705a83710eSEric Fiselier     test<const int*, random_access_iterator<int*> >();
1715a83710eSEric Fiselier     test<const int*, int*>();
1725a83710eSEric Fiselier 
1734f73dbf4SEric Fiselier #if TEST_STD_VER >= 11
1745a83710eSEric Fiselier     X x[3] = {X(1), X(2), X(3)};
1755a83710eSEric Fiselier     Y y[3] = {Y(1), Y(2), Y(3)};
1765a83710eSEric Fiselier     std::adjacent_difference(x, x+3, y, std::minus<X>());
1775a83710eSEric Fiselier #endif
1782df59c50SJF Bastien 
179b2943765Szoecarver #if TEST_STD_VER > 17
180b2943765Szoecarver     test_use_move();
181b2943765Szoecarver #endif // TEST_STD_VER > 17
18267c88e47SMark de Wever     // C++20 can use string in constexpr evaluation, but both libc++ and MSVC
18367c88e47SMark de Wever     // don't have the support yet. In these cases omit the constexpr test.
18467c88e47SMark de Wever     // FIXME Remove constexpr string workaround introduced in D90569
18567c88e47SMark de Wever #if TEST_STD_VER > 17 && \
18667c88e47SMark de Wever 	(!defined(__cpp_lib_constexpr_string) || __cpp_lib_constexpr_string < 201907L)
18767c88e47SMark de Wever 	if (!std::is_constant_evaluated())
18867c88e47SMark de Wever #endif
189b2943765Szoecarver     test_string();
190b2943765Szoecarver 
19167c88e47SMark de Wever     return true;
19267c88e47SMark de Wever }
19367c88e47SMark de Wever 
main(int,char **)19467c88e47SMark de Wever int main(int, char**)
19567c88e47SMark de Wever {
19667c88e47SMark de Wever     test();
19767c88e47SMark de Wever #if TEST_STD_VER > 17
19867c88e47SMark de Wever     static_assert(test());
19967c88e47SMark de Wever #endif
2002df59c50SJF Bastien     return 0;
2015a83710eSEric Fiselier }
202