Commit dec823bf authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum

[CHANGE] - adopted WValueSetHistogram_test to meet the new interface...

[CHANGE] - adopted WValueSetHistogram_test to meet the new interface reqirements. Now works perfectly.
parent d82ca2af
......@@ -30,12 +30,12 @@
#include "WValueSetHistogram.h"
WValueSetHistogram::WValueSetHistogram( boost::shared_ptr< WValueSetBase > valueSet ):
WValueSetHistogram::WValueSetHistogram( boost::shared_ptr< WValueSetBase > valueSet, size_t buckets ):
m_minimum( valueSet->getMinimumValue() ),
m_maximum( valueSet->getMaximumValue() )
{
// create base histogram
m_nInitialBuckets = 100;
m_nInitialBuckets = buckets - 1;
m_initialBucketSize = ( m_maximum - m_minimum ) / static_cast< double >( m_nInitialBuckets );
WAssert( m_initialBucketSize > 0.0, "WValueSetHistogram::WValueSetHistogram() : m_initialBucketSize to small." );
......@@ -64,12 +64,12 @@ WValueSetHistogram::WValueSetHistogram( boost::shared_ptr< WValueSetBase > value
}
}
WValueSetHistogram::WValueSetHistogram( const WValueSetBase& valueSet ):
WValueSetHistogram::WValueSetHistogram( const WValueSetBase& valueSet, size_t buckets ):
m_minimum( valueSet.getMinimumValue() ),
m_maximum( valueSet.getMaximumValue() )
{
// create base histogram
m_nInitialBuckets = 100;
m_nInitialBuckets = buckets - 1;
m_initialBucketSize = ( m_maximum - m_minimum ) / static_cast< double >( m_nInitialBuckets );
WAssert( m_initialBucketSize > 0.0, "WValueSetHistogram::WValueSetHistogram() : m_initialBucketSize to small." );
......@@ -103,7 +103,7 @@ WValueSetHistogram::WValueSetHistogram( const WValueSetHistogram& histogram, siz
m_maximum( histogram.m_maximum ),
m_initialBucketSize( histogram.m_initialBucketSize ),
m_initialBuckets( histogram.m_initialBuckets ),
m_nInitialBuckets( histogram.m_nInitialBuckets - 1 ), // remove the last, open interval
m_nInitialBuckets( histogram.m_nInitialBuckets ),
m_mappedBuckets( histogram.m_mappedBuckets ),
m_nMappedBuckets( histogram.m_nMappedBuckets ),
m_mappedBucketSize( histogram.m_mappedBucketSize )
......@@ -129,7 +129,6 @@ WValueSetHistogram::WValueSetHistogram( const WValueSetHistogram& histogram, siz
// NOTE: as all the intervals are right-open, we need an additional slot in our array for the last interval [m_maximum,\infinity). For the
// calculation of interval sizes, the value must not be incremented
m_nMappedBuckets++;
m_nInitialBuckets++;
size_t* mappedBuckets = new size_t[ m_nMappedBuckets ];
memset( mappedBuckets, 0, m_nMappedBuckets * sizeof( size_t ) );
......@@ -147,6 +146,25 @@ WValueSetHistogram::WValueSetHistogram( const WValueSetHistogram& histogram, siz
}
}
WValueSetHistogram& WValueSetHistogram::operator=( const WValueSetHistogram& other )
{
if ( this != &other ) // protect against invalid self-assignment
{
m_minimum = other.m_minimum;
m_maximum = other.m_maximum;
m_initialBucketSize = other.m_initialBucketSize;
m_nInitialBuckets = other.m_nInitialBuckets;
m_nMappedBuckets = other.m_nMappedBuckets;
m_mappedBucketSize = other.m_mappedBucketSize;
// copy the initial/mapped buckets reference, no deep copy here!
m_initialBuckets = other.m_initialBuckets;
m_mappedBuckets = other.m_mappedBuckets;
}
return *this;
}
WValueSetHistogram::~WValueSetHistogram()
{
}
......
......@@ -43,35 +43,49 @@
*/
class WValueSetHistogram
{
friend class WValueSetHistogramTest;
public:
/**
* Constructor. Creates the histogram for the specified value set.
*
* \param valueSet source of the data for the histogram
* \param buckets the number of buckets to use. If not specified, 1000 is used as default
*/
explicit WValueSetHistogram( boost::shared_ptr< WValueSetBase > valueSet );
explicit WValueSetHistogram( boost::shared_ptr< WValueSetBase > valueSet, size_t buckets = 1000 );
/**
* Constructor. Creates the histogram for the specified value set.
*
* \param valueSet source of the data for the histogram
* \param buckets the number of buckets to use. If not specified, 1000 is used as default
*/
explicit WValueSetHistogram( const WValueSetBase& valueSet );
explicit WValueSetHistogram( const WValueSetBase& valueSet, size_t buckets = 1000 );
/**
* Copy constructor. If another interval size is given the histogram gets matched to it using the initial bucket data.
* \note this does not deep copy the m_initialBuckets and m_mappedBuckets array as these are shared_array instances.
*
* \param histogram another WValueSetHistogram
* \param buckets the new number of buckets.
*/
explicit WValueSetHistogram( const WValueSetHistogram& histogram, size_t buckets = 0 );
WValueSetHistogram( const WValueSetHistogram& histogram, size_t buckets = 0 );
/**
* Destructor.
*/
~WValueSetHistogram();
/**
* Copy assignment. Copies the contents of the specified histogram to this instance.
*
* \param other the other instance
*
* \return this instance with the contents of the other one.
* \note this does not deep copy the m_initialBuckets and m_mappedBuckets array as these are shared_array instances.
*/
WValueSetHistogram& operator=( const WValueSetHistogram& other );
/**
* Get the size of the bucket.
*
......
......@@ -22,49 +22,51 @@
//
//---------------------------------------------------------------------------
#ifndef WHISTOGRAM_TEST_H
#define WHISTOGRAM_TEST_H
#ifndef WVALUESETHISTOGRAM_TEST_H
#define WVALUESETHISTOGRAM_TEST_H
//#include <stdint.h>
#include <vector>
#include <cxxtest/TestSuite.h>
#include "../../WValueSet.h"
//#include "../../WDataHandlerEnums.h"
//#include "../../WValueSetBase.h"
#include "../WHistogram.h"
#include "../WValueSetHistogram.h"
/**
* Test WHistogram
* Test WValueSetHistogram
**/
class WHistogramTest : public CxxTest::TestSuite
class WValueSetHistogramTest : public CxxTest::TestSuite
{
public:
/**
* Test instantiation
**/
*/
void testInstantiation( void )
{
// create some test data
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 ) );
// test both constructors with default bucket size
TS_ASSERT_THROWS_NOTHING( WValueSetHistogram hist( *valueSet ) );
boost::shared_ptr< WValueSet< double > > vsPtr( valueSet );
TS_ASSERT_THROWS_NOTHING( WHistogram hist( vsPtr ) );
TS_ASSERT_THROWS_NOTHING( WValueSetHistogram hist( vsPtr ) );
}
/**
* Test operator[]
**/
*/
void testOperator( void )
{
// create some test data
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 );
// create the histogram
WValueSetHistogram hist( *valueSet, 5 );
TS_ASSERT_EQUALS( hist[0], 1 ); // 0.0
TS_ASSERT_EQUALS( hist[1], 2 ); // 1.0
TS_ASSERT_EQUALS( hist[2], 0 ); // 2.0
......@@ -74,51 +76,105 @@ class WHistogramTest : public CxxTest::TestSuite
/**
* Test at()
**/
*/
void testAt( void )
{
// create some test data
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 );
// create histogram
WValueSetHistogram hist( *valueSet, 5 );
// test access with valid indices
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
// test access with invalid index
TS_ASSERT_THROWS_ANYTHING( hist.at( 5 ) );
}
/**
* Test getMin(), getMax()
**/
* Test getMinimum(), getMaximum()
*/
void testMinMax( void )
{
// create some test data
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 );
// create histogram
WValueSetHistogram hist( *valueSet );
TS_ASSERT_EQUALS( hist.getMinimum(), -5.0 );
TS_ASSERT_EQUALS( hist.getMaximum(), 2.0 );
}
/**
* Test size(), setInterval()
**/
void testSizeSetInterval( void )
* Test size(), and getBucketSize()
*/
void testSizes( void )
{
double a[4] = { 0.0, 4.0, 1.0, 2.0 };
// create some test data
double a[5] = { 0.0, 4.0, 1.0, 2.0, 1.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 );
// create histogram
WValueSetHistogram hist( *valueSet, 5 );
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 );
TS_ASSERT_EQUALS( hist.getBucketSize(), 1.0 ); // 0.0, 1.0, 2.0, 3.0 and 4.0
}
/**
* Test copy construction.
**/
void testCopyWithoutIntervalChanges( void )
{
// create some test data
double a[5] = { 0.0, 4.0, 1.0, 2.0, 1.0 };
const std::vector< double > v( a, a + sizeof( a ) / sizeof( double ) );
WValueSet< double >* valueSet = new WValueSet< double >( 0, 1, v, W_DT_DOUBLE );
// create histogram
WValueSetHistogram hist( *valueSet, 5 );
WValueSetHistogram hist2 = hist; // copy constructor
WValueSetHistogram hist3( *valueSet, 10 );
// is everything the same?
TS_ASSERT_EQUALS( hist.m_minimum, hist2.m_minimum );
TS_ASSERT_EQUALS( hist.m_maximum, hist2.m_maximum );
TS_ASSERT_EQUALS( hist.m_initialBucketSize, hist2.m_initialBucketSize );
TS_ASSERT_EQUALS( hist.m_initialBuckets.get(), hist2.m_initialBuckets.get() ); // initial buckets must be the same as it is a shared array
TS_ASSERT_EQUALS( hist.m_nInitialBuckets, hist2.m_nInitialBuckets );
TS_ASSERT_EQUALS( hist.m_mappedBuckets.get(), hist2.m_mappedBuckets.get() );
TS_ASSERT_EQUALS( hist.m_nMappedBuckets, hist2.m_nMappedBuckets );
TS_ASSERT_EQUALS( hist.m_mappedBucketSize, hist2.m_mappedBucketSize );
// test copy assignment
hist = hist3;
TS_ASSERT_EQUALS( hist.m_minimum, hist3.m_minimum );
TS_ASSERT_EQUALS( hist.m_maximum, hist3.m_maximum );
TS_ASSERT_EQUALS( hist.m_initialBucketSize, hist3.m_initialBucketSize );
TS_ASSERT_EQUALS( hist.m_initialBuckets.get(), hist3.m_initialBuckets.get() ); // initial buckets must be the same as it is a shared array
TS_ASSERT_EQUALS( hist.m_nInitialBuckets, hist3.m_nInitialBuckets );
TS_ASSERT_EQUALS( hist.m_mappedBuckets, hist3.m_mappedBuckets );
TS_ASSERT_EQUALS( hist.m_nMappedBuckets, hist3.m_nMappedBuckets );
TS_ASSERT_EQUALS( hist.m_mappedBucketSize, hist3.m_mappedBucketSize );
}
/**
* Test copy construction with changed interval sizes.
**/
void testCopyWithIntervalChanges( void )
{
}
};
#endif // WHISTOGRAM_TEST_H
#endif // WVALUESETHISTOGRAM_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