| 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 Iter1, InputIterator Iter2, MoveConstructible T, |
| 13 |
// class BinaryOperation1, |
| 14 |
// Callable<auto, Iter1::reference, Iter2::reference> BinaryOperation2> |
| 15 |
// requires Callable<BinaryOperation1, const T&, BinaryOperation2::result_type> |
| 16 |
// && HasAssign<T, BinaryOperation1::result_type> |
| 17 |
// && CopyConstructible<BinaryOperation1> |
| 18 |
// && CopyConstructible<BinaryOperation2> |
| 19 |
// T |
| 20 |
// inner_product(Iter1 first1, Iter1 last1, Iter2 first2, |
| 21 |
// T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); |
| 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 do_nothing_op |
| 33 |
{ |
| 34 |
template<class T> |
| 35 |
constexpr T operator()(T a, T) |
| 36 |
{ return a; } |
| 37 |
}; |
| 38 |
|
| 39 |
struct rvalue_addable |
| 40 |
{ |
| 41 |
bool correctOperatorUsed = false; |
| 42 |
|
| 43 |
constexpr rvalue_addable operator*(rvalue_addable const&) { return *this; } |
| 44 |
|
| 45 |
// make sure the predicate is passed an rvalue and an lvalue (so check that the first argument was moved) |
| 46 |
constexpr rvalue_addable operator()(rvalue_addable&& r, rvalue_addable const&) { |
| 47 |
r.correctOperatorUsed = true; |
| 48 |
return std::move(r); |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
constexpr rvalue_addable operator+(rvalue_addable& lhs, rvalue_addable const&) |
| 53 |
{ |
| 54 |
lhs.correctOperatorUsed = false; |
| 55 |
return lhs; |
| 56 |
} |
| 57 |
|
| 58 |
constexpr rvalue_addable operator+(rvalue_addable&& lhs, rvalue_addable const&) |
| 59 |
{ |
| 60 |
lhs.correctOperatorUsed = true; |
| 61 |
return std::move(lhs); |
| 62 |
} |
| 63 |
|
| 64 |
constexpr void |
| 65 |
test_use_move() |
| 66 |
{ |
| 67 |
rvalue_addable arr[100]; |
| 68 |
auto res1 = std::inner_product(arr, arr + 100, arr, rvalue_addable()); |
| 69 |
auto res2 = std::inner_product(arr, arr + 100, arr, rvalue_addable(), /*predicate=*/rvalue_addable(), do_nothing_op()); |
| 70 |
|
| 71 |
assert(res1.correctOperatorUsed); |
| 72 |
assert(res2.correctOperatorUsed); |
| 73 |
} |
| 74 |
#endif // TEST_STD_VER > 17 |
| 75 |
|
| 76 |
TEST_CONSTEXPR_CXX20 void test_string() { |
| 77 |
std::string sa[] = {"a", "b", "c"}; |
| 78 |
assert(std::accumulate(sa, sa + 3, std::string()) == "abc"); |
| 79 |
assert(std::accumulate(sa, sa + 3, std::string(), std::plus<std::string>()) == "abc"); |
| 80 |
} |
| 81 |
|
| 82 |
template <class Iter1, class Iter2, class T> |
| 83 |
TEST_CONSTEXPR_CXX20 void |
| 84 |
test(Iter1 first1, Iter1 last1, Iter2 first2, T init, T x) |
| 85 |
{ |
| 86 |
assert(std::inner_product(first1, last1, first2, init, |
| 87 |
std::multiplies<int>(), std::plus<int>()) == x); |
| 88 |
} |
| 89 |
|
| 90 |
template <class Iter1, class Iter2> |
| 91 |
TEST_CONSTEXPR_CXX20 void |
| 92 |
test() |
| 93 |
{ |
| 94 |
int a[] = {1, 2, 3, 4, 5, 6}; |
| 95 |
int b[] = {6, 5, 4, 3, 2, 1}; |
| 96 |
unsigned sa = sizeof(a) / sizeof(a[0]); |
| 97 |
test(Iter1(a), Iter1(a), Iter2(b), 1, 1); |
| 98 |
test(Iter1(a), Iter1(a), Iter2(b), 10, 10); |
| 99 |
test(Iter1(a), Iter1(a+1), Iter2(b), 1, 7); |
| 100 |
test(Iter1(a), Iter1(a+1), Iter2(b), 10, 70); |
| 101 |
test(Iter1(a), Iter1(a+2), Iter2(b), 1, 49); |
| 102 |
test(Iter1(a), Iter1(a+2), Iter2(b), 10, 490); |
| 103 |
test(Iter1(a), Iter1(a+sa), Iter2(b), 1, 117649); |
| 104 |
test(Iter1(a), Iter1(a+sa), Iter2(b), 10, 1176490); |
| 105 |
} |
| 106 |
|
| 107 |
TEST_CONSTEXPR_CXX20 bool |
| 108 |
test() |
| 109 |
{ |
| 110 |
test<cpp17_input_iterator<const int*>, cpp17_input_iterator<const int*> >(); |
| 111 |
test<cpp17_input_iterator<const int*>, forward_iterator<const int*> >(); |
| 112 |
test<cpp17_input_iterator<const int*>, bidirectional_iterator<const int*> >(); |
| 113 |
test<cpp17_input_iterator<const int*>, random_access_iterator<const int*> >(); |
| 114 |
test<cpp17_input_iterator<const int*>, const int*>(); |
| 115 |
|
| 116 |
test<forward_iterator<const int*>, cpp17_input_iterator<const int*> >(); |
| 117 |
test<forward_iterator<const int*>, forward_iterator<const int*> >(); |
| 118 |
test<forward_iterator<const int*>, bidirectional_iterator<const int*> >(); |
| 119 |
test<forward_iterator<const int*>, random_access_iterator<const int*> >(); |
| 120 |
test<forward_iterator<const int*>, const int*>(); |
| 121 |
|
| 122 |
test<bidirectional_iterator<const int*>, cpp17_input_iterator<const int*> >(); |
| 123 |
test<bidirectional_iterator<const int*>, forward_iterator<const int*> >(); |
| 124 |
test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >(); |
| 125 |
test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >(); |
| 126 |
test<bidirectional_iterator<const int*>, const int*>(); |
| 127 |
|
| 128 |
test<random_access_iterator<const int*>, cpp17_input_iterator<const int*> >(); |
| 129 |
test<random_access_iterator<const int*>, forward_iterator<const int*> >(); |
| 130 |
test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >(); |
| 131 |
test<random_access_iterator<const int*>, random_access_iterator<const int*> >(); |
| 132 |
test<random_access_iterator<const int*>, const int*>(); |
| 133 |
|
| 134 |
test<const int*, cpp17_input_iterator<const int*> >(); |
| 135 |
test<const int*, forward_iterator<const int*> >(); |
| 136 |
test<const int*, bidirectional_iterator<const int*> >(); |
| 137 |
test<const int*, random_access_iterator<const int*> >(); |
| 138 |
test<const int*, const int*>(); |
| 139 |
|
| 140 |
#if TEST_STD_VER > 17 |
| 141 |
test_use_move(); |
| 142 |
#endif // TEST_STD_VER > 17 |
| 143 |
|
| 144 |
test_string(); |
| 145 |
|
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
int main(int, char**) |
| 150 |
{ |
| 151 |
test(); |
| 152 |
#if TEST_STD_VER > 17 |
| 153 |
static_assert(test()); |
| 154 |
#endif |
| 155 |
return 0; |
| 156 |
} |