xref: /aosp_15_r20/external/libcxx/test/std/numerics/numeric.ops/reduce/reduce.pass.cpp (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
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>
14  //     typename iterator_traits<InputIterator>::value_type
15  //     reduce(InputIterator first, InputIterator last);
16  
17  #include <numeric>
18  #include <cassert>
19  
20  #include "test_iterators.h"
21  
22  template <class Iter, class T>
23  void
test(Iter first,Iter last,T x)24  test(Iter first, Iter last, T x)
25  {
26      static_assert( std::is_same_v<typename std::iterator_traits<decltype(first)>::value_type,
27                                  decltype(std::reduce(first, last))> );
28      assert(std::reduce(first, last) == x);
29  }
30  
31  template <class Iter>
32  void
test()33  test()
34  {
35      int ia[] = {1, 2, 3, 4, 5, 6};
36      unsigned sa = sizeof(ia) / sizeof(ia[0]);
37      test(Iter(ia), Iter(ia), 0);
38      test(Iter(ia), Iter(ia+1), 1);
39      test(Iter(ia), Iter(ia+2), 3);
40      test(Iter(ia), Iter(ia+sa), 21);
41  }
42  
43  template <typename T>
test_return_type()44  void test_return_type()
45  {
46      T *p = nullptr;
47      static_assert( std::is_same_v<T, decltype(std::reduce(p, p))> );
48  }
49  
main()50  int main()
51  {
52      test_return_type<char>();
53      test_return_type<int>();
54      test_return_type<unsigned long>();
55      test_return_type<float>();
56      test_return_type<double>();
57  
58      test<input_iterator<const int*> >();
59      test<forward_iterator<const int*> >();
60      test<bidirectional_iterator<const int*> >();
61      test<random_access_iterator<const int*> >();
62      test<const int*>();
63  }
64