[MLIR][NVVM] Add support for griddepcontrol Ops (#124603)
[llvm-project.git] / libcxx / test / std / numerics / numeric.ops / exclusive.scan / exclusive_scan.pass.cpp
blobf6cac33e944387e1bf1cc45df067837ca1023482
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>
15 // OutputIterator exclusive_scan(InputIterator first, InputIterator last,
16 // OutputIterator result, T init);
19 #include <algorithm>
20 #include <array>
21 #include <cassert>
22 #include <cstddef>
23 #include <numeric>
25 #include "test_macros.h"
26 #include "test_iterators.h"
28 template <class Iter1, class T>
29 TEST_CONSTEXPR_CXX20 void
30 test(Iter1 first, Iter1 last, T init, const T *rFirst, const T *rLast)
32 assert((rLast - rFirst) <= 5); // or else increase the size of "out"
33 T out[5];
35 // Not in place
36 T *end = std::exclusive_scan(first, last, out, init);
37 assert(std::equal(out, end, rFirst, rLast));
39 // In place
40 std::copy(first, last, out);
41 end = std::exclusive_scan(out, end, out, init);
42 assert(std::equal(out, end, rFirst, rLast));
45 template <class Iter>
46 TEST_CONSTEXPR_CXX20 void
47 test()
49 int ia[] = {1, 3, 5, 7, 9};
50 const int pRes[] = {0, 1, 4, 9, 16};
51 const unsigned sa = sizeof(ia) / sizeof(ia[0]);
52 static_assert(sa == sizeof(pRes) / sizeof(pRes[0])); // just to be sure
54 for (unsigned int i = 0; i < sa; ++i) {
55 test(Iter(ia), Iter(ia + i), 0, pRes, pRes + i);
59 constexpr std::size_t triangle(size_t n) { return n*(n+1)/2; }
61 // Basic sanity
62 TEST_CONSTEXPR_CXX20 void
63 basic_tests()
66 std::array<std::size_t, 10> v;
67 std::fill(v.begin(), v.end(), 3);
68 std::exclusive_scan(v.begin(), v.end(), v.begin(), std::size_t{50});
69 for (std::size_t i = 0; i < v.size(); ++i)
70 assert(v[i] == 50 + i * 3);
74 std::array<std::size_t, 10> v;
75 std::iota(v.begin(), v.end(), 0);
76 std::exclusive_scan(v.begin(), v.end(), v.begin(), std::size_t{30});
77 for (std::size_t i = 0; i < v.size(); ++i)
78 assert(v[i] == 30 + triangle(i-1));
82 std::array<std::size_t, 10> v;
83 std::iota(v.begin(), v.end(), 1);
84 std::exclusive_scan(v.begin(), v.end(), v.begin(), std::size_t{40});
85 for (std::size_t i = 0; i < v.size(); ++i)
86 assert(v[i] == 40 + triangle(i));
91 TEST_CONSTEXPR_CXX20 bool
92 test()
94 basic_tests();
96 // All the iterator categories
97 test<cpp17_input_iterator <const int*> >();
98 test<forward_iterator <const int*> >();
99 test<bidirectional_iterator<const int*> >();
100 test<random_access_iterator<const int*> >();
101 test<const int*>();
102 test< int*>();
104 return true;
107 int main(int, char**)
109 test();
110 #if TEST_STD_VER > 17
111 static_assert(test());
112 #endif
113 return 0;