]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // (C) Copyright Eric Niebler 2005. |
2 | // Use, modification and distribution are subject to the | |
3 | // Boost Software License, Version 1.0. (See accompanying file | |
4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | #include <iostream> | |
7 | #include <vector> | |
8 | #include <boost/utility/enable_if.hpp> | |
9 | #include <boost/type_traits/is_floating_point.hpp> | |
10 | #include <boost/test/unit_test.hpp> | |
11 | #include <boost/test/floating_point_comparison.hpp> | |
12 | #include <boost/accumulators/accumulators.hpp> | |
13 | #include <boost/accumulators/numeric/functional/vector.hpp> | |
14 | #include <boost/accumulators/statistics/stats.hpp> | |
15 | #include <boost/accumulators/statistics/min.hpp> | |
16 | #include <boost/accumulators/statistics/max.hpp> | |
17 | #include <boost/accumulators/statistics/mean.hpp> | |
18 | #include <boost/accumulators/statistics/weighted_mean.hpp> | |
19 | ||
20 | using namespace boost; | |
21 | using namespace unit_test; | |
22 | using namespace accumulators; | |
23 | ||
24 | template<typename T> | |
25 | typename boost::enable_if<is_floating_point<T> >::type is_equal_or_close(T const &left, T const &right) | |
26 | { | |
27 | BOOST_CHECK_CLOSE(left, right, 1e-5); | |
28 | } | |
29 | ||
30 | template<typename T> | |
31 | typename boost::disable_if<is_floating_point<T> >::type is_equal_or_close(T const &left, T const &right) | |
32 | { | |
33 | BOOST_CHECK_EQUAL(left, right); | |
34 | } | |
35 | ||
36 | template<typename T> | |
37 | void is_equal(std::vector<T> const &left, std::vector<T> const &right) | |
38 | { | |
39 | BOOST_CHECK_EQUAL(left.size(), right.size()); | |
40 | if(left.size() == right.size()) | |
41 | { | |
42 | for(std::size_t i = 0; i < left.size(); ++i) | |
43 | { | |
44 | is_equal_or_close(left[i], right[i]); | |
45 | } | |
46 | } | |
47 | } | |
48 | ||
49 | namespace std | |
50 | { | |
51 | template<typename T> | |
52 | inline std::ostream &operator <<(std::ostream &sout, std::vector<T> const &arr) | |
53 | { | |
54 | sout << '('; | |
55 | for(std::size_t i = 0; i < arr.size(); ++i) | |
56 | { | |
57 | sout << arr[i] << ','; | |
58 | } | |
59 | sout << ')' << std::endl; | |
60 | return sout; | |
61 | } | |
62 | } | |
63 | ||
64 | /////////////////////////////////////////////////////////////////////////////// | |
65 | // test_stat | |
66 | // | |
67 | void test_stat() | |
68 | { | |
69 | typedef std::vector<int> sample_t; | |
70 | ||
71 | // test sum | |
72 | { | |
73 | accumulator_set<sample_t, stats<tag::sum> > acc(sample = sample_t(3,0)); | |
74 | ||
75 | acc(sample_t(3,1)); | |
76 | acc(sample_t(3,2)); | |
77 | acc(sample_t(3,3)); | |
78 | ||
79 | is_equal(sample_t(3,6), sum(acc)); | |
80 | } | |
81 | ||
82 | // test min and max | |
83 | { | |
84 | int s1[] = {1,2,3}, s2[] = {0,3,4}, s3[] = {2,1,4}, min_res[] = {0,1,3}, max_res[] = {2,3,4}; | |
85 | accumulator_set<sample_t, stats<tag::min, tag::max> > acc(sample = sample_t(3,0)); | |
86 | ||
87 | acc(sample_t(s1,s1+3)); | |
88 | acc(sample_t(s2,s2+3)); | |
89 | acc(sample_t(s3,s3+3)); | |
90 | ||
91 | is_equal(sample_t(min_res,min_res+3), (min)(acc)); | |
92 | is_equal(sample_t(max_res,max_res+3), (max)(acc)); | |
93 | } | |
94 | ||
95 | // test mean(lazy) and mean(immediate) | |
96 | { | |
97 | accumulator_set<sample_t, stats<tag::mean> > acc(sample = sample_t(3,0)); | |
98 | ||
99 | acc(sample_t(3,1)); | |
100 | is_equal(std::vector<double>(3, 1.), mean(acc)); | |
101 | BOOST_CHECK_EQUAL(1u, count(acc)); | |
102 | is_equal(sample_t(3,1), sum(acc)); | |
103 | ||
104 | acc(sample_t(3,0)); | |
105 | is_equal(std::vector<double>(3, 0.5), mean(acc)); | |
106 | BOOST_CHECK_EQUAL(2u, count(acc)); | |
107 | is_equal(sample_t(3,1), sum(acc)); | |
108 | ||
109 | acc(sample_t(3,2)); | |
110 | is_equal(std::vector<double>(3, 1.), mean(acc)); | |
111 | BOOST_CHECK_EQUAL(3u, count(acc)); | |
112 | is_equal(sample_t(3,3), sum(acc)); | |
113 | ||
114 | ||
115 | accumulator_set<sample_t, stats<tag::mean(immediate)> > acc2(sample = sample_t(3,0)); | |
116 | ||
117 | acc2(sample_t(3,1)); | |
118 | is_equal(std::vector<double>(3,1.), mean(acc2)); | |
119 | BOOST_CHECK_EQUAL(1u, count(acc2)); | |
120 | ||
121 | acc2(sample_t(3,0)); | |
122 | is_equal(std::vector<double>(3,0.5), mean(acc2)); | |
123 | BOOST_CHECK_EQUAL(2u, count(acc2)); | |
124 | ||
125 | acc2(sample_t(3,2)); | |
126 | is_equal(std::vector<double>(3,1.), mean(acc2)); | |
127 | BOOST_CHECK_EQUAL(3u, count(acc2)); | |
128 | } | |
129 | ||
130 | // test weighted_mean | |
131 | { | |
132 | accumulator_set<sample_t, stats<tag::weighted_mean>, int> acc(sample = sample_t(3,0)); | |
133 | ||
134 | acc(sample_t(3,10), weight = 2); // 20 | |
135 | BOOST_CHECK_EQUAL(2, sum_of_weights(acc)); // | |
136 | // | |
137 | acc(sample_t(3,6), weight = 3); // 18 | |
138 | BOOST_CHECK_EQUAL(5, sum_of_weights(acc)); // | |
139 | // | |
140 | acc(sample_t(3,4), weight = 4); // 16 | |
141 | BOOST_CHECK_EQUAL(9, sum_of_weights(acc)); // | |
142 | // | |
143 | acc(sample_t(3,6), weight = 5); //+ 30 | |
144 | BOOST_CHECK_EQUAL(14, sum_of_weights(acc)); // | |
145 | //= 84 / 14 = 6 | |
146 | ||
147 | is_equal(std::vector<double>(3,6.), weighted_mean(acc)); | |
148 | ||
149 | ||
150 | accumulator_set<sample_t, stats<tag::weighted_mean(immediate)>, int> acc2(sample = sample_t(3,0)); | |
151 | ||
152 | acc2(sample_t(3,10), weight = 2); // 20 | |
153 | BOOST_CHECK_EQUAL(2, sum_of_weights(acc2)); // | |
154 | // | |
155 | acc2(sample_t(3,6), weight = 3); // 18 | |
156 | BOOST_CHECK_EQUAL(5, sum_of_weights(acc2)); // | |
157 | // | |
158 | acc2(sample_t(3,4), weight = 4); // 16 | |
159 | BOOST_CHECK_EQUAL(9, sum_of_weights(acc2)); // | |
160 | // | |
161 | acc2(sample_t(3,6), weight = 5); //+ 30 | |
162 | BOOST_CHECK_EQUAL(14, sum_of_weights(acc2));// | |
163 | //= 84 / 14 = 6 | |
164 | ||
165 | is_equal(std::vector<double>(3,6.), weighted_mean(acc2)); | |
166 | ||
167 | } | |
168 | } | |
169 | ||
170 | /////////////////////////////////////////////////////////////////////////////// | |
171 | // init_unit_test_suite | |
172 | // | |
173 | test_suite* init_unit_test_suite( int argc, char* argv[] ) | |
174 | { | |
175 | test_suite *test = BOOST_TEST_SUITE("vector test"); | |
176 | ||
177 | test->add(BOOST_TEST_CASE(&test_stat)); | |
178 | ||
179 | return test; | |
180 | } |