OpenWalnut  1.4.0
WHistogram2D_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 WHISTOGRAM2D_TEST_H
26 #define WHISTOGRAM2D_TEST_H
27 
28 #include <cxxtest/TestSuite.h>
29 
30 #include "../WHistogram2D.h"
31 #include "../WLimits.h"
32 #include "../WLogger.h"
33 
34 /**
35  * Unit tests the WHistogramBasic class.
36  */
37 class WHistogram2DTest : 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  WHistogram2D h( 0.0, 1.0, 0.0, 0.1, 10, 10 );
54  TS_ASSERT_EQUALS( h.size(), 100 );
55  }
56 
57  /**
58  * Check normal insertion inside the min max boundaries.
59  */
60  void testInsert( void )
61  {
62  WHistogram2D h( 0.0, 1.0, 0.0, 1.0, 3, 3 );
63  TS_ASSERT_EQUALS( h.size(), 9 );
64  for( size_t i = 0; i < 3; ++i )
65  {
66  for( size_t j = 0; j < 3; ++j )
67  {
68  TS_ASSERT_EQUALS( h( i, j ), 0 );
69  }
70  }
71  h.insert( 0.1, 0.1 );
72  h.insert( 0.1, 0.4 );
73  h.insert( 0.1, 0.7 );
74  h.insert( 0.4, 0.1 );
75  h.insert( 0.4, 0.4 );
76  h.insert( 0.4, 0.7 );
77  h.insert( 0.7, 0.1 );
78  h.insert( 0.7, 0.4 );
79  h.insert( 0.7, 0.7 );
80  for( size_t i = 0; i < 3; ++i )
81  {
82  for( size_t j = 0; j < 3; ++j )
83  {
84  TS_ASSERT_EQUALS( h( i, j ), 1 );
85  }
86  }
87  }
88 
89  /**
90  * If the value is directly on the borderline it counts to the right interval.
91  */
93  {
94  WHistogram2D h( 0.0, 1.0, 0.0, 1.0, 10, 10 );
95  h.insert( 0.0999999, 0.0 );
96  TS_ASSERT_EQUALS( h( 0, 0 ), 1 );
97  h.insert( 0.1, 0.0 );
98  TS_ASSERT_EQUALS( h( 1, 0 ), 1 );
99  h.insert( 0.1001, 0.0 );
100  TS_ASSERT_EQUALS( h( 1, 0 ), 2 );
101  h.insert( 0.39999, 0.39999 );
102  TS_ASSERT_EQUALS( h( 3, 3 ), 1 );
103  }
104 
105  /**
106  * If the minimum is inserted the first bin should be incremented.
107  */
108  void testInsertMin( void )
109  {
110  WHistogram2D h( 0.0, 1.0, 0.0, 1.0, 2, 2 );
111  h.insert( 0.0, 0.0 );
112  TS_ASSERT_EQUALS( h( 0, 0 ), 1 );
113  TS_ASSERT_EQUALS( h( 1, 0 ), 0 );
114  TS_ASSERT_EQUALS( h( 0, 1 ), 0 );
115  TS_ASSERT_EQUALS( h( 1, 1 ), 0 );
116  }
117 
118  /**
119  * If the maximum is inserted the right most interval is used.
120  */
121  void testInsertMax( void )
122  {
123  WHistogram2D h( 0.0, 1.0, 0.0, 1.0, 2, 2 );
124  h.insert( 1.0, 1.0 );
125  TS_ASSERT_EQUALS( h( 0, 0 ), 0 );
126  TS_ASSERT_EQUALS( h( 1, 0 ), 0 );
127  TS_ASSERT_EQUALS( h( 0, 1 ), 0 );
128  TS_ASSERT_EQUALS( h( 1, 1 ), 1 );
129  }
130 
131  /**
132  * If above the maximum values are inserted a warning should be printed and nothing should happen.
133  */
135  {
136  WHistogram2D h( 0.0, 1.0, 0.0, 1.0, 10, 10 );
137  h.insert( 1.0 + wlimits::DBL_EPS, 0.0 );
138  h.insert( 0.0 - wlimits::DBL_EPS, 0.0 );
139  for( size_t i = 0; i < 10; ++i )
140  {
141  for( size_t j = 0; j < 10; ++j )
142  {
143  TS_ASSERT_EQUALS( h( i, j ), 0 );
144  }
145  }
146  }
147 
148  /**
149  * Also for values near the maxium. You may also see #186 for further details.
150  */
151  void testInsertAlmostMax( void )
152  {
153  double max = 10000.000000010001;
154  WHistogram2D h( -2147483646, max, -2147483646, max, 2, 2 );
155  h.insert( 10000, 10000 );
156  h.insert( max - 2.0 * wlimits::FLT_EPS, max - 2.0 * wlimits::FLT_EPS );
157  TS_ASSERT_EQUALS( h( 1, 1 ), 2 );
158  }
159 };
160 
161 #endif // WHISTOGRAM2D_TEST_H