Home | History | Annotate | Download | only in alg.transform
      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<InputIterator InIter, class OutIter,
     13 //          Callable<auto, const InIter::value_type&> Op>
     14 //   requires OutputIterator<OutIter, Op::result_type> && CopyConstructible<Op>
     15 // constexpr OutIter      // constexpr after C++17
     16 //   transform(InIter first, InIter last, OutIter result, Op op);
     17 
     18 #include <algorithm>
     19 #include <functional>
     20 #include <cassert>
     21 
     22 #include "test_macros.h"
     23 #include "test_iterators.h"
     24 
     25 TEST_CONSTEXPR int plusOne(int v) { return v + 1; }
     26 
     27 
     28 #if TEST_STD_VER > 17
     29 TEST_CONSTEXPR bool test_constexpr() {
     30     int ia[] = {1, 3, 6, 7};
     31     int ib[] = {0, 0, 0, 0, 0}; // one bigger
     32     const int expected[] = {2, 4, 7, 8};
     33 
     34     auto it = std::transform(std::begin(ia), std::end(ia), std::begin(ib), plusOne);
     35 
     36     return it == (std::begin(ib) + std::size(ia))
     37         && *it == 0 // don't overwrite the last value in the output array
     38         && std::equal(std::begin(ib), it, std::begin(expected), std::end(expected))
     39         ;
     40     }
     41 #endif
     42 
     43 
     44 template <class InIter, class OutIter>
     45 void
     46 test()
     47 {
     48     int ia[] = {0, 1, 2, 3, 4};
     49     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
     50     int ib[sa] = {0};
     51     OutIter r = std::transform(InIter(ia), InIter(ia+sa),
     52                                OutIter(ib), plusOne);
     53     assert(base(r) == ib + sa);
     54     assert(ib[0] == 1);
     55     assert(ib[1] == 2);
     56     assert(ib[2] == 3);
     57     assert(ib[3] == 4);
     58     assert(ib[4] == 5);
     59 }
     60 
     61 int main()
     62 {
     63     test<input_iterator<const int*>, output_iterator<int*> >();
     64     test<input_iterator<const int*>, input_iterator<int*> >();
     65     test<input_iterator<const int*>, forward_iterator<int*> >();
     66     test<input_iterator<const int*>, bidirectional_iterator<int*> >();
     67     test<input_iterator<const int*>, random_access_iterator<int*> >();
     68     test<input_iterator<const int*>, int*>();
     69 
     70     test<forward_iterator<const int*>, output_iterator<int*> >();
     71     test<forward_iterator<const int*>, input_iterator<int*> >();
     72     test<forward_iterator<const int*>, forward_iterator<int*> >();
     73     test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
     74     test<forward_iterator<const int*>, random_access_iterator<int*> >();
     75     test<forward_iterator<const int*>, int*>();
     76 
     77     test<bidirectional_iterator<const int*>, output_iterator<int*> >();
     78     test<bidirectional_iterator<const int*>, input_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*>, input_iterator<int*> >();
     86     test<random_access_iterator<const int*>, forward_iterator<int*> >();
     87     test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
     88     test<random_access_iterator<const int*>, random_access_iterator<int*> >();
     89     test<random_access_iterator<const int*>, int*>();
     90 
     91     test<const int*, output_iterator<int*> >();
     92     test<const int*, input_iterator<int*> >();
     93     test<const int*, forward_iterator<int*> >();
     94     test<const int*, bidirectional_iterator<int*> >();
     95     test<const int*, random_access_iterator<int*> >();
     96     test<const int*, int*>();
     97 
     98 #if TEST_STD_VER > 17
     99     static_assert(test_constexpr());
    100 #endif
    101 }
    102