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
|
// Copyright (C) Eric Niebler 2008.
// Copyright (C) Pieter Bastiaan Ober 2014.
// 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 <boost/test/unit_test.hpp>
#include <boost/test/tools/floating_point_comparison.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>
#include <sstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
using namespace boost;
using namespace unit_test;
using namespace accumulators;
template<typename T>
void assert_is_double(T const &)
{
BOOST_MPL_ASSERT((is_same<T, double>));
}
// test_rolling_mean_test_impl
// implements a test for window_size = 5
size_t window_size = 5;
template<typename accumulator_set_type>
void
test_rolling_mean_test_impl(accumulator_set_type& acc)
{
acc(1);
BOOST_CHECK_CLOSE(1., rolling_mean(acc), 1e-5);
acc(2);
BOOST_CHECK_CLOSE(1.5, rolling_mean(acc), 1e-5);
acc(3);
BOOST_CHECK_CLOSE(2., rolling_mean(acc), 1e-5);
acc(4);
BOOST_CHECK_CLOSE(2.5, rolling_mean(acc), 1e-5);
acc(5);
BOOST_CHECK_CLOSE(3., rolling_mean(acc), 1e-5);
acc(6);
BOOST_CHECK_CLOSE(4., rolling_mean(acc), 1e-5);
acc(7);
BOOST_CHECK_CLOSE(5., rolling_mean(acc), 1e-5);
assert_is_double(rolling_mean(acc));
}
template<typename accumulator_set_type>
void
test_rolling_mean_unsigned_test_impl(accumulator_set_type& acc)
{
acc(7U);
BOOST_CHECK_CLOSE(7., rolling_mean(acc), 1e-5);
acc(6U);
BOOST_CHECK_CLOSE(6.5, rolling_mean(acc), 1e-5);
acc(5U);
BOOST_CHECK_CLOSE(6., rolling_mean(acc), 1e-5);
acc(4U);
BOOST_CHECK_CLOSE(5.5, rolling_mean(acc), 1e-5);
acc(3U);
BOOST_CHECK_CLOSE(5., rolling_mean(acc), 1e-5);
acc(2U);
BOOST_CHECK_CLOSE(4., rolling_mean(acc), 1e-5);
acc(1U);
BOOST_CHECK_CLOSE(3., rolling_mean(acc), 1e-5);
assert_is_double(rolling_mean(acc));
}
///////////////////////////////////////////////////////////////////////////////
// test_persistency_impl
//
template<typename accumulator_set_type>
void test_persistency_impl(accumulator_set_type& acc)
{
std::stringstream ss;
{
acc(1);
acc(2);
acc(3);
acc(4);
acc(5);
acc(6);
acc(7);
BOOST_CHECK_CLOSE(5., rolling_mean(acc), 1e-5);
boost::archive::text_oarchive oa(ss);
acc.serialize(oa, 0);
}
// initialize from acc to make sure all values are passed
accumulator_set_type other_acc = acc;
// accumulate more, to make sure that deserialization set the right value
// and not the copy ctor
other_acc(100);
other_acc(100);
other_acc(100);
other_acc(100);
other_acc(100);
boost::archive::text_iarchive ia(ss);
other_acc.serialize(ia, 0);
BOOST_CHECK_CLOSE(5., rolling_mean(other_acc), 1e-5);
}
///////////////////////////////////////////////////////////////////////////////
// test_rolling_mean
void test_rolling_mean()
{
accumulator_set<int,stats<tag::immediate_rolling_mean> >
acc_immediate_rolling_mean(tag::immediate_rolling_mean::window_size = window_size),
acc_immediate_rolling_mean2(tag::immediate_rolling_mean::window_size = window_size, sample = 0);
accumulator_set<int,stats<tag::rolling_mean(immediate)> >
acc_immediate_rolling_mean3(tag::immediate_rolling_mean::window_size = window_size);
accumulator_set<int,stats<tag::lazy_rolling_mean> >
acc_lazy_rolling_mean(tag::lazy_rolling_mean::window_size = window_size),
acc_lazy_rolling_mean2(tag::lazy_rolling_mean::window_size = window_size, sample = 0);
accumulator_set<int,stats<tag::rolling_mean(lazy)> >
acc_lazy_rolling_mean3(tag::lazy_rolling_mean::window_size = window_size);
accumulator_set<int,stats<tag::rolling_mean> >
acc_default_rolling_mean(tag::rolling_mean::window_size = window_size),
acc_default_rolling_mean2(tag::rolling_mean::window_size = window_size, sample = 0);
//// test the different implementations
test_rolling_mean_test_impl(acc_lazy_rolling_mean);
test_rolling_mean_test_impl(acc_default_rolling_mean);
test_rolling_mean_test_impl(acc_immediate_rolling_mean);
test_rolling_mean_test_impl(acc_lazy_rolling_mean2);
test_rolling_mean_test_impl(acc_default_rolling_mean2);
test_rolling_mean_test_impl(acc_immediate_rolling_mean2);
test_rolling_mean_test_impl(acc_lazy_rolling_mean3);
test_rolling_mean_test_impl(acc_immediate_rolling_mean3);
//// test that the default implementation is the 'immediate' computation
BOOST_REQUIRE(sizeof(acc_lazy_rolling_mean) != sizeof(acc_immediate_rolling_mean));
BOOST_CHECK (sizeof(acc_default_rolling_mean) == sizeof(acc_immediate_rolling_mean));
//// test the equivalence of the different ways to indicate a feature
BOOST_CHECK (sizeof(acc_lazy_rolling_mean) == sizeof(acc_lazy_rolling_mean2));
BOOST_CHECK (sizeof(acc_lazy_rolling_mean) == sizeof(acc_lazy_rolling_mean3));
BOOST_CHECK (sizeof(acc_immediate_rolling_mean) == sizeof(acc_immediate_rolling_mean2));
BOOST_CHECK (sizeof(acc_immediate_rolling_mean) == sizeof(acc_immediate_rolling_mean3));
//// test unsigned int with both implementations
accumulator_set<unsigned int,stats<tag::immediate_rolling_mean> >
acc_immediate_rolling_mean4(tag::immediate_rolling_mean::window_size = window_size),
acc_immediate_rolling_mean5(tag::immediate_rolling_mean::window_size = window_size, sample = 0);
test_rolling_mean_unsigned_test_impl(acc_immediate_rolling_mean4);
test_rolling_mean_unsigned_test_impl(acc_immediate_rolling_mean5);
}
///////////////////////////////////////////////////////////////////////////////
// test_persistency
void test_persistency()
{
accumulator_set<int,stats<tag::immediate_rolling_mean> >
acc_immediate_rolling_mean(tag::immediate_rolling_mean::window_size = window_size),
acc_immediate_rolling_mean2(tag::immediate_rolling_mean::window_size = window_size, sample = 0);
accumulator_set<int,stats<tag::rolling_mean(immediate)> >
acc_immediate_rolling_mean3(tag::immediate_rolling_mean::window_size = window_size);
accumulator_set<int,stats<tag::lazy_rolling_mean> >
acc_lazy_rolling_mean(tag::lazy_rolling_mean::window_size = window_size),
acc_lazy_rolling_mean2(tag::lazy_rolling_mean::window_size = window_size, sample = 0);
accumulator_set<int,stats<tag::rolling_mean(lazy)> >
acc_lazy_rolling_mean3(tag::lazy_rolling_mean::window_size = window_size);
accumulator_set<int,stats<tag::rolling_mean> >
acc_default_rolling_mean(tag::rolling_mean::window_size = window_size),
acc_default_rolling_mean2(tag::rolling_mean::window_size = window_size, sample = 0);
//// test the different implementations
test_persistency_impl(acc_lazy_rolling_mean);
test_persistency_impl(acc_default_rolling_mean);
test_persistency_impl(acc_immediate_rolling_mean);
test_persistency_impl(acc_lazy_rolling_mean2);
test_persistency_impl(acc_default_rolling_mean2);
test_persistency_impl(acc_immediate_rolling_mean2);
test_persistency_impl(acc_lazy_rolling_mean3);
test_persistency_impl(acc_immediate_rolling_mean3);
}
///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite *test = BOOST_TEST_SUITE("rolling mean test");
test->add(BOOST_TEST_CASE(&test_rolling_mean));
test->add(BOOST_TEST_CASE(&test_persistency));
return test;
}
|