| 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 | // class BinaryOperation, class UnaryOperation> |
| 16 | // OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last, |
| 17 | // OutputIterator result, |
| 18 | // BinaryOperation binary_op, |
| 19 | // UnaryOperation unary_op); |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <array> |
| 23 | #include <cassert> |
| 24 | #include <cstddef> |
| 25 | #include <functional> |
| 26 | #include <numeric> |
| 27 | |
| 28 | #include "test_macros.h" |
| 29 | #include "test_iterators.h" |
| 30 | |
| 31 | struct add_one { |
| 32 | template <typename T> |
| 33 | constexpr T operator()(T x) const { |
| 34 | return x + 1; |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | template <class Iter1, class BOp, class UOp, class T> |
| 39 | TEST_CONSTEXPR_CXX20 void |
| 40 | test(Iter1 first, Iter1 last, BOp bop, UOp uop, const T *rFirst, const T *rLast) |
| 41 | { |
| 42 | assert((rLast - rFirst) <= 5); // or else increase the size of "out" |
| 43 | T out[5]; |
| 44 | |
| 45 | // Not in place |
| 46 | T *end = std::transform_inclusive_scan(first, last, out, bop, uop); |
| 47 | assert(std::equal(out, end, rFirst, rLast)); |
| 48 | |
| 49 | // In place |
| 50 | std::copy(first, last, out); |
| 51 | end = std::transform_inclusive_scan(out, end, out, bop, uop); |
| 52 | assert(std::equal(out, end, rFirst, rLast)); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | template <class Iter> |
| 57 | TEST_CONSTEXPR_CXX20 void |
| 58 | test() |
| 59 | { |
| 60 | int ia[] = { 1, 3, 5, 7, 9 }; |
| 61 | const int pResI0[] = { 2, 6, 12, 20, 30 }; // with add_one |
| 62 | const int mResI0[] = { 2, 8, 48, 384, 3840 }; |
| 63 | const int pResN0[] = { -1, -4, -9, -16, -25 }; // with negate |
| 64 | const int mResN0[] = { -1, 3, -15, 105, -945 }; |
| 65 | const unsigned sa = sizeof(ia) / sizeof(ia[0] ); |
| 66 | static_assert(sa == sizeof(pResI0) / sizeof(pResI0[0])); // just to be sure |
| 67 | static_assert(sa == sizeof(mResI0) / sizeof(mResI0[0])); // just to be sure |
| 68 | static_assert(sa == sizeof(pResN0) / sizeof(pResN0[0])); // just to be sure |
| 69 | static_assert(sa == sizeof(mResN0) / sizeof(mResN0[0])); // just to be sure |
| 70 | |
| 71 | for (unsigned int i = 0; i < sa; ++i ) { |
| 72 | test(Iter(ia), Iter(ia + i), std::plus<>(), add_one{}, pResI0, pResI0 + i); |
| 73 | test(Iter(ia), Iter(ia + i), std::multiplies<>(), add_one{}, mResI0, mResI0 + i); |
| 74 | test(Iter(ia), Iter(ia + i), std::plus<>(), std::negate<>(), pResN0, pResN0 + i); |
| 75 | test(Iter(ia), Iter(ia + i), std::multiplies<>(), std::negate<>(), mResN0, mResN0 + i); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | constexpr std::size_t triangle(size_t n) { return n*(n+1)/2; } |
| 80 | |
| 81 | // Basic sanity |
| 82 | TEST_CONSTEXPR_CXX20 void |
| 83 | basic_tests() |
| 84 | { |
| 85 | { |
| 86 | std::array<std::size_t, 10> v; |
| 87 | std::fill(first: v.begin(), last: v.end(), value: 3); |
| 88 | std::transform_inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), binary_op: std::plus<>(), unary_op: add_one{}); |
| 89 | for (std::size_t i = 0; i < v.size(); ++i) |
| 90 | assert(v[i] == (i+1) * 4); |
| 91 | } |
| 92 | |
| 93 | { |
| 94 | std::array<std::size_t, 10> v; |
| 95 | std::iota(first: v.begin(), last: v.end(), value: 0); |
| 96 | std::transform_inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), binary_op: std::plus<>(), unary_op: add_one{}); |
| 97 | for (std::size_t i = 0; i < v.size(); ++i) |
| 98 | assert(v[i] == triangle(i) + i + 1); |
| 99 | } |
| 100 | |
| 101 | { |
| 102 | std::array<std::size_t, 10> v; |
| 103 | std::iota(first: v.begin(), last: v.end(), value: 1); |
| 104 | std::transform_inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), binary_op: std::plus<>(), unary_op: add_one{}); |
| 105 | for (std::size_t i = 0; i < v.size(); ++i) |
| 106 | assert(v[i] == triangle(i + 1) + i + 1); |
| 107 | } |
| 108 | |
| 109 | { |
| 110 | std::array<std::size_t, 0> v, res; |
| 111 | std::transform_inclusive_scan(first: v.begin(), last: v.end(), result: res.begin(), binary_op: std::plus<>(), unary_op: add_one{}); |
| 112 | assert(res.empty()); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | TEST_CONSTEXPR_CXX20 bool |
| 117 | test() |
| 118 | { |
| 119 | basic_tests(); |
| 120 | |
| 121 | // All the iterator categories |
| 122 | test<cpp17_input_iterator <const int*> >(); |
| 123 | test<forward_iterator <const int*> >(); |
| 124 | test<bidirectional_iterator<const int*> >(); |
| 125 | test<random_access_iterator<const int*> >(); |
| 126 | test<const int*>(); |
| 127 | test< int*>(); |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | int main(int, char**) |
| 133 | { |
| 134 | test(); |
| 135 | #if TEST_STD_VER > 17 |
| 136 | static_assert(test()); |
| 137 | #endif |
| 138 | return 0; |
| 139 | } |
| 140 | |