OpenWalnut  1.4.0
WHistogramBasic_test.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WHISTOGRAMBASIC_TEST_H
26 #define WHISTOGRAMBASIC_TEST_H
27 
28 #include <cxxtest/TestSuite.h>
29 
30 #include "../WHistogramBasic.h"
31 #include "../WLimits.h"
32 #include "../WLogger.h"
33 
34 /**
35  * Unit tests the WHistogramBasic class.
36  */
37 class WHistogramBasicTest : public CxxTest::TestSuite
38 {
39 public:
40  /**
41  * Setup logger and other stuff for each test.
42  */
43  void setUp()
44  {
46  }
47 
48  /**
49  * Check when nothing was inserted every thing is empty.
50  */
51  void testInitialization( void )
52  {
53  WHistogramBasic h( 0.0, 1.0 );
54  TS_ASSERT_EQUALS( h.size(), 1000 );
55  TS_ASSERT_EQUALS( h.valuesSize(), 0 );
56  }
57 
58  /**
59  * Check normal insertion inside the min max boundaries.
60  */
61  void testInsert( void )
62  {
63  WHistogramBasic h( 0.0, 1.0 );
64  h.insert( 0.7234 );
65  TS_ASSERT_EQUALS( h.size(), 1000 );
66  TS_ASSERT_EQUALS( h.valuesSize(), 1 );
67  TS_ASSERT_EQUALS( h[723], 1 );
68  }
69 
70  /**
71  * If the value is directly on the borderline it counts to the right interval.
72  */
74  {
75  WHistogramBasic h( 0.0, 1.0 );
76  h.insert( 0.001 );
77  TS_ASSERT_EQUALS( h[1], 1 );
78  h.insert( 0.0039999 );
79  TS_ASSERT_EQUALS( h[3], 1 );
80  h.insert( 0.0070001 );
81  TS_ASSERT_EQUALS( h[7], 1 );
82  }
83 
84  /**
85  * If the minimum is inserted the first bin should be incremented.
86  */
87  void testInsertMin( void )
88  {
89  WHistogramBasic h( 0.0, 1.0 );
90  h.insert( 0.0 );
91  TS_ASSERT_EQUALS( h[0], 1 );
92  TS_ASSERT_EQUALS( h[1], 0 );
93  }
94 
95  /**
96  * If the maximum is inserted the right most interval is used.
97  */
98  void testInsertMax( void )
99  {
100  WHistogramBasic h( 0.0, 1.0 );
101  h.insert( 0.0 );
102  h.insert( 1.0 );
103  TS_ASSERT_EQUALS( h[999], 1 );
104  TS_ASSERT_EQUALS( h[0], 1 );
105  }
106 
107  /**
108  * If above the maximum values are inserted a warning should be printed and nothing should happen.
109  */
111  {
112  WHistogramBasic h( 0.0, 1.0 );
113  h.insert( 1.0 + wlimits::DBL_EPS );
114  h.insert( 0.0 - wlimits::DBL_EPS );
115  for( size_t i = 0; i < h.size(); ++i )
116  {
117  TS_ASSERT_EQUALS( h[i], 0 );
118  }
119  }
120 
121  /**
122  * For each insert this number should increase by one.
123  */
125  {
126  WHistogramBasic h( 0.0, 1.0 );
127  for( size_t i = 0; i < h.size(); ++i )
128  {
129  TS_ASSERT_EQUALS( h[i], 0 );
130  }
131  h.insert( 0.0 );
132  h.insert( 0.0 );
133  TS_ASSERT_EQUALS( h.valuesSize(), 2 );
134  }
135 
136  /**
137  * Also for values near the maxium. You may also see #186 for further details.
138  */
139  void testInsertAlmostMax( void )
140  {
141  double max = 10000.000000010001;
142  WHistogramBasic h( -2147483646, max );
143  h.insert( 10000 );
144  h.insert( max - 2.0 * wlimits::FLT_EPS );
145  TS_ASSERT_EQUALS( h[999], 2 );
146  }
147 };
148 
149 #endif // WHISTOGRAMBASIC_TEST_H