Commit 24154c59 authored by math's avatar math
Browse files

[ADD #56] implemented rough template for WValueSet, see #56 for more details.

parent 840fac91
......@@ -22,10 +22,10 @@
//---------------------------------------------------------------------------
#include "WDataSetSingle.h"
#include "WValueSet.h"
#include "WValueSet.hpp"
#include "WGrid.h"
WDataSetSingle::WDataSetSingle( boost::shared_ptr<WValueSet> newValueSet,
WDataSetSingle::WDataSetSingle( boost::shared_ptr<WValueSetBase> newValueSet,
boost::shared_ptr<WGrid> newGrid,
boost::shared_ptr< WMetaInfo > newMetaInfo )
: WDataSet( newMetaInfo )
......@@ -33,8 +33,7 @@ WDataSetSingle::WDataSetSingle( boost::shared_ptr<WValueSet> newValueSet,
assert( newValueSet );
assert( newGrid );
assert( newMetaInfo );
assert( newValueSet->getNumberOfValues() == newGrid->getNumberOfPositions() );
assert( newValueSet->size() == newGrid->size() );
m_valueSet = newValueSet;
m_grid = newGrid;
......@@ -45,7 +44,7 @@ WDataSetSingle::~WDataSetSingle()
// TODO(wiebel): Auto-generated destructor stub
}
boost::shared_ptr<WValueSet> WDataSetSingle::getValueSet() const
boost::shared_ptr<WValueSetBase> WDataSetSingle::getValueSet() const
{
return m_valueSet;
}
......
......@@ -28,7 +28,7 @@
#include "WDataSet.h"
class WValueSet;
class WValueSetBase;
class WGrid;
/**
......@@ -40,9 +40,9 @@ public:
/**
* Constructs an instance out of a value set, grid and meta information.
*/
WDataSetSingle( boost::shared_ptr<WValueSet> newValueSet,
boost::shared_ptr<WGrid> newGrid,
boost::shared_ptr<WMetaInfo> newMetaInfo );
WDataSetSingle( boost::shared_ptr< WValueSetBase > newValueSet,
boost::shared_ptr< WGrid > newGrid,
boost::shared_ptr< WMetaInfo > newMetaInfo );
/**
* Destroys this DataSet instance
......@@ -52,12 +52,12 @@ public:
/**
* \return Reference to its WValueSet
*/
boost::shared_ptr<WValueSet> getValueSet() const;
boost::shared_ptr< WValueSetBase > getValueSet() const;
/**
* \return Reference to its WGrid
*/
boost::shared_ptr<WGrid> getGrid() const;
boost::shared_ptr< WGrid > getGrid() const;
private:
/**
......@@ -68,7 +68,7 @@ private:
/**
* Stores the reference of the WValueSet of this DataSetSingle instance.
*/
boost::shared_ptr< WValueSet > m_valueSet;
boost::shared_ptr< WValueSetBase > m_valueSet;
};
#endif // WDATASETSINGLE_H
......@@ -21,17 +21,18 @@
//
//---------------------------------------------------------------------------
#include <cstddef>
#include "WGrid.h"
WGrid::WGrid()
: m_numberOfPositions( 0 )
WGrid::WGrid( size_t size )
: m_size( size )
{
}
unsigned int WGrid::getNumberOfPositions() const
size_t WGrid::size() const
{
return m_numberOfPositions;
return m_size;
}
......@@ -24,6 +24,8 @@
#ifndef WGRID_H
#define WGRID_H
#include <cstddef>
/**
* Base class to all grid types, e.g. a regular grid.
*/
......@@ -33,12 +35,12 @@ public:
/**
* Constructs a new WGrid instance.
*/
WGrid();
explicit WGrid( size_t size );
/**
* \return The number of position in this grid.
*/
unsigned int getNumberOfPositions() const;
size_t size() const;
protected:
......@@ -46,7 +48,7 @@ private:
/**
* Stores the number of positions.
*/
unsigned int m_numberOfPositions;
size_t m_size;
};
#endif // WGRID_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut
//
// Copyright 2009 SomeCopyrightowner
//
// 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 WVALUESET_H
#define WVALUESET_H
#include <cassert>
#include <cstddef>
#include <vector>
#include "WValueSetBase.h"
/**
* Base Class for all value set types.
*/
template< typename T > class WValueSet : public WValueSetBase
{
/**
* Only UnitTests are allowed to be friends
*/
friend class WValueSetTest;
public:
/**
* Constructs an empty WValueSet instance holding size many values.
*/
WValueSet( char order, char dimension, const std::vector< T > data )
: WValueSetBase( order, dimension ),
m_data( data )
{
assert( m_dimension >= 1 );
assert( m_order <= 2 );
}
/**
* \return The number of tensors stored in this set.
*/
virtual size_t size() const
{
switch( m_order )
{
case 0 : // scalar
assert( m_dimension == 1 && "but m_order was 0");
return rawSize();
case 1 : // vector
assert( rawSize() % m_dimension == 0 );
return rawSize() / m_dimension;
case 2 : // matrix
assert( rawSize() % ( m_dimension * m_dimension ) == 0 );
return rawSize() / ( m_dimension * m_dimension );
default : // other
assert( 1 == 0 && "Unsupported tensor order" );
return 0;
}
}
/**
* \return The number of integrals stored in this set.
*/
virtual size_t rawSize() const
{
return m_data.size();
}
/**
* Sometimes we need raw access to the data array, for e.g. OpenGL.
*/
const T * const rawData() const
{
return &m_data[0];
}
protected:
private:
/**
* Stores the values of type T as simple array which never should be modified.
*/
const std::vector< T > m_data; // WARNING: don't remove constness since &m_data[0] won't work anymore!
};
#endif // WVALUESET_H
......@@ -21,14 +21,18 @@
//
//---------------------------------------------------------------------------
#include "WValueSet.h"
#include <cstddef>
WValueSet::WValueSet()
: m_numberOfValues( 0 )
#include "WValueSetBase.h"
WValueSetBase::WValueSetBase( char order, char dimension )
: m_order( order ),
m_dimension( dimension )
{
}
unsigned int WValueSet::getNumberOfValues() const
WValueSetBase::~WValueSetBase()
{
return m_numberOfValues;
// empty since WValueSetBase is an abstract class
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut
//
// Copyright 2009 SomeCopyrightowner
//
// 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 WVALUESETBASE_H
#define WVALUESETBASE_H
#include <cstddef>
/**
* Abstract base class to all WValueSets. This class doesn't provide any genericness.
*/
class WValueSetBase
{
public:
/**
* Despite this is an abstract class all subclasses should have an order
* and dimension.
*/
WValueSetBase( char order, char dimension );
/**
* Dummy since each class with virtual member functions needs one.
*/
virtual ~WValueSetBase() = 0;
/**
* \return The number of tensors in this ValueSet.
*/
virtual size_t size() const = 0;
/**
* \return The number of integrals (POD like int, double) in this ValueSet.
*/
virtual size_t rawSize() const = 0;
protected:
/**
* The order of the tensors for this ValueSet
*/
char m_order;
/**
* The dimension of the tensors for this ValueSet
*/
char m_dimension;
private:
};
#endif // WVALUESETBASE_H
......@@ -24,18 +24,20 @@
#ifndef WDATASETSINGLE_TEST_H
#define WDATASETSINGLE_TEST_H
#include <vector>
#include <cxxtest/TestSuite.h>
#include "../WDataSetSingle.h"
#include "../WMetaInfo.h"
#include "../WValueSet.h"
#include "../WValueSet.hpp"
#include "../WGrid.h"
class WDataSetSingleTest : public CxxTest::TestSuite
{
public:
boost::shared_ptr< WGrid > gridDummy;
boost::shared_ptr< WValueSet > valueSetDummy;
boost::shared_ptr< WValueSetBase > valueSetDummy;
boost::shared_ptr< WMetaInfo > metaInfoDummy;
/**
......@@ -45,8 +47,9 @@ public:
{
// create dummies, since they are needed in almost every test
metaInfoDummy = boost::shared_ptr< WMetaInfo >( new WMetaInfo );
gridDummy = boost::shared_ptr< WGrid >( new WGrid );
valueSetDummy = boost::shared_ptr< WValueSet >( new WValueSet );
gridDummy = boost::shared_ptr< WGrid >( new WGrid( 1 ) );
std::vector< int > data( 1, 1 );
valueSetDummy = boost::shared_ptr< WValueSet< int > >( new WValueSet< int >( 0, 1, data ) );
}
/**
......@@ -58,11 +61,13 @@ public:
}
/**
* Retrive a WValueSet should always give the original pointer.
* Retrive a WValueSetBase should always give the original pointer.
*/
void testGetValueSet( void )
{
boost::shared_ptr< WValueSet > other = boost::shared_ptr< WValueSet >( new WValueSet );
std::vector< double > data( 1, 3.1415 );
boost::shared_ptr< WValueSet< double > > other;
other = boost::shared_ptr< WValueSet< double > >( new WValueSet< double >( 0, 1, data ) );
WDataSetSingle dataSetSingle( valueSetDummy, gridDummy, metaInfoDummy );
TS_ASSERT_EQUALS( dataSetSingle.getValueSet(), valueSetDummy );
TS_ASSERT_DIFFERS( dataSetSingle.getValueSet(), other );
......@@ -73,7 +78,7 @@ public:
*/
void testGetGrid( void )
{
boost::shared_ptr< WGrid > other = boost::shared_ptr< WGrid >( new WGrid );
boost::shared_ptr< WGrid > other = boost::shared_ptr< WGrid >( new WGrid( 1 ) );
WDataSetSingle dataSetSingle( valueSetDummy, gridDummy, metaInfoDummy );
TS_ASSERT_EQUALS( dataSetSingle.getGrid(), gridDummy );
TS_ASSERT_DIFFERS( dataSetSingle.getGrid(), other );
......
......@@ -39,16 +39,16 @@ public:
*/
void testInstantiation( void )
{
TS_ASSERT_THROWS_NOTHING( WGrid grid() );
TS_ASSERT_THROWS_NOTHING( WGrid grid( 1 ) );
}
/**
* After instantiation there should be no positions.
*/
void testGetNumberOfPositions( void )
void testSize( void )
{
WGrid grid;
TS_ASSERT_EQUALS( grid.getNumberOfPositions(), 0 );
WGrid grid( 1 );
TS_ASSERT_EQUALS( grid.size(), 1 );
}
};
......
......@@ -21,37 +21,25 @@
//
//---------------------------------------------------------------------------
#ifndef WVALUESET_H
#define WVALUESET_H
#ifndef WVALUESETBASE_TEST_H
#define WVALUESETBASE_TEST_H
#include <cxxtest/TestSuite.h>
#include "../WValueSetBase.h"
/**
* Base Class for all value set types.
* TODO(lmath): Document this!
*/
class WValueSet
class WValueSetBaseTest : public CxxTest::TestSuite
{
/**
* Only UnitTests are allowed to be friends
*/
friend class WValueSetTest;
public:
/**
* Constructs an empty WValueSet instance
*/
WValueSet();
/**
* \return The number of values stored in this set.
*/
unsigned int getNumberOfValues() const;
protected:
private:
/**
* Stores the number of values in this set
* TODO(lmath): Document this!
*/
unsigned int m_numberOfValues;
void testSomething( void )
{
}
};
#endif // WVALUESET_H
#endif // WVALUESETBASE_TEST_H
......@@ -24,9 +24,11 @@
#ifndef WVALUESET_TEST_H
#define WVALUESET_TEST_H
#include <vector>
#include <cxxtest/TestSuite.h>
#include "../WValueSet.h"
#include "../WValueSet.hpp"
/**
* UnitTests the WValueSet class
......@@ -39,18 +41,51 @@ public:
*/
void testInstantiation( void )
{
TS_ASSERT_THROWS_NOTHING( WValueSet valueSet() );
double a[2] = { 0.0, 3.1415 };
const std::vector< double > v( a, a + sizeof( a ) / sizeof( double ) );
TS_ASSERT_THROWS_NOTHING( WValueSet< double > valueSet( 0, 1, v ) );
}
/**
* The number of values retrieved is the same used internally
* The number of values retrieved is correct
*/
void testGetNumberOfValues( void )
{
WValueSet valueSet;
TS_ASSERT_EQUALS( valueSet.getNumberOfValues(), 0 );
valueSet.m_numberOfValues = 42;
TS_ASSERT_EQUALS( valueSet.getNumberOfValues(), 42 );
int a[4] = { 0, -5, 1, 2 };
const std::vector< int > v( a, a + sizeof( a ) / sizeof( int ) );
WValueSet< int > first( 0, 1, v );
TS_ASSERT_EQUALS( first.size(), 4 );
WValueSet< int > second( 1, 2, v );
TS_ASSERT_EQUALS( second.size(), 2 );
WValueSet< int > third( 2, 2, v );
TS_ASSERT_EQUALS( third.size(), 1 );
}
/**
* The raw size is the size of the number of integral elements inside
* this ValueSet.
*/
void testRawSize( void )
{
int a[4] = { 0, -5, 1, 2 };
const std::vector< int > v( a, a + sizeof( a ) / sizeof( int ) );
WValueSet< int > first( 0, 1, v );
TS_ASSERT_EQUALS( first.rawSize(), 4 );
WValueSet< int > second( 2, 2, v );
TS_ASSERT_EQUALS( first.rawSize(), 4 );
}
/**
* Raw Access should provide data access to the underlying array.
*/
void testReadOnlyRawAccess( void )
{
double a[2] = { 0.0, 3.1415 };
const std::vector< double > v( a, a + sizeof( a ) / sizeof( double ) );
WValueSet< double > valueSet( 0, 1, v );
const double * const b = valueSet.rawData();
TS_ASSERT_EQUALS( b[0], 0.0 );
TS_ASSERT_EQUALS( b[1], 3.1415 );
}
};
......
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