aboutsummaryrefslogtreecommitdiff
path: root/pstl/test/std/algorithms/alg.modifying.operations/generate.pass.cpp
blob: 025fa387a16c0bdd8bdc30a5ee72a97be7b95528 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// -*- C++ -*-
//===-- generate.pass.cpp -------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03, c++11, c++14

#include "support/pstl_test_config.h"

#include <atomic>
#include <execution>
#include <algorithm>

#include "support/utils.h"

using namespace TestUtils;

template <typename T>
struct Generator_count
{
    const T def_val = T(-1);
    T
    operator()()
    {
        return def_val;
    }
    T
    default_value() const
    {
        return def_val;
    }
};

struct test_generate
{
    template <typename Policy, typename Iterator, typename Size>
    void
    operator()(Policy&& exec, Iterator first, Iterator last, Size n)
    {
        using namespace std;
        typedef typename std::iterator_traits<Iterator>::value_type T;

        // Try random-access iterator
        {
            Generator_count<T> g;
            generate(exec, first, last, g);
            Size count = std::count(first, last, g.default_value());
            EXPECT_TRUE(count == n, "generate wrong result for generate");
            std::fill(first, last, T(0));
        }

        {
            Generator_count<T> g;
            const auto m = n / 2;
            auto actual_last = generate_n(exec, first, m, g);
            Size count = std::count(first, actual_last, g.default_value());
            EXPECT_TRUE(count == m && actual_last == std::next(first, m),
                        "generate_n wrong result for generate_n");
            std::fill(first, actual_last, T(0));
        }
    }
};

template <typename T>
void
test_generate_by_type()
{
    for (size_t n = 0; n <= 100000; n = n < 16 ? n + 1 : size_t(3.1415 * n))
    {
        Sequence<T> in(n, [](size_t) -> T { return T(0); }); //fill by zero

        invoke_on_all_policies(test_generate(), in.begin(), in.end(), in.size());
    }
}

template <typename T>
struct test_non_const
{
    template <typename Policy, typename Iterator>
    void
    operator()(Policy&& exec, Iterator iter)
    {
        auto gen = []() { return T(0); };

        generate(exec, iter, iter, non_const(gen));
        generate_n(exec, iter, 0, non_const(gen));
    }
};

int32_t
main()
{

    test_generate_by_type<int32_t>();
    test_generate_by_type<float64_t>();

    test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());

    std::cout << done() << std::endl;
    return 0;
}