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