Home | History | Annotate | Download | only in exclusive.scan
      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 // <numeric>
     11 // UNSUPPORTED: c++98, c++03, c++11, c++14
     12 
     13 // template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
     14 //     OutputIterator
     15 //     exclusive_scan(InputIterator first, InputIterator last,
     16 //                    OutputIterator result,
     17 //                    T init, BinaryOperation binary_op); // C++17
     18 
     19 #include <numeric>
     20 #include <algorithm>
     21 #include <cassert>
     22 #include <functional>
     23 #include <iterator>
     24 #include <vector>
     25 
     26 #include "test_iterators.h"
     27 
     28 template <class Iter1, class T, class Op, class Iter2>
     29 void
     30 test(Iter1 first, Iter1 last, T init, Op op, Iter2 rFirst, Iter2 rLast)
     31 {
     32     std::vector<typename std::iterator_traits<Iter1>::value_type> v;
     33 
     34 //  Not in place
     35     std::exclusive_scan(first, last, std::back_inserter(v), init, op);
     36     assert(std::equal(v.begin(), v.end(), rFirst, rLast));
     37 
     38 //  In place
     39     v.clear();
     40     v.assign(first, last);
     41     std::exclusive_scan(v.begin(), v.end(), v.begin(), init, op);
     42     assert(std::equal(v.begin(), v.end(), rFirst, rLast));
     43 }
     44 
     45 
     46 template <class Iter>
     47 void
     48 test()
     49 {
     50           int ia[]   = {1, 3, 5,  7,   9};
     51     const int pRes[] = {0, 1, 4,  9,  16};
     52     const int mRes[] = {1, 1, 3, 15, 105};
     53     const unsigned sa = sizeof(ia) / sizeof(ia[0]);
     54     static_assert(sa == sizeof(pRes) / sizeof(pRes[0]));       // just to be sure
     55     static_assert(sa == sizeof(mRes) / sizeof(mRes[0]));       // just to be sure
     56 
     57     for (unsigned int i = 0; i < sa; ++i ) {
     58         test(Iter(ia), Iter(ia + i), 0, std::plus<>(),       pRes, pRes + i);
     59         test(Iter(ia), Iter(ia + i), 1, std::multiplies<>(), mRes, mRes + i);
     60         }
     61 }
     62 
     63 int main()
     64 {
     65 //  All the iterator categories
     66     test<input_iterator        <const int*> >();
     67     test<forward_iterator      <const int*> >();
     68     test<bidirectional_iterator<const int*> >();
     69     test<random_access_iterator<const int*> >();
     70     test<const int*>();
     71     test<      int*>();
     72 
     73 //  Make sure that the calculations are done using the init typedef
     74     {
     75     std::vector<unsigned char> v(10);
     76     std::iota(v.begin(), v.end(), static_cast<unsigned char>(1));
     77     std::vector<int> res;
     78     std::exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 1, std::multiplies<>());
     79 
     80     assert(res.size() == 10);
     81     int j = 1;
     82     assert(res[0] == 1);
     83     for (size_t i = 1; i < v.size(); ++i)
     84     {
     85         j *= i;
     86         assert(res[i] == j);
     87     }
     88     }
     89 }
     90