1 // -*- C++ -*- 2 //===-- generate.pass.cpp -------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "support/pstl_test_config.h" 11 12 #ifdef PSTL_STANDALONE_TESTS 13 #include <atomic> 14 15 #include "pstl/execution" 16 #include "pstl/algorithm" 17 #else 18 #include <execution> 19 #include <algorithm> 20 #endif // PSTL_STANDALONE_TESTS 21 22 #include "support/utils.h" 23 24 using namespace TestUtils; 25 26 template <typename T> 27 struct Generator_count 28 { 29 const T def_val = T(-1); 30 T 31 operator()() 32 { 33 return def_val; 34 } 35 T 36 default_value() const 37 { 38 return def_val; 39 } 40 }; 41 42 struct test_generate 43 { 44 template <typename Policy, typename Iterator, typename Size> 45 void 46 operator()(Policy&& exec, Iterator first, Iterator last, Size n) 47 { 48 using namespace std; 49 typedef typename std::iterator_traits<Iterator>::value_type T; 50 51 // Try random-access iterator 52 { 53 Generator_count<T> g; 54 generate(exec, first, last, g); 55 EXPECT_TRUE(std::count(first, last, g.default_value()) == n, "generate wrong result for generate"); 56 std::fill(first, last, T(0)); 57 } 58 59 { 60 Generator_count<T> g; 61 const auto m = n / 2; 62 auto last = generate_n(exec, first, m, g); 63 EXPECT_TRUE(std::count(first, last, g.default_value()) == m && last == std::next(first, m), 64 "generate_n wrong result for generate_n"); 65 std::fill(first, last, T(0)); 66 } 67 } 68 }; 69 70 template <typename T> 71 void 72 test_generate_by_type() 73 { 74 for (size_t n = 0; n <= 100000; n = n < 16 ? n + 1 : size_t(3.1415 * n)) 75 { 76 Sequence<T> in(n, [](size_t v) -> T { return T(0); }); //fill by zero 77 78 invoke_on_all_policies(test_generate(), in.begin(), in.end(), in.size()); 79 } 80 } 81 82 template <typename T> 83 struct test_non_const 84 { 85 template <typename Policy, typename Iterator> 86 void 87 operator()(Policy&& exec, Iterator iter) 88 { 89 auto gen = []() { return T(0); }; 90 91 generate(exec, iter, iter, non_const(gen)); 92 generate_n(exec, iter, 0, non_const(gen)); 93 } 94 }; 95 96 int32_t 97 main() 98 { 99 100 test_generate_by_type<int32_t>(); 101 test_generate_by_type<float64_t>(); 102 103 test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); 104 105 std::cout << done() << std::endl; 106 return 0; 107 } 108