1/*
2 Copyright (c) Marshall Clow 2017.
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 For more information, see http://www.boost.org
8*/
9
10#include <vector>
11#include <functional>
12#include <numeric>
13#include <algorithm>
14
15#include <boost/config.hpp>
16#include <boost/algorithm/cxx11/iota.hpp>
17#include <boost/algorithm/cxx17/inclusive_scan.hpp>
18
19#include "iterator_test.hpp"
20
21#define BOOST_TEST_MAIN
22#include <boost/test/unit_test.hpp>
23
24namespace ba = boost::algorithm;
25
26int triangle(int n) { return n*(n+1)/2; }
27
28void basic_tests_op()
29{
30 {
31 std::vector<int> v(10);
32 std::fill(first: v.begin(), last: v.end(), value: 3);
33 ba::inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>());
34 for (size_t i = 0; i < v.size(); ++i)
35 assert(v[i] == (int)(i+1) * 3);
36 }
37
38 {
39 std::vector<int> v(10);
40 ba::iota(first: v.begin(), last: v.end(), value: 0);
41 ba::inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>());
42 for (size_t i = 0; i < v.size(); ++i)
43 assert(v[i] == triangle(i));
44 }
45
46 {
47 std::vector<int> v(10);
48 ba::iota(first: v.begin(), last: v.end(), value: 1);
49 ba::inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>());
50 for (size_t i = 0; i < v.size(); ++i)
51 assert(v[i] == triangle(i + 1));
52 }
53
54 {
55 std::vector<int> v, res;
56 ba::inclusive_scan(first: v.begin(), last: v.end(), result: std::back_inserter(x&: res), bOp: std::plus<int>());
57 assert(res.empty());
58 }
59}
60
61void test_inclusive_scan_op()
62{
63 basic_tests_op();
64 BOOST_CHECK(true);
65}
66
67void basic_tests_init()
68{
69 {
70 std::vector<int> v(10);
71 std::fill(first: v.begin(), last: v.end(), value: 3);
72 ba::inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>(), init: 50);
73 for (size_t i = 0; i < v.size(); ++i)
74 assert(v[i] == 50 + (int)(i+1) * 3);
75 }
76
77 {
78 std::vector<int> v(10);
79 ba::iota(first: v.begin(), last: v.end(), value: 0);
80 ba::inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>(), init: 40);
81 for (size_t i = 0; i < v.size(); ++i)
82 assert(v[i] == 40 + triangle(i));
83 }
84
85 {
86 std::vector<int> v(10);
87 ba::iota(first: v.begin(), last: v.end(), value: 1);
88 ba::inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>(), init: 30);
89 for (size_t i = 0; i < v.size(); ++i)
90 assert(v[i] == 30 + triangle(i + 1));
91 }
92
93 {
94 std::vector<int> v, res;
95 ba::inclusive_scan(first: v.begin(), last: v.end(), result: std::back_inserter(x&: res), bOp: std::plus<int>(), init: 40);
96 assert(res.empty());
97 }
98}
99
100
101void test_inclusive_scan_init()
102{
103 basic_tests_init();
104 BOOST_CHECK(true);
105}
106
107void basic_tests_op_init()
108{
109 {
110 std::vector<int> v(10);
111 std::fill(first: v.begin(), last: v.end(), value: 3);
112 ba::inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>(), init: 50);
113 for (size_t i = 0; i < v.size(); ++i)
114 BOOST_CHECK(v[i] == 50 + (int)(i+1) * 3);
115 }
116
117 {
118 std::vector<int> v(10);
119 ba::iota(first: v.begin(), last: v.end(), value: 0);
120 ba::inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>(), init: 40);
121 for (size_t i = 0; i < v.size(); ++i)
122 BOOST_CHECK(v[i] == 40 + triangle(i));
123 }
124
125 {
126 std::vector<int> v(10);
127 ba::iota(first: v.begin(), last: v.end(), value: 1);
128 ba::inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>(), init: 30);
129 for (size_t i = 0; i < v.size(); ++i)
130 BOOST_CHECK(v[i] == 30 + triangle(i + 1));
131 }
132
133 {
134 std::vector<int> v, res;
135 ba::inclusive_scan(first: v.begin(), last: v.end(), result: std::back_inserter(x&: res), bOp: std::plus<int>(), init: 40);
136 BOOST_CHECK(res.empty());
137 }
138}
139
140void test_inclusive_scan_op_init()
141{
142 basic_tests_op_init();
143 BOOST_CHECK(true);
144}
145
146
147
148BOOST_AUTO_TEST_CASE( test_main )
149{
150 test_inclusive_scan_op();
151 test_inclusive_scan_init();
152 test_inclusive_scan_op_init();
153}
154

source code of boost/libs/algorithm/test/inclusive_scan_test.cpp