Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / algorithms / alg.modifying.operations / alg.copy / copy_backward.pass.cpp
blob928903de1ade2d5bdcd4236d0ccf14867a8f9e11
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 //===----------------------------------------------------------------------===//
9 // <algorithm>
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);
16 #include <algorithm>
17 #include <cassert>
19 #include "test_macros.h"
20 #include "test_iterators.h"
21 #include "user_defined_integral.h"
23 class PaddedBase {
24 public:
25 TEST_CONSTEXPR PaddedBase(std::int16_t a, std::int8_t b) : a_(a), b_(b) {}
27 std::int16_t a_;
28 std::int8_t b_;
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) {}
35 std::int8_t c_;
38 template <class InIter, class OutIter>
39 TEST_CONSTEXPR_CXX20 void
40 test_copy_backward()
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};
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
57 test()
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*>();
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*>();
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
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);
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));
99 return true;
102 int main(int, char**)
104 test();
106 #if TEST_STD_VER > 17
107 static_assert(test());
108 #endif
110 return 0;