TuttleOFX  1
TuttleOFX/libraries/tuttle/tests/internalGraph/main.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 
00003 #include "dummy/DummyEdge.hpp"
00004 #include "dummy/DummyVertex.hpp"
00005 #include <tuttle/host/graph/InternalGraph.hpp>
00006 
00007 #define BOOST_TEST_MODULE internalGraph_tests
00008 #include <tuttle/test/unit_test.hpp>
00009 
00010 BOOST_AUTO_TEST_SUITE( tuttle_internalGraph_suite )
00011 
00012 using namespace boost::unit_test;
00013 
00014 BOOST_AUTO_TEST_CASE( internalGraph_add_one_vertex )
00015 {
00016         using namespace tuttle::test;
00017         using namespace tuttle::host;
00018 
00019         typedef graph::InternalGraph<DummyVertex, DummyEdge> DummyGraph;
00020         typedef DummyGraph::vertex_descriptor DummyVertexDescriptor;
00021 
00022         DummyGraph graph;
00023 
00024         DummyVertex A( "nodeA" );
00025         DummyVertexDescriptor ADesc = graph.addVertex( A );
00026 
00027         BOOST_CHECK_EQUAL( graph.getVertexCount(), 1 );
00028         BOOST_CHECK_EQUAL( graph.instance( ADesc ).getName(), "nodeA" );
00029 }
00030 
00031 BOOST_AUTO_TEST_CASE( connect_two_nodes )
00032 {
00033         using namespace tuttle::test;
00034         using namespace tuttle::host;
00035 
00036         typedef graph::InternalGraph<DummyVertex, DummyEdge> DummyGraph;
00037         typedef DummyGraph::vertex_descriptor DummyVertexDescriptor;
00038         typedef DummyGraph::edge_descriptor DummyEdgeDescriptor;
00039         DummyGraph graph;
00040 
00041         DummyVertex A( "nodeA" );
00042         DummyVertex B( "nodeB" );
00043         DummyVertexDescriptor aDesc = graph.addVertex( A );
00044         DummyVertexDescriptor bDesc = graph.addVertex( B );
00045 
00046         DummyEdge a_to_b( "edgeAtoB" );
00047         DummyEdgeDescriptor AtoBDesc = graph.addEdge( aDesc, bDesc, a_to_b );
00048 
00049         BOOST_CHECK_EQUAL( graph.instance( aDesc ).getName(), "nodeA" );
00050         BOOST_CHECK_EQUAL( graph.getVertexCount(), 2 );
00051         BOOST_CHECK_EQUAL( graph.getEdgeCount(), 1 );
00052         BOOST_CHECK_EQUAL( graph.instance( bDesc ).getName(), "nodeB" );
00053         BOOST_CHECK_EQUAL( graph.instance( AtoBDesc ).getName(), "edgeAtoB" );
00054 }
00055 
00056 BOOST_AUTO_TEST_CASE( delete_one_node )
00057 {
00058         using namespace tuttle::test;
00059         using namespace tuttle::host;
00060 
00061         typedef graph::InternalGraph<DummyVertex, DummyEdge> DummyGraph;
00062         typedef DummyGraph::vertex_descriptor DummyVertexDescriptor;
00063         typedef DummyGraph::edge_descriptor DummyEdgeDescriptor;
00064         DummyGraph graph;
00065 
00066         DummyVertex a( "nodeA" );
00067         DummyVertex b( "nodeB" );
00068         DummyVertex c( "nodeC" );
00069         DummyVertex d( "nodeD" );
00070 
00071         DummyEdge a_to_b( "A to B" );
00072         DummyEdge b_to_c( "B to C" );
00073         DummyEdge b_to_d( "B to D" );
00074         DummyEdge c_to_d( "C to D" );
00075 
00076         DummyVertexDescriptor aDesc = graph.addVertex( a );
00077         DummyVertexDescriptor bDesc = graph.addVertex( b );
00078         DummyVertexDescriptor cDesc = graph.addVertex( c );
00079         DummyVertexDescriptor dDesc = graph.addVertex( d );
00080 
00081         graph.addEdge( aDesc, bDesc, a_to_b ); // A -> B
00082         graph.addEdge( bDesc, cDesc, b_to_c ); // B -> C
00083         graph.addEdge( cDesc, dDesc, c_to_d ); // C -> D
00084 
00085         BOOST_CHECK_EQUAL( graph.getVertexCount(), 4 );
00086         BOOST_CHECK_EQUAL( graph.getEdgeCount(), 3 );
00087 
00088         graph.removeVertex( cDesc );
00089 
00090         BOOST_CHECK_EQUAL( graph.getVertexCount(), 3 );
00091         BOOST_CHECK_EQUAL( graph.getEdgeCount(), 1 );
00092 
00093         graph.addEdge(
00094                 graph.getVertexDescriptor( b.getKey() ),
00095                 graph.getVertexDescriptor( d.getKey() ),
00096                 b_to_d ); // B -> D
00097 
00098         BOOST_CHECK_EQUAL( graph.getVertexCount(), 3 );
00099         BOOST_CHECK_EQUAL( graph.getEdgeCount(), 2 );
00100 }
00101 
00102 BOOST_AUTO_TEST_CASE( detect_cycle )
00103 {
00104         using namespace tuttle::test;
00105         using namespace tuttle::host;
00106 
00107         typedef graph::InternalGraph<DummyVertex, DummyEdge> DummyGraph;
00108         typedef DummyGraph::vertex_descriptor DummyVertexDescriptor;
00109         typedef DummyGraph::edge_descriptor DummyEdgeDescriptor;
00110         DummyGraph graph;
00111 
00112         DummyVertex A( "nodeA" );
00113         DummyVertex B( "nodeB" );
00114         DummyVertex C( "nodeC" );
00115 
00116         DummyEdge AtoB( "AtoB" );
00117         DummyEdge BtoC( "BtoC" );
00118         DummyEdge CtoA( "CtoA" );
00119 
00120         DummyVertexDescriptor ADesc = graph.addVertex( A );
00121         DummyVertexDescriptor BDesc = graph.addVertex( B );
00122         DummyVertexDescriptor CDesc = graph.addVertex( C );
00123 
00124         // A -> B : ok
00125         BOOST_CHECK_NO_THROW( graph.addEdge( ADesc, BDesc, AtoB ) );
00126         // B -> C : ok
00127         BOOST_CHECK_NO_THROW( graph.addEdge( BDesc, CDesc, BtoC ) );
00128         // C -> A : cycle -> exception
00129         BOOST_CHECK_THROW( graph.addEdge( CDesc, ADesc, CtoA ), exception::Logic );
00130 }
00131 
00132 
00133 BOOST_AUTO_TEST_SUITE_END()
00134