xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy_backward.pass.cpp (revision e9612e9e851ccd288f83739a1950ebb45d212aee)
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 template <class InIter, class OutIter>
24 TEST_CONSTEXPR_CXX20 void
25 test_copy_backward()
26 {
27     const unsigned N = 1000;
28     int ia[N] = {};
29     for (unsigned i = 0; i < N; ++i)
30         ia[i] = i;
31     int ib[N] = {0};
32 
33     OutIter r = std::copy_backward(InIter(ia), InIter(ia+N), OutIter(ib+N));
34     assert(base(r) == ib);
35     for (unsigned i = 0; i < N; ++i)
36         assert(ia[i] == ib[i]);
37 }
38 
39 TEST_CONSTEXPR_CXX20 bool
40 test()
41 {
42     test_copy_backward<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
43     test_copy_backward<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
44     test_copy_backward<bidirectional_iterator<const int*>, int*>();
45 
46     test_copy_backward<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
47     test_copy_backward<random_access_iterator<const int*>, random_access_iterator<int*> >();
48     test_copy_backward<random_access_iterator<const int*>, int*>();
49 
50     test_copy_backward<const int*, bidirectional_iterator<int*> >();
51     test_copy_backward<const int*, random_access_iterator<int*> >();
52     test_copy_backward<const int*, int*>();
53 
54     return true;
55 }
56 
57 int main(int, char**)
58 {
59     test();
60 
61 #if TEST_STD_VER > 17
62     static_assert(test());
63 #endif
64 
65   return 0;
66 }
67