Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / numerics / numeric.ops / adjacent.difference / adjacent_difference_op.pass.cpp
blob4cd44580203f45599da285c6f215b3cbfcdd0f8e
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 //===----------------------------------------------------------------------===//
9 // <numeric>
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);
22 #include <numeric>
23 #include <functional>
24 #include <string>
25 #include <cassert>
27 #include "test_macros.h"
28 #include "test_iterators.h"
30 #if TEST_STD_VER > 17
31 struct rvalue_subtractable
33 bool correctOperatorUsed = false;
35 // make sure the predicate is passed an rvalue and an lvalue (so check that the first argument was moved)
36 constexpr rvalue_subtractable operator()(rvalue_subtractable const&, rvalue_subtractable&& r) {
37 r.correctOperatorUsed = true;
38 return std::move(r);
42 constexpr rvalue_subtractable operator-(rvalue_subtractable const&, rvalue_subtractable& rhs)
44 rhs.correctOperatorUsed = false;
45 return rhs;
48 constexpr rvalue_subtractable operator-(rvalue_subtractable const&, rvalue_subtractable&& rhs)
50 rhs.correctOperatorUsed = true;
51 return std::move(rhs);
54 constexpr void
55 test_use_move()
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);
67 #endif // TEST_STD_VER > 17
69 TEST_CONSTEXPR_CXX20 void test_string() {
70 std::string sa[] = {"a", "b", "c"};
71 std::string sr[] = {"a", "ba", "cb"};
72 std::string output[3];
73 std::adjacent_difference(sa, sa + 3, output, std::plus<std::string>());
74 for (unsigned i = 0; i < 3; ++i) assert(output[i] == sr[i]);
77 template <class InIter, class OutIter>
78 TEST_CONSTEXPR_CXX20 void
79 test()
81 int ia[] = {15, 10, 6, 3, 1};
82 int ir[] = {15, 25, 16, 9, 4};
83 const unsigned s = sizeof(ia) / sizeof(ia[0]);
84 int ib[s] = {0};
85 OutIter r = std::adjacent_difference(InIter(ia), InIter(ia+s), OutIter(ib),
86 std::plus<int>());
87 assert(base(r) == ib + s);
88 for (unsigned i = 0; i < s; ++i)
89 assert(ib[i] == ir[i]);
92 #if TEST_STD_VER >= 11
94 class Y;
96 class X
98 int i_;
100 TEST_CONSTEXPR_CXX20 X& operator=(const X&);
101 public:
102 TEST_CONSTEXPR_CXX20 explicit X(int i) : i_(i) {}
103 TEST_CONSTEXPR_CXX20 X(const X& x) : i_(x.i_) {}
104 TEST_CONSTEXPR_CXX20 X& operator=(X&& x)
106 i_ = x.i_;
107 x.i_ = -1;
108 return *this;
111 TEST_CONSTEXPR_CXX20 friend X operator-(const X& x, const X& y) {return X(x.i_ - y.i_);}
113 friend class Y;
116 class Y
118 int i_;
120 TEST_CONSTEXPR_CXX20 Y& operator=(const Y&);
121 public:
122 TEST_CONSTEXPR_CXX20 explicit Y(int i) : i_(i) {}
123 TEST_CONSTEXPR_CXX20 Y(const Y& y) : i_(y.i_) {}
124 TEST_CONSTEXPR_CXX20 void operator=(const X& x) {i_ = x.i_;}
127 #endif
130 TEST_CONSTEXPR_CXX20 bool
131 test()
133 test<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
134 test<cpp17_input_iterator<const int*>, forward_iterator<int*> >();
135 test<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
136 test<cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
137 test<cpp17_input_iterator<const int*>, int*>();
139 test<forward_iterator<const int*>, cpp17_output_iterator<int*> >();
140 test<forward_iterator<const int*>, forward_iterator<int*> >();
141 test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
142 test<forward_iterator<const int*>, random_access_iterator<int*> >();
143 test<forward_iterator<const int*>, int*>();
145 test<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
146 test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
147 test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
148 test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
149 test<bidirectional_iterator<const int*>, int*>();
151 test<random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
152 test<random_access_iterator<const int*>, forward_iterator<int*> >();
153 test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
154 test<random_access_iterator<const int*>, random_access_iterator<int*> >();
155 test<random_access_iterator<const int*>, int*>();
157 test<const int*, cpp17_output_iterator<int*> >();
158 test<const int*, forward_iterator<int*> >();
159 test<const int*, bidirectional_iterator<int*> >();
160 test<const int*, random_access_iterator<int*> >();
161 test<const int*, int*>();
163 #if TEST_STD_VER >= 11
164 X x[3] = {X(1), X(2), X(3)};
165 Y y[3] = {Y(1), Y(2), Y(3)};
166 std::adjacent_difference(x, x+3, y, std::minus<X>());
167 #endif
169 #if TEST_STD_VER > 17
170 test_use_move();
171 #endif // TEST_STD_VER > 17
173 test_string();
175 return true;
178 int main(int, char**)
180 test();
181 #if TEST_STD_VER > 17
182 static_assert(test());
183 #endif
184 return 0;