Skip to content

Commit

Permalink
add modifications to Report class (addFirstElement method and unit te…
Browse files Browse the repository at this point in the history
…sts)

issue #30
  • Loading branch information
Valentin Noel committed Jul 31, 2013
1 parent c94a9e0 commit 48c2e32
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 89 deletions.
42 changes: 28 additions & 14 deletions libraries/report/src/Report/Report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,39 @@
#include <common/global.hpp>
#include <boost/foreach.hpp>

static const std::string kReport = "report";

namespace report_generator
{

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 )
{
Expand All @@ -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();
}

Expand Down
5 changes: 1 addition & 4 deletions libraries/report/src/Report/Report.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
113 changes: 42 additions & 71 deletions libraries/report/tests/reportTests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
}
}

Expand Down

0 comments on commit 48c2e32

Please sign in to comment.