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 Iter, MoveConstructible T,
13 //           Callable<auto, const T&, Iter::reference> BinaryOperation>
14 //   requires HasAssign<T, BinaryOperation::result_type>
15 //         && CopyConstructible<BinaryOperation>
16 //   T
17 //   accumulate(Iter first, Iter last, T init, BinaryOperation binary_op);
18 
19 #include <numeric>
20 #include <functional>
21 #include <string>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 #include "test_iterators.h"
26 
27 #if TEST_STD_VER > 17
28 struct rvalue_addable
29 {
30     bool correctOperatorUsed = false;
31 
32     // make sure the predicate is passed an rvalue and an lvalue (so check that the first argument was moved)
operator ()rvalue_addable33     constexpr rvalue_addable operator()(rvalue_addable&& r, rvalue_addable const&) {
34         r.correctOperatorUsed = true;
35         return std::move(r);
36     }
37 };
38 
operator +(rvalue_addable & lhs,rvalue_addable const &)39 constexpr rvalue_addable operator+(rvalue_addable& lhs, rvalue_addable const&)
40 {
41     lhs.correctOperatorUsed = false;
42         return lhs;
43 }
44 
operator +(rvalue_addable && lhs,rvalue_addable const &)45 constexpr rvalue_addable operator+(rvalue_addable&& lhs, rvalue_addable const&)
46 {
47     lhs.correctOperatorUsed = true;
48     return std::move(lhs);
49 }
50 
51 constexpr void
test_use_move()52 test_use_move()
53 {
54     rvalue_addable arr[100];
55     auto res1 = std::accumulate(arr, arr + 100, rvalue_addable());
56     auto res2 = std::accumulate(arr, arr + 100, rvalue_addable(), /*predicate=*/rvalue_addable());
57     assert(res1.correctOperatorUsed);
58     assert(res2.correctOperatorUsed);
59 }
60 #endif // TEST_STD_VER > 17
61 
62 // C++20 can use string in constexpr evaluation, but both libc++ and MSVC
63 // don't have the support yet. In these cases omit the constexpr test.
64 // FIXME Remove constexpr string workaround introduced in D90569
65 #if TEST_STD_VER > 17 && \
66 	(!defined(__cpp_lib_constexpr_string) || __cpp_lib_constexpr_string < 201907L)
67 void
68 #else
69 TEST_CONSTEXPR_CXX20 void
70 #endif
test_string()71 test_string()
72 {
73     std::string sa[] = {"a", "b", "c"};
74     assert(std::accumulate(sa, sa + 3, std::string()) == "abc");
75     assert(std::accumulate(sa, sa + 3, std::string(), std::plus<std::string>()) == "abc");
76 }
77 
78 template <class Iter, class T>
79 TEST_CONSTEXPR_CXX20 void
test(Iter first,Iter last,T init,T x)80 test(Iter first, Iter last, T init, T x)
81 {
82     assert(std::accumulate(first, last, init, std::multiplies<T>()) == x);
83 }
84 
85 template <class Iter>
86 TEST_CONSTEXPR_CXX20 void
test()87 test()
88 {
89     int ia[] = {1, 2, 3, 4, 5, 6};
90     unsigned sa = sizeof(ia) / sizeof(ia[0]);
91     test(Iter(ia), Iter(ia), 1, 1);
92     test(Iter(ia), Iter(ia), 10, 10);
93     test(Iter(ia), Iter(ia+1), 1, 1);
94     test(Iter(ia), Iter(ia+1), 10, 10);
95     test(Iter(ia), Iter(ia+2), 1, 2);
96     test(Iter(ia), Iter(ia+2), 10, 20);
97     test(Iter(ia), Iter(ia+sa), 1, 720);
98     test(Iter(ia), Iter(ia+sa), 10, 7200);
99 }
100 
101 TEST_CONSTEXPR_CXX20 bool
test()102 test()
103 {
104     test<cpp17_input_iterator<const int*> >();
105     test<forward_iterator<const int*> >();
106     test<bidirectional_iterator<const int*> >();
107     test<random_access_iterator<const int*> >();
108     test<const int*>();
109 
110 #if TEST_STD_VER > 17
111     test_use_move();
112 #endif // TEST_STD_VER > 17
113     // C++20 can use string in constexpr evaluation, but both libc++ and MSVC
114     // don't have the support yet. In these cases omit the constexpr test.
115     // FIXME Remove constexpr string workaround introduced in D90569
116 #if TEST_STD_VER > 17 && \
117 	(!defined(__cpp_lib_constexpr_string) || __cpp_lib_constexpr_string < 201907L)
118 	if (!std::is_constant_evaluated())
119 #endif
120     test_string();
121 
122     return true;
123 }
124 
main(int,char **)125 int main(int, char**)
126 {
127     test();
128 #if TEST_STD_VER > 17
129     static_assert(test());
130 #endif
131     return 0;
132 }
133