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 Generator>
14 // void generate(ExecutionPolicy&& exec,
15 // ForwardIterator first, ForwardIterator last,
16 // Generator gen);
17
18 #include <algorithm>
19 #include <cassert>
20 #include <vector>
21
22 #include "test_iterators.h"
23 #include "test_execution_policies.h"
24 #include "type_algorithms.h"
25
26 template <class Iter>
27 struct Test {
28 template <class ExecutionPolicy>
operator ()Test29 void operator()(ExecutionPolicy&& policy) {
30 { // simple test
31 int a[10];
32 std::generate(policy, Iter(std::begin(a)), Iter(std::end(a)), []() { return 1; });
33 assert(std::all_of(std::begin(a), std::end(a), [](int i) { return i == 1; }));
34 }
35 { // empty range works
36 int a[10] {3};
37 std::generate(policy, Iter(std::begin(a)), Iter(std::begin(a)), []() { return 1; });
38 assert(a[0] == 3);
39 }
40 { // single-element range works
41 int a[] {3};
42 std::generate(policy, Iter(std::begin(a)), Iter(std::end(a)), []() { return 5; });
43 assert(a[0] == 5);
44 }
45 { // large range works
46 std::vector<int> vec(150, 4);
47 std::generate(policy, Iter(std::data(vec)), Iter(std::data(vec) + std::size(vec)), []() { return 5; });
48 assert(std::all_of(std::begin(vec), std::end(vec), [](int i) { return i == 5; }));
49 }
50 }
51 };
52
main(int,char **)53 int main(int, char**) {
54 types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});
55
56 return 0;
57 }
58