Skip to content

Commit

Permalink
add modif for shared_ptr<Element> support in ExpressionParser class (…
Browse files Browse the repository at this point in the history
…and unit tests)

issue #27
  • Loading branch information
Valentin Noel committed Sep 4, 2013
1 parent 786aad5 commit e1d57d4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
28 changes: 28 additions & 0 deletions libraries/basicElement/src/Tools/ExpressionParser.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,34 @@ void ExpressionParser::setVariables( const std::map < std::string, NumberType >&
}
}

template< >
void ExpressionParser::setVariables( const std::map < std::string, std::shared_ptr< Element > >& elementMap )
{
for( std::pair< std::string, std::shared_ptr< Element > > elem : elementMap )
{
std::ostringstream oss;
std::string value;
switch( elem.second->getNumberSubType() )
{
case eNumberTypeUnknown : throw std::runtime_error( "Invalid element subtype!"); break;
case eNumberTypeInt8 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::int8 > >( elem.second )->toString(); break;
case eNumberTypeUInt8 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::uint8 > >( elem.second )->toString(); break;
case eNumberTypeInt16 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::int16 > >( elem.second )->toString(); break;
case eNumberTypeUInt16 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::uint16 > >( elem.second )->toString(); break;
case eNumberTypeInt32 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::int32 > >( elem.second )->toString(); break;
case eNumberTypeUInt32 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::uint32 > >( elem.second )->toString(); break;
case eNumberTypeInt64 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::int64 > >( elem.second )->toString(); break;
case eNumberTypeUInt64 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::uint64 > >( elem.second )->toString(); break;
case eNumberTypeFloat : value = std::static_pointer_cast< be::number_element::Number< float > >( elem.second )->toString(); break;
case eNumberTypeDouble : value = std::static_pointer_cast< be::number_element::Number< double > >( elem.second )->toString(); break;
case eNumberTypeIeeeExtended : value = std::static_pointer_cast< be::number_element::Number< be::number_element::ieeeExtended > >( elem.second )->toString(); break;
}
oss << elem.first << " = " << value << std::endl;
_contextString += oss.str();
}
}


template< typename ResultType >
ResultType ExpressionParser::getExpressionResult( const std::string& expression )
{
Expand Down
38 changes: 38 additions & 0 deletions libraries/basicElement/tests/tools/expressionParserTests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,42 @@ BOOST_AUTO_TEST_CASE( basic_element_expression_parser_get_expression_result )
}
}


BOOST_AUTO_TEST_CASE( basic_element_expression_parser_shared_ptr )
{
LOG_WARNING( ">>> basic_element_expression_parser_shared_ptr <<<" );
{
std::shared_ptr< be::number_element::Number< int > > num1( new be::number_element::Number< int > );
std::shared_ptr< be::number_element::Number< short > > num2( new be::number_element::Number< short > );
std::shared_ptr< be::number_element::Number< float > > num3( new be::number_element::Number< float > );

char buffer1[] = { 0x00, 0x00, 0x00, 0x01 };
char buffer2[] = { 0x00, 0x02 };
char buffer3[] = { 0x3f, 0x7f, 0x00, 0x00 };

num1->setData( buffer1, 4 );
num2->setData( buffer2, 2 );
num3->setData( buffer3, 4 );

BOOST_CHECK_EQUAL( num1->toString(), "1" );
BOOST_CHECK_EQUAL( num2->toString(), "2" );
BOOST_CHECK_EQUAL( num3->toString(), "0.996094" );

std::map< std::string, std::shared_ptr< be::Element > > elementList;
elementList.insert( std::make_pair( "num1", num1 ) );
elementList.insert( std::make_pair( "num2", num2 ) );
elementList.insert( std::make_pair( "num3", num3 ) );

bep::ExpressionParser expParser = bep::ExpressionParser( elementList );
BOOST_CHECK_EQUAL( expParser.getExpressionResult< int >( "num1" ), 1 );
BOOST_CHECK_EQUAL( expParser.getExpressionResult< int >( "num2" ), 2 );
BOOST_CHECK_EQUAL( expParser.getExpressionResult< short >( "num1 + num2" ), 3 );
BOOST_CHECK_EQUAL( expParser.getExpressionResult< int >( "num2 - num1" ), 1 );
BOOST_CHECK_EQUAL( expParser.getExpressionResult< float >( "num1 - num2" ), -1 );
BOOST_CHECK_EQUAL( expParser.getExpressionResult< int >( "num1 * num2" ), 2 );
BOOST_CHECK_CLOSE( expParser.getExpressionResult< float >( "num1 * num3" ), 0.996094, 0.001 );
BOOST_CHECK_CLOSE( expParser.getExpressionResult< float >( "num3 / num2" ), 0.498047, 0.001 );
}
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit e1d57d4

Please sign in to comment.