From c5bf8ce501d4a893089015879d912f2883729f3d Mon Sep 17 00:00:00 2001 From: Valentin Noel Date: Thu, 12 Sep 2013 17:37:11 +0200 Subject: [PATCH] add modifications to Comparator for GroupSize check support (and unit tests) issue #31 --- libraries/comparator/src/Comparator.hpp | 1 + libraries/comparator/src/Comparator.tcc | 22 ++ .../comparator/tests/comparatorTests.cpp | 1 + .../tests/comparatorTestsGroupSize.hpp | 261 ++++++++++++++++++ 4 files changed, 285 insertions(+) create mode 100644 libraries/comparator/tests/comparatorTestsGroupSize.hpp diff --git a/libraries/comparator/src/Comparator.hpp b/libraries/comparator/src/Comparator.hpp index 7744882..ad097c3 100644 --- a/libraries/comparator/src/Comparator.hpp +++ b/libraries/comparator/src/Comparator.hpp @@ -47,6 +47,7 @@ class Comparator std::shared_ptr< be::Element > getElementFromNode( const spec_reader::SpecNode& node ); void extractRepetition( size_t& repetNumber, Vector< size_t >::Pair& repetRange, const Vector< std::string >::Pair& nodeRepetitions ); + size_t extractGroupSize( const std::string& groupSizeExpr ); private: filereader::FileReader* _file; diff --git a/libraries/comparator/src/Comparator.tcc b/libraries/comparator/src/Comparator.tcc index 7fee238..2e8f3fa 100644 --- a/libraries/comparator/src/Comparator.tcc +++ b/libraries/comparator/src/Comparator.tcc @@ -248,6 +248,22 @@ void Comparator::checkNode( const sr::SpecNode& node, report_generator::ReportNo // children nodes check if( node.hasGroup() && sentToReport ) checkNode( node.firstChild(), *nextReportNode, true ); + + // check group size + if( node.hasGroup() && ! node.getGroupSize().empty() ) + { + int sizeDiff = extractGroupSize( node.getGroupSize() ) - ( _file->getPosition() - ( initPos + element->getSize() ) ); + std::stringstream warning; + if( sizeDiff > 0 ) + warning << "Group size difference: " << sizeDiff << " missing bytes "; + if( sizeDiff < 0 ) + warning << "Group size difference: " << abs( sizeDiff ) << " unexpected bytes "; + if( ! warning.str().empty() ) + { + nextReportNode->getSecond()->begin()->second.data()->addWarningLabel( warning.str() ); + LOG_WARNING( warning.str() ); + } + } // next node check if( ! node.isLastNode() ) @@ -317,5 +333,11 @@ void Comparator::extractRepetition( size_t& repetNumber, Vector< size_t >::Pair& } } +size_t Comparator::extractGroupSize( const std::string& groupSizeExpr ) +{ + be::expression_parser::ExpressionParser repetParser( _elementList ); + return repetParser.getExpressionResult< size_t >( groupSizeExpr ); +} + } diff --git a/libraries/comparator/tests/comparatorTests.cpp b/libraries/comparator/tests/comparatorTests.cpp index ca03912..fe82a99 100644 --- a/libraries/comparator/tests/comparatorTests.cpp +++ b/libraries/comparator/tests/comparatorTests.cpp @@ -56,3 +56,4 @@ BOOST_AUTO_TEST_SUITE_END() #include "comparatorTestsFeatures.hpp" #include "comparatorTestsRepetitions.hpp" #include "comparatorTestsOptional.hpp" +#include "comparatorTestsGroupSize.hpp" diff --git a/libraries/comparator/tests/comparatorTestsGroupSize.hpp b/libraries/comparator/tests/comparatorTestsGroupSize.hpp new file mode 100644 index 0000000..3c5395c --- /dev/null +++ b/libraries/comparator/tests/comparatorTestsGroupSize.hpp @@ -0,0 +1,261 @@ + +BOOST_AUTO_TEST_SUITE( comparator_test_suite_group_size ) + +BOOST_AUTO_TEST_CASE( comparator_comparator_validation_group_size ) +{ + LOG_WARNING( ">>> comparator_comparator_validation_group_size <<<" ); + { + std::string jsonString = R"*( + { + "standard": + { + "id": "test", + "extension": [ + "ext1" + ] + }, + "header": [ + { + "id": "root1", + "label": "Root1", + "type": "ascii", + "values": "root1", + "groupSize": 10, + "group": [ + { + "id": "element1", + "label": "Element 1", + "type": "uint8" + }, + { + "id": "element2", + "label": "Element 2", + "type": "ascii", + "values": "1" + }, + { + "id": "element3", + "label": "Element 3", + "type": "uint64" + } + ] + }, + { + "id": "root2", + "label": "Root2", + "type": "ascii", + "values": "root2", + "groupSize": 1, + "group": [ + { + "id": "element4", + "label": "Element 4", + "type": "uint8" + }, + { + "id": "element5", + "label": "Element 5", + "type": "ascii", + "values": "1" + } + ] + }, + { + "id": "root3", + "label": "Root3", + "type": "ascii", + "values": "root3", + "groupSize": 3, + "group": [ + { + "id": "element6", + "label": "Element 6", + "type": "uint16" + } + ] + }, + { + "id": "elementEnd", + "label": "Element End", + "type": "ascii", + "values": "end" + } + ] + } + )*"; + + sr::Specification spec; + sr::SpecList specList; + + spec.setFromString( jsonString ); + specList.addSpecification( spec ); + + std::stringbuf buffer; + std::string str = "root1"; + str += " "; + str += "1"; + str += "00000001"; + str += "root2"; + str += " "; + str += "1"; + str += "root3"; + str += " "; + str += "end"; + buffer.str( str ); + fr::FileReader file( &buffer ); + + Comparator comp( &file, specList ); + + rg::Report report; + comp.compare( "test", report ); + + rg::Transform tr( report ); + rg::Export exporter( tr.transformTree( rg::Transform::eReportTypeXml ) ); + + LOG_INFO( "\n==== REPORT ====" ); + LOG_INFO( exporter.getXmlString() ); + + std::istringstream xmlStream( exporter.getXmlString() ); + std::istringstream jsonStream( jsonString ); + bpt::ptree xmlReport; + bpt::ptree jsonReport; + bpt::read_xml ( xmlStream, xmlReport ); + bpt::read_json( jsonStream, jsonReport ); + + BOOST_CHECK_EQUAL( xmlReport.size(), jsonReport.get_child( "header" ).size() ); + } + { + std::string jsonString = R"*( + { + "standard": + { + "id": "test", + "extension": [ + "ext1" + ] + }, + "header": [ + { + "id": "root1", + "label": "Root1", + "type": "ascii", + "values": "root1", + "groupSize": 11, + "group": [ + { + "id": "element1", + "label": "Element 1", + "type": "uint8", + "groupSize": 10, + "group" : [ + { + "id": "element1.1", + "label": "Element 1.1", + "type": "ascii", + "values": "w" + }, + { + "id": "element12", + "label": "Element 1.2", + "type": "uint64", + "groupSize": "element1 / 32", + "group": [ + { + "id": "element1.2.1", + "label": "Element 1.2.1", + "type": "ascii", + "values": "0" + } + ] + } + ] + } + ] + }, + { + "id": "root2", + "label": "Root2", + "type": "ascii", + "values": "root2", + "groupSize": 7, + "group": [ + { + "id": "element2", + "label": "Element 2", + "type": "hexa", + "values": "61" + }, + { + "id": "element3", + "label": "Element 3", + "type": "ascii", + "values": "1", + "groupSize": 2, + "repeated": 2, + "group": [ + { + "id": "element31", + "label": "Element 3.1", + "type": "uint16" + } + ] + } + ] + }, + { + "id": "elementEnd", + "label": "Element End", + "type": "ascii", + "values": "end" + } + ] + } + )*"; + + sr::Specification spec; + sr::SpecList specList; + + spec.setFromString( jsonString ); + specList.addSpecification( spec ); + + std::stringbuf buffer; + std::string str = "root1"; + str += " "; + str += "w"; + str += " "; + str += "0"; + str += "root2"; + str += "a"; + str += "1"; + str += " A"; + // str += "1"; + // str += " A"; + str += "end"; + buffer.str( str ); + fr::FileReader file( &buffer ); + + Comparator comp( &file, specList ); + + rg::Report report; + comp.compare( "test", report ); + + rg::Transform tr( report ); + rg::Export exporter( tr.transformTree( rg::Transform::eReportTypeXml ) ); + + LOG_INFO( "\n==== REPORT ====" ); + LOG_INFO( exporter.getXmlString() ); + + std::istringstream xmlStream( exporter.getXmlString() ); + std::istringstream jsonStream( jsonString ); + bpt::ptree xmlReport; + bpt::ptree jsonReport; + bpt::read_xml ( xmlStream, xmlReport ); + bpt::read_json( jsonStream, jsonReport ); + + BOOST_CHECK_EQUAL( xmlReport.size(), jsonReport.get_child( "header" ).size() ); + } +} + + + +BOOST_AUTO_TEST_SUITE_END()