Commit 0045f2a9 authored by Robert Frohl's avatar Robert Frohl
Browse files

[ADD] WHistogram unit test

parent 6a08886a
......@@ -25,7 +25,7 @@
#ifndef WDATASETSCALAR_H
#define WDATASETSCALAR_H
#include "../common/WHistogram.h"
#include "datastructures/WHistogram.h"
#include "WDataSetSingle.h"
......
......@@ -26,8 +26,8 @@
#include <cstring> // memset()
#include <iostream> // test() -> std::cout
#include "WAssert.h"
#include "WLimits.h"
#include "../../common/WAssert.h"
#include "../../common/WLimits.h"
#include "WHistogram.h"
......@@ -54,13 +54,13 @@ WHistogram::WHistogram( boost::shared_ptr< WValueSetBase > valueSet )
}
// create base histogram
m_nInitialBuckets = ( m_maximum - m_minimum ) / minDistance;
m_nInitialBuckets = ( ( m_maximum - m_minimum ) / minDistance ) + 1 ;
m_bucketSize = minDistance;
unsigned int* initialBuckets = new unsigned int[m_nInitialBuckets];
// initialize array to zero
memset( initialBuckets, 0, m_nInitialBuckets * sizeof( unsigned int ) );
//*initialBuckets = { 0 }; // this should works with C++0x (instead memset), TEST IT!
m_initialBuckets = boost::shared_array<unsigned int>( initialBuckets );
m_initialBuckets = boost::shared_array< unsigned int >( initialBuckets );
m_nMappedBuckets = 0;
......@@ -71,11 +71,51 @@ WHistogram::WHistogram( boost::shared_ptr< WValueSetBase > valueSet )
}
}
WHistogram::WHistogram( const WValueSetBase& valueSet )
{
// calculate min max
m_minimum = wlimits::MAX_DOUBLE;
m_maximum = wlimits::MIN_DOUBLE;
double minDistance = wlimits::MAX_DOUBLE;
for( size_t i = 0; i != valueSet.size(); ++i )
{
double tmp = valueSet.getScalarDouble( i );
m_maximum = m_maximum < tmp ? tmp : m_maximum;
m_minimum = m_minimum > tmp ? tmp : m_minimum;
if( m_minimum != tmp && m_minimum != wlimits::MAX_DOUBLE )
{
minDistance = tmp - m_minimum < minDistance ? tmp - m_minimum : minDistance;
}
if( m_maximum != tmp && m_maximum != wlimits::MIN_DOUBLE )
{
minDistance = m_maximum - tmp < minDistance ? m_maximum - tmp : minDistance;
}
}
// create base histogram
m_nInitialBuckets = ( ( m_maximum - m_minimum ) / minDistance ) + 1 ;
m_bucketSize = minDistance;
unsigned int* initialBuckets = new unsigned int[m_nInitialBuckets];
// initialize array to zero
memset( initialBuckets, 0, m_nInitialBuckets * sizeof( unsigned int ) );
//*initialBuckets = { 0 }; // this should works with C++0x (instead memset), TEST IT!
m_initialBuckets = boost::shared_array< unsigned int >( initialBuckets );
m_nMappedBuckets = 0;
for( size_t i = 0; i < valueSet.size(); ++i )
{
double tmp = valueSet.getScalarDouble( i );
increment( tmp );
}
}
WHistogram::WHistogram( const WHistogram& histogram, double intervalSize )
{
// copy constructor
m_nInitialBuckets = histogram.getNInitialBuckets();
m_initialBuckets = boost::shared_array<unsigned int>( histogram.getInitialBuckets() );
m_initialBuckets = boost::shared_array< unsigned int >( histogram.getInitialBuckets() );
m_bucketSize = histogram.getBucketSize();
m_nMappedBuckets = 0;
......@@ -94,7 +134,7 @@ WHistogram::~WHistogram()
{
}
boost::shared_array<unsigned int> WHistogram::getInitialBuckets() const
boost::shared_array< unsigned int > WHistogram::getInitialBuckets() const
{
return m_initialBuckets;
}
......@@ -118,7 +158,18 @@ void WHistogram::increment( double value )
unsigned int WHistogram::setInterval( double intervalSize )
{
calculateMapping( intervalSize );
if( m_bucketSize == intervalSize )
{
if( m_mappedBuckets )
{
m_mappedBuckets.reset();
m_nMappedBuckets = 0;
}
}
else
{
calculateMapping( intervalSize );
}
return m_nMappedBuckets;
}
......@@ -137,7 +188,7 @@ void WHistogram::calculateMapping( double intervalSize )
unsigned int* mappedBuckets = new unsigned int[m_nMappedBuckets];
memset( mappedBuckets, 0, m_nMappedBuckets * sizeof( unsigned int ) );
//*mappedBuckets = { 0 }; // works with C++0x
boost::scoped_array<unsigned int> scoped( mappedBuckets );
boost::scoped_array< unsigned int > scoped( mappedBuckets );
m_mappedBuckets.swap( scoped );
unsigned int index = 0;
for( unsigned int i = 0; i != m_nInitialBuckets; ++i )
......
......@@ -33,7 +33,7 @@
#include <boost/scoped_array.hpp>
#include <boost/shared_array.hpp>
#include "../dataHandler/WValueSet.h"
#include "../WValueSet.h"
/**
* Used to track (later: display) the occurrence frequencies of values in a value set.
......@@ -59,7 +59,7 @@ class WHistogram
/**
* Pointer to all initial buckets of the histogram.
**/
boost::shared_array<unsigned int> m_initialBuckets;
boost::shared_array< unsigned int > m_initialBuckets;
/**
* Number of buckets in the initial histogram.
......@@ -69,7 +69,7 @@ class WHistogram
/**
* Pointer to all the buckets in the mapped histogram.
**/
boost::scoped_array<unsigned int> m_mappedBuckets;
boost::scoped_array< unsigned int > m_mappedBuckets;
/**
* Tracks the number of a buckets in the mapped histogram.
......@@ -97,7 +97,7 @@ class WHistogram
*
* \return m_initialBuckets
**/
boost::shared_array<unsigned int> getInitialBuckets() const;
boost::shared_array< unsigned int > getInitialBuckets() const;
/**
* Return the number of initial buckets.
......@@ -121,6 +121,7 @@ class WHistogram
* \param nBuckets number of buckets this histogram should display
**/
explicit WHistogram( boost::shared_ptr< WValueSetBase > valueSet );
explicit WHistogram( const WValueSetBase& valueSet );
/**
* Copy constructor. If another interval size is given setInterval() is called and a
......
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WHISTOGRAM_TEST_H
#define WHISTOGRAM_TEST_H
//#include <stdint.h>
#include <vector>
#include <cxxtest/TestSuite.h>
#include "../../WValueSet.h"
//#include "../../WDataHandlerEnums.h"
//#include "../../WValueSetBase.h"
#include "../WHistogram.h"
/**
* Test WHistogram
**/
class WHistogramTest : public CxxTest::TestSuite
{
public:
/**
* Test instantiation
**/
void testInstantiation( void )
{
double a[2] = { 0.0, 3.1415 };
const std::vector< double > v( a, a + sizeof( a ) / sizeof( double ) );
WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
TS_ASSERT_THROWS_NOTHING( WHistogram hist( *valueSet ) );
boost::shared_ptr< WValueSet< double > > vsPtr( valueSet );
TS_ASSERT_THROWS_NOTHING( WHistogram hist( vsPtr ) );
}
/**
* Test operator[]
**/
void testOperator( void )
{
double a[4] = { 0.0, 1.0, 1.0, 4.0 };
const std::vector< double > v( a, a + sizeof( a ) / sizeof( double ) );
WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
WHistogram hist( *valueSet );
TS_ASSERT_EQUALS( hist[0], 1 ); // 0.0
TS_ASSERT_EQUALS( hist[1], 2 ); // 1.0
TS_ASSERT_EQUALS( hist[2], 0 ); // 2.0
TS_ASSERT_EQUALS( hist[3], 0 ); // 3.0
TS_ASSERT_EQUALS( hist[4], 1 ); // 4.0
}
/**
* Test at()
**/
void testAt( void )
{
double a[4] = { 0.0, 1.0, 1.0, 4.0 };
const std::vector< double > v( a, a + sizeof( a ) / sizeof( double ) );
WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
WHistogram hist( *valueSet );
TS_ASSERT_EQUALS( hist.at(0), 1 ); // 0.0
TS_ASSERT_EQUALS( hist.at(1), 2 ); // 1.0
TS_ASSERT_EQUALS( hist.at(2), 0 ); // 2.0
TS_ASSERT_EQUALS( hist.at(3), 0 ); // 3.0
TS_ASSERT_EQUALS( hist.at(4), 1 ); // 4.0
TS_ASSERT_THROWS_ANYTHING( hist.at( 5 ) );
}
/**
* Test getMin(), getMax()
**/
void testMinMax( void )
{
double a[4] = { 0.0, -5.0, 1.0, 2.0 };
const std::vector< double > v( a, a + sizeof( a ) / sizeof( double ) );
WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
WHistogram hist( *valueSet );
TS_ASSERT_EQUALS( hist.getMin(), -5.0 );
TS_ASSERT_EQUALS( hist.getMax(), 2.0 );
}
/**
* Test size(), setInterval()
**/
void testSizeSetInterval( void )
{
double a[4] = { 0.0, 4.0, 1.0, 2.0 };
const std::vector< double > v( a, a + sizeof( a ) / sizeof( double ) );
WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
WHistogram hist( *valueSet );
TS_ASSERT_EQUALS( hist.size(), 5 ); // 0.0, 1.0, 2.0, 3.0 and 4.0
TS_ASSERT_EQUALS( hist.setInterval( 2 ), 3 ); // setInterval() returns new size()
TS_ASSERT_EQUALS( hist.at(0), 2 ); // 0.0, 1.0
TS_ASSERT_EQUALS( hist.at(1), 1 ); // 2.0, 3.0
TS_ASSERT_EQUALS( hist.at(2), 1 ); // 4.0
hist.setInterval( 1 );
TS_ASSERT_EQUALS( hist.size(), 5 );
}
};
#endif // WHISTOGRAM_TEST_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment