xref: /llvm-project/libcxx/test/std/numerics/numeric.ops/inclusive.scan/inclusive_scan.pass.cpp (revision e99c4906e44ae3f921fa05356909d006cda8d954)
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 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // <numeric>
12 
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);
17 //
18 
19 #include <algorithm>
20 #include <array>
21 #include <cassert>
22 #include <cstddef>
23 #include <numeric>
24 
25 #include "test_macros.h"
26 #include "test_iterators.h"
27 
28 template <class Iter1, class T>
29 TEST_CONSTEXPR_CXX20 void
30 test(Iter1 first, Iter1 last, const T *rFirst, const T *rLast)
31 {
32     assert((rLast - rFirst) <= 5);  // or else increase the size of "out"
33     T out[5];
34 
35     // Not in place
36     T *end = std::inclusive_scan(first, last, out);
37     assert(std::equal(out, end, rFirst, rLast));
38 
39     // In place
40     std::copy(first, last, out);
41     end = std::inclusive_scan(out, end, out);
42     assert(std::equal(out, end, rFirst, rLast));
43 }
44 
45 
46 template <class Iter>
47 TEST_CONSTEXPR_CXX20 void
48 test()
49 {
50     int ia[]         = {1, 3, 5, 7,  9};
51     const int pRes[] = {1, 4, 9, 16, 25};
52     const unsigned sa = sizeof(ia) / sizeof(ia[0]);
53     static_assert(sa == sizeof(pRes) / sizeof(pRes[0]));       // just to be sure
54 
55     for (unsigned int i = 0; i < sa; ++i ) {
56         test(Iter(ia), Iter(ia + i), pRes, pRes + i);
57     }
58 }
59 
60 constexpr std::size_t triangle(size_t n) { return n*(n+1)/2; }
61 
62 //  Basic sanity
63 TEST_CONSTEXPR_CXX20 void
64 basic_tests()
65 {
66     {
67     std::array<std::size_t, 10> v;
68     std::fill(v.begin(), v.end(), 3);
69     std::inclusive_scan(v.begin(), v.end(), v.begin());
70     for (std::size_t i = 0; i < v.size(); ++i)
71         assert(v[i] == (i+1) * 3);
72     }
73 
74     {
75     std::array<std::size_t, 10> v;
76     std::iota(v.begin(), v.end(), 0);
77     std::inclusive_scan(v.begin(), v.end(), v.begin());
78     for (std::size_t i = 0; i < v.size(); ++i)
79         assert(v[i] == triangle(i));
80     }
81 
82     {
83     std::array<std::size_t, 10> v;
84     std::iota(v.begin(), v.end(), 1);
85     std::inclusive_scan(v.begin(), v.end(), v.begin());
86     for (std::size_t i = 0; i < v.size(); ++i)
87         assert(v[i] == triangle(i + 1));
88     }
89 
90     {
91     std::array<std::size_t, 0> v, res;
92     std::inclusive_scan(v.begin(), v.end(), res.begin());
93     assert(res.empty());
94     }
95 }
96 
97 TEST_CONSTEXPR_CXX20 bool
98 test()
99 {
100     basic_tests();
101 
102 //  All the iterator categories
103     test<cpp17_input_iterator        <const int*> >();
104     test<forward_iterator      <const int*> >();
105     test<bidirectional_iterator<const int*> >();
106     test<random_access_iterator<const int*> >();
107     test<const int*>();
108     test<      int*>();
109 
110     return true;
111 }
112 
113 int main(int, char**)
114 {
115     test();
116 #if TEST_STD_VER > 17
117     static_assert(test());
118 #endif
119     return 0;
120 }
121