blob: 56d6c8eb451f31083864608fb776169a658b62ab [file] [log] [blame]
Marshall Clowf4ea23d2017-06-14 04:48:45 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Marshall Clowf4ea23d2017-06-14 04:48:45 +00006//
7//===----------------------------------------------------------------------===//
8
9// <numeric>
10// UNSUPPORTED: c++98, c++03, c++11, c++14
11
12// template<class InputIterator>
13// typename iterator_traits<InputIterator>::value_type
14// reduce(InputIterator first, InputIterator last);
15
16#include <numeric>
17#include <cassert>
18
Marshall Clow7fc6a552019-05-31 18:35:30 +000019#include "test_macros.h"
Marshall Clowf4ea23d2017-06-14 04:48:45 +000020#include "test_iterators.h"
21
22template <class Iter, class T>
23void
24test(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
31template <class Iter>
32void
33test()
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
43template <typename T>
44void test_return_type()
45{
46 T *p = nullptr;
47 static_assert( std::is_same_v<T, decltype(std::reduce(p, p))> );
48}
49
JF Bastien2df59c52019-02-04 20:31:13 +000050int main(int, char**)
Marshall Clowf4ea23d2017-06-14 04:48:45 +000051{
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*>();
JF Bastien2df59c52019-02-04 20:31:13 +000063
64 return 0;
Marshall Clowf4ea23d2017-06-14 04:48:45 +000065}