Skip to content

Commit

Permalink
add new version of Comparator with unit tests
Browse files Browse the repository at this point in the history
issue #31
  • Loading branch information
Valentin Noel committed Nov 13, 2013
1 parent 584a9d4 commit b495962
Show file tree
Hide file tree
Showing 5 changed files with 388 additions and 47 deletions.
3 changes: 1 addition & 2 deletions libraries/Comparator/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ project.StaticLibrary(
libraries = [
libs.common,
libs.fileReader,
libs.specReader,
libs.basicElement,
libs.elementChecker,
libs.reportGenerator,
],
shared = True
Expand Down
157 changes: 114 additions & 43 deletions libraries/Comparator/src/Comparator/Comparator.cpp
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;
}

}


11 changes: 9 additions & 2 deletions libraries/Comparator/src/Comparator/Comparator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ namespace report_generator
class Report;
}

namespace comparator
{

class Comparator
{
public:
Comparator();
void check( spec_reader::Specification& spec, file_reader::FileReader& reader, report_generator::Report& report );

private:
std::shared_ptr< basic_element::Element > getElement( const std::shared_ptr< spec_reader::SpecNode > node, const std::shared_ptr< basic_element::Element > previous );
bool isInUnorderedGroup( const std::shared_ptr< basic_element::Element > element );
std::shared_ptr< basic_element::Element > getNextParent( const std::shared_ptr< basic_element::Element > element,
const std::shared_ptr< spec_reader::SpecNode > node );

};

}

#endif
12 changes: 12 additions & 0 deletions libraries/Comparator/test/SConscript
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,
]
)


Loading

0 comments on commit b495962

Please sign in to comment.