| 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 && !stdlib=libc++ |
| 10 | // ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=2000000 |
| 11 | |
| 12 | // <algorithm> |
| 13 | |
| 14 | // template<InputIterator InIter, typename OutIter> |
| 15 | // requires OutputIterator<OutIter, RvalueOf<InIter::reference>::type> |
| 16 | // OutIter |
| 17 | // move(InIter first, InIter last, OutIter result); |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <cassert> |
| 21 | #include <iterator> |
| 22 | #include <memory> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "MoveOnly.h" |
| 26 | #include "test_iterators.h" |
| 27 | #include "test_macros.h" |
| 28 | #include "type_algorithms.h" |
| 29 | |
| 30 | class PaddedBase { |
| 31 | public: |
| 32 | TEST_CONSTEXPR PaddedBase(std::int16_t a, std::int8_t b) : a_(a), b_(b) {} |
| 33 | |
| 34 | std::int16_t a_; |
| 35 | std::int8_t b_; |
| 36 | }; |
| 37 | |
| 38 | class Derived : public PaddedBase { |
| 39 | public: |
| 40 | TEST_CONSTEXPR Derived(std::int16_t a, std::int8_t b, std::int8_t c) : PaddedBase(a, b), c_(c) {} |
| 41 | |
| 42 | std::int8_t c_; |
| 43 | }; |
| 44 | |
| 45 | template <class InIter> |
| 46 | struct Test { |
| 47 | template <class OutIter> |
| 48 | TEST_CONSTEXPR_CXX20 void operator()() { |
| 49 | const unsigned N = 1000; |
| 50 | int ia[N] = {}; |
| 51 | for (unsigned i = 0; i < N; ++i) |
| 52 | ia[i] = i; |
| 53 | int ib[N] = {0}; |
| 54 | |
| 55 | OutIter r = std::move(InIter(ia), InIter(ia + N), OutIter(ib)); |
| 56 | assert(base(r) == ib + N); |
| 57 | for (unsigned i = 0; i < N; ++i) |
| 58 | assert(ia[i] == ib[i]); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | struct TestOutIters { |
| 63 | template <class InIter> |
| 64 | TEST_CONSTEXPR_CXX20 void operator()() { |
| 65 | types::for_each( |
| 66 | types::concatenate_t<types::cpp17_input_iterator_list<int*>, types::type_list<cpp17_output_iterator<int*> > >(), |
| 67 | Test<InIter>()); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | template <class InIter> |
| 72 | struct Test1 { |
| 73 | template <class OutIter> |
| 74 | TEST_CONSTEXPR_CXX23 void operator()() { |
| 75 | const unsigned N = 100; |
| 76 | std::unique_ptr<int> ia[N]; |
| 77 | for (unsigned i = 0; i < N; ++i) |
| 78 | ia[i].reset(p: new int(i)); |
| 79 | std::unique_ptr<int> ib[N]; |
| 80 | |
| 81 | OutIter r = std::move(InIter(ia), InIter(ia + N), OutIter(ib)); |
| 82 | assert(base(r) == ib + N); |
| 83 | for (unsigned i = 0; i < N; ++i) |
| 84 | assert(*ib[i] == static_cast<int>(i)); |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | struct Test1OutIters { |
| 89 | template <class InIter> |
| 90 | TEST_CONSTEXPR_CXX23 void operator()() { |
| 91 | types::for_each(types::concatenate_t<types::cpp17_input_iterator_list<std::unique_ptr<int>*>, |
| 92 | types::type_list<cpp17_output_iterator<std::unique_ptr<int>*> > >(), |
| 93 | Test1<InIter>()); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) { |
| 98 | std::vector<bool> v(N, false); |
| 99 | for (std::size_t i = 0; i < N; i += 2) |
| 100 | v[i] = true; |
| 101 | |
| 102 | { // Test move with aligned bytes |
| 103 | std::vector<bool> in(v); |
| 104 | std::vector<bool> out(N); |
| 105 | std::move(first: in.begin(), last: in.end(), result: out.begin()); |
| 106 | assert(out == v); |
| 107 | } |
| 108 | { // Test move with unaligned bytes |
| 109 | std::vector<bool> in(v); |
| 110 | std::vector<bool> out(N); |
| 111 | std::move(first: in.begin() + 4, last: in.end(), result: out.begin()); |
| 112 | for (std::size_t i = 0; i < N - 4; ++i) |
| 113 | assert(v[i + 4] == out[i]); |
| 114 | } |
| 115 | |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | TEST_CONSTEXPR_CXX20 bool test() { |
| 120 | types::for_each(types::cpp17_input_iterator_list<int*>(), TestOutIters()); |
| 121 | if (TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED) |
| 122 | types::for_each(types::cpp17_input_iterator_list<std::unique_ptr<int>*>(), Test1OutIters()); |
| 123 | { // Make sure that padding bits aren't copied |
| 124 | Derived src(1, 2, 3); |
| 125 | Derived dst(4, 5, 6); |
| 126 | std::move(first: static_cast<PaddedBase*>(&src), last: static_cast<PaddedBase*>(&src) + 1, result: static_cast<PaddedBase*>(&dst)); |
| 127 | assert(dst.a_ == 1); |
| 128 | assert(dst.b_ == 2); |
| 129 | assert(dst.c_ == 6); |
| 130 | } |
| 131 | { // Make sure that overlapping ranges can be copied |
| 132 | int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 133 | std::move(first: a + 3, last: a + 10, result: a); |
| 134 | int expected[] = {4, 5, 6, 7, 8, 9, 10, 8, 9, 10}; |
| 135 | assert(std::equal(a, a + 10, expected)); |
| 136 | } |
| 137 | { // Make sure that the algorithm works with move-only types |
| 138 | // When non-trivial |
| 139 | { |
| 140 | MoveOnly from[3] = {1, 2, 3}; |
| 141 | MoveOnly to[3] = {}; |
| 142 | std::move(std::begin(from), std::end(from), std::begin(to)); |
| 143 | assert(to[0] == MoveOnly(1)); |
| 144 | assert(to[1] == MoveOnly(2)); |
| 145 | assert(to[2] == MoveOnly(3)); |
| 146 | } |
| 147 | // When trivial |
| 148 | { |
| 149 | TrivialMoveOnly from[3] = {1, 2, 3}; |
| 150 | TrivialMoveOnly to[3] = {}; |
| 151 | std::move(std::begin(from), std::end(from), std::begin(to)); |
| 152 | assert(to[0] == TrivialMoveOnly(1)); |
| 153 | assert(to[1] == TrivialMoveOnly(2)); |
| 154 | assert(to[2] == TrivialMoveOnly(3)); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | { // Test vector<bool>::iterator optimization |
| 159 | assert(test_vector_bool(8)); |
| 160 | assert(test_vector_bool(19)); |
| 161 | assert(test_vector_bool(32)); |
| 162 | assert(test_vector_bool(49)); |
| 163 | assert(test_vector_bool(64)); |
| 164 | assert(test_vector_bool(199)); |
| 165 | assert(test_vector_bool(256)); |
| 166 | } |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | int main(int, char**) { |
| 172 | test(); |
| 173 | #if TEST_STD_VER >= 20 |
| 174 | static_assert(test()); |
| 175 | #endif |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |