diff --git a/libraries/ElementChecker/src/ElementChecker/Checker/Checker.cpp b/libraries/ElementChecker/src/ElementChecker/Checker/Checker.cpp index bbd4b8e..cdf408c 100644 --- a/libraries/ElementChecker/src/ElementChecker/Checker/Checker.cpp +++ b/libraries/ElementChecker/src/ElementChecker/Checker/Checker.cpp @@ -39,7 +39,7 @@ Checker::Checker() void Checker::check( const ShPtrElement element ) { - if( element->_size == 0 && element->_countExpr.empty() ) + if( element->_data.size() == 0 && element->_countExpr.empty() ) { std::string warning = "Null data size "; // LOG_WARNING( "[checker] " << element->_id << ": " << warning ); @@ -191,14 +191,46 @@ void Checker::check( const ShPtrElement element ) size_t Checker::getSize( const ShPtrElement element ) { - if( element->_type == eTypeHexa ) - element->_size = element->_size >> 1; - if( ! element->_countExpr.empty() && element->_size == 0 ) + size_t size = 0; + try { - element->_size = _exprParser->getExpressionResult< size_t >( element->_countExpr ); - LOG_TRACE( "[checker] get " << element->_id << "'s size: " << element->_size ); + if( ! element->_values.empty() ) + { + size = element->_values.at( 0 ).size(); + for( std::string value : element->_values ) + if( value.size() != size ) + throw std::runtime_error( "[checker] Values must have the same size (" + element->_id + ")" ); + } + + switch( element->_type ) + { + case eTypeHexa : size = size >> 1; break; + case eTypeInt8 : + case eTypeUInt8 : return 1; + case eTypeInt16 : + case eTypeUInt16 : return 2; + case eTypeInt32 : + case eTypeUInt32 : + case eTypeFloat : return 4; + case eTypeInt64 : + case eTypeUInt64 : + case eTypeDouble : return 8; + case eTypeIeeeExtended : return 10; + default: break; + } + + if( ! element->_countExpr.empty() && size == 0 ) + { + size = _exprParser->getExpressionResult< size_t >( element->_countExpr ); + LOG_TRACE( "[checker] get " << element->_id << "'s size: " << size ); + } + return size; + } + catch( std::runtime_error e ) + { + LOG_ERROR( "[checker] " << e.what() << " (" << element->_id << ")" ); } - return element->_size; + return size; } bool Checker::isIterationValid( const ShPtrElement element, std::string& errorMessage ) diff --git a/libraries/ElementChecker/src/ElementChecker/Translator/Translator.tcc b/libraries/ElementChecker/src/ElementChecker/Translator/Translator.tcc index 2a6fb17..9adc178 100644 --- a/libraries/ElementChecker/src/ElementChecker/Translator/Translator.tcc +++ b/libraries/ElementChecker/src/ElementChecker/Translator/Translator.tcc @@ -59,7 +59,7 @@ std::string Translator::get< std::string >() // get ascii character ! template< typename NumberType > std::vector< NumberType > Translator::convertToVector() { - if( _ref->_size % sizeof( NumberType ) != 0 ) + if( _ref->_data.size() % sizeof( NumberType ) != 0 ) throw std::runtime_error( "invalid data size" ); std::vector< NumberType > vector; @@ -74,7 +74,7 @@ std::vector< NumberType > Translator::convertToVector() else std::memcpy( buffer, &_ref->_data[0], _ref->_data.size() ); - std::memcpy( num.data, &buffer[i], _ref->_size ); + std::memcpy( num.data, &buffer[i], _ref->_data.size() ); delete[] buffer; vector.push_back( num.value ); } diff --git a/libraries/ElementChecker/test/checkerTestCount.hpp b/libraries/ElementChecker/test/checkerTestCount.hpp index 3c1c5d5..ffd9ffa 100644 --- a/libraries/ElementChecker/test/checkerTestCount.hpp +++ b/libraries/ElementChecker/test/checkerTestCount.hpp @@ -49,24 +49,24 @@ BOOST_AUTO_TEST_CASE( element_checker_checker_count ) std::shared_ptr< basic_element::Element > elem1( new basic_element::Element( node ) ); BOOST_CHECK_EQUAL( elem1->_status, eStatusNotChecked ); - BOOST_CHECK_EQUAL( elem1->_size, 4 ); BOOST_CHECK_EQUAL( checker.getSize( elem1 ), 4 ); elem1->set( (const char*)&buff1, checker.getSize( elem1 ) ); + BOOST_CHECK_EQUAL( elem1->_data.size(), 4 ); checker.check( elem1 ); BOOST_CHECK_EQUAL( elem1->_status, eStatusPassOver ); std::shared_ptr< basic_element::Element > elem2( new basic_element::Element( elem1->next(), elem1 ) ); - BOOST_CHECK_EQUAL( elem2->_size, 0 ); + BOOST_CHECK_EQUAL( elem2->_data.size(), 0 ); BOOST_CHECK_EQUAL( checker.getSize( elem2 ), 5 ); elem2->set( (const char*)&buff2, checker.getSize( elem2 ) ); checker.check( elem2 ); - BOOST_CHECK_EQUAL( elem2->_size, 5 ); + BOOST_CHECK_EQUAL( elem2->_data.size(), 5 ); BOOST_CHECK_EQUAL( elem2->_status, eStatusPassOver ); std::shared_ptr< basic_element::Element > elem3( new basic_element::Element( elem2->next(), elem2 ) ); - BOOST_CHECK_EQUAL( elem3->_size, 5 ); BOOST_CHECK_EQUAL( checker.getSize( elem3 ), 5 ); elem3->set( (const char*)&buff3, checker.getSize( elem3 ) ); + BOOST_CHECK_EQUAL( elem3->_data.size(), 5 ); checker.check( elem3 ); BOOST_CHECK_EQUAL( elem3->_status, eStatusValid ); @@ -88,24 +88,24 @@ BOOST_AUTO_TEST_CASE( element_checker_checker_count ) std::shared_ptr< basic_element::Element > elem1( new basic_element::Element( node ) ); BOOST_CHECK_EQUAL( elem1->_status, eStatusNotChecked ); - BOOST_CHECK_EQUAL( elem1->_size, 4 ); BOOST_CHECK_EQUAL( checker.getSize( elem1 ), 4 ); elem1->set( (const char*)&buff1, checker.getSize( elem1 ) ); + BOOST_CHECK_EQUAL( elem1->_data.size(), 4 ); checker.check( elem1 ); BOOST_CHECK_EQUAL( elem1->_status, eStatusPassOver ); std::shared_ptr< basic_element::Element > elem2( new basic_element::Element( elem1->next(), elem1 ) ); - BOOST_CHECK_EQUAL( elem2->_size, 0 ); + BOOST_CHECK_EQUAL( elem2->_data.size(), 0 ); BOOST_CHECK_EQUAL( checker.getSize( elem2 ), 3 ); elem2->set( (const char*)&buff2, checker.getSize( elem2 ) ); checker.check( elem2 ); - BOOST_CHECK_EQUAL( elem2->_size, 3 ); + BOOST_CHECK_EQUAL( elem2->_data.size(), 3 ); BOOST_CHECK_EQUAL( elem2->_status, eStatusPassOver ); std::shared_ptr< basic_element::Element > elem3( new basic_element::Element( elem2->next(), elem2 ) ); - BOOST_CHECK_EQUAL( elem3->_size, 5 ); BOOST_CHECK_EQUAL( checker.getSize( elem3 ), 5 ); elem3->set( (const char*)&buff3, checker.getSize( elem3 ) ); + BOOST_CHECK_EQUAL( elem3->_data.size(), 5 ); checker.check( elem3 ); BOOST_CHECK_EQUAL( elem3->_status, eStatusValid );