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/foreach.hpp>
7 #include <boost/test/unit_test.hpp>
8 #include <boost/accumulators/accumulators.hpp>
9 #include <boost/accumulators/statistics/stats.hpp>
10 #include <boost/accumulators/statistics/tail.hpp>
11 #include <boost/accumulators/statistics/tail_variate.hpp>
12 #include <boost/accumulators/statistics/variates/covariate.hpp>
13
14 using namespace boost;
15 using namespace unit_test;
16 using namespace accumulators;
17
18 template<typename Range>
check_tail(Range const & rng,char const * expected)19 void check_tail(Range const &rng, char const *expected)
20 {
21 BOOST_FOREACH(int i, rng)
22 {
23 if(!*expected)
24 {
25 BOOST_CHECK(false);
26 return;
27 }
28 BOOST_CHECK_EQUAL(i, *expected++);
29 }
30 BOOST_CHECK(!*expected);
31 }
32
33 ///////////////////////////////////////////////////////////////////////////////
34 // test_right_tail
35 //
test_right_tail()36 void test_right_tail()
37 {
38 accumulator_set<int, stats<tag::tail_weights<right>, tag::tail_variate<int, tag::covariate1, right> >, int > acc(
39 right_tail_cache_size = 4
40 );
41
42 acc(010, weight = 2, covariate1 = 3);
43 check_tail(tail(acc), "\10");
44 check_tail(tail_variate(acc), "\3");
45 check_tail(tail_weights(acc), "\2");
46
47 acc(020, weight = 7, covariate1 = 1);
48 check_tail(tail(acc), "\20\10");
49 check_tail(tail_variate(acc), "\1\3");
50 check_tail(tail_weights(acc), "\7\2");
51
52 acc(014, weight = 6, covariate1 = 4);
53 check_tail(tail(acc), "\20\14\10");
54 check_tail(tail_variate(acc), "\1\4\3");
55 check_tail(tail_weights(acc), "\7\6\2");
56
57 acc(030, weight = 4, covariate1 = 5);
58 check_tail(tail(acc), "\30\20\14\10");
59 check_tail(tail_variate(acc), "\5\1\4\3");
60 check_tail(tail_weights(acc), "\4\7\6\2");
61
62 acc(001, weight = 1, covariate1 = 9);
63 check_tail(tail(acc), "\30\20\14\10");
64 check_tail(tail_variate(acc), "\5\1\4\3");
65 check_tail(tail_weights(acc), "\4\7\6\2");
66
67 acc(011, weight = 3, covariate1 = 7);
68 check_tail(tail(acc), "\30\20\14\11");
69 check_tail(tail_variate(acc), "\5\1\4\7");
70 check_tail(tail_weights(acc), "\4\7\6\3");
71
72 }
73
74 ///////////////////////////////////////////////////////////////////////////////
75 // test_left_tail
76 //
test_left_tail()77 void test_left_tail()
78 {
79 accumulator_set<int, stats<tag::tail_weights<left>, tag::tail_variate<int, tag::covariate1, left> >, int > acc(
80 left_tail_cache_size = 4
81 );
82
83 acc(010, weight = 2, covariate1 = 3);
84 check_tail(tail(acc), "\10");
85 check_tail(tail_variate(acc), "\3");
86 check_tail(tail_weights(acc), "\2");
87
88 acc(020, weight = 7, covariate1 = 1);
89 check_tail(tail(acc), "\10\20");
90 check_tail(tail_variate(acc), "\3\1");
91 check_tail(tail_weights(acc), "\2\7");
92
93 acc(014, weight = 6, covariate1 = 4);
94 check_tail(tail(acc), "\10\14\20");
95 check_tail(tail_variate(acc), "\3\4\1");
96 check_tail(tail_weights(acc), "\2\6\7");
97
98 acc(030, weight = 4, covariate1 = 5);
99 check_tail(tail(acc), "\10\14\20\30");
100 check_tail(tail_variate(acc), "\3\4\1\5");
101 check_tail(tail_weights(acc), "\2\6\7\4");
102
103 acc(001, weight = 1, covariate1 = 9);
104 check_tail(tail(acc), "\1\10\14\20");
105 check_tail(tail_variate(acc), "\x9\3\4\1");
106 check_tail(tail_weights(acc), "\1\2\6\7");
107
108 acc(011, weight = 3, covariate1 = 7);
109 check_tail(tail(acc), "\1\10\11\14");
110 check_tail(tail_variate(acc), "\x9\3\7\4");
111 check_tail(tail_weights(acc), "\1\2\3\6");
112 }
113
114 ///////////////////////////////////////////////////////////////////////////////
115 // init_unit_test_suite
116 //
init_unit_test_suite(int argc,char * argv[])117 test_suite* init_unit_test_suite( int argc, char* argv[] )
118 {
119 test_suite *test = BOOST_TEST_SUITE("tail test");
120
121 test->add(BOOST_TEST_CASE(&test_right_tail));
122 test->add(BOOST_TEST_CASE(&test_left_tail));
123
124 return test;
125 }
126
127