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// REQUIRES: with-pstl
12
13// <algorithm>
14
15// template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
16// ForwardIterator2 copy(ExecutionPolicy&& policy,
17// ForwardIterator1 first, ForwardIterator1 last,
18// ForwardIterator2 result);
19
20#include <algorithm>
21#include <vector>
22
23#include "test_macros.h"
24#include "test_execution_policies.h"
25#include "test_iterators.h"
26#include "type_algorithms.h"
27
28EXECUTION_POLICY_SFINAE_TEST(copy);
29
30static_assert(sfinae_test_copy<int, int*, int*, bool (*)(int)>);
31static_assert(!sfinae_test_copy<std::execution::parallel_policy, int*, int*, int>);
32
33template <class Iter1, class Iter2>
34struct TestInt {
35 template <class Policy>
36 void operator()(Policy&& policy) {
37 // simple test
38 for (const int size : {0, 1, 2, 100, 350}) {
39 std::vector<int> a(size);
40 for (int i = 0; i != size; ++i)
41 a[i] = i + 1;
42
43 std::vector<int> out(std::size(cont: a));
44 decltype(auto) ret =
45 std::copy(policy, Iter1(std::data(cont&: a)), Iter1(std::data(cont&: a) + std::size(cont: a)), Iter2(std::data(cont&: out)));
46 static_assert(std::is_same_v<decltype(ret), Iter2>);
47 assert(base(ret) == std::data(cont&: out) + std::size(cont: out));
48 for (int i = 0; i != size; ++i)
49 assert(out[i] == i + 1);
50 }
51 }
52};
53
54struct CopiedToTester {
55 bool copied_to = false;
56 CopiedToTester() = default;
57 CopiedToTester(const CopiedToTester&) {}
58 CopiedToTester& operator=(const CopiedToTester&) {
59 assert(!copied_to);
60 copied_to = true;
61 return *this;
62 }
63 ~CopiedToTester() = default;
64};
65
66template <class Iter1, class Iter2>
67struct TestNonTrivial {
68 template <class Policy>
69 void operator()(Policy&& policy) {
70 // simple test
71 for (const int size : {0, 1, 2, 100, 350}) {
72 std::vector<CopiedToTester> a(size);
73
74 std::vector<CopiedToTester> out(std::size(cont: a));
75 auto ret = std::copy(policy, Iter1(std::data(cont&: a)), Iter1(std::data(cont&: a) + std::size(cont: a)), Iter2(std::data(cont&: out)));
76 assert(base(ret) == std::data(cont&: out) + std::size(cont: out));
77 assert(std::all_of(std::begin(cont&: out), std::end(cont&: out), [](CopiedToTester& t) { return t.copied_to; }));
78 assert(std::none_of(std::begin(cont&: a), std::end(cont&: a), [](CopiedToTester& t) { return t.copied_to; }));
79 }
80 }
81};
82
83struct TestIteratorsNonTrivial {
84 template <class Iter2>
85 void operator()() {}
86};
87
88int main(int, char**) {
89 types::for_each(types::forward_iterator_list<int*>{}, types::apply_type_identity{[](auto v) {
90 using Iter = typename decltype(v)::type;
91 types::for_each(
92 types::forward_iterator_list<int*>{},
93 TestIteratorWithPolicies< types::partial_instantiation<TestInt, Iter>::template apply>{});
94 }});
95
96 types::for_each(
97 types::forward_iterator_list<CopiedToTester*>{}, types::apply_type_identity{[](auto v) {
98 using Iter = typename decltype(v)::type;
99 types::for_each(
100 types::forward_iterator_list<CopiedToTester*>{},
101 TestIteratorWithPolicies< types::partial_instantiation<TestNonTrivial, Iter>::template apply>{});
102 }});
103
104 return 0;
105}
106

source code of libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/pstl.copy.pass.cpp