1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03 && !stdlib=libc++
13 // template<BidirectionalIterator InIter, BidirectionalIterator OutIter>
14 // requires OutputIterator<OutIter, RvalueOf<InIter::reference>::type>
16 // move_backward(InIter first, InIter last, OutIter result);
24 #include "test_iterators.h"
25 #include "test_macros.h"
29 TEST_CONSTEXPR
PaddedBase(std::int16_t a
, std::int8_t b
) : a_(a
), b_(b
) {}
35 class Derived
: public PaddedBase
{
37 TEST_CONSTEXPR
Derived(std::int16_t a
, std::int8_t b
, std::int8_t c
) : PaddedBase(a
, b
), c_(c
) {}
42 template <class InIter
>
44 template <class OutIter
>
45 TEST_CONSTEXPR_CXX20
void operator()() {
46 const unsigned N
= 1000;
48 for (unsigned i
= 0; i
< N
; ++i
)
52 OutIter r
= std::move_backward(InIter(ia
), InIter(ia
+N
), OutIter(ib
+N
));
53 assert(base(r
) == ib
);
54 for (unsigned i
= 0; i
< N
; ++i
)
55 assert(ia
[i
] == ib
[i
]);
60 template <class InIter
>
61 TEST_CONSTEXPR_CXX20
void operator()() {
63 types::concatenate_t
<types::bidirectional_iterator_list
<int*> >(),
68 template <class InIter
>
70 template <class OutIter
>
71 TEST_CONSTEXPR_CXX23
void operator()() {
72 const unsigned N
= 100;
73 std::unique_ptr
<int> ia
[N
];
74 for (unsigned i
= 0; i
< N
; ++i
)
75 ia
[i
].reset(new int(i
));
76 std::unique_ptr
<int> ib
[N
];
78 OutIter r
= std::move_backward(InIter(ia
), InIter(ia
+N
), OutIter(ib
+N
));
79 assert(base(r
) == ib
);
80 for (unsigned i
= 0; i
< N
; ++i
)
81 assert(*ib
[i
] == static_cast<int>(i
));
85 struct Test1OutIters
{
86 template <class InIter
>
87 TEST_CONSTEXPR_CXX23
void operator()() {
88 types::for_each(types::concatenate_t
<types::bidirectional_iterator_list
<std::unique_ptr
<int>*> >(),
93 TEST_CONSTEXPR_CXX20
bool test() {
94 types::for_each(types::bidirectional_iterator_list
<int*>(), TestOutIters());
95 if (TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED
)
96 types::for_each(types::bidirectional_iterator_list
<std::unique_ptr
<int>*>(), Test1OutIters());
98 { // Make sure that padding bits aren't copied
100 Derived
dst(4, 5, 6);
102 static_cast<PaddedBase
*>(&src
), static_cast<PaddedBase
*>(&src
) + 1, static_cast<PaddedBase
*>(&dst
) + 1);
108 { // Make sure that overlapping ranges can be copied
109 int a
[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
110 std::move_backward(a
, a
+ 7, a
+ 10);
111 int expected
[] = {1, 2, 3, 1, 2, 3, 4, 5, 6, 7};
112 assert(std::equal(a
, a
+ 10, expected
));
115 // Make sure that the algorithm works with move-only types
119 MoveOnly from
[3] = {1, 2, 3};
121 std::move_backward(std::begin(from
), std::end(from
), std::end(to
));
122 assert(to
[0] == MoveOnly(1));
123 assert(to
[1] == MoveOnly(2));
124 assert(to
[2] == MoveOnly(3));
128 TrivialMoveOnly from
[3] = {1, 2, 3};
129 TrivialMoveOnly to
[3] = {};
130 std::move_backward(std::begin(from
), std::end(from
), std::end(to
));
131 assert(to
[0] == TrivialMoveOnly(1));
132 assert(to
[1] == TrivialMoveOnly(2));
133 assert(to
[2] == TrivialMoveOnly(3));
140 int main(int, char**)
143 #if TEST_STD_VER >= 20
144 static_assert(test());