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