Skip to content

Commit

Permalink
add modifications to Number class for multi-range support (and unit t…
Browse files Browse the repository at this point in the history
…ests)

issue #27
  • Loading branch information
Valentin Noel committed Aug 7, 2013
1 parent f5761ce commit f47e347
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
9 changes: 5 additions & 4 deletions libraries/basicElement/src/Number/Number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ class Number : public Element
void getData ( char* buffer ) const;
size_t getSize () const;
NumberType getValue() const;
Range< NumberType >& getRange();
Map < NumberType >& getMap ();
void addRange( const NumberType& min, const NumberType& max );
std::vector< Range< NumberType > >& getRange();

std::string toString() const;

Expand All @@ -53,9 +54,9 @@ class Number : public Element
void setSubType();

private:
Range< NumberType > _range;
Map < NumberType > _map;
NumberData _numData;
std::vector< Range< NumberType > > _ranges;
Map < NumberType > _map;
NumberData _numData;
};


Expand Down
18 changes: 13 additions & 5 deletions libraries/basicElement/src/Number/Number.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,16 @@ NumberType Number< NumberType >::getValue() const
}


template< typename NumberType >
void Number< NumberType >::addRange( const NumberType& min, const NumberType& max )
{
_ranges.push_back( Range< NumberType >( min, max ) );
}

template< typename NumberType >
Range< NumberType >& Number< NumberType >::getRange()
std::vector< Range< NumberType > >& Number< NumberType >::getRange()
{
return _range;
return _ranges;
}

template< typename NumberType >
Expand Down Expand Up @@ -129,14 +134,17 @@ Element::EStatus Number< NumberType >::checkData()
{
_status = eStatusInvalid;

if( !_range.isSet() )
if( _ranges.empty() )
{
_status = eStatusPassOver;
return eStatusPassOver;
}

if( _range.isInRange( _numData.value ) )
_status = eStatusValid;
for( Range< NumberType > range : _ranges )
{
if( range.isInRange( _numData.value ) )
_status = eStatusValid;
}

return _status;
}
Expand Down
33 changes: 27 additions & 6 deletions libraries/basicElement/tests/element/numberTests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ BOOST_AUTO_TEST_CASE( basic_element_number )
num.setLabel( label );
BOOST_CHECK_EQUAL( num.getLabel(), label );

num.getRange().setRange( min, max );
BOOST_CHECK_EQUAL( num.getRange().isSet(), true );
num.addRange( min, max );
BOOST_CHECK_EQUAL( num.getRange().at(0).isSet(), true );

num.getMap().addPair( min, label );
BOOST_CHECK_EQUAL( num.getMap().getLabel(0), label );
Expand Down Expand Up @@ -586,16 +586,37 @@ BOOST_AUTO_TEST_CASE( basic_element_number_checkData )
}
{
nbe::Number< nbe::uint32 > num;
num.getRange().setRange( 254, 256 );
num.addRange( 254, 256 );
num.setData( dataInt32Low, sizeof( dataInt32High ) );
BOOST_CHECK_EQUAL( num.checkData(), be::Element::eStatusValid );
}
{
nbe::Number< nbe::uint32 > num;
num.getRange().setRange( 256, 256 );
num.addRange( 256, 256 );
num.setData( dataInt32Low, sizeof( dataInt32High ) );
BOOST_CHECK_EQUAL( num.checkData(), be::Element::eStatusInvalid );
}
{
nbe::Number< nbe::uint32 > num;
num.addRange( 254, 256 );
num.addRange( 0, 1 );
num.setData( dataInt32Low, sizeof( dataInt32High ) );
BOOST_CHECK_EQUAL( num.checkData(), be::Element::eStatusValid );
}
{
nbe::Number< nbe::uint32 > num;
num.addRange( 245, 254 );
num.addRange( 0, 1 );
num.setData( dataInt32Low, sizeof( dataInt32High ) );
BOOST_CHECK_EQUAL( num.checkData(), be::Element::eStatusInvalid );
}
{
nbe::Number< nbe::uint32 > num;
num.addRange( 245, 256 );
num.addRange( 254, 1000 );
num.setData( dataInt32Low, sizeof( dataInt32High ) );
BOOST_CHECK_EQUAL( num.checkData(), be::Element::eStatusValid );
}

{
nbe::Number< nbe::ieeeExtended > num;
Expand All @@ -604,13 +625,13 @@ BOOST_AUTO_TEST_CASE( basic_element_number_checkData )
}
{
nbe::Number< nbe::ieeeExtended > num;
num.getRange().setRange( 0, 2 );
num.addRange( 0, 2 );
num.setData( dataIeeeExtendedOneLittleEndian, sizeof( dataIeeeExtendedOneLittleEndian ) );
BOOST_CHECK_EQUAL( num.checkData(), be::Element::eStatusValid );
}
{
nbe::Number< nbe::ieeeExtended > num;
num.getRange().setRange( 1.5, 3.2 );
num.addRange( 1.5, 3.2 );
num.setData( dataIeeeExtendedOneLittleEndian, sizeof( dataIeeeExtendedOneLittleEndian ) );
BOOST_CHECK_EQUAL( num.checkData(), be::Element::eStatusInvalid );
}
Expand Down

0 comments on commit f47e347

Please sign in to comment.