Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +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 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <numeric> |
| 10 | |
Howard Hinnant | a0eaf60 | 2010-08-22 00:42:21 +0000 | [diff] [blame] | 11 | // template <InputIterator Iter, MoveConstructible T> |
| 12 | // requires HasPlus<T, Iter::reference> |
| 13 | // && HasAssign<T, HasPlus<T, Iter::reference>::result_type> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 14 | // T |
| 15 | // accumulate(Iter first, Iter last, T init); |
| 16 | |
| 17 | #include <numeric> |
| 18 | #include <cassert> |
| 19 | |
Marshall Clow | 3222708 | 2013-01-05 03:21:01 +0000 | [diff] [blame] | 20 | #include "test_iterators.h" |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | |
| 22 | template <class Iter, class T> |
| 23 | void |
| 24 | test(Iter first, Iter last, T init, T x) |
| 25 | { |
| 26 | assert(std::accumulate(first, last, init) == 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, 0); |
| 36 | test(Iter(ia), Iter(ia), 10, 10); |
| 37 | test(Iter(ia), Iter(ia+1), 0, 1); |
| 38 | test(Iter(ia), Iter(ia+1), 10, 11); |
| 39 | test(Iter(ia), Iter(ia+2), 0, 3); |
| 40 | test(Iter(ia), Iter(ia+2), 10, 13); |
| 41 | test(Iter(ia), Iter(ia+sa), 0, 21); |
| 42 | test(Iter(ia), Iter(ia+sa), 10, 31); |
| 43 | } |
| 44 | |
| 45 | int main() |
| 46 | { |
| 47 | test<input_iterator<const int*> >(); |
| 48 | test<forward_iterator<const int*> >(); |
| 49 | test<bidirectional_iterator<const int*> >(); |
| 50 | test<random_access_iterator<const int*> >(); |
| 51 | test<const int*>(); |
| 52 | } |