diff options
Diffstat (limited to 'libcxx/test/std/numerics/numeric.ops/transform.exclusive.scan/transform_exclusive_scan_init_bop_uop.pass.cpp')
-rw-r--r-- | libcxx/test/std/numerics/numeric.ops/transform.exclusive.scan/transform_exclusive_scan_init_bop_uop.pass.cpp | 79 |
1 files changed, 65 insertions, 14 deletions
diff --git a/libcxx/test/std/numerics/numeric.ops/transform.exclusive.scan/transform_exclusive_scan_init_bop_uop.pass.cpp b/libcxx/test/std/numerics/numeric.ops/transform.exclusive.scan/transform_exclusive_scan_init_bop_uop.pass.cpp index 301fb2f24242..d7fef8e90d19 100644 --- a/libcxx/test/std/numerics/numeric.ops/transform.exclusive.scan/transform_exclusive_scan_init_bop_uop.pass.cpp +++ b/libcxx/test/std/numerics/numeric.ops/transform.exclusive.scan/transform_exclusive_scan_init_bop_uop.pass.cpp @@ -8,7 +8,9 @@ // <numeric> // UNSUPPORTED: c++03, c++11, c++14 +// UNSUPPORTED: clang-8 +// Became constexpr in C++20 // template<class InputIterator, class OutputIterator, class T, // class BinaryOperation, class UnaryOperation> // OutputIterator transform_exclusive_scan(InputIterator first, InputIterator last, @@ -19,6 +21,7 @@ #include <numeric> #include <algorithm> +#include <array> #include <cassert> #include <functional> #include <iterator> @@ -26,6 +29,10 @@ #include "test_macros.h" #include "test_iterators.h" +// FIXME Remove constexpr vector workaround introduced in D90569 +#if TEST_STD_VER > 17 +#include <span> +#endif struct add_one { template <typename T> @@ -35,24 +42,37 @@ struct add_one { }; template <class Iter1, class BOp, class UOp, class T, class Iter2> -void +TEST_CONSTEXPR_CXX20 void test(Iter1 first, Iter1 last, BOp bop, UOp uop, T init, Iter2 rFirst, Iter2 rLast) { - std::vector<typename std::iterator_traits<Iter1>::value_type> v; + // C++17 doesn't test constexpr so can use a vector. + // C++20 can use vector in constexpr evaluation, but both libc++ and MSVC + // don't have the support yet. In these cases use a std::span for the test. + // FIXME Remove constexpr vector workaround introduced in D90569 + size_t size = std::distance(first, last); +#if TEST_STD_VER < 20 || \ + (defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L) + + std::vector<typename std::iterator_traits<Iter1>::value_type> v(size); +#else + assert((size <= 5) && "Increment the size of the array"); + typename std::iterator_traits<Iter1>::value_type b[5]; + std::span<typename std::iterator_traits<Iter1>::value_type> v{b, size}; +#endif + // Test not in-place - std::transform_exclusive_scan(first, last, std::back_inserter(v), init, bop, uop); + std::transform_exclusive_scan(first, last, v.begin(), init, bop, uop); assert(std::equal(v.begin(), v.end(), rFirst, rLast)); // Test in-place - v.clear(); - v.assign(first, last); + std::copy(first, last, v.begin()); std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), init, bop, uop); assert(std::equal(v.begin(), v.end(), rFirst, rLast)); } template <class Iter> -void +TEST_CONSTEXPR_CXX20 void test() { int ia[] = { 1, 3, 5, 7, 9 }; @@ -86,13 +106,14 @@ test() } } -size_t triangle(size_t n) { return n*(n+1)/2; } +constexpr size_t triangle(size_t n) { return n*(n+1)/2; } // Basic sanity -void basic_tests() +TEST_CONSTEXPR_CXX20 void +basic_tests() { { - std::vector<size_t> v(10); + std::array<size_t, 10> v; std::fill(v.begin(), v.end(), 3); std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), size_t{50}, std::plus<>(), add_one{}); for (size_t i = 0; i < v.size(); ++i) @@ -100,7 +121,7 @@ void basic_tests() } { - std::vector<size_t> v(10); + std::array<size_t, 10> v; std::iota(v.begin(), v.end(), 0); std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), size_t{30}, std::plus<>(), add_one{}); for (size_t i = 0; i < v.size(); ++i) @@ -108,7 +129,7 @@ void basic_tests() } { - std::vector<size_t> v(10); + std::array<size_t, 10> v; std::iota(v.begin(), v.end(), 1); std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), size_t{40}, std::plus<>(), add_one{}); for (size_t i = 0; i < v.size(); ++i) @@ -116,17 +137,37 @@ void basic_tests() } { + // C++17 doesn't test constexpr so can use a vector. + // C++20 can use vector in constexpr evaluation, but both libc++ and MSVC + // don't have the support yet. In these cases use a std::span for the test. + // FIXME Remove constexpr vector workaround introduced in D90569 +#if TEST_STD_VER < 20 || \ + (defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L) std::vector<size_t> v, res; std::transform_exclusive_scan(v.begin(), v.end(), std::back_inserter(res), size_t{40}, std::plus<>(), add_one{}); +#else + std::array<size_t, 0> v, res; + std::transform_exclusive_scan(v.begin(), v.end(), res.begin(), size_t{40}, std::plus<>(), add_one{}); +#endif assert(res.empty()); } // Make sure that the calculations are done using the init typedef { - std::vector<unsigned char> v(10); + std::array<unsigned char, 10> v; std::iota(v.begin(), v.end(), static_cast<unsigned char>(1)); + // C++17 doesn't test constexpr so can use a vector. + // C++20 can use vector in constexpr evaluation, but both libc++ and MSVC + // don't have the support yet. In these cases use a std::span for the test. + // FIXME Remove constexpr vector workaround introduced in D90569 +#if TEST_STD_VER < 20 || \ + (defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L) std::vector<size_t> res; std::transform_exclusive_scan(v.begin(), v.end(), std::back_inserter(res), size_t{1}, std::multiplies<>(), add_one{}); +#else + std::array<size_t, 10> res; + std::transform_exclusive_scan(v.begin(), v.end(), res.begin(), size_t{1}, std::multiplies<>(), add_one{}); +#endif assert(res.size() == 10); size_t j = 1; @@ -139,7 +180,8 @@ void basic_tests() } } -int main(int, char**) +TEST_CONSTEXPR_CXX20 bool +test() { basic_tests(); @@ -151,5 +193,14 @@ int main(int, char**) test<const int*>(); test< int*>(); - return 0; + return true; +} + +int main(int, char**) +{ + test(); +#if TEST_STD_VER > 17 + static_assert(test()); +#endif + return 0; } |