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 // exclusive_scan(InputIterator first, InputIterator last, 19 // OutputIterator result, 20 // T init, 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 T, class Op> 33 TEST_CONSTEXPR_CXX20 void 34 test(Iter1 first, Iter1 last, T init, 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::exclusive_scan(first, last, out, init, op); 41 assert(std::equal(out, end, rFirst, rLast)); 42 43 // In place 44 std::copy(first, last, out); 45 end = std::exclusive_scan(out, end, out, init, 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[] = {0, 1, 4, 9, 16}; 56 const int mRes[] = {1, 1, 3, 15, 105}; 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), 0, std::plus<>(), pRes, pRes + i); 63 test(Iter(ia), Iter(ia + i), 1, std::multiplies<>(), mRes, mRes + i); 64 } 65 } 66 67 TEST_CONSTEXPR_CXX20 bool 68 test() 69 { 70 // All the iterator categories 71 test<input_iterator <const int*> >(); 72 test<forward_iterator <const int*> >(); 73 test<bidirectional_iterator<const int*> >(); 74 test<random_access_iterator<const int*> >(); 75 test<const int*>(); 76 test< int*>(); 77 78 // Make sure that the calculations are done using the init typedef 79 { 80 std::array<unsigned char, 10> v; 81 std::iota(v.begin(), v.end(), static_cast<unsigned char>(1)); 82 std::array<size_t, 10> res; 83 std::exclusive_scan(v.begin(), v.end(), res.begin(), 1, std::multiplies<>()); 84 85 assert(res.size() == 10); 86 size_t j = 1; 87 assert(res[0] == 1); 88 for (size_t i = 1; i < v.size(); ++i) 89 { 90 j *= i; 91 assert(res[i] == j); 92 } 93 } 94 95 return true; 96 } 97 98 int main(int, char**) 99 { 100 test(); 101 #if TEST_STD_VER > 17 102 static_assert(test()); 103 #endif 104 return 0; 105 } 106