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 // <algorithm>
14 
15 // template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
16 //          class ForwardIterator, class BinaryOperation>
17 //   ForwardIterator
18 //     transform(ExecutionPolicy&& exec,
19 //               ForwardIterator1 first1, ForwardIterator1 last1,
20 //               ForwardIterator2 first2, ForwardIterator result,
21 //               BinaryOperation binary_op);
22 
23 #include <algorithm>
24 #include <vector>
25 
26 #include "test_macros.h"
27 #include "test_execution_policies.h"
28 #include "test_iterators.h"
29 
30 EXECUTION_POLICY_SFINAE_TEST(transform);
31 
32 static_assert(sfinae_test_transform<int, int*, int*, int*, int*, bool (*)(int)>);
33 static_assert(!sfinae_test_transform<std::execution::parallel_policy, int*, int*, int*, int*, int (*)(int, int)>);
34 
35 template <class Iter1, class Iter2, class Iter3>
36 struct Test {
37   template <class Policy>
38   void operator()(Policy&& policy) {
39     // simple test
40     for (const int size : {0, 1, 2, 100, 350}) {
41       std::vector<int> a(size);
42       std::vector<int> b(size);
43       for (int i = 0; i != size; ++i) {
44         a[i] = i + 1;
45         b[i] = i - 3;
46       }
47 
48       std::vector<int> out(std::size(a));
49       decltype(auto) ret = std::transform(
50           policy,
51           Iter1(std::data(a)),
52           Iter1(std::data(a) + std::size(a)),
53           Iter2(std::data(b)),
54           Iter3(std::data(out)),
55           [](int i, int j) { return i + j + 3; });
56       static_assert(std::is_same_v<decltype(ret), Iter3>);
57       assert(base(ret) == std::data(out) + std::size(out));
58       for (int i = 0; i != size; ++i) {
59         assert(out[i] == i * 2 + 1);
60       }
61     }
62   }
63 };
64 
65 template <class Iter3>
66 struct TestIterators2 {
67   template <class Iter2>
68   void operator()() {
69     types::for_each(types::forward_iterator_list<int*>{},
70                     TestIteratorWithPolicies<types::partial_instantiation<Test, Iter2, Iter3>::template apply>{});
71   }
72 };
73 
74 struct TestIterators1 {
75   template <class Iter3>
76   void operator()() {
77     types::for_each(types::forward_iterator_list<int*>{}, TestIterators2<Iter3>{});
78   }
79 };
80 
81 int main(int, char**) {
82   types::for_each(types::forward_iterator_list<int*>{}, TestIterators1{});
83 
84   return 0;
85 }
86