Skip to content

Commit

Permalink
add modifications to Data class for multiple values support (and unit…
Browse files Browse the repository at this point in the history
… tests)

issue #27
  • Loading branch information
Valentin Noel committed Aug 7, 2013
1 parent 7e96861 commit ae69831
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 5 deletions.
38 changes: 35 additions & 3 deletions libraries/basicElement/src/Data/Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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 );
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down
6 changes: 4 additions & 2 deletions libraries/basicElement/src/Data/Data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
};

}
Expand Down
75 changes: 75 additions & 0 deletions libraries/basicElement/tests/element/dataTests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 )
Expand Down

0 comments on commit ae69831

Please sign in to comment.