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 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>
19 // partial_sum(InIter first, InIter last, OutIter result, BinaryOperation binary_op);
26 #include "test_macros.h"
27 #include "test_iterators.h"
32 bool correctOperatorUsed
= false;
34 // make sure the predicate is passed an rvalue and an lvalue (so check that the first argument was moved)
35 constexpr rvalue_addable
operator()(rvalue_addable
&& r
, rvalue_addable
const&) {
36 r
.correctOperatorUsed
= true;
41 constexpr rvalue_addable
operator+(rvalue_addable
& lhs
, rvalue_addable
const&)
43 lhs
.correctOperatorUsed
= false;
47 constexpr rvalue_addable
operator+(rvalue_addable
&& lhs
, rvalue_addable
const&)
49 lhs
.correctOperatorUsed
= true;
50 return std::move(lhs
);
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
);
66 #endif // TEST_STD_VER > 17
68 TEST_CONSTEXPR_CXX20
void test_string() {
69 std::string sa
[] = {"a", "b", "c"};
70 std::string sr
[] = {"a", "ba", "cb"};
71 std::string output
[3];
72 std::adjacent_difference(sa
, sa
+ 3, output
, std::plus
<std::string
>());
73 for (unsigned i
= 0; i
< 3; ++i
) assert(output
[i
] == sr
[i
]);
76 template <class InIter
, class OutIter
>
77 TEST_CONSTEXPR_CXX20
void
80 int ia
[] = {1, 2, 3, 4, 5};
81 int ir
[] = {1, -1, -4, -8, -13};
82 const unsigned s
= sizeof(ia
) / sizeof(ia
[0]);
84 OutIter r
= std::partial_sum(InIter(ia
), InIter(ia
+s
), OutIter(ib
), std::minus
<int>());
85 assert(base(r
) == ib
+ s
);
86 for (unsigned i
= 0; i
< s
; ++i
)
87 assert(ib
[i
] == ir
[i
]);
90 TEST_CONSTEXPR_CXX20
bool
93 test
<cpp17_input_iterator
<const int*>, cpp17_output_iterator
<int*> >();
94 test
<cpp17_input_iterator
<const int*>, forward_iterator
<int*> >();
95 test
<cpp17_input_iterator
<const int*>, bidirectional_iterator
<int*> >();
96 test
<cpp17_input_iterator
<const int*>, random_access_iterator
<int*> >();
97 test
<cpp17_input_iterator
<const int*>, int*>();
99 test
<forward_iterator
<const int*>, cpp17_output_iterator
<int*> >();
100 test
<forward_iterator
<const int*>, forward_iterator
<int*> >();
101 test
<forward_iterator
<const int*>, bidirectional_iterator
<int*> >();
102 test
<forward_iterator
<const int*>, random_access_iterator
<int*> >();
103 test
<forward_iterator
<const int*>, int*>();
105 test
<bidirectional_iterator
<const int*>, cpp17_output_iterator
<int*> >();
106 test
<bidirectional_iterator
<const int*>, forward_iterator
<int*> >();
107 test
<bidirectional_iterator
<const int*>, bidirectional_iterator
<int*> >();
108 test
<bidirectional_iterator
<const int*>, random_access_iterator
<int*> >();
109 test
<bidirectional_iterator
<const int*>, int*>();
111 test
<random_access_iterator
<const int*>, cpp17_output_iterator
<int*> >();
112 test
<random_access_iterator
<const int*>, forward_iterator
<int*> >();
113 test
<random_access_iterator
<const int*>, bidirectional_iterator
<int*> >();
114 test
<random_access_iterator
<const int*>, random_access_iterator
<int*> >();
115 test
<random_access_iterator
<const int*>, int*>();
117 test
<const int*, cpp17_output_iterator
<int*> >();
118 test
<const int*, forward_iterator
<int*> >();
119 test
<const int*, bidirectional_iterator
<int*> >();
120 test
<const int*, random_access_iterator
<int*> >();
121 test
<const int*, int*>();
123 #if TEST_STD_VER > 17
125 #endif // TEST_STD_VER > 17
132 int main(int, char**)
135 #if TEST_STD_VER > 17
136 static_assert(test());