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 //===----------------------------------------------------------------------===//
11 // template<BidirectionalIterator InIter, BidirectionalIterator OutIter>
12 // requires OutputIterator<OutIter, InIter::reference>
13 // constexpr OutIter // constexpr after C++17
14 // copy_backward(InIter first, InIter last, OutIter result);
19 #include "test_macros.h"
20 #include "test_iterators.h"
21 #include "user_defined_integral.h"
25 TEST_CONSTEXPR
PaddedBase(std::int16_t a
, std::int8_t b
) : a_(a
), b_(b
) {}
31 class Derived
: public PaddedBase
{
33 TEST_CONSTEXPR
Derived(std::int16_t a
, std::int8_t b
, std::int8_t c
) : PaddedBase(a
, b
), c_(c
) {}
38 template <class InIter
, class OutIter
>
39 TEST_CONSTEXPR_CXX20
void
43 const unsigned N
= 1000;
45 for (unsigned i
= 0; i
< N
; ++i
)
49 OutIter r
= std::copy_backward(InIter(ia
), InIter(ia
+N
), OutIter(ib
+N
));
50 assert(base(r
) == ib
);
51 for (unsigned i
= 0; i
< N
; ++i
)
52 assert(ia
[i
] == ib
[i
]);
56 TEST_CONSTEXPR_CXX20
bool
59 test_copy_backward
<bidirectional_iterator
<const int*>, bidirectional_iterator
<int*> >();
60 test_copy_backward
<bidirectional_iterator
<const int*>, random_access_iterator
<int*> >();
61 test_copy_backward
<bidirectional_iterator
<const int*>, int*>();
63 test_copy_backward
<random_access_iterator
<const int*>, bidirectional_iterator
<int*> >();
64 test_copy_backward
<random_access_iterator
<const int*>, random_access_iterator
<int*> >();
65 test_copy_backward
<random_access_iterator
<const int*>, int*>();
67 test_copy_backward
<const int*, bidirectional_iterator
<int*> >();
68 test_copy_backward
<const int*, random_access_iterator
<int*> >();
69 test_copy_backward
<const int*, int*>();
72 test_copy_backward
<contiguous_iterator
<const int*>, bidirectional_iterator
<int*>>();
73 test_copy_backward
<contiguous_iterator
<const int*>, random_access_iterator
<int*>>();
74 test_copy_backward
<contiguous_iterator
<const int*>, int*>();
76 test_copy_backward
<bidirectional_iterator
<const int*>, contiguous_iterator
<int*>>();
77 test_copy_backward
<random_access_iterator
<const int*>, contiguous_iterator
<int*>>();
78 test_copy_backward
<contiguous_iterator
<const int*>, contiguous_iterator
<int*>>();
79 test_copy_backward
<const int*, contiguous_iterator
<int*>>();
82 { // Make sure that padding bits aren't copied
86 static_cast<PaddedBase
*>(&src
), static_cast<PaddedBase
*>(&src
) + 1, static_cast<PaddedBase
*>(&dst
) + 1);
92 { // Make sure that overlapping ranges can be copied
93 int a
[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
94 std::copy_backward(a
, a
+ 7, a
+ 10);
95 int expected
[] = {1, 2, 3, 1, 2, 3, 4, 5, 6, 7};
96 assert(std::equal(a
, a
+ 10, expected
));
102 int main(int, char**)
106 #if TEST_STD_VER > 17
107 static_assert(test());