Marshall Clow | f4ea23d | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // 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 Clow | f4ea23d | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 6 | // |
| 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 | |
| 19 | #include "test_iterators.h" |
| 20 | |
| 21 | template <class Iter, class T> |
| 22 | void |
| 23 | test(Iter first, Iter last, T x) |
| 24 | { |
| 25 | static_assert( std::is_same_v<typename std::iterator_traits<decltype(first)>::value_type, |
| 26 | decltype(std::reduce(first, last))> ); |
| 27 | assert(std::reduce(first, last) == x); |
| 28 | } |
| 29 | |
| 30 | template <class Iter> |
| 31 | void |
| 32 | test() |
| 33 | { |
| 34 | int ia[] = {1, 2, 3, 4, 5, 6}; |
| 35 | unsigned sa = sizeof(ia) / sizeof(ia[0]); |
| 36 | test(Iter(ia), Iter(ia), 0); |
| 37 | test(Iter(ia), Iter(ia+1), 1); |
| 38 | test(Iter(ia), Iter(ia+2), 3); |
| 39 | test(Iter(ia), Iter(ia+sa), 21); |
| 40 | } |
| 41 | |
| 42 | template <typename T> |
| 43 | void test_return_type() |
| 44 | { |
| 45 | T *p = nullptr; |
| 46 | static_assert( std::is_same_v<T, decltype(std::reduce(p, p))> ); |
| 47 | } |
| 48 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 49 | int main(int, char**) |
Marshall Clow | f4ea23d | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 50 | { |
| 51 | test_return_type<char>(); |
| 52 | test_return_type<int>(); |
| 53 | test_return_type<unsigned long>(); |
| 54 | test_return_type<float>(); |
| 55 | test_return_type<double>(); |
| 56 | |
| 57 | test<input_iterator<const int*> >(); |
| 58 | test<forward_iterator<const int*> >(); |
| 59 | test<bidirectional_iterator<const int*> >(); |
| 60 | test<random_access_iterator<const int*> >(); |
| 61 | test<const int*>(); |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 62 | |
| 63 | return 0; |
Marshall Clow | f4ea23d | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 64 | } |