]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/25_algorithms/pstl/alg_modifying_operations/generate.cc
libstdc++: Set dg-timeout-factor for more slow tests
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / pstl / alg_modifying_operations / generate.cc
1 // -*- C++ -*-
2 // { dg-options "-std=gnu++17 -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 EXPECT_TRUE(std::count(first, last, g.default_value()) == n, "generate wrong result for generate");
61 std::fill(first, last, T(0));
62 }
63
64 {
65 Generator_count<T> g;
66 const auto m = n / 2;
67 auto last = generate_n(exec, first, m, g);
68 EXPECT_TRUE(std::count(first, last, g.default_value()) == m && last == std::next(first, m),
69 "generate_n wrong result for generate_n");
70 std::fill(first, last, T(0));
71 }
72 }
73 };
74
75 template <typename T>
76 void
77 test_generate_by_type()
78 {
79 for (size_t n = 0; n <= 100000; n = n < 16 ? n + 1 : size_t(3.1415 * n))
80 {
81 Sequence<T> in(n, [](size_t v) -> T { return T(0); }); //fill by zero
82
83 invoke_on_all_policies(test_generate(), in.begin(), in.end(), in.size());
84 }
85 }
86
87 template <typename T>
88 struct test_non_const
89 {
90 template <typename Policy, typename Iterator>
91 void
92 operator()(Policy&& exec, Iterator iter)
93 {
94 auto gen = []() { return T(0); };
95
96 generate(exec, iter, iter, non_const(gen));
97 generate_n(exec, iter, 0, non_const(gen));
98 }
99 };
100
101 int32_t
102 main()
103 {
104
105 test_generate_by_type<int32_t>();
106 test_generate_by_type<float64_t>();
107
108 test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
109
110 std::cout << done() << std::endl;
111 return 0;
112 }