[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / libcxx / test / std / numerics / numeric.ops / accumulate / accumulate_op.pass.cpp
blobdb1ca0527a7918f3c7c6ea472a7d28adcb94985f
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 //===----------------------------------------------------------------------===//
9 // <numeric>
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);
19 #include <numeric>
20 #include <functional>
21 #include <string>
22 #include <cassert>
24 #include "test_macros.h"
25 #include "test_iterators.h"
27 #if TEST_STD_VER > 17
28 struct rvalue_addable
30 bool correctOperatorUsed = false;
32 // make sure the predicate is passed an rvalue and an lvalue (so check that the first argument was moved)
33 constexpr rvalue_addable operator()(rvalue_addable&& r, rvalue_addable const&) {
34 r.correctOperatorUsed = true;
35 return std::move(r);
39 constexpr rvalue_addable operator+(rvalue_addable& lhs, rvalue_addable const&)
41 lhs.correctOperatorUsed = false;
42 return lhs;
45 constexpr rvalue_addable operator+(rvalue_addable&& lhs, rvalue_addable const&)
47 lhs.correctOperatorUsed = true;
48 return std::move(lhs);
51 constexpr void
52 test_use_move()
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);
60 #endif // TEST_STD_VER > 17
62 TEST_CONSTEXPR_CXX20 void test_string() {
63 std::string sa[] = {"a", "b", "c"};
64 assert(std::accumulate(sa, sa + 3, std::string()) == "abc");
65 assert(std::accumulate(sa, sa + 3, std::string(), std::plus<std::string>()) == "abc");
68 template <class Iter, class T>
69 TEST_CONSTEXPR_CXX20 void
70 test(Iter first, Iter last, T init, T x)
72 assert(std::accumulate(first, last, init, std::multiplies<T>()) == x);
75 template <class Iter>
76 TEST_CONSTEXPR_CXX20 void
77 test()
79 int ia[] = {1, 2, 3, 4, 5, 6};
80 unsigned sa = sizeof(ia) / sizeof(ia[0]);
81 test(Iter(ia), Iter(ia), 1, 1);
82 test(Iter(ia), Iter(ia), 10, 10);
83 test(Iter(ia), Iter(ia+1), 1, 1);
84 test(Iter(ia), Iter(ia+1), 10, 10);
85 test(Iter(ia), Iter(ia+2), 1, 2);
86 test(Iter(ia), Iter(ia+2), 10, 20);
87 test(Iter(ia), Iter(ia+sa), 1, 720);
88 test(Iter(ia), Iter(ia+sa), 10, 7200);
91 TEST_CONSTEXPR_CXX20 bool
92 test()
94 test<cpp17_input_iterator<const int*> >();
95 test<forward_iterator<const int*> >();
96 test<bidirectional_iterator<const int*> >();
97 test<random_access_iterator<const int*> >();
98 test<const int*>();
100 #if TEST_STD_VER > 17
101 test_use_move();
102 #endif // TEST_STD_VER > 17
104 test_string();
106 return true;
109 int main(int, char**)
111 test();
112 #if TEST_STD_VER > 17
113 static_assert(test());
114 #endif
115 return 0;