Skip to content

Commit

Permalink
add modifications to Comparator for GroupSize check support (and unit…
Browse files Browse the repository at this point in the history
… tests)

issue #31
  • Loading branch information
Valentin Noel committed Sep 12, 2013
1 parent db14f07 commit c5bf8ce
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 0 deletions.
1 change: 1 addition & 0 deletions libraries/comparator/src/Comparator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions libraries/comparator/src/Comparator.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Expand Down Expand Up @@ -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 );
}

}

1 change: 1 addition & 0 deletions libraries/comparator/tests/comparatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ BOOST_AUTO_TEST_SUITE_END()
#include "comparatorTestsFeatures.hpp"
#include "comparatorTestsRepetitions.hpp"
#include "comparatorTestsOptional.hpp"
#include "comparatorTestsGroupSize.hpp"
261 changes: 261 additions & 0 deletions libraries/comparator/tests/comparatorTestsGroupSize.hpp
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit c5bf8ce

Please sign in to comment.