1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
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);
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
, T init
, const T
*rFirst
, const T
*rLast
)
33 assert((rLast
- rFirst
) <= 5); // or else increase the size of "out"
37 T
*end
= std::exclusive_scan(first
, last
, out
, init
);
38 assert(std::equal(out
, end
, rFirst
, rLast
));
41 std::copy(first
, last
, out
);
42 end
= std::exclusive_scan(out
, end
, out
, init
);
43 assert(std::equal(out
, end
, rFirst
, rLast
));
47 TEST_CONSTEXPR_CXX20
void
50 int ia
[] = {1, 3, 5, 7, 9};
51 const int pRes
[] = {0, 1, 4, 9, 16};
52 const unsigned sa
= sizeof(ia
) / sizeof(ia
[0]);
53 static_assert(sa
== sizeof(pRes
) / sizeof(pRes
[0])); // just to be sure
55 for (unsigned int i
= 0; i
< sa
; ++i
) {
56 test(Iter(ia
), Iter(ia
+ i
), 0, pRes
, pRes
+ i
);
60 constexpr std::size_t triangle(size_t n
) { return n
*(n
+1)/2; }
63 TEST_CONSTEXPR_CXX20
void
67 std::array
<std::size_t, 10> v
;
68 std::fill(v
.begin(), v
.end(), 3);
69 std::exclusive_scan(v
.begin(), v
.end(), v
.begin(), std::size_t{50});
70 for (std::size_t i
= 0; i
< v
.size(); ++i
)
71 assert(v
[i
] == 50 + i
* 3);
75 std::array
<std::size_t, 10> v
;
76 std::iota(v
.begin(), v
.end(), 0);
77 std::exclusive_scan(v
.begin(), v
.end(), v
.begin(), std::size_t{30});
78 for (std::size_t i
= 0; i
< v
.size(); ++i
)
79 assert(v
[i
] == 30 + triangle(i
-1));
83 std::array
<std::size_t, 10> v
;
84 std::iota(v
.begin(), v
.end(), 1);
85 std::exclusive_scan(v
.begin(), v
.end(), v
.begin(), std::size_t{40});
86 for (std::size_t i
= 0; i
< v
.size(); ++i
)
87 assert(v
[i
] == 40 + triangle(i
));
92 TEST_CONSTEXPR_CXX20
bool
97 // All the iterator categories
98 test
<cpp17_input_iterator
<const int*> >();
99 test
<forward_iterator
<const int*> >();
100 test
<bidirectional_iterator
<const int*> >();
101 test
<random_access_iterator
<const int*> >();
108 int main(int, char**)
111 #if TEST_STD_VER > 17
112 static_assert(test());