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, OutputIterator<auto, InIter::reference> OutIter>
12 //   constexpr OutIter          // constexpr after C++17
13 //   reverse_copy(InIter first, InIter last, OutIter result);
14 
15 #include <algorithm>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 #include "test_iterators.h"
20 
21 #if TEST_STD_VER > 17
test_constexpr()22 TEST_CONSTEXPR bool test_constexpr() {
23     int ia[] = {1, 3, 5, 2, 5, 6};
24     int ib[std::size(ia)] = {0};
25 
26     auto it = std::reverse_copy(std::begin(ia), std::end(ia), std::begin(ib));
27 
28     return std::distance(std::begin(ib), it) == static_cast<int>(std::size(ia))
29         && std::equal   (std::begin(ia), std::end(ia), std::rbegin(ib))
30            ;
31     }
32 #endif
33 
34 template <class InIter, class OutIter>
35 void
test()36 test()
37 {
38     const int ia[] = {0};
39     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
40     int ja[sa] = {-1};
41     OutIter r = std::reverse_copy(InIter(ia), InIter(ia), OutIter(ja));
42     assert(base(r) == ja);
43     assert(ja[0] == -1);
44     r = std::reverse_copy(InIter(ia), InIter(ia+sa), OutIter(ja));
45     assert(ja[0] == 0);
46 
47     const int ib[] = {0, 1};
48     const unsigned sb = sizeof(ib)/sizeof(ib[0]);
49     int jb[sb] = {-1};
50     r = std::reverse_copy(InIter(ib), InIter(ib+sb), OutIter(jb));
51     assert(base(r) == jb+sb);
52     assert(jb[0] == 1);
53     assert(jb[1] == 0);
54 
55     const int ic[] = {0, 1, 2};
56     const unsigned sc = sizeof(ic)/sizeof(ic[0]);
57     int jc[sc] = {-1};
58     r = std::reverse_copy(InIter(ic), InIter(ic+sc), OutIter(jc));
59     assert(base(r) == jc+sc);
60     assert(jc[0] == 2);
61     assert(jc[1] == 1);
62     assert(jc[2] == 0);
63 
64     int id[] = {0, 1, 2, 3};
65     const unsigned sd = sizeof(id)/sizeof(id[0]);
66     int jd[sd] = {-1};
67     r = std::reverse_copy(InIter(id), InIter(id+sd), OutIter(jd));
68     assert(base(r) == jd+sd);
69     assert(jd[0] == 3);
70     assert(jd[1] == 2);
71     assert(jd[2] == 1);
72     assert(jd[3] == 0);
73 }
74 
main(int,char **)75 int main(int, char**)
76 {
77     test<bidirectional_iterator<const int*>, output_iterator<int*> >();
78     test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
79     test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
80     test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
81     test<bidirectional_iterator<const int*>, int*>();
82 
83     test<random_access_iterator<const int*>, output_iterator<int*> >();
84     test<random_access_iterator<const int*>, forward_iterator<int*> >();
85     test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
86     test<random_access_iterator<const int*>, random_access_iterator<int*> >();
87     test<random_access_iterator<const int*>, int*>();
88 
89     test<const int*, output_iterator<int*> >();
90     test<const int*, forward_iterator<int*> >();
91     test<const int*, bidirectional_iterator<int*> >();
92     test<const int*, random_access_iterator<int*> >();
93     test<const int*, int*>();
94 
95 #if TEST_STD_VER > 17
96     static_assert(test_constexpr());
97 #endif
98 
99   return 0;
100 }
101