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#include "type_algorithms.h"
27
28EXECUTION_POLICY_SFINAE_TEST(move);
29
30static_assert(sfinae_test_move<int, int*, int*, int*>);
31static_assert(!sfinae_test_move<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::move(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 MovedToTester {
55 bool moved_to = false;
56 MovedToTester() = default;
57 MovedToTester(MovedToTester&&) {}
58 MovedToTester& operator=(MovedToTester&&) {
59 assert(!moved_to);
60 moved_to = true;
61 return *this;
62 }
63 ~MovedToTester() = 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<MovedToTester> a(size);
73
74 std::vector<MovedToTester> out(std::size(cont: a));
75 auto ret = std::move(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), [](MovedToTester& t) { return t.moved_to; }));
78 assert(std::none_of(std::begin(cont&: a), std::end(cont&: a), [](MovedToTester& t) { return t.moved_to; }));
79 }
80 }
81};
82
83int main(int, char**) {
84 types::for_each(types::forward_iterator_list<int*>{}, types::apply_type_identity{[](auto v) {
85 using Iter = typename decltype(v)::type;
86 types::for_each(
87 types::forward_iterator_list<int*>{},
88 TestIteratorWithPolicies< types::partial_instantiation<TestInt, Iter>::template apply>{});
89 }});
90
91 types::for_each(
92 types::forward_iterator_list<MovedToTester*>{}, types::apply_type_identity{[](auto v) {
93 using Iter = typename decltype(v)::type;
94 types::for_each(
95 types::forward_iterator_list<MovedToTester*>{},
96 TestIteratorWithPolicies< types::partial_instantiation<TestNonTrivial, Iter>::template apply>{});
97 }});
98
99 return 0;
100}
101

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