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