1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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>
20 // inner_product(Iter1 first1, Iter1 last1, Iter2 first2,
21 // T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
28 #include "test_macros.h"
29 #include "test_iterators.h"
35 constexpr T
operator()(T a
, T
)
41 bool correctOperatorUsed
= false;
43 constexpr rvalue_addable
operator*(rvalue_addable
const&) { return *this; }
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;
52 constexpr rvalue_addable
operator+(rvalue_addable
& lhs
, rvalue_addable
const&)
54 lhs
.correctOperatorUsed
= false;
58 constexpr rvalue_addable
operator+(rvalue_addable
&& lhs
, rvalue_addable
const&)
60 lhs
.correctOperatorUsed
= true;
61 return std::move(lhs
);
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());
71 assert(res1
.correctOperatorUsed
);
72 assert(res2
.correctOperatorUsed
);
74 #endif // TEST_STD_VER > 17
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");
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
)
86 assert(std::inner_product(first1
, last1
, first2
, init
,
87 std::multiplies
<int>(), std::plus
<int>()) == x
);
90 template <class Iter1
, class Iter2
>
91 TEST_CONSTEXPR_CXX20
void
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);
107 TEST_CONSTEXPR_CXX20
bool
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*>();
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*>();
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*>();
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*>();
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*>();
140 #if TEST_STD_VER > 17
142 #endif // TEST_STD_VER > 17
149 int main(int, char**)
152 #if TEST_STD_VER > 17
153 static_assert(test());