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// ForwardIterator2 move(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
27EXECUTION_POLICY_SFINAE_TEST(move);
28
29static_assert(sfinae_test_move<int, int*, int*, int*>);
30static_assert(!sfinae_test_move<std::execution::parallel_policy, int*, int*, int*>);
31
32template <class Iter1, class Iter2>
33struct TestInt {
34 template <class Policy>
35 void operator()(Policy&& policy) {
36 // simple test
37 for (const int size : {0, 1, 2, 100, 350}) {
38 std::vector<int> a(size);
39 for (int i = 0; i != size; ++i)
40 a[i] = i + 1;
41
42 std::vector<int> out(std::size(cont: a));
43 decltype(auto) ret =
44 std::move(policy, Iter1(std::data(cont&: a)), 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 MovedToTester {
54 bool moved_to = false;
55 MovedToTester() = default;
56 MovedToTester(MovedToTester&&) {}
57 MovedToTester& operator=(MovedToTester&&) {
58 assert(!moved_to);
59 moved_to = true;
60 return *this;
61 }
62 ~MovedToTester() = default;
63};
64
65template <class Iter1, class Iter2>
66struct TestNonTrivial {
67 template <class Policy>
68 void operator()(Policy&& policy) {
69 // simple test
70 for (const int size : {0, 1, 2, 100, 350}) {
71 std::vector<MovedToTester> a(size);
72
73 std::vector<MovedToTester> out(std::size(cont: a));
74 auto ret = std::move(policy, Iter1(std::data(cont&: a)), Iter1(std::data(cont&: a) + std::size(cont: a)), Iter2(std::data(cont&: out)));
75 assert(base(ret) == std::data(cont&: out) + std::size(cont: out));
76 assert(std::all_of(std::begin(cont&: out), std::end(cont&: out), [](MovedToTester& t) { return t.moved_to; }));
77 assert(std::none_of(std::begin(cont&: a), std::end(cont&: a), [](MovedToTester& t) { return t.moved_to; }));
78 }
79 }
80};
81
82int main(int, char**) {
83 types::for_each(types::forward_iterator_list<int*>{}, types::apply_type_identity{[](auto v) {
84 using Iter = typename decltype(v)::type;
85 types::for_each(
86 types::forward_iterator_list<int*>{},
87 TestIteratorWithPolicies< types::partial_instantiation<TestInt, Iter>::template apply>{});
88 }});
89
90 types::for_each(
91 types::forward_iterator_list<MovedToTester*>{}, types::apply_type_identity{[](auto v) {
92 using Iter = typename decltype(v)::type;
93 types::for_each(
94 types::forward_iterator_list<MovedToTester*>{},
95 TestIteratorWithPolicies< types::partial_instantiation<TestNonTrivial, Iter>::template apply>{});
96 }});
97
98 return 0;
99}
100

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