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 // UNSUPPORTED: c++98, c++03, c++11, c++14 11 12 #include "support/pstl_test_config.h" 13 14 #include <atomic> 15 #include <execution> 16 #include <algorithm> 17 18 #include "support/utils.h" 19 20 using namespace TestUtils; 21 22 template <typename T> 23 struct Generator_count 24 { 25 const T def_val = T(-1); 26 T 27 operator()() 28 { 29 return def_val; 30 } 31 T 32 default_value() const 33 { 34 return def_val; 35 } 36 }; 37 38 struct test_generate 39 { 40 template <typename Policy, typename Iterator, typename Size> 41 void 42 operator()(Policy&& exec, Iterator first, Iterator last, Size n) 43 { 44 using namespace std; 45 typedef typename std::iterator_traits<Iterator>::value_type T; 46 47 // Try random-access iterator 48 { 49 Generator_count<T> g; 50 generate(exec, first, last, g); 51 Size count = std::count(first, last, g.default_value()); 52 EXPECT_TRUE(count == n, "generate wrong result for generate"); 53 std::fill(first, last, T(0)); 54 } 55 56 { 57 Generator_count<T> g; 58 const auto m = n / 2; 59 auto actual_last = generate_n(exec, first, m, g); 60 Size count = std::count(first, actual_last, g.default_value()); 61 EXPECT_TRUE(count == m && actual_last == std::next(first, m), 62 "generate_n wrong result for generate_n"); 63 std::fill(first, actual_last, T(0)); 64 } 65 } 66 }; 67 68 template <typename T> 69 void 70 test_generate_by_type() 71 { 72 for (size_t n = 0; n <= 100000; n = n < 16 ? n + 1 : size_t(3.1415 * n)) 73 { 74 Sequence<T> in(n, [](size_t) -> T { return T(0); }); //fill by zero 75 76 invoke_on_all_policies(test_generate(), in.begin(), in.end(), in.size()); 77 } 78 } 79 80 template <typename T> 81 struct test_non_const 82 { 83 template <typename Policy, typename Iterator> 84 void 85 operator()(Policy&& exec, Iterator iter) 86 { 87 auto gen = []() { return T(0); }; 88 89 generate(exec, iter, iter, non_const(gen)); 90 generate_n(exec, iter, 0, non_const(gen)); 91 } 92 }; 93 94 int 95 main() 96 { 97 98 test_generate_by_type<int32_t>(); 99 test_generate_by_type<float64_t>(); 100 101 test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); 102 103 std::cout << done() << std::endl; 104 return 0; 105 } 106