-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new version of Comparator with unit tests
issue #31
- Loading branch information
Valentin Noel
committed
Nov 13, 2013
1 parent
584a9d4
commit b495962
Showing
5 changed files
with
388 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,133 @@ | ||
#include "Comparator.hpp" | ||
|
||
#include <iostream> | ||
#include <Common/log.hpp> | ||
#include <Common/common.hpp> | ||
|
||
#include <BasicElement/Element.hpp> | ||
#include <ElementChecker/Checker/Checker.hpp> | ||
|
||
#include <SpecReader/Specification.hpp> | ||
#include <FileReader/FileReader.hpp> | ||
#include <ReportGenerator/Report.hpp> | ||
|
||
namespace comparator | ||
{ | ||
|
||
Comparator::Comparator() | ||
{ | ||
} | ||
|
||
std::shared_ptr< basic_element::Element > Comparator::getElement( const std::shared_ptr< spec_reader::SpecNode > node, const std::shared_ptr< basic_element::Element > previous ) | ||
void Comparator::check( spec_reader::Specification& spec, file_reader::FileReader& file, report_generator::Report& report ) | ||
{ | ||
// switch( node->getType() ) | ||
// { | ||
// case eTypeData: | ||
// { | ||
// std::shared_ptr< basic_element::Data > d( new basic_element::Data( node, previous ) ); | ||
// return static_cast< std::shared_ptr< basic_element::Element > >( d ); | ||
// } | ||
// case eTypeNumber: | ||
// { | ||
// std::shared_ptr< basic_element::Number > n( new basic_element::Number( node, previous ) ); | ||
// return static_cast< std::shared_ptr< basic_element::Element > >( n ); | ||
// } | ||
// default: break; | ||
// } | ||
return NULL; | ||
} | ||
std::shared_ptr< spec_reader::SpecNode > node = spec.getFirstNode(); | ||
std::shared_ptr< basic_element::Element > element( new basic_element::Element( node ) ); | ||
|
||
element_checker::Checker checker; | ||
size_t size = checker.getSize( element ); | ||
|
||
char buffer1[ size ]; | ||
if( ! file.readData( buffer1, size ) ) | ||
throw std::runtime_error( "End of file, cannot read data" ); | ||
element->set( buffer1, size ); | ||
|
||
checker.check( element ); | ||
|
||
void Comparator::check( spec_reader::Specification& spec, file_reader::FileReader& reader, report_generator::Report& report ) | ||
std::shared_ptr< basic_element::Element > parent; | ||
if( element->_isGroup ) | ||
parent = element; | ||
|
||
LOG_ERROR( element->_id << ": " << statusMap.at( element->_status ) << " | " << element->_dispValue ); | ||
|
||
while( ( node = element->next() ) != nullptr ) // if end of specification : stop | ||
{ | ||
std::shared_ptr< basic_element::Element > previous = element; | ||
|
||
switch( element->_status ) | ||
{ | ||
case eStatusInvalidButOptional : | ||
case eStatusInvalidButSkip : | ||
case eStatusInvalidForIteration : | ||
case eStatusSkip : | ||
{ | ||
LOG_WARNING( "COMPARATOR : Go BACK in file" ); | ||
file.goBack( size ); | ||
previous = element->getPrevious(); | ||
break; | ||
} | ||
default: break; | ||
} | ||
|
||
element = std::make_shared< basic_element::Element >( node, previous, parent ); | ||
size = checker.getSize( element ); | ||
|
||
if( size > ( file.getLength() - file.getPosition() ) && ( isInUnorderedGroup( element ) || element->_isOptional ) ) | ||
{ | ||
LOG_WARNING( "Critical remaining file data size: " << size << "/" << file.getLength() - file.getPosition() ); | ||
size = file.getLength() - file.getPosition(); | ||
} | ||
|
||
LOG_WARNING( "Size: " << size << "/" << file.getLength() - file.getPosition() ); | ||
char buffer[ size ]; | ||
if( ! file.readData( buffer, size ) ) | ||
throw std::runtime_error( "End of file, cannot read data" ); | ||
element->set( buffer, size ); | ||
|
||
checker.check( element ); | ||
LOG_WARNING( "COMPARATOR : " << element->_id << " << Prev: " << previous << " - " << statusMap.at( element->_status ) << " | " << element->_dispValue ); | ||
|
||
parent = getNextParent( element, node ); | ||
} | ||
|
||
report.init( checker.getElementList() ); | ||
report.print(); // @todelete | ||
} | ||
|
||
bool Comparator::isInUnorderedGroup( const std::shared_ptr< basic_element::Element > element ) | ||
{ | ||
std::cout << "check start" << std::endl; | ||
|
||
// std::shared_ptr< spec_reader::SpecNode > s = NULL; | ||
// std::shared_ptr< basic_element::Number > prev; | ||
// std::shared_ptr< basic_element::Element > e = getElement( spec.getFirstNode( ), prev ); // get first element | ||
|
||
// while( ( s = e->next() ) != NULL ) // if end of specification : stop | ||
// { | ||
// e = getElement( s, e ); // get an element | ||
// // e->check(); // check it | ||
|
||
// if( e->getStatus() == eStatusInvalidButOptional || | ||
// e->getStatus() == eStatusInvalidButSkip ) | ||
// { | ||
// std::cout << "go back in raw file" << std::endl; | ||
// continue; | ||
// } | ||
|
||
// report.addElement( e ); // add the element to report | ||
|
||
// if( e->getUniqueId() > 20 ) // @todelete | ||
// break; | ||
// } | ||
|
||
std::cout << "check end" << std::endl; | ||
std::shared_ptr< basic_element::Element > parent = element->getParent(); | ||
while( parent != nullptr ) | ||
{ | ||
if( ! parent->_isOrdered ) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
std::shared_ptr< basic_element::Element > Comparator::getNextParent( const std::shared_ptr< basic_element::Element > element, | ||
const std::shared_ptr< spec_reader::SpecNode > node ) | ||
{ | ||
std::shared_ptr< basic_element::Element > parent = element->getParent(); | ||
bool isLastInGroup = ( node->next() == nullptr && ( parent->_isOrdered || ( ! parent->_isOrdered && element->_status == eStatusInvalidButSkip ) ) ); | ||
|
||
if( element->_isGroup | ||
&& ! element->_checkedGroup | ||
&& element->_status != eStatusInvalidButOptional | ||
&& element->_status != eStatusInvalidButSkip | ||
&& element->_status != eStatusInvalidForIteration ) | ||
{ | ||
LOG_WARNING( "COMPARATOR::getNextParent - CASE 1" ); | ||
parent = element; | ||
} | ||
else if( isLastInGroup && element->getParent() != nullptr ) | ||
{ | ||
LOG_WARNING( "COMPARATOR::getNextParent - CASE 2" ); | ||
parent = element->getParent(); | ||
while( parent->getSpecNode()->next() == nullptr && parent->getParent() != nullptr ) | ||
{ | ||
LOG_WARNING( "parent of : " << parent->_id ); | ||
parent = parent->getParent(); | ||
} | ||
parent = parent->getParent(); | ||
} | ||
else | ||
{ | ||
LOG_WARNING( "COMPARATOR::getNextParent - CASE 3" ); | ||
parent = element->getParent(); | ||
} | ||
return parent; | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Import( 'project', 'libs' ) | ||
|
||
project.UnitTest( | ||
target = "TestComparator", | ||
sources = ['comparatorTest.cpp'], | ||
libraries = [ | ||
libs.comparator, | ||
libs.boost_unit_test_framework, | ||
] | ||
) | ||
|
||
|
Oops, something went wrong.