OpenWalnut  1.4.0
WValueSetHistogram_test.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WVALUESETHISTOGRAM_TEST_H
00026 #define WVALUESETHISTOGRAM_TEST_H
00027 
00028 #include <vector>
00029 
00030 #include <cxxtest/TestSuite.h>
00031 
00032 #include "../../WValueSet.h"
00033 
00034 #include "../WValueSetHistogram.h"
00035 
00036 /**
00037  * Test WValueSetHistogram
00038  **/
00039 class WValueSetHistogramTest : public CxxTest::TestSuite
00040 {
00041     public:
00042         /**
00043          * Test instantiation
00044          */
00045         void testInstantiation( void )
00046         {
00047             // create some test data
00048             double a[2] = { 0.0, 3.1415 };
00049             const boost::shared_ptr< std::vector< double > > v =
00050                 boost::shared_ptr< std::vector< double > >(
00051                     new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
00052             WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
00053 
00054             // test both constructors with default bucket size
00055             TS_ASSERT_THROWS_NOTHING( WValueSetHistogram hist( *valueSet ) );
00056             boost::shared_ptr< WValueSet< double > > vsPtr( valueSet );
00057             TS_ASSERT_THROWS_NOTHING( WValueSetHistogram hist( vsPtr ) );
00058         }
00059 
00060         /**
00061          * Test operator[]
00062          */
00063         void testOperator( void )
00064         {
00065             // create some test data
00066             double a[4] = { 0.0, 1.0, 1.0, 4.0 };
00067             const boost::shared_ptr< std::vector< double > > v =
00068                 boost::shared_ptr< std::vector< double > >(
00069                     new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
00070             WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
00071 
00072             // create the histogram
00073             WValueSetHistogram hist( *valueSet, 5 );
00074             TS_ASSERT_EQUALS( hist[0], 1 ); // 0.0
00075             TS_ASSERT_EQUALS( hist[1], 2 ); // 1.0
00076             TS_ASSERT_EQUALS( hist[2], 0 ); // 2.0
00077             TS_ASSERT_EQUALS( hist[3], 0 ); // 3.0
00078             TS_ASSERT_EQUALS( hist[4], 1 ); // 4.0
00079         }
00080 
00081         /**
00082          * Test at()
00083          */
00084         void testAt( void )
00085         {
00086             // create some test data
00087             double a[4] = { 0.0, 1.0, 1.0, 4.0 };
00088             const boost::shared_ptr< std::vector< double > > v =
00089                 boost::shared_ptr< std::vector< double > >(
00090                     new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
00091             WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
00092 
00093             // create histogram
00094             WValueSetHistogram hist( *valueSet, 5 );
00095 
00096             // test access with valid indices
00097             TS_ASSERT_EQUALS( hist.at( 0 ), 1 ); // 0.0
00098             TS_ASSERT_EQUALS( hist.at( 1 ), 2 ); // 1.0
00099             TS_ASSERT_EQUALS( hist.at( 2 ), 0 ); // 2.0
00100             TS_ASSERT_EQUALS( hist.at( 3 ), 0 ); // 3.0
00101             TS_ASSERT_EQUALS( hist.at( 4 ), 1 ); // 4.0
00102 
00103             // test access with invalid index
00104             TS_ASSERT_THROWS_ANYTHING( hist.at( 5 ) );
00105         }
00106 
00107         /**
00108          * Test getMinimum(), getMaximum()
00109          */
00110         void testMinMax( void )
00111         {
00112             // create some test data
00113             double a[4] = { 0.0, -5.0, 1.0, 2.0 };
00114             const boost::shared_ptr< std::vector< double > > v =
00115                 boost::shared_ptr< std::vector< double > >(
00116                     new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
00117             WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
00118 
00119             // create histogram
00120             WValueSetHistogram hist( *valueSet );
00121 
00122             TS_ASSERT_EQUALS( hist.getMinimum(), -5.0 );
00123             TS_ASSERT_EQUALS( hist.getMaximum(), 2.0 );
00124         }
00125 
00126         /**
00127          * Test size(), and getBucketSize()
00128          */
00129         void testSizes( void )
00130         {
00131             // create some test data
00132             double a[5] = { 0.0, 4.0, 1.0, 2.0, 1.0 };
00133             const boost::shared_ptr< std::vector< double > > v =
00134                 boost::shared_ptr< std::vector< double > >(
00135                     new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
00136             WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
00137 
00138             // create histogram
00139             WValueSetHistogram hist( *valueSet, 5 );
00140 
00141             TS_ASSERT_EQUALS( hist.size(), 5 ); // 0.0, 1.0, 2.0, 3.0 and 4.0
00142             TS_ASSERT_EQUALS( hist.getBucketSize(), 1.0 ); // 0.0, 1.0, 2.0, 3.0 and 4.0
00143         }
00144 
00145         /**
00146          * Test getIndexForValue()
00147          */
00148         void testIndex( void )
00149         {
00150             // create some test data
00151             double a[5] = { 0.0, 4.0, 1.0, 2.0, 1.0 };
00152             const boost::shared_ptr< std::vector< double > > v =
00153                 boost::shared_ptr< std::vector< double > >(
00154                     new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
00155             WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
00156 
00157             // create histogram
00158             WValueSetHistogram hist( *valueSet, 5 );
00159             // 0 = [0, 1) = 1
00160             // 1 = [1, 2) = 2
00161             // 2 = [2, 3) = 1
00162             // 3 = [3, 4) = 0
00163             // 4 = [4, inf) = 1
00164 
00165             TS_ASSERT_EQUALS( hist.getIndexForValue( 4.0 ), 4 );
00166             TS_ASSERT_EQUALS( hist.getIndexForValue( 3.999 ), 3 );
00167             TS_ASSERT_EQUALS( hist.getIndexForValue( 0.0 ), 0 );
00168             TS_ASSERT_EQUALS( hist.getIndexForValue( 122.0 ), 4 );  // test values above maximum
00169             TS_ASSERT_EQUALS( hist.getIndexForValue( -122.0 ), 0 ); // test values below minumum
00170         }
00171 
00172         /**
00173          * Test accumulate
00174          */
00175         void testAccum( void )
00176         {
00177             // create some test data
00178             double a[5] = { 0.0, 4.0, 1.0, 2.0, 1.0 };
00179             const boost::shared_ptr< std::vector< double > > v =
00180                 boost::shared_ptr< std::vector< double > >(
00181                     new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
00182             WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
00183 
00184             // create histogram
00185             WValueSetHistogram hist( *valueSet, 5 );
00186             std::cout << hist << std::endl;
00187 
00188             TS_ASSERT_EQUALS( hist.accumulate( 0, 2 ), 3 );
00189             TS_ASSERT_EQUALS( hist.accumulate( 2, 0 ), 3 ); // it also needs to handle switched indices
00190             TS_ASSERT_EQUALS( hist.accumulate( 2, 2 ), 0 ); // exclude second index properly?
00191             TS_ASSERT( hist.accumulate( 2, 2 ) != hist[ 2 ] ); // exclude second index properly?
00192 
00193             TS_ASSERT_THROWS_ANYTHING( hist.accumulate( 0, 123 ) );
00194         }
00195 
00196         /**
00197          * Test copy construction.
00198          **/
00199         void testCopyWithoutIntervalChanges( void )
00200         {
00201             // create some test data
00202             double a[5] = { 0.0, 4.0, 1.0, 2.0, 1.0 };
00203             const boost::shared_ptr< std::vector< double > > v =
00204                 boost::shared_ptr< std::vector< double > >(
00205                     new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
00206             WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
00207 
00208             // create histogram
00209             WValueSetHistogram hist( *valueSet, 5 );
00210             WValueSetHistogram hist2 = hist;    // copy constructor
00211             WValueSetHistogram hist3( *valueSet, 10 );
00212 
00213             // is everything the same?
00214             TS_ASSERT_EQUALS( hist.m_minimum, hist2.m_minimum );
00215             TS_ASSERT_EQUALS( hist.m_maximum, hist2.m_maximum );
00216             TS_ASSERT_EQUALS( hist.m_initialBucketSize, hist2.m_initialBucketSize );
00217             TS_ASSERT_EQUALS( hist.m_initialBuckets.get(), hist2.m_initialBuckets.get() ); // initial buckets must be the same as it is a shared array
00218             TS_ASSERT_EQUALS( hist.m_nInitialBuckets, hist2.m_nInitialBuckets );
00219             TS_ASSERT_EQUALS( hist.m_mappedBuckets.get(), hist2.m_mappedBuckets.get() );
00220             TS_ASSERT_EQUALS( hist.m_nMappedBuckets, hist2.m_nMappedBuckets );
00221             TS_ASSERT_EQUALS( hist.m_mappedBucketSize, hist2.m_mappedBucketSize );
00222 
00223             // test copy assignment
00224             hist = hist3;
00225             TS_ASSERT_EQUALS( hist.m_minimum, hist3.m_minimum );
00226             TS_ASSERT_EQUALS( hist.m_maximum, hist3.m_maximum );
00227             TS_ASSERT_EQUALS( hist.m_initialBucketSize, hist3.m_initialBucketSize );
00228             TS_ASSERT_EQUALS( hist.m_initialBuckets.get(), hist3.m_initialBuckets.get() ); // initial buckets must be the same as it is a shared array
00229             TS_ASSERT_EQUALS( hist.m_nInitialBuckets, hist3.m_nInitialBuckets );
00230             TS_ASSERT_EQUALS( hist.m_mappedBuckets, hist3.m_mappedBuckets );
00231             TS_ASSERT_EQUALS( hist.m_nMappedBuckets, hist3.m_nMappedBuckets );
00232             TS_ASSERT_EQUALS( hist.m_nMappedBuckets, 10 );
00233             TS_ASSERT_EQUALS( hist.m_mappedBucketSize, hist3.m_mappedBucketSize );
00234         }
00235 
00236         /**
00237          * Test copy construction with changed interval sizes.
00238          **/
00239         void testCopyWithIntervalChanges( void )
00240         {
00241             // create some test data
00242             double a[5] = { 0.0, 4.0, 1.0, 2.0, 1.0 };
00243             const boost::shared_ptr< std::vector< double > > v =
00244                 boost::shared_ptr< std::vector< double > >(
00245                     new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
00246             WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
00247 
00248             // create histogram
00249             WValueSetHistogram hist( *valueSet, 4 );
00250             WValueSetHistogram hist2( hist, 2 );    // create a copy of hist but change the number of intervals.
00251             TS_ASSERT_THROWS_ANYTHING( WValueSetHistogram hist2( hist, 1 ) ); // number of buckets must be at least 1
00252 
00253             // it needs to keep the original initialBucket stuff
00254             TS_ASSERT_EQUALS( hist.m_minimum, hist2.m_minimum );
00255             TS_ASSERT_EQUALS( hist.m_maximum, hist2.m_maximum );
00256             TS_ASSERT_EQUALS( hist.m_initialBucketSize, hist2.m_initialBucketSize );
00257             TS_ASSERT_EQUALS( hist.m_initialBuckets.get(), hist2.m_initialBuckets.get() ); // initial buckets must be the same as it is a shared array
00258             TS_ASSERT_EQUALS( hist.m_nInitialBuckets, hist2.m_nInitialBuckets );
00259 
00260             // test the mapped stuff
00261             TS_ASSERT_EQUALS( hist2.m_mappedBucketSize, 4.0 );
00262             TS_ASSERT_EQUALS( hist2.m_nMappedBuckets, 2 );
00263             TS_ASSERT( hist2.m_mappedBuckets != hist.m_mappedBuckets );
00264 
00265             // test values
00266             TS_ASSERT_EQUALS( hist2.at( 0 ), 4 );   // 0.0, 1.0, 2.0 and 1.0
00267             TS_ASSERT_EQUALS( hist2.at( 1 ), 1 );   // 4.0
00268         }
00269 };
00270 
00271 #endif  // WVALUESETHISTOGRAM_TEST_H