Move sourcecode into 'senf/' directory
[senf.git] / senf / Utils / Statistics.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Statistics unit tests */
25
26 //#include "Statistics.test.hh"
27 //#include "Statistics.test.ih"
28
29 // Custom includes
30 #include "Statistics.hh"
31 #include "StatisticsTargets.hh"
32
33 #include "auto_unit_test.hh"
34 #include <boost/test/test_tools.hpp>
35 #include <boost/test/floating_point_comparison.hpp>
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 namespace {
41     
42     struct GetRange
43     {
44         typedef senf::Collector const & first_argument_type;
45         typedef unsigned result_type;
46         result_type operator()(first_argument_type arg) const
47             { return arg.rank(); }
48     };
49
50 }
51
52 BOOST_AUTO_UNIT_TEST(statistics)
53 {
54     senf::Statistics stats;
55     senf::log::StringTarget statslog;
56
57     statslog.tag("");
58     statslog.showTime(false);
59     statslog.showArea(false);
60     statslog.showLevel(false);
61     statslog.route<senf::StatisticsStream>();
62
63     stats
64             .output(2u).connect(
65                 senf::StatisticsLogger("level0"))
66         .collect(4u)
67             .output().connect(
68                 senf::StatisticsLogger("level1"))
69             .output(3u).connect(
70                 senf::StatisticsLogger("averaged1"))
71         .collect(3u)
72         .collect(2u)
73             .output().connect(
74                 senf::StatisticsLogger("level3"));
75
76     unsigned children1[] = { 4u };
77     BOOST_CHECK_EQUAL_COLLECTIONS( 
78         boost::make_transform_iterator(stats.collectors().begin(), GetRange()),
79         boost::make_transform_iterator(stats.collectors().end(), GetRange()),
80         children1, children1 + sizeof(children1)/sizeof(children1[0]) );
81
82     unsigned children2[] = { 3u };
83     BOOST_CHECK_EQUAL_COLLECTIONS( 
84         boost::make_transform_iterator(stats[4].collectors().begin(), GetRange()),
85         boost::make_transform_iterator(stats[4].collectors().end(), GetRange()),
86         children2, children2 + sizeof(children2)/sizeof(children2[0]) );
87
88     unsigned children3[] = { 2u };
89     BOOST_CHECK_EQUAL_COLLECTIONS( 
90         boost::make_transform_iterator(stats[4][3].collectors().begin(), GetRange()),
91         boost::make_transform_iterator(stats[4][3].collectors().end(), GetRange()),
92         children3, children3 + sizeof(children3)/sizeof(children3[0]) );
93
94     float values[][3] = { 
95         { -1.0f,  2.3f,  2.5f }, {  0.3f,  2.4f,  3.8f }, { -1.1f, -0.3f,  0.0f },
96         { -0.3f,  3.2f,  3.3f }, {  1.0f,  1.1f,  1.1f }, {  0.5f,  0.5f,  0.5f },
97         {  0.0f,  0.0f,  0.0f }, { -2.0f, -1.8f, -1.0f }, {  0.0f,  2.3f,  2.4f },
98         {  0.4f,  1.2f,  2.0f }, { -1.0f,  2.3f,  2.5f }, {  0.3f,  2.4f,  3.8f },
99         { -1.1f, -0.3f,  0.0f }, { -0.3f,  3.2f,  3.3f }, {  1.0f,  1.1f,  1.1f },
100         {  0.5f,  0.5f,  0.5f }, {  0.0f,  0.0f,  0.0f }, { -2.0f, -1.8f, -1.0f },
101         {  0.0f,  2.3f,  2.4f }, {  0.4f,  1.2f,  2.0f }, { -1.0f,  2.3f,  2.5f },
102         {  0.3f,  2.4f,  3.8f }, { -1.1f, -0.3f,  0.0f }, { -0.3f,  3.2f,  3.3f },
103         {  1.0f,  1.1f,  1.1f }, {  0.5f,  0.5f,  0.5f }, {  0.0f,  0.0f,  0.0f },
104         { -2.0f, -1.8f, -1.0f }, {  0.0f,  2.3f,  2.4f }, {  0.4f,  1.2f,  4.0f } };
105
106     for (unsigned i (0); i < sizeof(values)/sizeof(values[0]); ++i)
107         stats(values[i][0], values[i][1], values[i][2]);
108
109     BOOST_CHECK_CLOSE( stats.min(), 0.4f, .1f );
110     BOOST_CHECK_CLOSE( stats.avg(), 1.2f, .1f );
111     BOOST_CHECK_CLOSE( stats.max(), 4.0f, .1f );
112
113     BOOST_CHECK_CLOSE( stats[4].min(), -2.0f, .1f );
114     BOOST_CHECK_CLOSE( stats[4].avg(), -0.05f, .1f );
115     BOOST_CHECK_CLOSE( stats[4].max(), 1.1f, .1f );
116
117     BOOST_CHECK_CLOSE( stats[4][3].min(), -2.0f, .1f );
118     BOOST_CHECK_CLOSE( stats[4][3].avg(), 1.15f, .1f );
119     BOOST_CHECK_CLOSE( stats[4][3].max(),  3.8f, .1f );
120
121     BOOST_CHECK_EQUAL( statslog.str(), 
122                        "level0 -1 2.3 2.5\n"
123                        "level0 -0.35 2.35 3.15\n"
124                        "level0 -0.4 1.05 1.9\n"
125                        "level0 -0.7 1.45 1.65\n"
126                        "level1 -1.1 1.9 3.8\n"
127                        "averaged1 -1.1 1.9 3.8\n"
128                        "level0 0.35 2.15 2.2\n"
129                        "level0 0.75 0.8 0.8\n"
130                        "level0 0.25 0.25 0.25\n"
131                        "level0 -1 -0.9 -0.5\n"
132                        "level1 -2 -0.05 1.1\n"
133                        "averaged1 -1.55 0.925 2.45\n"
134                        "level0 -1 0.25 0.7\n"
135                        "level0 0.2 1.75 2.2\n"
136                        "level0 -0.3 1.75 2.25\n"
137                        "level0 -0.35 2.35 3.15\n"
138                        "level1 -1 2.05 3.8\n"
139                        "averaged1 -1.36667 1.3 2.9\n"
140                        "level0 -0.4 1.05 1.9\n"
141                        "level0 -0.7 1.45 1.65\n"
142                        "level0 0.35 2.15 2.2\n"
143                        "level0 0.75 0.8 0.8\n"
144                        "level1 -1.1 1.125 3.3\n"
145                        "averaged1 -1.36667 1.04167 2.73333\n"
146                        "level0 0.25 0.25 0.25\n"
147                        "level0 -1 -0.9 -0.5\n"
148                        "level0 -1 0.25 0.7\n"
149                        "level0 0.2 1.75 2.2\n"
150                        "level1 -2 0.425 2.4\n"
151                        "averaged1 -1.36667 1.2 3.16667\n"
152                        "level0 -0.3 1.75 2.25\n"
153                        "level0 -0.35 2.35 3.15\n"
154                        "level0 -0.4 1.05 1.9\n"
155                        "level0 -0.7 1.45 1.65\n"
156                        "level1 -1.1 1.9 3.8\n"
157                        "averaged1 -1.4 1.15 3.16667\n"
158                        "level3 -2 1.225 3.8\n"
159                        "level0 0.35 2.15 2.2\n"
160                        "level0 0.75 0.8 0.8\n"
161                        "level0 0.25 0.25 0.25\n"
162                        "level0 -1 -0.9 -0.5\n"
163                        "level1 -2 -0.05 1.1\n"
164                        "averaged1 -1.7 0.758333 2.43333\n"
165                        "level0 -1 0.25 0.7\n"
166                        "level0 0.2 1.75 3.2\n" );
167 }
168
169 ///////////////////////////////cc.e////////////////////////////////////////
170 #undef prefix_
171
172 \f
173 // Local Variables:
174 // mode: c++
175 // fill-column: 100
176 // comment-column: 40
177 // c-file-style: "senf"
178 // indent-tabs-mode: nil
179 // ispell-local-dictionary: "american"
180 // compile-command: "scons -u test"
181 // End: