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