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 <boost/test/unit_test.hpp> |
7 | #include <boost/test/tools/floating_point_comparison.hpp> |
8 | #include <boost/accumulators/accumulators.hpp> |
9 | #include <boost/accumulators/statistics/stats.hpp> |
10 | #include <boost/accumulators/statistics/moment.hpp> |
11 | #include <sstream> |
12 | #include <boost/archive/text_oarchive.hpp> |
13 | #include <boost/archive/text_iarchive.hpp> |
14 | |
15 | using namespace boost; |
16 | using namespace unit_test; |
17 | using namespace accumulators; |
18 | |
19 | /////////////////////////////////////////////////////////////////////////////// |
20 | // test_stat |
21 | // |
22 | void test_stat() |
23 | { |
24 | accumulator_set<int, stats<tag::moment<2> > > acc1; |
25 | |
26 | acc1(2); // 4 |
27 | acc1(4); // 16 |
28 | acc1(5); // + 25 |
29 | // = 45 / 3 = 15 |
30 | |
31 | BOOST_CHECK_CLOSE(15., accumulators::moment<2>(acc1), 1e-5); |
32 | |
33 | accumulator_set<int, stats<tag::moment<5> > > acc2; |
34 | |
35 | acc2(2); // 32 |
36 | acc2(3); // 243 |
37 | acc2(4); // 1024 |
38 | acc2(5); // + 3125 |
39 | // = 4424 / 4 = 1106 |
40 | |
41 | BOOST_CHECK_CLOSE(1106., accumulators::moment<5>(acc2), 1e-5); |
42 | } |
43 | |
44 | /////////////////////////////////////////////////////////////////////////////// |
45 | // test_persistency |
46 | // |
47 | void test_persistency() |
48 | { |
49 | double epsilon = 1e-5; |
50 | std::stringstream ss; |
51 | { |
52 | accumulator_set<int, stats<tag::moment<2> > > acc; |
53 | acc(2); // 4 |
54 | acc(4); // 16 |
55 | acc(5); // + 25 |
56 | // = 45 / 3 = 15 |
57 | BOOST_CHECK_CLOSE(15., accumulators::moment<2>(acc), epsilon); |
58 | boost::archive::text_oarchive oa(ss); |
59 | acc.serialize(ar&: oa, file_version: 0); |
60 | } |
61 | accumulator_set<int, stats<tag::moment<2> > > acc; |
62 | boost::archive::text_iarchive ia(ss); |
63 | acc.serialize(ar&: ia, file_version: 0); |
64 | BOOST_CHECK_CLOSE(15., accumulators::moment<2>(acc), epsilon); |
65 | } |
66 | |
67 | /////////////////////////////////////////////////////////////////////////////// |
68 | // init_unit_test_suite |
69 | // |
70 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
71 | { |
72 | test_suite *test = BOOST_TEST_SUITE("moment test" ); |
73 | |
74 | test->add(BOOST_TEST_CASE(&test_stat)); |
75 | test->add(BOOST_TEST_CASE(&test_persistency)); |
76 | |
77 | return test; |
78 | } |
79 | |
80 | |