| 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 exclusive_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, T init, 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::exclusive_scan(first, last, out, init); |
| 37 | assert(std::equal(out, end, rFirst, rLast)); |
| 38 | |
| 39 | // In place |
| 40 | std::copy(first, last, out); |
| 41 | end = std::exclusive_scan(out, end, out, init); |
| 42 | assert(std::equal(out, end, rFirst, rLast)); |
| 43 | } |
| 44 | |
| 45 | template <class Iter> |
| 46 | TEST_CONSTEXPR_CXX20 void |
| 47 | test() |
| 48 | { |
| 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 |
| 53 | |
| 54 | for (unsigned int i = 0; i < sa; ++i) { |
| 55 | test(Iter(ia), Iter(ia + i), 0, pRes, pRes + i); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | constexpr std::size_t triangle(size_t n) { return n*(n+1)/2; } |
| 60 | |
| 61 | // Basic sanity |
| 62 | TEST_CONSTEXPR_CXX20 void |
| 63 | basic_tests() |
| 64 | { |
| 65 | { |
| 66 | std::array<std::size_t, 10> v; |
| 67 | std::fill(first: v.begin(), last: v.end(), value: 3); |
| 68 | std::exclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), init: std::size_t{50}); |
| 69 | for (std::size_t i = 0; i < v.size(); ++i) |
| 70 | assert(v[i] == 50 + i * 3); |
| 71 | } |
| 72 | |
| 73 | { |
| 74 | std::array<std::size_t, 10> v; |
| 75 | std::iota(first: v.begin(), last: v.end(), value: 0); |
| 76 | std::exclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), init: std::size_t{30}); |
| 77 | for (std::size_t i = 0; i < v.size(); ++i) |
| 78 | assert(v[i] == 30 + triangle(i-1)); |
| 79 | } |
| 80 | |
| 81 | { |
| 82 | std::array<std::size_t, 10> v; |
| 83 | std::iota(first: v.begin(), last: v.end(), value: 1); |
| 84 | std::exclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), init: std::size_t{40}); |
| 85 | for (std::size_t i = 0; i < v.size(); ++i) |
| 86 | assert(v[i] == 40 + triangle(i)); |
| 87 | } |
| 88 | |
| 89 | } |
| 90 | |
| 91 | TEST_CONSTEXPR_CXX20 bool |
| 92 | test() |
| 93 | { |
| 94 | basic_tests(); |
| 95 | |
| 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*> >(); |
| 101 | test<const int*>(); |
| 102 | test< int*>(); |
| 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | int main(int, char**) |
| 108 | { |
| 109 | test(); |
| 110 | #if TEST_STD_VER > 17 |
| 111 | static_assert(test()); |
| 112 | #endif |
| 113 | return 0; |
| 114 | } |
| 115 | |