OpenWalnut  1.4.0
WValueSetHistogram_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 WVALUESETHISTOGRAM_TEST_H
26 #define WVALUESETHISTOGRAM_TEST_H
27 
28 #include <vector>
29 
30 #include <cxxtest/TestSuite.h>
31 
32 #include "../../WValueSet.h"
33 
34 #include "../WValueSetHistogram.h"
35 
36 /**
37  * Test WValueSetHistogram
38  **/
39 class WValueSetHistogramTest : public CxxTest::TestSuite
40 {
41  public:
42  /**
43  * Test instantiation
44  */
45  void testInstantiation( void )
46  {
47  // create some test data
48  double a[2] = { 0.0, 3.1415 };
49  const boost::shared_ptr< std::vector< double > > v =
50  boost::shared_ptr< std::vector< double > >(
51  new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
52  WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
53 
54  // test both constructors with default bucket size
55  TS_ASSERT_THROWS_NOTHING( WValueSetHistogram hist( *valueSet ) );
56  boost::shared_ptr< WValueSet< double > > vsPtr( valueSet );
57  TS_ASSERT_THROWS_NOTHING( WValueSetHistogram hist( vsPtr ) );
58  }
59 
60  /**
61  * Test operator[]
62  */
63  void testOperator( void )
64  {
65  // create some test data
66  double a[4] = { 0.0, 1.0, 1.0, 4.0 };
67  const boost::shared_ptr< std::vector< double > > v =
68  boost::shared_ptr< std::vector< double > >(
69  new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
70  WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
71 
72  // create the histogram
73  WValueSetHistogram hist( *valueSet, 5 );
74  TS_ASSERT_EQUALS( hist[0], 1 ); // 0.0
75  TS_ASSERT_EQUALS( hist[1], 2 ); // 1.0
76  TS_ASSERT_EQUALS( hist[2], 0 ); // 2.0
77  TS_ASSERT_EQUALS( hist[3], 0 ); // 3.0
78  TS_ASSERT_EQUALS( hist[4], 1 ); // 4.0
79  }
80 
81  /**
82  * Test at()
83  */
84  void testAt( void )
85  {
86  // create some test data
87  double a[4] = { 0.0, 1.0, 1.0, 4.0 };
88  const boost::shared_ptr< std::vector< double > > v =
89  boost::shared_ptr< std::vector< double > >(
90  new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
91  WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
92 
93  // create histogram
94  WValueSetHistogram hist( *valueSet, 5 );
95 
96  // test access with valid indices
97  TS_ASSERT_EQUALS( hist.at( 0 ), 1 ); // 0.0
98  TS_ASSERT_EQUALS( hist.at( 1 ), 2 ); // 1.0
99  TS_ASSERT_EQUALS( hist.at( 2 ), 0 ); // 2.0
100  TS_ASSERT_EQUALS( hist.at( 3 ), 0 ); // 3.0
101  TS_ASSERT_EQUALS( hist.at( 4 ), 1 ); // 4.0
102 
103  // test access with invalid index
104  TS_ASSERT_THROWS_ANYTHING( hist.at( 5 ) );
105  }
106 
107  /**
108  * Test getMinimum(), getMaximum()
109  */
110  void testMinMax( void )
111  {
112  // create some test data
113  double a[4] = { 0.0, -5.0, 1.0, 2.0 };
114  const boost::shared_ptr< std::vector< double > > v =
115  boost::shared_ptr< std::vector< double > >(
116  new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
117  WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
118 
119  // create histogram
120  WValueSetHistogram hist( *valueSet );
121 
122  TS_ASSERT_EQUALS( hist.getMinimum(), -5.0 );
123  TS_ASSERT_EQUALS( hist.getMaximum(), 2.0 );
124  }
125 
126  /**
127  * Test size(), and getBucketSize()
128  */
129  void testSizes( void )
130  {
131  // create some test data
132  double a[5] = { 0.0, 4.0, 1.0, 2.0, 1.0 };
133  const boost::shared_ptr< std::vector< double > > v =
134  boost::shared_ptr< std::vector< double > >(
135  new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
136  WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
137 
138  // create histogram
139  WValueSetHistogram hist( *valueSet, 5 );
140 
141  TS_ASSERT_EQUALS( hist.size(), 5 ); // 0.0, 1.0, 2.0, 3.0 and 4.0
142  TS_ASSERT_EQUALS( hist.getBucketSize(), 1.0 ); // 0.0, 1.0, 2.0, 3.0 and 4.0
143  }
144 
145  /**
146  * Test getIndexForValue()
147  */
148  void testIndex( void )
149  {
150  // create some test data
151  double a[5] = { 0.0, 4.0, 1.0, 2.0, 1.0 };
152  const boost::shared_ptr< std::vector< double > > v =
153  boost::shared_ptr< std::vector< double > >(
154  new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
155  WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
156 
157  // create histogram
158  WValueSetHistogram hist( *valueSet, 5 );
159  // 0 = [0, 1) = 1
160  // 1 = [1, 2) = 2
161  // 2 = [2, 3) = 1
162  // 3 = [3, 4) = 0
163  // 4 = [4, inf) = 1
164 
165  TS_ASSERT_EQUALS( hist.getIndexForValue( 4.0 ), 4 );
166  TS_ASSERT_EQUALS( hist.getIndexForValue( 3.999 ), 3 );
167  TS_ASSERT_EQUALS( hist.getIndexForValue( 0.0 ), 0 );
168  TS_ASSERT_EQUALS( hist.getIndexForValue( 122.0 ), 4 ); // test values above maximum
169  TS_ASSERT_EQUALS( hist.getIndexForValue( -122.0 ), 0 ); // test values below minumum
170  }
171 
172  /**
173  * Test accumulate
174  */
175  void testAccum( void )
176  {
177  // create some test data
178  double a[5] = { 0.0, 4.0, 1.0, 2.0, 1.0 };
179  const boost::shared_ptr< std::vector< double > > v =
180  boost::shared_ptr< std::vector< double > >(
181  new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
182  WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
183 
184  // create histogram
185  WValueSetHistogram hist( *valueSet, 5 );
186  std::cout << hist << std::endl;
187 
188  TS_ASSERT_EQUALS( hist.accumulate( 0, 2 ), 3 );
189  TS_ASSERT_EQUALS( hist.accumulate( 2, 0 ), 3 ); // it also needs to handle switched indices
190  TS_ASSERT_EQUALS( hist.accumulate( 2, 2 ), 0 ); // exclude second index properly?
191  TS_ASSERT( hist.accumulate( 2, 2 ) != hist[ 2 ] ); // exclude second index properly?
192 
193  TS_ASSERT_THROWS_ANYTHING( hist.accumulate( 0, 123 ) );
194  }
195 
196  /**
197  * Test copy construction.
198  **/
200  {
201  // create some test data
202  double a[5] = { 0.0, 4.0, 1.0, 2.0, 1.0 };
203  const boost::shared_ptr< std::vector< double > > v =
204  boost::shared_ptr< std::vector< double > >(
205  new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
206  WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
207 
208  // create histogram
209  WValueSetHistogram hist( *valueSet, 5 );
210  WValueSetHistogram hist2 = hist; // copy constructor
211  WValueSetHistogram hist3( *valueSet, 10 );
212 
213  // is everything the same?
214  TS_ASSERT_EQUALS( hist.m_minimum, hist2.m_minimum );
215  TS_ASSERT_EQUALS( hist.m_maximum, hist2.m_maximum );
216  TS_ASSERT_EQUALS( hist.m_initialBucketSize, hist2.m_initialBucketSize );
217  TS_ASSERT_EQUALS( hist.m_initialBuckets.get(), hist2.m_initialBuckets.get() ); // initial buckets must be the same as it is a shared array
218  TS_ASSERT_EQUALS( hist.m_nInitialBuckets, hist2.m_nInitialBuckets );
219  TS_ASSERT_EQUALS( hist.m_mappedBuckets.get(), hist2.m_mappedBuckets.get() );
220  TS_ASSERT_EQUALS( hist.m_nMappedBuckets, hist2.m_nMappedBuckets );
221  TS_ASSERT_EQUALS( hist.m_mappedBucketSize, hist2.m_mappedBucketSize );
222 
223  // test copy assignment
224  hist = hist3;
225  TS_ASSERT_EQUALS( hist.m_minimum, hist3.m_minimum );
226  TS_ASSERT_EQUALS( hist.m_maximum, hist3.m_maximum );
227  TS_ASSERT_EQUALS( hist.m_initialBucketSize, hist3.m_initialBucketSize );
228  TS_ASSERT_EQUALS( hist.m_initialBuckets.get(), hist3.m_initialBuckets.get() ); // initial buckets must be the same as it is a shared array
229  TS_ASSERT_EQUALS( hist.m_nInitialBuckets, hist3.m_nInitialBuckets );
230  TS_ASSERT_EQUALS( hist.m_mappedBuckets, hist3.m_mappedBuckets );
231  TS_ASSERT_EQUALS( hist.m_nMappedBuckets, hist3.m_nMappedBuckets );
232  TS_ASSERT_EQUALS( hist.m_nMappedBuckets, 10 );
233  TS_ASSERT_EQUALS( hist.m_mappedBucketSize, hist3.m_mappedBucketSize );
234  }
235 
236  /**
237  * Test copy construction with changed interval sizes.
238  **/
240  {
241  // create some test data
242  double a[5] = { 0.0, 4.0, 1.0, 2.0, 1.0 };
243  const boost::shared_ptr< std::vector< double > > v =
244  boost::shared_ptr< std::vector< double > >(
245  new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
246  WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
247 
248  // create histogram
249  WValueSetHistogram hist( *valueSet, 4 );
250  WValueSetHistogram hist2( hist, 2 ); // create a copy of hist but change the number of intervals.
251  TS_ASSERT_THROWS_ANYTHING( WValueSetHistogram hist2( hist, 1 ) ); // number of buckets must be at least 1
252 
253  // it needs to keep the original initialBucket stuff
254  TS_ASSERT_EQUALS( hist.m_minimum, hist2.m_minimum );
255  TS_ASSERT_EQUALS( hist.m_maximum, hist2.m_maximum );
256  TS_ASSERT_EQUALS( hist.m_initialBucketSize, hist2.m_initialBucketSize );
257  TS_ASSERT_EQUALS( hist.m_initialBuckets.get(), hist2.m_initialBuckets.get() ); // initial buckets must be the same as it is a shared array
258  TS_ASSERT_EQUALS( hist.m_nInitialBuckets, hist2.m_nInitialBuckets );
259 
260  // test the mapped stuff
261  TS_ASSERT_EQUALS( hist2.m_mappedBucketSize, 4.0 );
262  TS_ASSERT_EQUALS( hist2.m_nMappedBuckets, 2 );
263  TS_ASSERT( hist2.m_mappedBuckets != hist.m_mappedBuckets );
264 
265  // test values
266  TS_ASSERT_EQUALS( hist2.at( 0 ), 4 ); // 0.0, 1.0, 2.0 and 1.0
267  TS_ASSERT_EQUALS( hist2.at( 1 ), 1 ); // 4.0
268  }
269 };
270 
271 #endif // WVALUESETHISTOGRAM_TEST_H