Home | History | Annotate | Download | only in alg.reverse
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <algorithm>
     11 
     12 // template<BidirectionalIterator InIter, OutputIterator<auto, InIter::reference> OutIter>
     13 //   constexpr OutIter          // constexpr after C++17
     14 //   reverse_copy(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 
     22 #if TEST_STD_VER > 17
     23 TEST_CONSTEXPR bool test_constexpr() {
     24     int ia[] = {1, 3, 5, 2, 5, 6};
     25     int ib[std::size(ia)] = {0};
     26 
     27     auto it = std::reverse_copy(std::begin(ia), std::end(ia), std::begin(ib));
     28 
     29     return std::distance(std::begin(ib), it) == std::size(ia)
     30         && std::equal   (std::begin(ia), std::end(ia), std::rbegin(ib))
     31            ;
     32     }
     33 #endif
     34 
     35 template <class InIter, class OutIter>
     36 void
     37 test()
     38 {
     39     const int ia[] = {0};
     40     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
     41     int ja[sa] = {-1};
     42     OutIter r = std::reverse_copy(InIter(ia), InIter(ia), OutIter(ja));
     43     assert(base(r) == ja);
     44     assert(ja[0] == -1);
     45     r = std::reverse_copy(InIter(ia), InIter(ia+sa), OutIter(ja));
     46     assert(ja[0] == 0);
     47 
     48     const int ib[] = {0, 1};
     49     const unsigned sb = sizeof(ib)/sizeof(ib[0]);
     50     int jb[sb] = {-1};
     51     r = std::reverse_copy(InIter(ib), InIter(ib+sb), OutIter(jb));
     52     assert(base(r) == jb+sb);
     53     assert(jb[0] == 1);
     54     assert(jb[1] == 0);
     55 
     56     const int ic[] = {0, 1, 2};
     57     const unsigned sc = sizeof(ic)/sizeof(ic[0]);
     58     int jc[sc] = {-1};
     59     r = std::reverse_copy(InIter(ic), InIter(ic+sc), OutIter(jc));
     60     assert(base(r) == jc+sc);
     61     assert(jc[0] == 2);
     62     assert(jc[1] == 1);
     63     assert(jc[2] == 0);
     64 
     65     int id[] = {0, 1, 2, 3};
     66     const unsigned sd = sizeof(id)/sizeof(id[0]);
     67     int jd[sd] = {-1};
     68     r = std::reverse_copy(InIter(id), InIter(id+sd), OutIter(jd));
     69     assert(base(r) == jd+sd);
     70     assert(jd[0] == 3);
     71     assert(jd[1] == 2);
     72     assert(jd[2] == 1);
     73     assert(jd[3] == 0);
     74 }
     75 
     76 int main()
     77 {
     78     test<bidirectional_iterator<const int*>, output_iterator<int*> >();
     79     test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
     80     test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
     81     test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
     82     test<bidirectional_iterator<const int*>, int*>();
     83 
     84     test<random_access_iterator<const int*>, output_iterator<int*> >();
     85     test<random_access_iterator<const int*>, forward_iterator<int*> >();
     86     test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
     87     test<random_access_iterator<const int*>, random_access_iterator<int*> >();
     88     test<random_access_iterator<const int*>, int*>();
     89 
     90     test<const int*, output_iterator<int*> >();
     91     test<const int*, forward_iterator<int*> >();
     92     test<const int*, bidirectional_iterator<int*> >();
     93     test<const int*, random_access_iterator<int*> >();
     94     test<const int*, int*>();
     95 
     96 #if TEST_STD_VER > 17
     97     static_assert(test_constexpr());
     98 #endif
     99 }
    100