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 // <numeric>
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: clang-8
12 // UNSUPPORTED: gcc-9
13 
14 // Became constexpr in C++20
15 // template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
16 //     OutputIterator
17 //     inclusive_scan(InputIterator first, InputIterator last,
18 //                    OutputIterator result,
19 //                    BinaryOperation binary_op, T init); // C++17
20 
21 #include <numeric>
22 #include <algorithm>
23 #include <array>
24 #include <cassert>
25 #include <functional>
26 #include <iterator>
27 #include <vector>
28 
29 #include "test_macros.h"
30 #include "test_iterators.h"
31 // FIXME Remove constexpr vector workaround introduced in D90569
32 #if TEST_STD_VER > 17
33 #include <span>
34 #endif
35 
36 template <class Iter1, class T, class Op, class Iter2>
37 TEST_CONSTEXPR_CXX20 void
38 test(Iter1 first, Iter1 last, Op op, T init, Iter2 rFirst, Iter2 rLast)
39 {
40     // C++17 doesn't test constexpr so can use a vector.
41     // C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
42     // don't have the support yet. In these cases use a std::span for the test.
43     // FIXME Remove constexpr vector workaround introduced in D90569
44     size_t size = std::distance(first, last);
45 #if TEST_STD_VER < 20 || \
46     (defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)
47 
48     std::vector<typename std::iterator_traits<Iter1>::value_type> v(size);
49 #else
50     assert((size <= 5) && "Increment the size of the array");
51     typename std::iterator_traits<Iter1>::value_type b[5];
52     std::span<typename std::iterator_traits<Iter1>::value_type> v{b, size};
53 #endif
54 
55 //  Not in place
56     std::inclusive_scan(first, last, v.begin(), op, init);
57     assert(std::equal(v.begin(), v.end(), rFirst, rLast));
58 
59 //  In place
60     std::copy(first, last, v.begin());
61     std::inclusive_scan(v.begin(), v.end(), v.begin(), op, init);
62     assert(std::equal(v.begin(), v.end(), rFirst, rLast));
63 }
64 
65 
66 template <class Iter>
67 TEST_CONSTEXPR_CXX20 void
68 test()
69 {
70           int ia[]   = {1, 3,  5,   7,   9};
71     const int pRes[] = {1, 4,  9,  16,  25};
72     const int mRes[] = {1, 3, 15, 105, 945};
73     const unsigned sa = sizeof(ia) / sizeof(ia[0]);
74     static_assert(sa == sizeof(pRes) / sizeof(pRes[0]));       // just to be sure
75     static_assert(sa == sizeof(mRes) / sizeof(mRes[0]));       // just to be sure
76 
77     for (unsigned int i = 0; i < sa; ++i ) {
78         test(Iter(ia), Iter(ia + i), std::plus<>(),       0, pRes, pRes + i);
79         test(Iter(ia), Iter(ia + i), std::multiplies<>(), 1, mRes, mRes + i);
80         }
81 }
82 
83 constexpr size_t triangle(size_t n) { return n*(n+1)/2; }
84 
85 //  Basic sanity
86 TEST_CONSTEXPR_CXX20 void
87 basic_tests()
88 {
89     {
90     std::array<size_t, 10> v;
91     std::fill(v.begin(), v.end(), 3);
92     std::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), size_t{50});
93     for (size_t i = 0; i < v.size(); ++i)
94         assert(v[i] == 50 + (i+1) * 3);
95     }
96 
97     {
98     std::array<size_t, 10> v;
99     std::iota(v.begin(), v.end(), 0);
100     std::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), size_t{40});
101     for (size_t i = 0; i < v.size(); ++i)
102         assert(v[i] == 40 + triangle(i));
103     }
104 
105     {
106     std::array<size_t, 10> v;
107     std::iota(v.begin(), v.end(), 1);
108     std::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), size_t{30});
109     for (size_t i = 0; i < v.size(); ++i)
110         assert(v[i] == 30 + triangle(i + 1));
111     }
112 
113     {
114     // C++17 doesn't test constexpr so can use a vector.
115     // C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
116     // don't have the support yet. In these cases use a std::span for the test.
117     // FIXME Remove constexpr vector workaround introduced in D90569
118 #if TEST_STD_VER < 20 || \
119     (defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)
120     std::vector<size_t> v, res;
121     std::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<>(), size_t{40});
122 #else
123     std::array<size_t, 0> v, res;
124     std::inclusive_scan(v.begin(), v.end(), res.begin(), std::plus<>(), size_t{40});
125 #endif
126     assert(res.empty());
127     }
128 
129 //  Make sure that the calculations are done using the init typedef
130     {
131     std::array<unsigned char, 10> v;
132     std::iota(v.begin(), v.end(), static_cast<unsigned char>(1));
133     // C++17 doesn't test constexpr so can use a vector.
134     // C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
135     // don't have the support yet. In these cases use a std::span for the test.
136     // FIXME Remove constexpr vector workaround introduced in D90569
137 #if TEST_STD_VER < 20 || \
138     (defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)
139     std::vector<size_t> res;
140     std::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::multiplies<>(), size_t{1});
141 #else
142     std::array<size_t, 10> res;
143     std::inclusive_scan(v.begin(), v.end(), res.begin(), std::multiplies<>(), size_t{1});
144 #endif
145 
146     assert(res.size() == 10);
147     size_t j = 1;
148     assert(res[0] == 1);
149     for (size_t i = 1; i < v.size(); ++i)
150     {
151         j *= i + 1;
152         assert(res[i] == j);
153     }
154     }
155 }
156 
157 TEST_CONSTEXPR_CXX20 bool
158 test()
159 {
160     basic_tests();
161 
162 //  All the iterator categories
163     test<input_iterator        <const int*> >();
164     test<forward_iterator      <const int*> >();
165     test<bidirectional_iterator<const int*> >();
166     test<random_access_iterator<const int*> >();
167     test<const int*>();
168     test<      int*>();
169 
170     return true;
171 }
172 
173 int main(int, char**)
174 {
175     test();
176 #if TEST_STD_VER > 17
177     static_assert(test());
178 #endif
179     return 0;
180 }
181