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