diff --git a/libraries/report/src/Report/Report.cpp b/libraries/report/src/Report/Report.cpp index f5fb305..d7f28eb 100644 --- a/libraries/report/src/Report/Report.cpp +++ b/libraries/report/src/Report/Report.cpp @@ -3,8 +3,6 @@ #include #include -static const std::string kReport = "report"; - namespace report_generator { @@ -12,28 +10,32 @@ Report::Report() { } -Report::Report( const ReportTree& reportTree ) - : _basicElementTree( reportTree ) -{ -} - Report::~Report() { } - -void Report::addBasicElement( std::shared_ptr< be::Element > element ) +ReportNode Report::addFirstElement( std::shared_ptr< be::Element > element ) { - ReportTree tmpTree; - tmpTree.add( toKey( element->getUniqueId() ), element ); - _basicElementTree.add_child( kReport + ".", tmpTree ); + if( getSize() > 0 ) + { + LOG_WARNING( "addFirstElement: first node already exist."); + } + else + { + ReportTree tree; + tree.add( toKey( getSize() ), element ); + _basicElementTree.add_child( kReport + ".", tree ); + } + return getFirstNode(); } ReportNode Report::getFirstNode() { try { - return ReportNode( getBegin(), 0, getSize() ); + if( getSize() == 0 ) + throw std::runtime_error( "getFirstNode: empty tree." ); + return ReportNode( getBegin(), 0, &_basicElementTree ); } catch( const std::runtime_error& e ) { @@ -44,11 +46,23 @@ ReportNode Report::getFirstNode() ReportIterator Report::getBegin() { - return _basicElementTree.get_child( kReport ).begin(); + try + { + if( getSize() == 0 ) + throw std::runtime_error( "getBegin: empty tree." ); + return _basicElementTree.get_child( kReport ).begin(); + } + catch( const std::runtime_error& e ) + { + LOG_ERROR( e.what() ); + throw; + } } size_t Report::getSize() { + if( _basicElementTree.empty() ) + return 0; return _basicElementTree.get_child( kReport ).size(); } diff --git a/libraries/report/src/Report/Report.hpp b/libraries/report/src/Report/Report.hpp index 636f3d2..3643947 100644 --- a/libraries/report/src/Report/Report.hpp +++ b/libraries/report/src/Report/Report.hpp @@ -16,13 +16,10 @@ class Report { public: Report(); - Report( const ReportTree& reportTree ); ~Report(); - void addBasicElement( std::shared_ptr< be::Element > element ); - + ReportNode addFirstElement( std::shared_ptr< be::Element > element ); ReportNode getFirstNode(); - ReportIterator getBegin(); size_t getSize(); diff --git a/libraries/report/tests/reportTests.hpp b/libraries/report/tests/reportTests.hpp index fd100c7..78a55e7 100644 --- a/libraries/report/tests/reportTests.hpp +++ b/libraries/report/tests/reportTests.hpp @@ -5,87 +5,58 @@ BOOST_AUTO_TEST_CASE( report_report ) { LOG_INFO( ">>> report_report <<<" ); { - LOG_INFO( ">>> report_report <<<" ); Report report; std::shared_ptr< ben::Number< int > > numPtr1( new ben::Number< int > ); - std::shared_ptr< ben::Number< int > > numPtr2( new ben::Number< int > ); std::shared_ptr< bed::Data > dataPtr( new bed::Data ); - report.addBasicElement( numPtr1 ); - report.addBasicElement( dataPtr ); - report.addBasicElement( numPtr2 ); - BOOST_CHECK_EQUAL( report.getSize(), 3 ); - - ReportNode node = report.getFirstNode(); - BOOST_CHECK_EQUAL( node.getIndex(), 0 ); - BOOST_CHECK_EQUAL( node.getIndexTotal(), 3 ); - BOOST_CHECK_EQUAL( node.getElementPointer()->getType(), be::Element::eTypeNumber ); - - node = node.next(); - BOOST_CHECK_EQUAL( node.getIndex(), 1 ); - BOOST_CHECK_EQUAL( node.getIndexTotal(), 3 ); - BOOST_CHECK_EQUAL( node.getElementPointer()->getType(), be::Element::eTypeData ); - - node = node.next(); - BOOST_CHECK_EQUAL( node.getIndex(), 2 ); - BOOST_CHECK_EQUAL( node.getIndexTotal(), 3 ); - BOOST_CHECK_EQUAL( node.getElementPointer()->getType(), be::Element::eTypeNumber ); + BOOST_CHECK_EQUAL( report.getSize(), 0 ); + + ReportNode node1 = report.addFirstElement( numPtr1 ); + BOOST_CHECK_EQUAL( report.getSize(), 1 ); + BOOST_CHECK_EQUAL( node1.getIndex(), 0 ); + + ReportNode node2 = report.addFirstElement( dataPtr ); + BOOST_CHECK_EQUAL( report.getSize(), 1 ); + BOOST_CHECK_EQUAL( node2.getIndex(), 0 ); } { - ReportTree tree; - ReportTree group; - ReportTree node1, node2, node3, node4, node5; - std::shared_ptr< ben::Number< int > > numPtr1( new ben::Number< int > ); - std::shared_ptr< ben::Number< int > > numPtr2( new ben::Number< int > ); - std::shared_ptr< bed::Data > dataPtr1( new bed::Data ); - std::shared_ptr< bed::Data > dataPtr2( new bed::Data ); - std::shared_ptr< bed::Data > dataPtr3( new bed::Data ); - - node1.add( "0", ( std::shared_ptr< be::Element > ) numPtr1 ); - node2.add( "1", ( std::shared_ptr< be::Element > ) numPtr2 ); - node3.add( "2", ( std::shared_ptr< be::Element > ) dataPtr1 ); - node4.add( "3", ( std::shared_ptr< be::Element > ) dataPtr2 ); - node5.add( "4", ( std::shared_ptr< be::Element > ) dataPtr3 ); - - node2.add_child( "group.", node5 ); - - tree.add_child( "report.", node1 ); - tree.add_child( "report.", node2 ); - tree.add_child( "report.", node3 ); - tree.add_child( "report.", node4 ); - - Report report( tree ); - - BOOST_CHECK_EQUAL( report.getSize(), 4 ); - - ReportNode node = report.getFirstNode(); - BOOST_CHECK_EQUAL( node.getIndex(), 0 ); - BOOST_CHECK_EQUAL( node.getIndexTotal(), 4 ); - BOOST_CHECK_EQUAL( node.getElementPointer()->getType(), be::Element::eTypeNumber ); - - node = node.next(); - BOOST_CHECK_EQUAL( node.getIndex(), 1 ); - BOOST_CHECK_EQUAL( node.getIndexTotal(), 4 ); - BOOST_CHECK_EQUAL( node.getElementPointer()->getType(), be::Element::eTypeNumber ); + Report report; + BOOST_CHECK_EQUAL( report.getSize(), 0 ); + BOOST_CHECK_THROW( report.getFirstNode(), std::runtime_error ); + BOOST_CHECK_THROW( report.getBegin(), std::runtime_error ); + } +} - ReportNode child = node.firstChild(); - BOOST_CHECK_EQUAL( child.getIndex(), 0 ); - BOOST_CHECK_EQUAL( child.getIndexTotal(), 1 ); - BOOST_CHECK_EQUAL( child.parent()->getIndex(), 1 ); - BOOST_CHECK_EQUAL( child.parent()->getIndexTotal(), 4 ); - BOOST_CHECK_EQUAL( child.getElementPointer()->getType(), be::Element::eTypeData ); +BOOST_AUTO_TEST_CASE( report_report_and_node_report ) +{ + LOG_INFO( ">>> report_report_and_node_report <<<" ); + { + Report report; - node = child.parent()->next(); - BOOST_CHECK_EQUAL( node.getIndex(), 2 ); - BOOST_CHECK_EQUAL( node.getIndexTotal(), 4 ); - BOOST_CHECK_EQUAL( node.getElementPointer()->getType(), be::Element::eTypeData ); + std::shared_ptr< ben::Number< int > > numPtr1( new ben::Number< int > ); + std::shared_ptr< bed::Data > dataPtr( new bed::Data ); + std::shared_ptr< ben::Number< short > > numPtr2( new ben::Number< short > ); - node = node.next(); - BOOST_CHECK_EQUAL( node.getIndex(), 3 ); - BOOST_CHECK_EQUAL( node.getIndexTotal(), 4 ); - BOOST_CHECK_EQUAL( node.getElementPointer()->getType(), be::Element::eTypeData ); - BOOST_CHECK_THROW( node.parent(), std::runtime_error ); + BOOST_CHECK_EQUAL( report.getSize(), 0 ); + + ReportNode node1 = report.addFirstElement( numPtr1 ); + BOOST_CHECK_EQUAL( report.getSize(), 1 ); + BOOST_CHECK_EQUAL( node1.getIndex(), 0 ); + BOOST_CHECK_EQUAL( node1.getSecond()->begin()->first, "0" ); + BOOST_CHECK_THROW( node1.parent(), std::runtime_error ); + + node1 = node1.appendNext( dataPtr ); + BOOST_CHECK_EQUAL( report.getSize(), 2 ); + BOOST_CHECK_EQUAL( node1.getIndex(), 1 ); + BOOST_CHECK_EQUAL( node1.getSecond()->begin()->first, "1" ); + BOOST_CHECK_THROW( node1.parent(), std::runtime_error ); + + ReportNode child1 = node1.appendChild( numPtr2 ); + BOOST_CHECK_EQUAL( report.getSize(), 2 ); + BOOST_CHECK_EQUAL( child1.getIndex(), 0 ); + BOOST_CHECK_EQUAL( child1.getSecond()->begin()->first, "1-0" ); + BOOST_CHECK_EQUAL( child1.parent()->getSecond()->begin()->first, "1" ); } }