Commit 4cfa3254 authored by math's avatar math

[CHANGE]

 * various documentation added
 * refactored some tests
 * added a unit test to the exception class
 * linked the WNoSuchDataSetException .cpp-code into the dataHandler code
 * removed Whitespace in CMakeLists.txt
 * Exceptions now subclassed from std::logic_error instead of std::runtime_error, see the code for details
parent 1f778594
ADD_SUBDIRECTORY( io )
ADD_SUBDIRECTORY( exceptions )
FILE( GLOB DATAHANDLER_SRC "*.cpp" )
FILE( GLOB DATAHANDLER_SRC "*.cpp" "exceptions/*.cpp" )
ADD_LIBRARY( dataHandler SHARED ${DATAHANDLER_SRC} )
# Unit tests
IF( CXXTEST_FOUND )
CXXTEST_ADD_TEST( unittest_WMetaInfo
WMetaInfo_test.cc
CXXTEST_ADD_TEST( unittest_WMetaInfo
WMetaInfo_test.cc
${CMAKE_CURRENT_SOURCE_DIR}/test/WMetaInfo_test.h )
TARGET_LINK_LIBRARIES( unittest_WMetaInfo dataHandler )
CXXTEST_ADD_TEST( unittest_WGrid
WGrid_test.cc
CXXTEST_ADD_TEST( unittest_WGrid
WGrid_test.cc
${CMAKE_CURRENT_SOURCE_DIR}/test/WGrid_test.h )
TARGET_LINK_LIBRARIES( unittest_WGrid dataHandler )
CXXTEST_ADD_TEST( unittest_WValueSet
WValueSet_test.cc
CXXTEST_ADD_TEST( unittest_WValueSet
WValueSet_test.cc
${CMAKE_CURRENT_SOURCE_DIR}/test/WValueSet_test.h )
TARGET_LINK_LIBRARIES( unittest_WValueSet dataHandler )
CXXTEST_ADD_TEST( unittest_WDataSet
WDataSet_test.cc
CXXTEST_ADD_TEST( unittest_WDataSet
WDataSet_test.cc
${CMAKE_CURRENT_SOURCE_DIR}/test/WDataSet_test.h )
TARGET_LINK_LIBRARIES( unittest_WDataSet dataHandler )
CXXTEST_ADD_TEST( unittest_WDataSetSingle
WDataSetSingle_test.cc
CXXTEST_ADD_TEST( unittest_WDataSetSingle
WDataSetSingle_test.cc
${CMAKE_CURRENT_SOURCE_DIR}/test/WDataSetSingle_test.h )
TARGET_LINK_LIBRARIES( unittest_WDataSetSingle dataHandler )
TARGET_LINK_LIBRARIES( unittest_WDataSetSingle dataHandler )
CXXTEST_ADD_TEST( unittest_WDataSetTimeDependent
WDataSetTimeDependent_test.cc
WDataSetTimeDependent_test.cc
${CMAKE_CURRENT_SOURCE_DIR}/test/WDataSetTimeDependent_test.h )
TARGET_LINK_LIBRARIES( unittest_WDataSetTimeDependent dataHandler )
CXXTEST_ADD_TEST( unittest_WDataHandler
TARGET_LINK_LIBRARIES( unittest_WDataSetTimeDependent dataHandler )
CXXTEST_ADD_TEST( unittest_WDataHandler
WDataHandler_test.cc
${CMAKE_CURRENT_SOURCE_DIR}/test/WDataHandler_test.h )
TARGET_LINK_LIBRARIES( unittest_WDataHandler dataHandler )
ENDIF( CXXTEST_FOUND )
......@@ -25,11 +25,11 @@
#include "WDataSet.h"
#include "exceptions/WNoSuchDataSetException.h"
boost::shared_ptr< WDataSet > WDataHandler::getDataSet( unsigned int dataSetId ) const
boost::shared_ptr< const WDataSet > WDataHandler::getDataSet( const unsigned int dataSetId ) const
{
if( dataSetId >= m_dataSets.size() )
throw WNoSuchDataSetException( "Index to large." );
return m_dataSets[dataSetId];
throw WNoSuchDataSetException( "Index too large." );
return m_dataSets.at(dataSetId);
}
void WDataHandler::addDataSet( boost::shared_ptr< WDataSet > newDataSet )
......
......@@ -24,26 +24,46 @@
#ifndef WDATAHANDLER_H
#define WDATAHANDLER_H
#include <boost/shared_ptr.hpp>
#include <vector>
#include <boost/shared_ptr.hpp>
class WDataSet;
/**
* Provides the environment for storing and accessing data sets.
* Provides the environment for storing and accessing DataSets.
* \ingroup dataHandler
*/
class WDataHandler
{
/**
* Only UnitTests may be friends.
*/
friend class WDataHandlerTest;
public:
boost::shared_ptr< WDataSet > getDataSet( unsigned int dataSetId ) const;
/**
* Get the pointer to the i'th DataSet. The return type is const since we
* want to ensure that each DataSet cannot modified after retrival.
*/
boost::shared_ptr< const WDataSet > getDataSet( const unsigned int dataSetId ) const;
/**
* Insert a new DataSet referenced by a pointer.
*/
void addDataSet( boost::shared_ptr< WDataSet > newDataSet );
/**
* Get the number of DataSets which are actually handled by our DataHandler.
*/
unsigned int getNumberOfDataSets() const;
protected:
private:
/**
* A container for all DataSets.
*/
std::vector< boost::shared_ptr< WDataSet > > m_dataSets;
};
......@@ -55,5 +75,4 @@ private:
*/
#endif // WDATAHANDLER_H
......@@ -23,8 +23,8 @@
#include "WDataSet.h"
WDataSet::WDataSet( boost::shared_ptr<WMetaInfo> newMetaInfo ) :
m_metaInfo( newMetaInfo )
WDataSet::WDataSet( boost::shared_ptr<WMetaInfo> newMetaInfo )
: m_metaInfo( newMetaInfo )
{
assert( newMetaInfo );
}
......@@ -34,9 +34,3 @@ boost::shared_ptr<WMetaInfo> WDataSet::getMetaInfo() const
return m_metaInfo;
}
......@@ -29,21 +29,29 @@
class WMetaInfo;
/**
* Base class for all data set types. This class has a number of
* subclasses specifying the different types of data sets.
* Two of dataset type reprent single and time-dependent
* datasets (compound of several time steps) respectively.
* This bas class contains the pointer to the meta information.
*
* Base class for all data set types. This class has a number of subclasses
* specifying the different types of data sets. Two of dataset type reprent
* single and time-dependent datasets (compound of several time steps)
* respectively. This base class contains the pointer to the meta information.
*/
class WDataSet
{
public:
/**
* Since every DataSet should have a MetaInfo, we ensure hereby you never
* go without one.
*/
explicit WDataSet( boost::shared_ptr< WMetaInfo > newMetaInfo );
/**
* Returns the MetaInfo object for this DataSet.
*/
boost::shared_ptr< WMetaInfo > getMetaInfo() const;
protected:
/**
* Stores meta information like name, dimension, etc. for this DataSet.
*/
boost::shared_ptr< WMetaInfo > m_metaInfo;
private:
};
......
......@@ -26,9 +26,9 @@
#include "WGrid.h"
WDataSetSingle::WDataSetSingle( boost::shared_ptr<WValueSet> newValueSet,
boost::shared_ptr<WGrid> newGrid,
boost::shared_ptr< WMetaInfo > newMetaInfo )
:WDataSet( newMetaInfo )
boost::shared_ptr<WGrid> newGrid,
boost::shared_ptr< WMetaInfo > newMetaInfo )
: WDataSet( newMetaInfo )
{
assert( newValueSet );
assert( newGrid );
......
......@@ -25,6 +25,7 @@
#define WDATASETSINGLE_H
#include <boost/shared_ptr.hpp>
#include "WDataSet.h"
class WValueSet;
......@@ -33,18 +34,40 @@ class WGrid;
/**
* A data set consisting of a set of values based on a grid.
*/
class WDataSetSingle : WDataSet
class WDataSetSingle : public WDataSet
{
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 );
/**
* Destroys this DataSet instance
*/
virtual ~WDataSetSingle();
/**
* \return Reference to its WValueSet
*/
boost::shared_ptr<WValueSet> getValueSet() const;
/**
* \return Reference to its WGrid
*/
boost::shared_ptr<WGrid> getGrid() const;
private:
/**
* Stores the reference of the WGrid of this DataSetSingle instance.
*/
boost::shared_ptr< WGrid > m_grid;
/**
* Stores the reference of the WValueSet of this DataSetSingle instance.
*/
boost::shared_ptr< WValueSet > m_valueSet;
};
......
......@@ -24,7 +24,7 @@
#include "WGrid.h"
WGrid::WGrid()
:m_numberOfPositions( 0 )
: m_numberOfPositions( 0 )
{
}
......
......@@ -25,16 +25,27 @@
#define WGRID_H
/**
* Base class for all different grid types. The first and most
* important will be some kind of regular grid.
* Base class to all grid types, e.g. a regular grid.
*/
class WGrid
{
public:
/**
* Constructs a new WGrid instance.
*/
WGrid();
/**
* \return The number of position in this grid.
*/
unsigned int getNumberOfPositions() const;
protected:
private:
/**
* Stores the number of positions.
*/
unsigned int m_numberOfPositions;
};
......
......@@ -27,19 +27,48 @@
#include <string>
/**
* Stores meta information for data sets loaded into OpenWalnut
* Stores meta information such as types, file names, etc. associated with a
* WDataSet loaded into OpenWalnut.
*/
class WMetaInfo
{
/**
* Only UnitTests are allowed to be friends of this class
*/
friend class WMetaInfoTest;
public:
/**
* \return Name of the WDataSet
*/
std::string getName() const;
/**
* Reset the name of the DataSet
*/
void setName( std::string name );
/**
* \return The file name from which the DataSet got its data.
*/
std::string getFileName() const;
/**
* Reset the file name from which the data is gathered for the WDataSet.
*/
void setFileName( std::string fileName );
protected:
private:
/**
* Stores the name of the WDataSet this MetaInfo is associated with
*/
std::string m_name;
/**
* Stores the name of the input file from which this DataSet gets its data.
*/
std::string m_fileName;
};
......
......@@ -24,7 +24,7 @@
#include "WValueSet.h"
WValueSet::WValueSet()
:m_numberOfValues( 0 )
: m_numberOfValues( 0 )
{
}
......
......@@ -29,11 +29,28 @@
*/
class WValueSet
{
/**
* 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
*/
unsigned int m_numberOfValues;
};
......
......@@ -25,17 +25,17 @@
#include <stdexcept>
#include <string>
#include "WNoSuchDataSetException.h"
#include <boost/algorithm/string.hpp>
#include "WNoSuchDataSetException.h"
std::string WNoSuchDataSetException::getMessage() const
{
std::string out;
out+=what();
out+="\n\n";
for( std::list<std::string>::const_iterator i = trace.begin();
i != trace.end(); ++i )
out+= "trace: "+*i+"\n";
return out;
std::string result( what() );
result += "\n\n";
std::list< std::string >::const_iterator citer;
for( citer = m_trace.begin(); citer != m_trace.end(); ++citer )
result += "trace: " + *citer + "\n";
boost::trim( result );
return result;
}
......@@ -29,13 +29,24 @@
#include <string>
/**
* TODO(wiebel): Document this!
* Should be thrown when an invalid index is used to get a WDataSet from the
* WDataHandler. An index is invalid if it's greater or equal than the number
* of WDataSets in that WDataHandler.
*
* It's subclassed from std::logic_error since it represents a mistake by a
* programmer, not by the runtime system (e.g. allocation memory) or other
* libraries.
*/
class WNoSuchDataSetException : public std::runtime_error
class WNoSuchDataSetException : public std::logic_error
{
/**
* Only UnitTests are allowed to be a friend of this class.
*/
friend class WNoSuchDataSetExceptionTest;
public:
WNoSuchDataSetException( const std::string &s = std::string() ) throw()
: std::runtime_error( s )
: std::logic_error( s )
{
}
......@@ -46,8 +57,12 @@ public:
std::string getMessage( ) const;
protected:
private:
std::list< std::string > trace;
/**
* Stack trace for identifying the source where this exception came from
*/
std::list< std::string > m_trace;
};
#endif // WNOSUCHDATASETEXCEPTION_H
......@@ -34,51 +34,83 @@
class WDataHandlerTest : public CxxTest::TestSuite
{
public:
void testInstantiation( void )
boost::shared_ptr< WDataSet > dummyDataSet;
/**
* Constructs unit test environment.
*/
void setUp( void )
{
// create a DataSet dummy, since this is needed in almost every test
boost::shared_ptr< WMetaInfo > metaInfo =
boost::shared_ptr< WMetaInfo >( new WMetaInfo );
dummyDataSet = boost::shared_ptr< WDataSet >( new WDataSet( metaInfo ) );
}
/**
* An instantiation should never ever throw anything.
* ToDo(math): What about insufficient memory? Is it then still true?
*/
void testInstatiation( void )
{
TS_ASSERT_THROWS_NOTHING( WDataHandler dh );
TS_ASSERT_THROWS_NOTHING( WDataHandler dh() );
}
void testAddDataSets( void )
/**
* When a DataSet is added the size of our container increments by one.
*/
void testAddDataSet( void )
{
WDataHandler dataHandler;
boost::shared_ptr< WMetaInfo > metaInfo =
boost::shared_ptr< WMetaInfo >( new WMetaInfo );
boost::shared_ptr< WDataSet > dataSet =
boost::shared_ptr< WDataSet >( new WDataSet( metaInfo ) );
TS_ASSERT_THROWS_NOTHING( dataHandler.addDataSet( dataSet ) )
TS_ASSERT_EQUALS( dataHandler.m_dataSets.size(), 0 );
TS_ASSERT_THROWS_NOTHING( dataHandler.addDataSet( dummyDataSet ) )
TS_ASSERT_EQUALS( dataHandler.m_dataSets.size(), 1 );
}
/**
* Checks if the size (meaning the number of elements) of our container
* works properly.
*/
void testGetNumberOfDataSets( void )
{
WDataHandler dataHandler;
boost::shared_ptr< WMetaInfo > metaInfo =
boost::shared_ptr< WMetaInfo >( new WMetaInfo );
boost::shared_ptr< WDataSet > dataSet =
boost::shared_ptr< WDataSet >( new WDataSet( metaInfo ) );
dataHandler.addDataSet( dataSet );
TS_ASSERT_EQUALS( dataHandler.getNumberOfDataSets(), 0 )
dataHandler.addDataSet( dummyDataSet );
TS_ASSERT_EQUALS( dataHandler.getNumberOfDataSets(), 1 )
dataHandler.addDataSet( dataSet );
dataHandler.addDataSet( dummyDataSet );
TS_ASSERT_EQUALS( dataHandler.getNumberOfDataSets(), 2 )
}
/**
* When retrieving a DataSet only valid indices are allowed.
*/
void testGetDataSets( void )
{
WDataHandler dataHandler;
boost::shared_ptr< WMetaInfo > metaInfo0 = boost::shared_ptr< WMetaInfo >( new WMetaInfo );
boost::shared_ptr< WMetaInfo > metaInfo1 = boost::shared_ptr< WMetaInfo >( new WMetaInfo );
metaInfo0->setName( "Data Set 0" );
metaInfo1->setName( "Data Set 1" );
boost::shared_ptr< WDataSet > dataSet0 =
boost::shared_ptr< WDataSet >( new WDataSet( metaInfo0 ) );
boost::shared_ptr< WDataSet > dataSet1 =
boost::shared_ptr< WDataSet >( new WDataSet( metaInfo1 ) );
dataHandler.addDataSet( dataSet0 );
dataHandler.addDataSet( dataSet1 );
boost::shared_ptr< WMetaInfo > metaInfo = boost::shared_ptr< WMetaInfo >( new WMetaInfo );
metaInfo->setName( "Other Data Set" );
boost::shared_ptr< WDataSet > otherDataSet =
boost::shared_ptr< WDataSet >( new WDataSet( metaInfo ) );
dataHandler.addDataSet( dummyDataSet );
dataHandler.addDataSet( otherDataSet );
TS_ASSERT_THROWS_NOTHING( dataHandler.getDataSet( 0 ) )
TS_ASSERT_THROWS_NOTHING( dataHandler.getDataSet( 1 ) )
TS_ASSERT_EQUALS( dataHandler.getDataSet( 0 ), dataSet0 )
TS_ASSERT_EQUALS( dataHandler.getDataSet( 1 ), dataSet1 )
TS_ASSERT_DIFFERS( dataHandler.getDataSet( 1 ), dataSet0 )
TS_ASSERT_EQUALS( dataHandler.getDataSet( 0 ), dummyDataSet )
TS_ASSERT_EQUALS( dataHandler.getDataSet( 1 ), otherDataSet )
TS_ASSERT_DIFFERS( dataHandler.getDataSet( 1 ), dummyDataSet )
TS_ASSERT_THROWS( dataHandler.getDataSet( 2 ), WNoSuchDataSetException )
}
/**
* Test prevention of modification of a retrieved DataSet.
*/
void testConstnessOnDataSet( void )
{
WDataHandler dh;
dh.addDataSet( dummyDataSet );
boost::shared_ptr< const WDataSet > dataSet = dh.getDataSet( 0 );
// ToDo(math): I need to try to modify dataSet in order to test
}
};
#endif // WDATAHANDLER_TEST_H
......@@ -34,32 +34,49 @@
class WDataSetSingleTest : public CxxTest::TestSuite
{
public:
boost::shared_ptr< WGrid > gridDummy;
boost::shared_ptr< WValueSet > valueSetDummy;
boost::shared_ptr< WMetaInfo > metaInfoDummy;
/**
* Constructs unit test environment.
*/
void setUp( void )
{
// 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 );
}
/**
* During instantiation nothing should be thrown.
*/
void testInstantiation( void )
{
boost::shared_ptr< WMetaInfo > metaInfo = boost::shared_ptr< WMetaInfo >( new WMetaInfo );
boost::shared_ptr< WGrid > grid = boost::shared_ptr< WGrid >( new WGrid );
boost::shared_ptr< WValueSet > valueSet = boost::shared_ptr< WValueSet >( new WValueSet );
TS_ASSERT_THROWS_NOTHING( WDataSetSingle dataSetSingle( valueSet, grid, metaInfo ) )
TS_ASSERT_THROWS_NOTHING( WDataSetSingle ds( valueSetDummy, gridDummy, metaInfoDummy ) );
}
/**
* Retrive a WValueSet should always give the original pointer.
*/
void testGetValueSet( void )
{
boost::shared_ptr< WMetaInfo > metaInfo = boost::shared_ptr< WMetaInfo >( new WMetaInfo );
boost::shared_ptr< WGrid > grid = boost::shared_ptr< WGrid >( new WGrid );
boost::shared_ptr< WValueSet > valueSet = boost::shared_ptr< WValueSet >( new WValueSet );