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>
16 //     OutputIterator exclusive_scan(InputIterator first, InputIterator last,
17 //                                   OutputIterator result, T init);
18 //
19 
20 #include <numeric>
21 #include <algorithm>
22 #include <array>
23 #include <cassert>
24 #include <functional>
25 #include <iterator>
26 #include <vector>
27 
28 #include "test_macros.h"
29 #include "test_iterators.h"
30 // FIXME Remove constexpr vector workaround introduced in D90569
31 #if TEST_STD_VER > 17
32 #include <span>
33 #endif
34 
35 template <class Iter1, class T, class Iter2>
36 TEST_CONSTEXPR_CXX20 void
37 test(Iter1 first, Iter1 last, T init, Iter2 rFirst, Iter2 rLast)
38 {
39     // C++17 doesn't test constexpr so can use a vector.
40     // C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
41     // don't have the support yet. In these cases use a std::span for the test.
42     // FIXME Remove constexpr vector workaround introduced in D90569
43     size_t size = std::distance(first, last);
44 #if TEST_STD_VER < 20 || \
45     (defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)
46 
47     std::vector<typename std::iterator_traits<Iter1>::value_type> v(size);
48 #else
49     assert((size <= 5) && "Increment the size of the array");
50     typename std::iterator_traits<Iter1>::value_type b[5];
51     std::span<typename std::iterator_traits<Iter1>::value_type> v{b, size};
52 #endif
53 
54 //  Not in place
55     std::exclusive_scan(first, last, v.begin(), init);
56     assert(std::equal(v.begin(), v.end(), rFirst, rLast));
57 
58 //  In place
59     std::copy(first, last, v.begin());
60     std::exclusive_scan(v.begin(), v.end(), v.begin(), init);
61     assert(std::equal(v.begin(), v.end(), rFirst, rLast));
62 }
63 
64 template <class Iter>
65 TEST_CONSTEXPR_CXX20 void
66 test()
67 {
68           int ia[]   = {1, 3, 5, 7,  9};
69     const int pRes[] = {0, 1, 4, 9, 16};
70     const unsigned sa = sizeof(ia) / sizeof(ia[0]);
71     static_assert(sa == sizeof(pRes) / sizeof(pRes[0]));       // just to be sure
72 
73     for (unsigned int i = 0; i < sa; ++i )
74         test(Iter(ia), Iter(ia + i), 0, pRes, pRes + i);
75 }
76 
77 constexpr size_t triangle(size_t n) { return n*(n+1)/2; }
78 
79 //  Basic sanity
80 TEST_CONSTEXPR_CXX20 void
81 basic_tests()
82 {
83     {
84     std::array<size_t, 10> v;
85     std::fill(v.begin(), v.end(), 3);
86     std::exclusive_scan(v.begin(), v.end(), v.begin(), size_t{50});
87     for (size_t i = 0; i < v.size(); ++i)
88         assert(v[i] == 50 + i * 3);
89     }
90 
91     {
92     std::array<size_t, 10> v;
93     std::iota(v.begin(), v.end(), 0);
94     std::exclusive_scan(v.begin(), v.end(), v.begin(), size_t{30});
95     for (size_t i = 0; i < v.size(); ++i)
96         assert(v[i] == 30 + triangle(i-1));
97     }
98 
99     {
100     std::array<size_t, 10> v;
101     std::iota(v.begin(), v.end(), 1);
102     std::exclusive_scan(v.begin(), v.end(), v.begin(), size_t{40});
103     for (size_t i = 0; i < v.size(); ++i)
104         assert(v[i] == 40 + triangle(i));
105     }
106 
107 }
108 
109 TEST_CONSTEXPR_CXX20 bool
110 test()
111 {
112     basic_tests();
113 
114 //  All the iterator categories
115     test<input_iterator        <const int*> >();
116     test<forward_iterator      <const int*> >();
117     test<bidirectional_iterator<const int*> >();
118     test<random_access_iterator<const int*> >();
119     test<const int*>();
120     test<      int*>();
121 
122     return true;
123 }
124 
125 int main(int, char**)
126 {
127     test();
128 #if TEST_STD_VER > 17
129     static_assert(test());
130 #endif
131     return 0;
132 }
133