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 OutputIterator, class T, class BinaryOperation>
14 // OutputIterator
15 // inclusive_scan(InputIterator first, InputIterator last,
16 // OutputIterator result,
17 // BinaryOperation binary_op); // C++17
18
19 #include <numeric>
20 #include <algorithm>
21 #include <cassert>
22 #include <functional>
23 #include <iostream>
24 #include <iterator>
25 #include <vector>
26
27 #include "test_iterators.h"
28
29 template <class Iter1, class T, class Op, class Iter2>
30 void
test(Iter1 first,Iter1 last,Op op,Iter2 rFirst,Iter2 rLast)31 test(Iter1 first, Iter1 last, Op op, Iter2 rFirst, Iter2 rLast)
32 {
33 std::vector<typename std::iterator_traits<Iter1>::value_type> v;
34
35 // Not in place
36 std::inclusive_scan(first, last, std::back_inserter(v), op);
37 assert(std::equal(v.begin(), v.end(), rFirst, rLast));
38
39 // In place
40 v.clear();
41 v.assign(first, last);
42 std::inclusive_scan(v.begin(), v.end(), v.begin(), op);
43 assert(std::equal(v.begin(), v.end(), rFirst, rLast));
44 }
45
46
47 template <class Iter>
48 void
test()49 test()
50 {
51 int ia[] = {1, 3, 5, 7, 9};
52 const int pRes[] = {1, 4, 9, 16, 25};
53 const int mRes[] = {1, 3, 15, 105, 945};
54 const unsigned sa = sizeof(ia) / sizeof(ia[0]);
55 static_assert(sa == sizeof(pRes) / sizeof(pRes[0])); // just to be sure
56 static_assert(sa == sizeof(mRes) / sizeof(mRes[0])); // just to be sure
57
58 for (unsigned int i = 0; i < sa; ++i ) {
59 test(Iter(ia), Iter(ia + i), std::plus<>(), pRes, pRes + i);
60 test(Iter(ia), Iter(ia + i), std::multiplies<>(), mRes, mRes + i);
61 }
62 }
63
triangle(size_t n)64 size_t triangle(size_t n) { return n*(n+1)/2; }
65
66 // Basic sanity
basic_tests()67 void basic_tests()
68 {
69 {
70 std::vector<size_t> v(10);
71 std::fill(v.begin(), v.end(), 3);
72 std::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>());
73 for (size_t i = 0; i < v.size(); ++i)
74 assert(v[i] == (i+1) * 3);
75 }
76
77 {
78 std::vector<size_t> v(10);
79 std::iota(v.begin(), v.end(), 0);
80 std::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>());
81 for (size_t i = 0; i < v.size(); ++i)
82 assert(v[i] == triangle(i));
83 }
84
85 {
86 std::vector<size_t> v(10);
87 std::iota(v.begin(), v.end(), 1);
88 std::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>());
89 for (size_t i = 0; i < v.size(); ++i)
90 assert(v[i] == triangle(i + 1));
91 }
92
93 {
94 std::vector<size_t> v, res;
95 std::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<>());
96 assert(res.empty());
97 }
98 }
99
100
main()101 int main()
102 {
103
104 basic_tests();
105
106 // All the iterator categories
107 // test<input_iterator <const int*> >();
108 // test<forward_iterator <const int*> >();
109 // test<bidirectional_iterator<const int*> >();
110 // test<random_access_iterator<const int*> >();
111 // test<const int*>();
112 // test< int*>();
113
114 }
115