libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

reduce_init_op.pass.cpp (2360B)


      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 T, class BinaryOperation>
     14 //   T reduce(InputIterator first, InputIterator last, T init, BinaryOperation op);
     15 
     16 #include <numeric>
     17 #include <cassert>
     18 
     19 #include "test_iterators.h"
     20 
     21 template <class Iter, class T, class Op>
     22 void
     23 test(Iter first, Iter last, T init, Op op, T x)
     24 {
     25     static_assert( std::is_same_v<T, decltype(std::reduce(first, last, init, op))>, "" );
     26     assert(std::reduce(first, last, init, op) == x);
     27 }
     28 
     29 template <class Iter>
     30 void
     31 test()
     32 {
     33     int ia[] = {1, 2, 3, 4, 5, 6};
     34     unsigned sa = sizeof(ia) / sizeof(ia[0]);
     35     test(Iter(ia), Iter(ia), 0, std::plus<>(), 0);
     36     test(Iter(ia), Iter(ia), 1, std::multiplies<>(), 1);
     37     test(Iter(ia), Iter(ia+1), 0, std::plus<>(), 1);
     38     test(Iter(ia), Iter(ia+1), 2, std::multiplies<>(), 2);
     39     test(Iter(ia), Iter(ia+2), 0, std::plus<>(), 3);
     40     test(Iter(ia), Iter(ia+2), 3, std::multiplies<>(), 6);
     41     test(Iter(ia), Iter(ia+sa), 0, std::plus<>(), 21);
     42     test(Iter(ia), Iter(ia+sa), 4, std::multiplies<>(), 2880);
     43 }
     44 
     45 template <typename T, typename Init>
     46 void test_return_type()
     47 {
     48     T *p = nullptr;
     49     static_assert( std::is_same_v<Init, decltype(std::reduce(p, p, Init{}, std::plus<>()))>, "" );
     50 }
     51 
     52 int main()
     53 {
     54     test_return_type<char, int>();
     55     test_return_type<int, int>();
     56     test_return_type<int, unsigned long>();
     57     test_return_type<float, int>();
     58     test_return_type<short, float>();
     59     test_return_type<double, char>();
     60     test_return_type<char, double>();
     61 
     62     test<input_iterator<const int*> >();
     63     test<forward_iterator<const int*> >();
     64     test<bidirectional_iterator<const int*> >();
     65     test<random_access_iterator<const int*> >();
     66     test<const int*>();
     67 
     68 //  Make sure the math is done using the correct type
     69     {
     70     auto v = {1, 2, 3, 4, 5, 6, 7, 8};
     71     unsigned res = std::reduce(v.begin(), v.end(), 1U, std::multiplies<>());
     72     assert(res == 40320);       // 8! will not fit into a char
     73     }
     74 }