| 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 |
// <algorithm> |
| 10 |
|
| 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); |
| 15 |
|
| 16 |
#include <algorithm> |
| 17 |
#include <cassert> |
| 18 |
|
| 19 |
#include "test_macros.h" |
| 20 |
#include "test_iterators.h" |
| 21 |
#include "user_defined_integral.h" |
| 22 |
|
| 23 |
class PaddedBase { |
| 24 |
public: |
| 25 |
TEST_CONSTEXPR PaddedBase(std::int16_t a, std::int8_t b) : a_(a), b_(b) {} |
| 26 |
|
| 27 |
std::int16_t a_; |
| 28 |
std::int8_t b_; |
| 29 |
}; |
| 30 |
|
| 31 |
class Derived : public PaddedBase { |
| 32 |
public: |
| 33 |
TEST_CONSTEXPR Derived(std::int16_t a, std::int8_t b, std::int8_t c) : PaddedBase(a, b), c_(c) {} |
| 34 |
|
| 35 |
std::int8_t c_; |
| 36 |
}; |
| 37 |
|
| 38 |
template <class InIter, class OutIter> |
| 39 |
TEST_CONSTEXPR_CXX20 void |
| 40 |
test_copy_backward() |
| 41 |
{ |
| 42 |
{ |
| 43 |
const unsigned N = 1000; |
| 44 |
int ia[N] = {}; |
| 45 |
for (unsigned i = 0; i < N; ++i) |
| 46 |
ia[i] = i; |
| 47 |
int ib[N] = {0}; |
| 48 |
|
| 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]); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
TEST_CONSTEXPR_CXX20 bool |
| 57 |
test() |
| 58 |
{ |
| 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*>(); |
| 62 |
|
| 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*>(); |
| 66 |
|
| 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*>(); |
| 70 |
|
| 71 |
#if TEST_STD_VER > 17 |
| 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*>(); |
| 75 |
|
| 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*>>(); |
| 80 |
#endif |
| 81 |
|
| 82 |
{ // Make sure that padding bits aren't copied |
| 83 |
Derived src(1, 2, 3); |
| 84 |
Derived dst(4, 5, 6); |
| 85 |
std::copy_backward( |
| 86 |
static_cast<PaddedBase*>(&src), static_cast<PaddedBase*>(&src) + 1, static_cast<PaddedBase*>(&dst) + 1); |
| 87 |
assert(dst.a_ == 1); |
| 88 |
assert(dst.b_ == 2); |
| 89 |
assert(dst.c_ == 6); |
| 90 |
} |
| 91 |
|
| 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)); |
| 97 |
} |
| 98 |
|
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
int main(int, char**) |
| 103 |
{ |
| 104 |
test(); |
| 105 |
|
| 106 |
#if TEST_STD_VER > 17 |
| 107 |
static_assert(test()); |
| 108 |
#endif |
| 109 |
|
| 110 |
return 0; |
| 111 |
} |