From 4941db17ae55a1df14e6643629df807bae05e680 Mon Sep 17 00:00:00 2001 From: Valentin Noel Date: Wed, 11 Dec 2013 13:51:36 +0100 Subject: [PATCH] add modifications to Comparator for ASCII WORD support (and unit tests) issue #31 --- .../Comparator/src/Comparator/Comparator.cpp | 30 ++++++++++ .../Comparator/src/Comparator/Comparator.hpp | 1 + libraries/Comparator/test/comparatorTest.cpp | 55 +++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/libraries/Comparator/src/Comparator/Comparator.cpp b/libraries/Comparator/src/Comparator/Comparator.cpp index c9863cb..0f8fcde 100644 --- a/libraries/Comparator/src/Comparator/Comparator.cpp +++ b/libraries/Comparator/src/Comparator/Comparator.cpp @@ -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 ) ) @@ -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" ); @@ -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(); diff --git a/libraries/Comparator/src/Comparator/Comparator.hpp b/libraries/Comparator/src/Comparator/Comparator.hpp index 6aa2c9d..b7e8ad7 100644 --- a/libraries/Comparator/src/Comparator/Comparator.hpp +++ b/libraries/Comparator/src/Comparator/Comparator.hpp @@ -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 ); diff --git a/libraries/Comparator/test/comparatorTest.cpp b/libraries/Comparator/test/comparatorTest.cpp index 39c267d..0e22257 100644 --- a/libraries/Comparator/test/comparatorTest.cpp +++ b/libraries/Comparator/test/comparatorTest.cpp @@ -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()