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);
25 #include "test_macros.h"
26 #include "test_iterators.h"
28 template <class Iter1
, class T
>
29 TEST_CONSTEXPR_CXX20
void
30 test(Iter1 first
, Iter1 last
, T init
, const T
*rFirst
, const T
*rLast
)
32 assert((rLast
- rFirst
) <= 5); // or else increase the size of "out"
36 T
*end
= std::exclusive_scan(first
, last
, out
, init
);
37 assert(std::equal(out
, end
, rFirst
, rLast
));
40 std::copy(first
, last
, out
);
41 end
= std::exclusive_scan(out
, end
, out
, init
);
42 assert(std::equal(out
, end
, rFirst
, rLast
));
46 TEST_CONSTEXPR_CXX20
void
49 int ia
[] = {1, 3, 5, 7, 9};
50 const int pRes
[] = {0, 1, 4, 9, 16};
51 const unsigned sa
= sizeof(ia
) / sizeof(ia
[0]);
52 static_assert(sa
== sizeof(pRes
) / sizeof(pRes
[0])); // just to be sure
54 for (unsigned int i
= 0; i
< sa
; ++i
) {
55 test(Iter(ia
), Iter(ia
+ i
), 0, pRes
, pRes
+ i
);
59 constexpr std::size_t triangle(size_t n
) { return n
*(n
+1)/2; }
62 TEST_CONSTEXPR_CXX20
void
66 std::array
<std::size_t, 10> v
;
67 std::fill(v
.begin(), v
.end(), 3);
68 std::exclusive_scan(v
.begin(), v
.end(), v
.begin(), std::size_t{50});
69 for (std::size_t i
= 0; i
< v
.size(); ++i
)
70 assert(v
[i
] == 50 + i
* 3);
74 std::array
<std::size_t, 10> v
;
75 std::iota(v
.begin(), v
.end(), 0);
76 std::exclusive_scan(v
.begin(), v
.end(), v
.begin(), std::size_t{30});
77 for (std::size_t i
= 0; i
< v
.size(); ++i
)
78 assert(v
[i
] == 30 + triangle(i
-1));
82 std::array
<std::size_t, 10> v
;
83 std::iota(v
.begin(), v
.end(), 1);
84 std::exclusive_scan(v
.begin(), v
.end(), v
.begin(), std::size_t{40});
85 for (std::size_t i
= 0; i
< v
.size(); ++i
)
86 assert(v
[i
] == 40 + triangle(i
));
91 TEST_CONSTEXPR_CXX20
bool
96 // All the iterator categories
97 test
<cpp17_input_iterator
<const int*> >();
98 test
<forward_iterator
<const int*> >();
99 test
<bidirectional_iterator
<const int*> >();
100 test
<random_access_iterator
<const int*> >();
107 int main(int, char**)
110 #if TEST_STD_VER > 17
111 static_assert(test());