Skip to content

Commit

Permalink
change Translator constructor
Browse files Browse the repository at this point in the history
issue #33
  • Loading branch information
Valentin Noel committed Oct 31, 2013
1 parent bcab912 commit ed1046c
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 106 deletions.
24 changes: 12 additions & 12 deletions libraries/ElementChecker/src/ElementChecker/Checker/Checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,75 +36,75 @@ void Checker::check()
case eTypeUnknown : LOG_ERROR( "Unknown element type, cannot check it" ); break;
case eTypeInt8 :
{
if( Ranges< be::int8 >( _element->_rangeExpr ).isInRanges( Translator( _element.get() ).get< be::int8 >() ) )
if( Ranges< be::int8 >( _element->_rangeExpr ).isInRanges( Translator( _element ).get< be::int8 >() ) )
status = eStatusValid;
break;
}
case eTypeUInt8 :
{
if( Ranges< be::uint8 >( _element->_rangeExpr ).isInRanges( Translator( _element.get() ).get< be::uint8 >() ) )
if( Ranges< be::uint8 >( _element->_rangeExpr ).isInRanges( Translator( _element ).get< be::uint8 >() ) )
status = eStatusValid;
break;
}
case eTypeInt16 :
{
if( Ranges< be::int16 >( _element->_rangeExpr ).isInRanges( Translator( _element.get() ).get< be::int16 >() ) )
if( Ranges< be::int16 >( _element->_rangeExpr ).isInRanges( Translator( _element ).get< be::int16 >() ) )
status = eStatusValid;
break;
}
case eTypeUInt16 :
{
if( Ranges< be::uint16 >( _element->_rangeExpr ).isInRanges( Translator( _element.get() ).get< be::uint16 >() ) )
if( Ranges< be::uint16 >( _element->_rangeExpr ).isInRanges( Translator( _element ).get< be::uint16 >() ) )
status = eStatusValid;
break;
}
case eTypeInt32 :
{
if( Ranges< be::int32 >( _element->_rangeExpr ).isInRanges( Translator( _element.get() ).get< be::int32 >() ) )
if( Ranges< be::int32 >( _element->_rangeExpr ).isInRanges( Translator( _element ).get< be::int32 >() ) )
status = eStatusValid;
break;
}
case eTypeUInt32 :
{
if( Ranges< be::uint32 >( _element->_rangeExpr ).isInRanges( Translator( _element.get() ).get< be::uint32 >() ) )
if( Ranges< be::uint32 >( _element->_rangeExpr ).isInRanges( Translator( _element ).get< be::uint32 >() ) )
status = eStatusValid;
break;
}
case eTypeInt64 :
{
if( Ranges< be::int64 >( _element->_rangeExpr ).isInRanges( Translator( _element.get() ).get< be::int64 >() ) )
if( Ranges< be::int64 >( _element->_rangeExpr ).isInRanges( Translator( _element ).get< be::int64 >() ) )
status = eStatusValid;
break;
}
case eTypeUInt64 :
{
if( Ranges< be::uint64 >( _element->_rangeExpr ).isInRanges( Translator( _element.get() ).get< be::uint64 >() ) )
if( Ranges< be::uint64 >( _element->_rangeExpr ).isInRanges( Translator( _element ).get< be::uint64 >() ) )
status = eStatusValid;
break;
}
case eTypeFloat :
{
if( Ranges< float >( _element->_rangeExpr ).isInRanges( Translator( _element.get() ).get< float >() ) )
if( Ranges< float >( _element->_rangeExpr ).isInRanges( Translator( _element ).get< float >() ) )
status = eStatusValid;
break;
}
case eTypeDouble :
{
if( Ranges< double >( _element->_rangeExpr ).isInRanges( Translator( _element.get() ).get< double >() ) )
if( Ranges< double >( _element->_rangeExpr ).isInRanges( Translator( _element ).get< double >() ) )
status = eStatusValid;
break;
}
case eTypeIeeeExtended :
{
if( Ranges< be::ieeeExtended >( _element->_rangeExpr ).isInRanges( Translator( _element.get() ).get< be::ieeeExtended >() ) )
if( Ranges< be::ieeeExtended >( _element->_rangeExpr ).isInRanges( Translator( _element ).get< be::ieeeExtended >() ) )
status = eStatusValid;
break;
}

