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
|
// (C) Copyright Eric Niebler 2005.
// Use, modification and distribution are subject to 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)
#include <iostream>
#include <vector>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_floating_point.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/tools/floating_point_comparison.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/numeric/functional/vector.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/min.hpp>
#include <boost/accumulators/statistics/max.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/weighted_mean.hpp>
using namespace boost;
using namespace unit_test;
using namespace accumulators;
template<typename T>
typename boost::enable_if<is_floating_point<T> >::type is_equal_or_close(T const &left, T const &right)
{
BOOST_CHECK_CLOSE(left, right, 1e-5);
}
template<typename T>
typename boost::disable_if<is_floating_point<T> >::type is_equal_or_close(T const &left, T const &right)
{
BOOST_CHECK_EQUAL(left, right);
}
template<typename T>
void is_equal(std::vector<T> const &left, std::vector<T> const &right)
{
BOOST_CHECK_EQUAL(left.size(), right.size());
if(left.size() == right.size())
{
for(std::size_t i = 0; i < left.size(); ++i)
{
is_equal_or_close(left[i], right[i]);
}
}
}
namespace std
{
template<typename T>
inline std::ostream &operator <<(std::ostream &sout, std::vector<T> const &arr)
{
sout << '(';
for(std::size_t i = 0; i < arr.size(); ++i)
{
sout << arr[i] << ',';
}
sout << ')' << std::endl;
return sout;
}
}
///////////////////////////////////////////////////////////////////////////////
// test_stat
//
void test_stat()
{
typedef std::vector<int> sample_t;
// test sum
{
accumulator_set<sample_t, stats<tag::sum> > acc(sample = sample_t(3,0));
acc(sample_t(3,1));
acc(sample_t(3,2));
acc(sample_t(3,3));
is_equal(sample_t(3,6), sum(acc));
}
// test min and max
{
int s1[] = {1,2,3}, s2[] = {0,3,4}, s3[] = {2,1,4}, min_res[] = {0,1,3}, max_res[] = {2,3,4};
accumulator_set<sample_t, stats<tag::min, tag::max> > acc(sample = sample_t(3,0));
acc(sample_t(s1,s1+3));
acc(sample_t(s2,s2+3));
acc(sample_t(s3,s3+3));
is_equal(sample_t(min_res,min_res+3), (min)(acc));
is_equal(sample_t(max_res,max_res+3), (max)(acc));
}
// test mean(lazy) and mean(immediate)
{
accumulator_set<sample_t, stats<tag::mean> > acc(sample = sample_t(3,0));
acc(sample_t(3,1));
is_equal(std::vector<double>(3, 1.), mean(acc));
BOOST_CHECK_EQUAL(1u, count(acc));
is_equal(sample_t(3,1), sum(acc));
acc(sample_t(3,0));
is_equal(std::vector<double>(3, 0.5), mean(acc));
BOOST_CHECK_EQUAL(2u, count(acc));
is_equal(sample_t(3,1), sum(acc));
acc(sample_t(3,2));
is_equal(std::vector<double>(3, 1.), mean(acc));
BOOST_CHECK_EQUAL(3u, count(acc));
is_equal(sample_t(3,3), sum(acc));
accumulator_set<sample_t, stats<tag::mean(immediate)> > acc2(sample = sample_t(3,0));
acc2(sample_t(3,1));
is_equal(std::vector<double>(3,1.), mean(acc2));
BOOST_CHECK_EQUAL(1u, count(acc2));
acc2(sample_t(3,0));
is_equal(std::vector<double>(3,0.5), mean(acc2));
BOOST_CHECK_EQUAL(2u, count(acc2));
acc2(sample_t(3,2));
is_equal(std::vector<double>(3,1.), mean(acc2));
BOOST_CHECK_EQUAL(3u, count(acc2));
}
// test weighted_mean
{
accumulator_set<sample_t, stats<tag::weighted_mean>, int> acc(sample = sample_t(3,0));
acc(sample_t(3,10), weight = 2); // 20
BOOST_CHECK_EQUAL(2, sum_of_weights(acc)); //
//
acc(sample_t(3,6), weight = 3); // 18
BOOST_CHECK_EQUAL(5, sum_of_weights(acc)); //
//
acc(sample_t(3,4), weight = 4); // 16
BOOST_CHECK_EQUAL(9, sum_of_weights(acc)); //
//
acc(sample_t(3,6), weight = 5); //+ 30
BOOST_CHECK_EQUAL(14, sum_of_weights(acc)); //
//= 84 / 14 = 6
is_equal(std::vector<double>(3,6.), weighted_mean(acc));
accumulator_set<sample_t, stats<tag::weighted_mean(immediate)>, int> acc2(sample = sample_t(3,0));
acc2(sample_t(3,10), weight = 2); // 20
BOOST_CHECK_EQUAL(2, sum_of_weights(acc2)); //
//
acc2(sample_t(3,6), weight = 3); // 18
BOOST_CHECK_EQUAL(5, sum_of_weights(acc2)); //
//
acc2(sample_t(3,4), weight = 4); // 16
BOOST_CHECK_EQUAL(9, sum_of_weights(acc2)); //
//
acc2(sample_t(3,6), weight = 5); //+ 30
BOOST_CHECK_EQUAL(14, sum_of_weights(acc2));//
//= 84 / 14 = 6
is_equal(std::vector<double>(3,6.), weighted_mean(acc2));
}
}
///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite *test = BOOST_TEST_SUITE("vector test");
test->add(BOOST_TEST_CASE(&test_stat));
return test;
}
|