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&, InIter::reference> BinaryOperation>
15 // requires HasAssign<InIter::value_type, BinaryOperation::result_type>
16 // && Constructible<InIter::value_type, InIter::reference>
17 // && CopyConstructible<BinaryOperation>
18 // OutIter
19 // partial_sum(InIter first, InIter last, OutIter result, BinaryOperation binary_op);
20
21 #include <numeric>
22 #include <functional>
23 #include <string>
24 #include <cassert>
25
26 #include "test_macros.h"
27 #include "test_iterators.h"
28
29 #if TEST_STD_VER > 17
30 struct rvalue_addable
31 {
32 bool correctOperatorUsed = false;
33
34 // make sure the predicate is passed an rvalue and an lvalue (so check that the first argument was moved)
operator ()rvalue_addable35 constexpr rvalue_addable operator()(rvalue_addable&& r, rvalue_addable const&) {
36 r.correctOperatorUsed = true;
37 return std::move(r);
38 }
39 };
40
operator +(rvalue_addable & lhs,rvalue_addable const &)41 constexpr rvalue_addable operator+(rvalue_addable& lhs, rvalue_addable const&)
42 {
43 lhs.correctOperatorUsed = false;
44 return lhs;
45 }
46
operator +(rvalue_addable && lhs,rvalue_addable const &)47 constexpr rvalue_addable operator+(rvalue_addable&& lhs, rvalue_addable const&)
48 {
49 lhs.correctOperatorUsed = true;
50 return std::move(lhs);
51 }
52
53 constexpr void
test_use_move()54 test_use_move()
55 {
56 const std::size_t size = 100;
57 rvalue_addable arr[size];
58 rvalue_addable res1[size];
59 rvalue_addable res2[size];
60 std::partial_sum(arr, arr + size, res1);
61 std::partial_sum(arr, arr + size, res2, /*predicate=*/rvalue_addable());
62 // start at 1 because the first element is not moved
63 for (unsigned i = 1; i < size; ++i) assert(res1[i].correctOperatorUsed);
64 for (unsigned i = 1; i < size; ++i) assert(res2[i].correctOperatorUsed);
65 }
66 #endif // TEST_STD_VER > 17
67
68 // C++20 can use string in constexpr evaluation, but both libc++ and MSVC
69 // don't have the support yet. In these cases omit the constexpr test.
70 // FIXME Remove constexpr string workaround introduced in D90569
71 #if TEST_STD_VER > 17 && \
72 (!defined(__cpp_lib_constexpr_string) || __cpp_lib_constexpr_string < 201907L)
73 void
74 #else
75 TEST_CONSTEXPR_CXX20 void
76 #endif
test_string()77 test_string()
78 {
79 std::string sa[] = {"a", "b", "c"};
80 std::string sr[] = {"a", "ba", "cb"};
81 std::string output[3];
82 std::adjacent_difference(sa, sa + 3, output, std::plus<std::string>());
83 for (unsigned i = 0; i < 3; ++i) assert(output[i] == sr[i]);
84 }
85
86 template <class InIter, class OutIter>
87 TEST_CONSTEXPR_CXX20 void
test()88 test()
89 {
90 int ia[] = {1, 2, 3, 4, 5};
91 int ir[] = {1, -1, -4, -8, -13};
92 const unsigned s = sizeof(ia) / sizeof(ia[0]);
93 int ib[s] = {0};
94 OutIter r = std::partial_sum(InIter(ia), InIter(ia+s), OutIter(ib), std::minus<int>());
95 assert(base(r) == ib + s);
96 for (unsigned i = 0; i < s; ++i)
97 assert(ib[i] == ir[i]);
98 }
99
100 TEST_CONSTEXPR_CXX20 bool
test()101 test()
102 {
103 test<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
104 test<cpp17_input_iterator<const int*>, forward_iterator<int*> >();
105 test<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
106 test<cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
107 test<cpp17_input_iterator<const int*>, int*>();
108
109 test<forward_iterator<const int*>, cpp17_output_iterator<int*> >();
110 test<forward_iterator<const int*>, forward_iterator<int*> >();
111 test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
112 test<forward_iterator<const int*>, random_access_iterator<int*> >();
113 test<forward_iterator<const int*>, int*>();
114
115 test<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
116 test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
117 test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
118 test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
119 test<bidirectional_iterator<const int*>, int*>();
120
121 test<random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
122 test<random_access_iterator<const int*>, forward_iterator<int*> >();
123 test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
124 test<random_access_iterator<const int*>, random_access_iterator<int*> >();
125 test<random_access_iterator<const int*>, int*>();
126
127 test<const int*, cpp17_output_iterator<int*> >();
128 test<const int*, forward_iterator<int*> >();
129 test<const int*, bidirectional_iterator<int*> >();
130 test<const int*, random_access_iterator<int*> >();
131 test<const int*, int*>();
132
133 #if TEST_STD_VER > 17
134 test_use_move();
135 #endif // TEST_STD_VER > 17
136 // C++20 can use string in constexpr evaluation, but both libc++ and MSVC
137 // don't have the support yet. In these cases omit the constexpr test.
138 // FIXME Remove constexpr string workaround introduced in D90569
139 #if TEST_STD_VER > 17 && \
140 (!defined(__cpp_lib_constexpr_string) || __cpp_lib_constexpr_string < 201907L)
141 if (!std::is_constant_evaluated())
142 #endif
143 test_string();
144
145 return true;
146 }
147
main(int,char **)148 int main(int, char**)
149 {
150 test();
151 #if TEST_STD_VER > 17
152 static_assert(test());
153 #endif
154
155 return 0;
156 }
157