Skip to content

Commit

Permalink
add modifications to Comparator for ASCII WORD support (and unit tests)
Browse files Browse the repository at this point in the history
issue #31
  • Loading branch information
Valentin Noel committed Dec 11, 2013
1 parent 2139ab8 commit 4941db1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
30 changes: 30 additions & 0 deletions libraries/Comparator/src/Comparator/Comparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ void Comparator::check( spec_reader::Specification& spec, file_reader::FileReade

element_checker::Checker checker;
size_t size = checker.getSize( element );

if( size == 0 && ( size = getWordLength( element, file ) ) == 0 )
element->_warning.push_back( "[checker] Null data size " );

std::vector< char > buffer1;
if( ! file.readData( buffer1, size ) )
Expand Down Expand Up @@ -62,6 +65,9 @@ void Comparator::check( spec_reader::Specification& spec, file_reader::FileReade
break;
}

if( size == 0 && ( size = getWordLength( element, file ) ) == 0 )
element->_warning.push_back( "[checker] Null data size " );

std::vector< char > buffer;
if( ! file.readData( buffer, size ) )
throw std::runtime_error( "[comparator] End of file, cannot read data" );
Expand Down Expand Up @@ -104,6 +110,30 @@ bool Comparator::isInUnorderedGroup( const ShPtrElement element )
return false;
}

size_t Comparator::getWordLength( const ShPtrElement element, file_reader::FileReader& file )
{
if( element->_type != eTypeAscii )
return 0;

size_t length = 0;
char endChar;
std::vector< char > buff;

while( 1 )
{
if( ! file.readData( buff, 1 ) )
throw std::runtime_error( "[comparator] End of file, cannot read data" );
endChar = buff.at(0);
++length;
if( element->_endChar == endChar )
break;
}
LOG_TRACE( "[comparator] " << element->_id << " is a " << length << "-char word" );

file.goBack( length );
return length;
}

void Comparator::updateParentSize( const ShPtrElement element )
{
ShPtrElement parent = element->getParent();
Expand Down
1 change: 1 addition & 0 deletions libraries/Comparator/src/Comparator/Comparator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Comparator

private:
bool isInUnorderedGroup( const ShPtrElement element );
size_t getWordLength ( const ShPtrElement element, file_reader::FileReader& file );
void updateParentSize ( const ShPtrElement element );
ShPtrElement getNextParent ( const ShPtrElement element, const ShPtrSpecNode node );
void checkGroupSize ( const ShPtrElement element, file_reader::FileReader& file );
Expand Down
55 changes: 55 additions & 0 deletions libraries/Comparator/test/comparatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,59 @@ BOOST_AUTO_TEST_CASE( comparator_test_comparator_4 )
BOOST_CHECK_THROW( comp.check( spec, file, report ), std::runtime_error );
}

BOOST_AUTO_TEST_CASE( comparator_test_comparator_word )
{
LOG_INFO( "\n>>> comparator_test_comparator word <<<" );
std::string jsonString = R"*(
{
"content": [
{
"id": "value1",
"label": "Value1",
"type": "ascii"
},
{
"id": "value2",
"label": "Value2",
"type": "ascii",
"endsWith": "x"
},
{
"id": "valueEnd",
"label": "ValueEnd",
"type": "ascii",
"values": "END"
}
]
}
)*";

spec_reader::Specification spec;
spec.setFromString( jsonString );
std::shared_ptr< spec_reader::SpecNode > node = spec.getFirstNode();
BOOST_CHECK_EQUAL( node->getId(), "value1" );
BOOST_CHECK_EQUAL( node->next()->getId(), "value2" );
BOOST_CHECK_EQUAL( node->next()->next()->getId(), "valueEnd" );

Comparator comp;

std::stringbuf buffer;
file_reader::FileReader file( &buffer );

std::string str { 'H','E', 'L', 'L', 'O', '\0' };
str += { 'W', 'O', 'R', 'L', 'D', 'x' };
str += { 'E', 'N', 'D' };

buffer.sputn( str.c_str(), str.size() );
BOOST_CHECK_EQUAL( file.getLength(), str.size() );

report_generator::Report report;
report.printHelper();


comp.check( spec, file, report );

BOOST_CHECK_EQUAL( file.isEndOfFile(), true );
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 4941db1

Please sign in to comment.