[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / libcxx / test / std / numerics / numeric.ops / exclusive.scan / exclusive_scan_init_op.pass.cpp
blobca2de4aec16002943d23635b64be2c84057995b3
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 // UNSUPPORTED: c++03, c++11, c++14
11 // <numeric>
13 // Became constexpr in C++20
14 // template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
15 // OutputIterator
16 // exclusive_scan(InputIterator first, InputIterator last,
17 // OutputIterator result,
18 // T init, BinaryOperation binary_op); // C++17
20 #include <numeric>
21 #include <algorithm>
22 #include <array>
23 #include <cassert>
24 #include <functional>
25 #include <iterator>
27 #include "test_macros.h"
28 #include "test_iterators.h"
30 template <class Iter1, class T, class Op>
31 TEST_CONSTEXPR_CXX20 void
32 test(Iter1 first, Iter1 last, T init, Op op, const T *rFirst, const T *rLast)
34 assert((rLast - rFirst) <= 5); // or else increase the size of "out"
35 T out[5];
37 // Not in place
38 T *end = std::exclusive_scan(first, last, out, init, op);
39 assert(std::equal(out, end, rFirst, rLast));
41 // In place
42 std::copy(first, last, out);
43 end = std::exclusive_scan(out, end, out, init, op);
44 assert(std::equal(out, end, rFirst, rLast));
48 template <class Iter>
49 TEST_CONSTEXPR_CXX20 void
50 test()
52 int ia[] = {1, 3, 5, 7, 9};
53 const int pRes[] = {0, 1, 4, 9, 16};
54 const int mRes[] = {1, 1, 3, 15, 105};
55 const unsigned sa = sizeof(ia) / sizeof(ia[0]);
56 static_assert(sa == sizeof(pRes) / sizeof(pRes[0])); // just to be sure
57 static_assert(sa == sizeof(mRes) / sizeof(mRes[0])); // just to be sure
59 for (unsigned int i = 0; i < sa; ++i ) {
60 test(Iter(ia), Iter(ia + i), 0, std::plus<>(), pRes, pRes + i);
61 test(Iter(ia), Iter(ia + i), 1, std::multiplies<>(), mRes, mRes + i);
65 TEST_CONSTEXPR_CXX20 bool
66 test()
68 // All the iterator categories
69 test<cpp17_input_iterator <const int*> >();
70 test<forward_iterator <const int*> >();
71 test<bidirectional_iterator<const int*> >();
72 test<random_access_iterator<const int*> >();
73 test<const int*>();
74 test< int*>();
76 // Make sure that the calculations are done using the init typedef
78 std::array<unsigned char, 10> v;
79 std::iota(v.begin(), v.end(), static_cast<unsigned char>(1));
80 std::array<std::size_t, 10> res;
81 std::exclusive_scan(v.begin(), v.end(), res.begin(), 1, std::multiplies<>());
83 assert(res.size() == 10);
84 std::size_t j = 1;
85 assert(res[0] == 1);
86 for (std::size_t i = 1; i < v.size(); ++i)
88 j *= i;
89 assert(res[i] == j);
93 return true;
96 int main(int, char**)
98 test();
99 #if TEST_STD_VER > 17
100 static_assert(test());
101 #endif
102 return 0;