-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathtransform_exclusive_scan_test.cpp
234 lines (203 loc) · 9.89 KB
/
transform_exclusive_scan_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
Copyright (c) Marshall Clow 2017.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <vector>
#include <functional>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/iota.hpp>
#include <boost/algorithm/cxx17/transform_exclusive_scan.hpp>
#include "iterator_test.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
namespace ba = boost::algorithm;
int triangle(int n) { return n*(n+1)/2; }
template <class _Tp>
struct identity
{
BOOST_CXX14_CONSTEXPR const _Tp& operator()(const _Tp& __x) const { return __x;}
};
template <class Iter1, class BOp, class UOp, class T, class Iter2>
void
test_single_input(Iter1 first, Iter1 last, BOp bop, UOp uop, T init, Iter2 rFirst, Iter2 rLast)
{
std::vector<typename std::iterator_traits<Iter1>::value_type> v;
// Test not in-place
ba::transform_exclusive_scan(first, last, std::back_inserter(v), init, bop, uop);
BOOST_CHECK(std::distance(rFirst, rLast) == v.size());
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
// Test in-place
v.clear();
v.assign(first, last);
ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), init, bop, uop);
BOOST_CHECK(std::distance(rFirst, rLast) == v.size());
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
}
template <class Iter1, class Iter2, class ScanOperation, class TransformOperation, class T, class Iter3>
void
test_dual_input(Iter1 first1, Iter1 last1,
Iter2 first2,
ScanOperation scan_op, TransformOperation trans_op,
T init, Iter3 expected_first, Iter3 expected_last)
{
{ // Test not in-place
std::vector<typename std::iterator_traits<Iter3>::value_type> output;
ba::transform_exclusive_scan(first1, last1, first2, std::back_inserter(output), init, scan_op, trans_op);
const typename std::iterator_traits<Iter3>::difference_type result_size = std::distance(expected_first, expected_last);
BOOST_CHECK(result_size >= 0);
BOOST_CHECK(static_cast<std::size_t>(result_size) == output.size());
BOOST_CHECK(std::equal(output.begin(), output.end(), expected_first));
}
{ // Test in-place
std::vector<typename std::iterator_traits<Iter3>::value_type> v(first1, last1);
ba::transform_exclusive_scan(v.begin(), v.end(), first2, v.begin(), init, scan_op, trans_op);
const typename std::iterator_traits<Iter3>::difference_type result_size = std::distance(expected_first, expected_last);
BOOST_CHECK(result_size >= 0);
BOOST_CHECK(static_cast<std::size_t>(result_size) == v.size());
BOOST_CHECK(std::equal(v.begin(), v.end(), expected_first));
}
}
template <class Iter>
void
test()
{
int ia1[] = { 1, 3, 5, 7, 9};
int ia2[] = { 2, 4, 6, 8, 10};
const unsigned sa = sizeof(ia1) / sizeof(ia1[0]);
BOOST_CHECK(sa == sizeof(ia2) / sizeof(ia2[0])); // just to be sure
// single input results
const int pResI0[] = { 0, 1, 4, 9, 16}; // with identity
const int mResI0[] = { 0, 0, 0, 0, 0};
const int pResN0[] = { 0, -1, -4, -9, -16}; // with negate
const int mResN0[] = { 0, 0, 0, 0, 0};
const int pResI2[] = { 2, 3, 6, 11, 18}; // with identity
const int mResI2[] = { 2, 2, 6, 30, 210};
const int pResN2[] = { 2, 1, -2, -7, -14}; // with negate
const int mResN2[] = { 2, -2, 6, -30, 210};
BOOST_CHECK(sa == sizeof(pResI0) / sizeof(pResI0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResI0) / sizeof(mResI0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(pResN0) / sizeof(pResN0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResN0) / sizeof(mResN0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(pResI2) / sizeof(pResI2[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResI2) / sizeof(mResI2[0])); // just to be sure
BOOST_CHECK(sa == sizeof(pResN2) / sizeof(pResN2[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResN2) / sizeof(mResN2[0])); // just to be sure
// dual input results
const int pResP0[] = { 0, 3, 10, 21, 36};
const int pResP2[] = { 2, 5, 12, 23, 38};
const int pResM0[] = { 0, -1, -2, -3, -4};
const int pResM2[] = { 2, 1, 0, -1, -2};
const int mResP0[] = { 0, -3, -10, -21, -36};
const int mResP2[] = { 2, -1, -8, -19, -34};
const int mResM0[] = { 0, 1, 2, 3, 4};
const int mResM2[] = { 2, 3, 4, 5, 6};
BOOST_CHECK(sa == sizeof(pResP0) / sizeof(pResP0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(pResP2) / sizeof(pResP2[0])); // just to be sure
BOOST_CHECK(sa == sizeof(pResM0) / sizeof(pResM0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(pResM2) / sizeof(pResM2[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResP0) / sizeof(mResP0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResP2) / sizeof(mResP2[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResM0) / sizeof(mResM0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResM2) / sizeof(mResM2[0])); // just to be sure
for (unsigned int i = 0; i < sa; ++i ) {
test_single_input(Iter(ia1), Iter(ia1 + i), std::plus<int>(), identity<int>(), 0, pResI0, pResI0 + i);
test_single_input(Iter(ia1), Iter(ia1 + i), std::multiplies<int>(), identity<int>(), 0, mResI0, mResI0 + i);
test_single_input(Iter(ia1), Iter(ia1 + i), std::plus<int>(), std::negate<int>(), 0, pResN0, pResN0 + i);
test_single_input(Iter(ia1), Iter(ia1 + i), std::multiplies<int>(), std::negate<int>(), 0, mResN0, mResN0 + i);
test_single_input(Iter(ia1), Iter(ia1 + i), std::plus<int>(), identity<int>(), 2, pResI2, pResI2 + i);
test_single_input(Iter(ia1), Iter(ia1 + i), std::multiplies<int>(), identity<int>(), 2, mResI2, mResI2 + i);
test_single_input(Iter(ia1), Iter(ia1 + i), std::plus<int>(), std::negate<int>(), 2, pResN2, pResN2 + i);
test_single_input(Iter(ia1), Iter(ia1 + i), std::multiplies<int>(), std::negate<int>(), 2, mResN2, mResN2 + i);
test_dual_input(Iter(ia1), Iter(ia1 + i), Iter(ia2), std::plus<int>(), std::plus<int>(), 0, pResP0, pResP0 + i);
test_dual_input(Iter(ia1), Iter(ia1 + i), Iter(ia2), std::plus<int>(), std::plus<int>(), 2, pResP2, pResP2 + i);
test_dual_input(Iter(ia1), Iter(ia1 + i), Iter(ia2), std::plus<int>(), std::minus<int>(), 0, pResM0, pResM0 + i);
test_dual_input(Iter(ia1), Iter(ia1 + i), Iter(ia2), std::plus<int>(), std::minus<int>(), 2, pResM2, pResM2 + i);
test_dual_input(Iter(ia1), Iter(ia1 + i), Iter(ia2), std::minus<int>(), std::plus<int>(), 0, mResP0, mResP0 + i);
test_dual_input(Iter(ia1), Iter(ia1 + i), Iter(ia2), std::minus<int>(), std::plus<int>(), 2, mResP2, mResP2 + i);
test_dual_input(Iter(ia1), Iter(ia1 + i), Iter(ia2), std::minus<int>(), std::minus<int>(), 0, mResM0, mResM0 + i);
test_dual_input(Iter(ia1), Iter(ia1 + i), Iter(ia2), std::minus<int>(), std::minus<int>(), 2, mResM2, mResM2 + i);
}
}
void basic_tests()
{
{
std::vector<int> v(10);
std::fill(v.begin(), v.end(), 3);
ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 50, std::plus<int>(), identity<int>());
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 50 + (int) i * 3);
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 0);
ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 30, std::plus<int>(), identity<int>());
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 30 + triangle(i-1));
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 1);
ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 40, std::plus<int>(), identity<int>());
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 40 + triangle(i));
}
{
std::vector<int> v, res;
ba::transform_exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 40, std::plus<int>(), identity<int>());
BOOST_CHECK(res.empty());
}
}
BOOST_CXX14_CONSTEXPR bool constexpr_transform_exclusive_scan_tests() {
typedef random_access_iterator<int*> iterator_t;
const int NUM_ELEMENTS = 3;
bool status = true;
{ // Single input range
int input[NUM_ELEMENTS] = {3, 3, 3};
int output[NUM_ELEMENTS] = {0, 0, 0};
ba::transform_exclusive_scan(
iterator_t(input), iterator_t(input + NUM_ELEMENTS),
iterator_t(output),
30,
std::plus<int>(), identity<int>());
for (size_t i = 0; i < NUM_ELEMENTS; ++i)
status &= (output[i] == 30 + (int)(i * 3));
}
{ // Dual input ranges
int input1[NUM_ELEMENTS] = {3, 3, 3};
int input2[NUM_ELEMENTS] = {1, 1, 1};
int output[NUM_ELEMENTS] = {0, 0, 0};
ba::transform_exclusive_scan(
iterator_t(input1), iterator_t(input1 + NUM_ELEMENTS),
iterator_t(input2),
iterator_t(output),
30,
std::plus<int>(), std::minus<int>());
for (size_t i = 0; i < NUM_ELEMENTS; ++i)
status &= (output[i] == 30 + (int)(i * 2));
}
return status;
}
void test_transform_exclusive_scan_init()
{
basic_tests();
// All the iterator categories
test<input_iterator <const int*> >();
test<forward_iterator <const int*> >();
test<bidirectional_iterator<const int*> >();
test<random_access_iterator<const int*> >();
test<const int*>();
test< int*>();
{
BOOST_CXX14_CONSTEXPR bool status = constexpr_transform_exclusive_scan_tests();
BOOST_CHECK(status == true);
}
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_transform_exclusive_scan_init();
}