From ae69831a92c97445faa9495029f57bfe0f95b1e7 Mon Sep 17 00:00:00 2001 From: Valentin Noel Date: Wed, 7 Aug 2013 11:08:31 +0200 Subject: [PATCH] add modifications to Data class for multiple values support (and unit tests) issue #27 --- libraries/basicElement/src/Data/Data.cpp | 38 +++++++++- libraries/basicElement/src/Data/Data.hpp | 6 +- .../basicElement/tests/element/dataTests.hpp | 75 +++++++++++++++++++ 3 files changed, 114 insertions(+), 5 deletions(-) diff --git a/libraries/basicElement/src/Data/Data.cpp b/libraries/basicElement/src/Data/Data.cpp index 099cf1f..04fca8c 100644 --- a/libraries/basicElement/src/Data/Data.cpp +++ b/libraries/basicElement/src/Data/Data.cpp @@ -54,6 +54,9 @@ size_t Data::getSize() const std::string Data::getAscii() const { + if( _data == NULL ) + return ""; + std::stringstream sstr; for (size_t i = 0; i < _size; ++i) sstr << _data[i]; @@ -63,6 +66,9 @@ std::string Data::getAscii() const std::string Data::getHexa() const { + if( _data == NULL ) + return ""; + std::stringstream sstr; char* buffer = new char [ _size ]; std::memcpy( buffer, _data, _size ); @@ -89,11 +95,25 @@ std::vector< unsigned int > Data::toIntVector() void Data::setSpecData( const std::string& specValue ) { _specValue = specValue; + _size = _specValue.size(); +} + +void Data::setSpecData( const std::vector< std::string >& specValues ) +{ + _specValues = specValues; + size_t size = 0; + for( std::string value: _specValues ) + { + if( size != 0 && size != value.size() ) + throw std::runtime_error( "Specification Error: Multiple values must have the same size" ); + size = value.size(); + } + _size = size; } Element::EStatus Data::checkData() { - if( _specValue.empty() ) + if( _specValue.empty() && _specValues.empty() ) { setStatus( eStatusPassOver ); return eStatusPassOver; @@ -109,15 +129,27 @@ Element::EStatus Data::checkData() } case eDataTypeAscii : { - if( _specValue == getAscii() ) + if( ! _specValue.empty() && _specValue == getAscii() ) status = eStatusValid; + if( ! _specValues.empty() ) + { + for( std::string value : _specValues ) + if( value == getAscii() ) + status = eStatusValid; + } break; } case eDataTypeHexa : { - if( _specValue == getHexa() ) + if( ! _specValue.empty() && _specValue == getHexa() ) status = eStatusValid; + if( ! _specValues.empty() ) + { + for( std::string value : _specValues ) + if( value == getHexa() ) + status = eStatusValid; + } break; } diff --git a/libraries/basicElement/src/Data/Data.hpp b/libraries/basicElement/src/Data/Data.hpp index 5334257..0d9c389 100644 --- a/libraries/basicElement/src/Data/Data.hpp +++ b/libraries/basicElement/src/Data/Data.hpp @@ -23,6 +23,7 @@ class Data : public Element std::vector< unsigned int > toIntVector(); void setSpecData( const std::string& specValue ); + void setSpecData( const std::vector< std::string >& specValues ); Element::EStatus checkData(); std::vector< std::pair< std::string, std::string > > getElementInfo(); @@ -35,8 +36,9 @@ class Data : public Element char* getDataPtr ( ) const; private: - std::string _specValue; - char* _data; + std::string _specValue; + std::vector< std::string > _specValues; + char* _data; }; } diff --git a/libraries/basicElement/tests/element/dataTests.hpp b/libraries/basicElement/tests/element/dataTests.hpp index 022306e..73e34eb 100644 --- a/libraries/basicElement/tests/element/dataTests.hpp +++ b/libraries/basicElement/tests/element/dataTests.hpp @@ -104,6 +104,43 @@ BOOST_AUTO_TEST_CASE( basic_element_data_hexa ) data.setData( buffer, sizeof( buffer ) ); BOOST_CHECK_EQUAL( data.checkData(), be::Element::eStatusValid ); } + { + dbe::Data data( eDataTypeHexa ); + std::vector< std::string > values; + values.push_back( "00ff" ); + data.setSpecData( values ); + char buffer[] = { 0x00, static_cast< char >( 0xff ) }; + data.setData( buffer, sizeof( buffer ) ); + BOOST_CHECK_EQUAL( data.checkData(), be::Element::eStatusValid ); + } + { + dbe::Data data( eDataTypeHexa ); + std::vector< std::string > values; + values.push_back( "f00f" ); + values.push_back( "ff00" ); + data.setSpecData( values ); + data.setBigEndianness( false ); + char buffer[] = { 0x00, static_cast< char >( 0xff ) }; + data.setData( buffer, sizeof( buffer ) ); + BOOST_CHECK_EQUAL( data.checkData(), be::Element::eStatusValid ); + } + { + dbe::Data data( eDataTypeHexa ); + std::vector< std::string > values; + values.push_back( "f00f" ); + values.push_back( "0ff0" ); + data.setSpecData( values ); + char buffer[] = { 0x00, static_cast< char >( 0xff ) }; + data.setData( buffer, sizeof( buffer ) ); + BOOST_CHECK_EQUAL( data.checkData(), be::Element::eStatusInvalid ); + } + { + dbe::Data data( eDataTypeHexa ); + std::vector< std::string > values; + values.push_back( "000000" ); + values.push_back( "ffffffff" ); + BOOST_CHECK_THROW( data.setSpecData( values ), std::runtime_error ); + } } BOOST_AUTO_TEST_CASE( basic_element_data_ascii ) @@ -140,6 +177,44 @@ BOOST_AUTO_TEST_CASE( basic_element_data_ascii ) data.setData( buffer, sizeof( buffer ) ); BOOST_CHECK_EQUAL( data.checkData(), be::Element::eStatusValid ); } + { + dbe::Data data( eDataTypeAscii ); + std::vector< std::string > values; + values.push_back( "atad" ); + data.setSpecData( values ); + data.setBigEndianness( false ); + char buffer[] = {'d','a','t','a'}; + data.setData( buffer, sizeof( buffer ) ); + BOOST_CHECK_EQUAL( data.checkData(), be::Element::eStatusValid ); + } + { + dbe::Data data( eDataTypeAscii ); + std::vector< std::string > values; + values.push_back( "atad" ); + values.push_back( "taad" ); + data.setSpecData( values ); + data.setBigEndianness( false ); + char buffer[] = {'d','a','t','a'}; + data.setData( buffer, sizeof( buffer ) ); + BOOST_CHECK_EQUAL( data.checkData(), be::Element::eStatusValid ); + } + { + dbe::Data data( eDataTypeAscii ); + std::vector< std::string > values; + values.push_back( "atad" ); + values.push_back( "taad" ); + data.setSpecData( values ); + char buffer[] = {'d','a','t','a'}; + data.setData( buffer, sizeof( buffer ) ); + BOOST_CHECK_EQUAL( data.checkData(), be::Element::eStatusInvalid ); + } + { + dbe::Data data( eDataTypeAscii ); + std::vector< std::string > values; + values.push_back( "long" ); + values.push_back( "toolong" ); + BOOST_CHECK_THROW( data.setSpecData( values ), std::runtime_error ); + } } BOOST_AUTO_TEST_CASE( basic_element_data_other )