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 Size, class ForwardIterator2>
16// ForwardIterator2 copy_n(ExecutionPolicy&& exec,
17// ForwardIterator1 first, Size n,
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_n);
29
30static_assert(sfinae_test_copy_n<int, int*, int*, bool (*)(int)>);
31static_assert(!sfinae_test_copy_n<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 = std::copy_n(policy, Iter1(std::data(cont&: a)), std::size(cont: a), Iter2(std::data(cont&: out)));
45 static_assert(std::is_same_v<decltype(ret), Iter2>);
46 assert(base(ret) == std::data(cont&: out) + std::size(cont: out));
47 for (int i = 0; i != size; ++i)
48 assert(out[i] == i + 1);
49 }
50 }
51};
52
53struct TestIteratorsInt {
54 template <class Iter2>
55 void operator()() {
56 types::for_each(types::forward_iterator_list<int*>{},
57 TestIteratorWithPolicies<types::partial_instantiation<TestInt, Iter2>::template apply>{});
58 }
59};
60
61struct CopiedToTester {
62 bool copied_to = false;
63 CopiedToTester() = default;
64 CopiedToTester(const CopiedToTester&) {}
65 CopiedToTester& operator=(const CopiedToTester&) {
66 assert(!copied_to);
67 copied_to = true;
68 return *this;
69 }
70 ~CopiedToTester() = default;
71};
72
73template <class Iter1, class Iter2>
74struct TestNonTrivial {
75 template <class Policy>
76 void operator()(Policy&& policy) {
77 // simple test
78 for (const int size : {0, 1, 2, 100, 350}) {
79 std::vector<CopiedToTester> a(size);
80
81 std::vector<CopiedToTester> out(std::size(cont: a));
82 auto ret = std::copy_n(policy, Iter1(std::data(cont&: a)), std::size(cont: a), Iter2(std::data(cont&: out)));
83 assert(base(ret) == std::data(cont&: out) + std::size(cont: out));
84 assert(std::all_of(std::begin(cont&: out), std::end(cont&: out), [](CopiedToTester& t) { return t.copied_to; }));
85 assert(std::none_of(std::begin(cont&: a), std::end(cont&: a), [](CopiedToTester& t) { return t.copied_to; }));
86 }
87 }
88};
89
90struct TestIteratorsNonTrivial {
91 template <class Iter2>
92 void operator()() {
93 types::for_each(types::forward_iterator_list<CopiedToTester*>{},
94 TestIteratorWithPolicies<types::partial_instantiation<TestNonTrivial, Iter2>::template apply>{});
95 }
96};
97
98int main(int, char**) {
99 types::for_each(types::forward_iterator_list<int*>{}, TestIteratorsInt{});
100 types::for_each(types::forward_iterator_list<CopiedToTester*>{}, TestIteratorsNonTrivial{});
101
102 return 0;
103}
104

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