Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / numerics / numeric.ops / inclusive.scan / inclusive_scan.pass.cpp
blob299d085d01a062d5233b4a184fec55452af30067
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 inclusive_scan(InputIterator first, InputIterator last,
16 // OutputIterator result, T init);
19 #include <numeric>
20 #include <algorithm>
21 #include <array>
22 #include <cassert>
23 #include <functional>
24 #include <iterator>
26 #include "test_macros.h"
27 #include "test_iterators.h"
29 template <class Iter1, class T>
30 TEST_CONSTEXPR_CXX20 void
31 test(Iter1 first, Iter1 last, const T *rFirst, const T *rLast)
33 assert((rLast - rFirst) <= 5); // or else increase the size of "out"
34 T out[5];
36 // Not in place
37 T *end = std::inclusive_scan(first, last, out);
38 assert(std::equal(out, end, rFirst, rLast));
40 // In place
41 std::copy(first, last, out);
42 end = std::inclusive_scan(out, end, out);
43 assert(std::equal(out, end, rFirst, rLast));
47 template <class Iter>
48 TEST_CONSTEXPR_CXX20 void
49 test()
51 int ia[] = {1, 3, 5, 7, 9};
52 const int pRes[] = {1, 4, 9, 16, 25};
53 const unsigned sa = sizeof(ia) / sizeof(ia[0]);
54 static_assert(sa == sizeof(pRes) / sizeof(pRes[0])); // just to be sure
56 for (unsigned int i = 0; i < sa; ++i ) {
57 test(Iter(ia), Iter(ia + i), pRes, pRes + i);
61 constexpr std::size_t triangle(size_t n) { return n*(n+1)/2; }
63 // Basic sanity
64 TEST_CONSTEXPR_CXX20 void
65 basic_tests()
68 std::array<std::size_t, 10> v;
69 std::fill(v.begin(), v.end(), 3);
70 std::inclusive_scan(v.begin(), v.end(), v.begin());
71 for (std::size_t i = 0; i < v.size(); ++i)
72 assert(v[i] == (i+1) * 3);
76 std::array<std::size_t, 10> v;
77 std::iota(v.begin(), v.end(), 0);
78 std::inclusive_scan(v.begin(), v.end(), v.begin());
79 for (std::size_t i = 0; i < v.size(); ++i)
80 assert(v[i] == triangle(i));
84 std::array<std::size_t, 10> v;
85 std::iota(v.begin(), v.end(), 1);
86 std::inclusive_scan(v.begin(), v.end(), v.begin());
87 for (std::size_t i = 0; i < v.size(); ++i)
88 assert(v[i] == triangle(i + 1));
92 std::array<std::size_t, 0> v, res;
93 std::inclusive_scan(v.begin(), v.end(), res.begin());
94 assert(res.empty());
98 TEST_CONSTEXPR_CXX20 bool
99 test()
101 basic_tests();
103 // All the iterator categories
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 test< int*>();
111 return true;
114 int main(int, char**)
116 test();
117 #if TEST_STD_VER > 17
118 static_assert(test());
119 #endif
120 return 0;