-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathreduce_test.cpp
More file actions
128 lines (109 loc) · 3.4 KB
/
reduce_test.cpp
File metadata and controls
128 lines (109 loc) · 3.4 KB
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
/*
Copyright (c) Marshall Clow 2013.
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 <boost/config.hpp>
#include <boost/algorithm/cxx17/reduce.hpp>
#include "iterator_test.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
namespace ba = boost::algorithm;
template <class Iter, class T, class Op>
void
test_reduce(Iter first, Iter last, T init, Op op, T x)
{
BOOST_CHECK(ba::reduce(first, last, init, op) == x);
}
template <class Iter, class T, class Op>
void
test_reduce(Iter first, Iter last, Op op, T x)
{
BOOST_CHECK(ba::reduce(first, last, op) == x);
}
template <class Iter, class T>
void
test_reduce(Iter first, Iter last, T x)
{
BOOST_CHECK(ba::reduce(first, last) == x);
}
template <class Iter>
void
test_init_op()
{
int ia[] = {1, 2, 3, 4, 5, 6};
unsigned sa = sizeof(ia) / sizeof(ia[0]);
test_reduce(Iter(ia), Iter(ia), 0, std::plus<int>(), 0);
test_reduce(Iter(ia), Iter(ia), 1, std::multiplies<int>(), 1);
test_reduce(Iter(ia), Iter(ia+1), 0, std::plus<int>(), 1);
test_reduce(Iter(ia), Iter(ia+1), 2, std::multiplies<int>(), 2);
test_reduce(Iter(ia), Iter(ia+2), 0, std::plus<int>(), 3);
test_reduce(Iter(ia), Iter(ia+2), 3, std::multiplies<int>(), 6);
test_reduce(Iter(ia), Iter(ia+sa), 0, std::plus<int>(), 21);
test_reduce(Iter(ia), Iter(ia+sa), 4, std::multiplies<int>(), 2880);
}
void test_reduce_init_op()
{
test_init_op<input_iterator<const int*> >();
test_init_op<forward_iterator<const int*> >();
test_init_op<bidirectional_iterator<const int*> >();
test_init_op<random_access_iterator<const int*> >();
test_init_op<const int*>();
{
char ia[] = {1, 2, 3, 4, 5, 6, 7, 8};
unsigned sa = sizeof(ia) / sizeof(ia[0]);
unsigned res = boost::algorithm::reduce(ia, ia+sa, 1U, std::multiplies<unsigned>());
BOOST_CHECK(res == 40320); // 8! will not fit into a char
}
}
template <class Iter>
void
test_init()
{
int ia[] = {1, 2, 3, 4, 5, 6};
unsigned sa = sizeof(ia) / sizeof(ia[0]);
test_reduce(Iter(ia), Iter(ia), 0, 0);
test_reduce(Iter(ia), Iter(ia), 1, 1);
test_reduce(Iter(ia), Iter(ia+1), 0, 1);
test_reduce(Iter(ia), Iter(ia+1), 2, 3);
test_reduce(Iter(ia), Iter(ia+2), 0, 3);
test_reduce(Iter(ia), Iter(ia+2), 3, 6);
test_reduce(Iter(ia), Iter(ia+sa), 0, 21);
test_reduce(Iter(ia), Iter(ia+sa), 4, 25);
}
void test_reduce_init()
{
test_init<input_iterator<const int*> >();
test_init<forward_iterator<const int*> >();
test_init<bidirectional_iterator<const int*> >();
test_init<random_access_iterator<const int*> >();
test_init<const int*>();
}
template <class Iter>
void
test()
{
int ia[] = {1, 2, 3, 4, 5, 6};
unsigned sa = sizeof(ia) / sizeof(ia[0]);
test_reduce(Iter(ia), Iter(ia), 0);
test_reduce(Iter(ia), Iter(ia+1), 1);
test_reduce(Iter(ia), Iter(ia+2), 3);
test_reduce(Iter(ia), Iter(ia+sa), 21);
}
void test_reduce()
{
test<input_iterator<const int*> >();
test<forward_iterator<const int*> >();
test<bidirectional_iterator<const int*> >();
test<random_access_iterator<const int*> >();
test<const int*>();
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_reduce();
test_reduce_init();
test_reduce_init_op();
}