case eTypeAscii :
case eTypeHexa :
{
std::string orig = Translator( _element.get() ).get();
std::string orig = Translator( _element ).get();
std::string lowCase = orig;
std::string upCase = orig;
std::transform( lowCase.begin(), lowCase.end(), lowCase.begin(), ::tolower );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ union NumberData
class Translator
{
public:
Translator( const basic_element::Element* elem )
Translator( const std::shared_ptr< basic_element::Element > elem )
: _ref( elem )
{
}
Expand All @@ -41,7 +41,7 @@ class Translator
void getOrderedData( char* buffer );

private:
const basic_element::Element* _ref;
const std::shared_ptr< basic_element::Element > _ref;
};

}
Expand Down
184 changes: 92 additions & 92 deletions libraries/ElementChecker/test/translatorTest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,58 +28,58 @@ BOOST_AUTO_TEST_CASE( element_checker_translator_1 )
spec.setFromString( jsonString );
std::shared_ptr< spec_reader::SpecNode > node = spec.getFirstNode();

basic_element::Element elem( node );
std::shared_ptr< basic_element::Element > elem( new basic_element::Element( node ) );

{
Translator tr = Translator( &elem );
Translator tr = Translator( elem );
BOOST_CHECK_THROW( tr.get< std::string >(), std::runtime_error );
}
{
const char buff[4] { 'W', 'A', 'V', 'E' };
elem.set( (const char*)&buff, 4 );

BOOST_CHECK_EQUAL( Translator( &elem ).get< std::string >(), "WAVE" );
BOOST_CHECK_EQUAL( Translator( &elem ).get< std::vector< int > >().at(0), 1163280727 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(0), 87 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(1), 65 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(2), 86 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(3), 69 );
BOOST_CHECK_EQUAL( Translator( &elem ).get< int >(), 1163280727 );
BOOST_CHECK_THROW( Translator( &elem ).get< short >(), std::runtime_error );
elem->set( (const char*)&buff, 4 );

BOOST_CHECK_EQUAL( Translator( elem ).get< std::string >(), "WAVE" );
BOOST_CHECK_EQUAL( Translator( elem ).get< std::vector< int > >().at(0), 1163280727 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(0), 87 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(1), 65 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(2), 86 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(3), 69 );
BOOST_CHECK_EQUAL( Translator( elem ).get< int >(), 1163280727 );
BOOST_CHECK_THROW( Translator( elem ).get< short >(), std::runtime_error );
}
{
const char buff[2] { 'a', 'b' };
elem.set( (const char*)&buff, 2 );

BOOST_CHECK_EQUAL( Translator( &elem ).get< std::string >(), "ab" );
BOOST_CHECK_EQUAL( Translator( &elem ).get< std::vector< short > >().at(0), 25185 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(0), 97 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(1), 98 );
BOOST_CHECK_EQUAL( Translator( &elem ).get< short >(), 25185 );
BOOST_CHECK_THROW( Translator( &elem ).get< int >(), std::runtime_error );
BOOST_CHECK_THROW( Translator( &elem ).get< std::vector< int > >(), std::runtime_error );
elem->set( (const char*)&buff, 2 );

BOOST_CHECK_EQUAL( Translator( elem ).get< std::string >(), "ab" );
BOOST_CHECK_EQUAL( Translator( elem ).get< std::vector< short > >().at(0), 25185 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(0), 97 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(1), 98 );
BOOST_CHECK_EQUAL( Translator( elem ).get< short >(), 25185 );
BOOST_CHECK_THROW( Translator( elem ).get< int >(), std::runtime_error );
BOOST_CHECK_THROW( Translator( elem ).get< std::vector< int > >(), std::runtime_error );
}
{
const char buff[4] { 'W', 'A', 'V', 'E' };
elem.set( (const char*)&buff, 4 );
elem->set( (const char*)&buff, 4 );

BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeDefault ), "" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeNumbers ), "87658669" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeAscii ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeHexa ), "57415645" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeRaw ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeDefault ), "" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeNumbers ), "87658669" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeAscii ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeHexa ), "57415645" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeRaw ), "WAVE" );

}
{
basic_element::Element data( node );
std::shared_ptr< basic_element::Element > data( new basic_element::Element( node ) );
const char buff[4] { 'W', 'A', 'V', 'E' };
data.set( (const char*)&buff, 4 );
data->set( (const char*)&buff, 4 );

BOOST_CHECK_EQUAL( Translator( &data ).get( eDisplayTypeDefault ), "" );
BOOST_CHECK_EQUAL( Translator( &data ).get( eDisplayTypeNumbers ), "87658669" );
BOOST_CHECK_EQUAL( Translator( &data ).get( eDisplayTypeAscii ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( &data ).get( eDisplayTypeHexa ), "57415645" );
BOOST_CHECK_EQUAL( Translator( &data ).get( eDisplayTypeRaw ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( data ).get( eDisplayTypeDefault ), "" );
BOOST_CHECK_EQUAL( Translator( data ).get( eDisplayTypeNumbers ), "87658669" );
BOOST_CHECK_EQUAL( Translator( data ).get( eDisplayTypeAscii ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( data ).get( eDisplayTypeHexa ), "57415645" );
BOOST_CHECK_EQUAL( Translator( data ).get( eDisplayTypeRaw ), "WAVE" );

}
}
Expand All @@ -105,26 +105,26 @@ BOOST_AUTO_TEST_CASE( element_checker_translator_2 )
spec.setFromString( jsonString );
std::shared_ptr< spec_reader::SpecNode > node = spec.getFirstNode();

basic_element::Element elem( node );
std::shared_ptr< basic_element::Element > elem( new basic_element::Element( node ) );

const char buff[4] { 'W', 'A', 'V', 'E' };
elem.set( (const char*)&buff, 4 );
elem->set( (const char*)&buff, 4 );


BOOST_CHECK_EQUAL( Translator( &elem ).get< std::string >(), "WAVE" );
BOOST_CHECK_EQUAL( Translator( &elem ).get< std::vector< int > >().at(0), 1163280727 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(0), 87 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(1), 65 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(2), 86 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(3), 69 );
BOOST_CHECK_EQUAL( Translator( &elem ).get< int >(), 1163280727 );
BOOST_CHECK_THROW( Translator( &elem ).get< short >(), std::runtime_error );
BOOST_CHECK_EQUAL( Translator( elem ).get< std::string >(), "WAVE" );
BOOST_CHECK_EQUAL( Translator( elem ).get< std::vector< int > >().at(0), 1163280727 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(0), 87 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(1), 65 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(2), 86 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(3), 69 );
BOOST_CHECK_EQUAL( Translator( elem ).get< int >(), 1163280727 );
BOOST_CHECK_THROW( Translator( elem ).get< short >(), std::runtime_error );

BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeDefault ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeNumbers ), "87658669" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeAscii ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeHexa ), "57415645" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeRaw ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeDefault ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeNumbers ), "87658669" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeAscii ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeHexa ), "57415645" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeRaw ), "WAVE" );

}
{
Expand All @@ -145,26 +145,26 @@ BOOST_AUTO_TEST_CASE( element_checker_translator_2 )
spec.setFromString( jsonString );
std::shared_ptr< spec_reader::SpecNode > node = spec.getFirstNode();

basic_element::Element elem( node );
std::shared_ptr< basic_element::Element > elem( new basic_element::Element( node ) );

const char buff[4] { 'W', 'A', 'V', 'E' };
elem.set( (const char*)&buff, 4 );
elem->set( (const char*)&buff, 4 );


BOOST_CHECK_EQUAL( Translator( &elem ).get< std::string >(), "WAVE" );
BOOST_CHECK_EQUAL( Translator( &elem ).get< std::vector< int > >().at(0), 1463899717 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(0), 87 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(1), 65 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(2), 86 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(3), 69 );
BOOST_CHECK_EQUAL( Translator( &elem ).get< int >(), 1463899717 );
BOOST_CHECK_THROW( Translator( &elem ).get< short >(), std::runtime_error );
BOOST_CHECK_EQUAL( Translator( elem ).get< std::string >(), "WAVE" );
BOOST_CHECK_EQUAL( Translator( elem ).get< std::vector< int > >().at(0), 1463899717 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(0), 87 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(1), 65 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(2), 86 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(3), 69 );
BOOST_CHECK_EQUAL( Translator( elem ).get< int >(), 1463899717 );
BOOST_CHECK_THROW( Translator( elem ).get< short >(), std::runtime_error );

BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeDefault ), "1463899717" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeNumbers ), "87658669" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeAscii ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeHexa ), "57415645" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeRaw ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeDefault ), "1463899717" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeNumbers ), "87658669" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeAscii ), "WAVE" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeHexa ), "57415645" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeRaw ), "WAVE" );

}
{
Expand All @@ -185,24 +185,24 @@ BOOST_AUTO_TEST_CASE( element_checker_translator_2 )
spec.setFromString( jsonString );
std::shared_ptr< spec_reader::SpecNode > node = spec.getFirstNode();

basic_element::Element elem( node );
std::shared_ptr< basic_element::Element > elem( new basic_element::Element( node ) );

const char buff[1] { 'W' };
elem.set( (const char*)&buff, 1 );
elem->set( (const char*)&buff, 1 );


BOOST_CHECK_EQUAL( Translator( &elem ).get< std::string >(), "W" );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(0), 87 );
BOOST_CHECK_EQUAL( Translator( &elem ).get< char >(), 'W' );
BOOST_CHECK_THROW( Translator( &elem ).get< std::vector< int > >(), std::runtime_error );
BOOST_CHECK_THROW( Translator( &elem ).get< int >(), std::runtime_error );
BOOST_CHECK_THROW( Translator( &elem ).get< short >(), std::runtime_error );
BOOST_CHECK_EQUAL( Translator( elem ).get< std::string >(), "W" );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(0), 87 );
BOOST_CHECK_EQUAL( Translator( elem ).get< char >(), 'W' );
BOOST_CHECK_THROW( Translator( elem ).get< std::vector< int > >(), std::runtime_error );
BOOST_CHECK_THROW( Translator( elem ).get< int >(), std::runtime_error );
BOOST_CHECK_THROW( Translator( elem ).get< short >(), std::runtime_error );

BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeDefault ), "87" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeNumbers ), "87" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeAscii ), "W" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeHexa ), "57" );
BOOST_CHECK_EQUAL( Translator( &elem ).get( eDisplayTypeRaw ), "W" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeDefault ), "87" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeNumbers ), "87" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeAscii ), "W" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeHexa ), "57" );
BOOST_CHECK_EQUAL( Translator( elem ).get( eDisplayTypeRaw ), "W" );
}

{
Expand All @@ -224,24 +224,24 @@ BOOST_AUTO_TEST_CASE( element_checker_translator_2 )
spec.setFromString( jsonString );
std::shared_ptr< spec_reader::SpecNode > node = spec.getFirstNode();

basic_element::Element elem( node );
std::shared_ptr< basic_element::Element > elem( new basic_element::Element( node ) );

const char buff[4] { 0x01, 0x00, 0x00, 0x00 };
elem.set( (const char*)&buff, 4 );

BOOST_CHECK_EQUAL( elem._isBigEndian, false );
BOOST_CHECK_EQUAL( Translator( &elem ).get< int >(), 1 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().size(), 4 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(0), 1 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(1), 0 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(2), 0 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< char > >().at(3), 0 );

BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< short > >().size(), 2 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< short > >().at(0), 1 );
BOOST_CHECK_EQUAL( (short) Translator( &elem ).get< std::vector< short > >().at(1), 0 );
elem->set( (const char*)&buff, 4 );

BOOST_CHECK_EQUAL( elem->_isBigEndian, false );
BOOST_CHECK_EQUAL( Translator( elem ).get< int >(), 1 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().size(), 4 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(0), 1 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(1), 0 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(2), 0 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< char > >().at(3), 0 );

BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< short > >().size(), 2 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< short > >().at(0), 1 );
BOOST_CHECK_EQUAL( (short) Translator( elem ).get< std::vector< short > >().at(1), 0 );

BOOST_CHECK_THROW( Translator( &elem ).get< short >(), std::runtime_error );
BOOST_CHECK_THROW( Translator( elem ).get< short >(), std::runtime_error );
}
}

Expand Down

0 comments on commit ed1046c

Please sign in to comment.