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 // <numeric> 10 // UNSUPPORTED: c++03, c++11, c++14 11 // UNSUPPORTED: clang-8 12 // UNSUPPORTED: gcc-9 13 14 // Became constexpr in C++20 15 // template<class InputIterator, class OutputIterator, class T, class BinaryOperation> 16 // OutputIterator 17 // exclusive_scan(InputIterator first, InputIterator last, 18 // OutputIterator result, 19 // T init, BinaryOperation binary_op); // C++17 20 21 #include <numeric> 22 #include <algorithm> 23 #include <array> 24 #include <cassert> 25 #include <functional> 26 #include <iterator> 27 #include <vector> 28 29 #include "test_macros.h" 30 #include "test_iterators.h" 31 // FIXME Remove constexpr vector workaround introduced in D90569 32 #if TEST_STD_VER > 17 33 #include <span> 34 #endif 35 36 template <class Iter1, class T, class Op, class Iter2> 37 TEST_CONSTEXPR_CXX20 void 38 test(Iter1 first, Iter1 last, T init, Op op, Iter2 rFirst, Iter2 rLast) 39 { 40 // C++17 doesn't test constexpr so can use a vector. 41 // C++20 can use vector in constexpr evaluation, but both libc++ and MSVC 42 // don't have the support yet. In these cases use a std::span for the test. 43 // FIXME Remove constexpr vector workaround introduced in D90569 44 size_t size = std::distance(first, last); 45 #if TEST_STD_VER < 20 || \ 46 (defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L) 47 48 std::vector<typename std::iterator_traits<Iter1>::value_type> v(size); 49 #else 50 assert((size <= 5) && "Increment the size of the array"); 51 typename std::iterator_traits<Iter1>::value_type b[5]; 52 std::span<typename std::iterator_traits<Iter1>::value_type> v{b, size}; 53 #endif 54 55 // Not in place 56 std::exclusive_scan(first, last, v.begin(), init, op); 57 assert(std::equal(v.begin(), v.end(), rFirst, rLast)); 58 59 // In place 60 std::copy(first, last, v.begin()); 61 std::exclusive_scan(v.begin(), v.end(), v.begin(), init, op); 62 assert(std::equal(v.begin(), v.end(), rFirst, rLast)); 63 } 64 65 66 template <class Iter> 67 TEST_CONSTEXPR_CXX20 void 68 test() 69 { 70 int ia[] = {1, 3, 5, 7, 9}; 71 const int pRes[] = {0, 1, 4, 9, 16}; 72 const int mRes[] = {1, 1, 3, 15, 105}; 73 const unsigned sa = sizeof(ia) / sizeof(ia[0]); 74 static_assert(sa == sizeof(pRes) / sizeof(pRes[0])); // just to be sure 75 static_assert(sa == sizeof(mRes) / sizeof(mRes[0])); // just to be sure 76 77 for (unsigned int i = 0; i < sa; ++i ) { 78 test(Iter(ia), Iter(ia + i), 0, std::plus<>(), pRes, pRes + i); 79 test(Iter(ia), Iter(ia + i), 1, std::multiplies<>(), mRes, mRes + i); 80 } 81 } 82 83 TEST_CONSTEXPR_CXX20 bool 84 test() 85 { 86 // All the iterator categories 87 test<input_iterator <const int*> >(); 88 test<forward_iterator <const int*> >(); 89 test<bidirectional_iterator<const int*> >(); 90 test<random_access_iterator<const int*> >(); 91 test<const int*>(); 92 test< int*>(); 93 94 // Make sure that the calculations are done using the init typedef 95 { 96 std::array<unsigned char, 10> v; 97 std::iota(v.begin(), v.end(), static_cast<unsigned char>(1)); 98 // C++17 doesn't test constexpr so can use a vector. 99 // C++20 can use vector in constexpr evaluation, but both libc++ and MSVC 100 // don't have the support yet. In these cases use a std::span for the test. 101 // FIXME Remove constexpr vector workaround introduced in D90569 102 #if TEST_STD_VER < 20 || \ 103 (defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L) 104 std::vector<size_t> res; 105 std::exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 1, std::multiplies<>()); 106 #else 107 std::array<size_t, 10> res; 108 std::exclusive_scan(v.begin(), v.end(), res.begin(), 1, std::multiplies<>()); 109 #endif 110 111 assert(res.size() == 10); 112 size_t j = 1; 113 assert(res[0] == 1); 114 for (size_t i = 1; i < v.size(); ++i) 115 { 116 j *= i; 117 assert(res[i] == j); 118 } 119 } 120 121 return true; 122 } 123 124 int main(int, char**) 125 { 126 test(); 127 #if TEST_STD_VER > 17 128 static_assert(test()); 129 #endif 130 return 0; 131 } 132