xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy_backward.pass.cpp (revision cc89063bff0f73ec7049a1dcb5d4688ae6806941)
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 // #if TEST_STD_VER > 17
24 // TEST_CONSTEXPR bool test_constexpr() {
25 //     int ia[] = {1, 2, 3, 4, 5};
26 //     int ic[] = {6, 6, 6, 6, 6, 6, 6};
27 //
28 //     size_t N = std::size(ia);
29 //     auto p = std::copy_backward(std::begin(ia), std::end(ia), std::begin(ic) + N);
30 //     return std::equal(std::begin(ic), p, std::begin(ia))
31 //         && std::all_of(p, std::end(ic), [](int a){return a == 6;})
32 //         ;
33 //     }
34 // #endif
35 
36 template <class InIter, class OutIter>
37 void
38 test()
39 {
40     const unsigned N = 1000;
41     int ia[N];
42     for (unsigned i = 0; i < N; ++i)
43         ia[i] = i;
44     int ib[N] = {0};
45 
46     OutIter r = std::copy_backward(InIter(ia), InIter(ia+N), OutIter(ib+N));
47     assert(base(r) == ib);
48     for (unsigned i = 0; i < N; ++i)
49         assert(ia[i] == ib[i]);
50 }
51 
52 int main(int, char**)
53 {
54     test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
55     test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
56     test<bidirectional_iterator<const int*>, int*>();
57 
58     test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
59     test<random_access_iterator<const int*>, random_access_iterator<int*> >();
60     test<random_access_iterator<const int*>, int*>();
61 
62     test<const int*, bidirectional_iterator<int*> >();
63     test<const int*, random_access_iterator<int*> >();
64     test<const int*, int*>();
65 
66 // #if TEST_STD_VER > 17
67 //     static_assert(test_constexpr());
68 // #endif
69 
70   return 0;
71 }
72