1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // UNSUPPORTED: c++03, c++11, c++14 |
10 | |
11 | // UNSUPPORTED: libcpp-has-no-incomplete-pstl |
12 | |
13 | // template<class ExecutionPolicy, class ForwardIterator, class Size, class Generator> |
14 | // ForwardIterator generate_n(ExecutionPolicy&& exec, |
15 | // ForwardIterator first, Size n, Generator gen); |
16 | |
17 | #include <algorithm> |
18 | #include <cassert> |
19 | #include <vector> |
20 | |
21 | #include "test_iterators.h" |
22 | #include "test_execution_policies.h" |
23 | #include "type_algorithms.h" |
24 | |
25 | template <class Iter> |
26 | struct Test { |
27 | template <class ExecutionPolicy> |
28 | void operator()(ExecutionPolicy&& policy) { |
29 | { // simple test |
30 | int a[10]; |
31 | std::generate_n(policy, Iter(std::begin(arr&: a)), std::size(a), []() { return 1; }); |
32 | assert(std::all_of(std::begin(a), std::end(a), [](int i) { return i == 1; })); |
33 | } |
34 | { // empty range works |
35 | int a[10] {3}; |
36 | std::generate_n(policy, Iter(std::begin(arr&: a)), 0, []() { return 1; }); |
37 | assert(a[0] == 3); |
38 | } |
39 | { // single-element range works |
40 | int a[] {3}; |
41 | std::generate_n(policy, Iter(std::begin(arr&: a)), std::size(a), []() { return 5; }); |
42 | assert(a[0] == 5); |
43 | } |
44 | { // large range works |
45 | std::vector<int> vec(150, 4); |
46 | std::generate_n(policy, Iter(std::data(cont&: vec)), std::size(cont: vec), []() { return 5; }); |
47 | assert(std::all_of(std::begin(vec), std::end(vec), [](int i) { return i == 5; })); |
48 | } |
49 | } |
50 | }; |
51 | |
52 | int main(int, char**) { |
53 | types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{}); |
54 | |
55 | return 0; |
56 | } |
57 | |