From 83010cfb93a6e6f4734fcbee8767cea90e605058 Mon Sep 17 00:00:00 2001 From: David Rosca <roscadav@fit.cvut.cz> Date: Mon, 9 Mar 2015 10:07:57 +0100 Subject: [PATCH] Directed graph: Fixed adding nodes when adding edge + added tests --- .../directed/AdjacencyListDirectedGraph.cpp | 1 + .../directed/AdjacencyMatrixDirectedGraph.cpp | 1 + alib2data/test-src/graph/GraphTest.cpp | 74 +++++++++++++++++++ alib2data/test-src/graph/GraphTest.h | 3 + 4 files changed, 79 insertions(+) diff --git a/alib2data/src/graph/directed/AdjacencyListDirectedGraph.cpp b/alib2data/src/graph/directed/AdjacencyListDirectedGraph.cpp index 3821f9826f..ccdbdda61b 100644 --- a/alib2data/src/graph/directed/AdjacencyListDirectedGraph.cpp +++ b/alib2data/src/graph/directed/AdjacencyListDirectedGraph.cpp @@ -119,6 +119,7 @@ bool AdjacencyListDirectedGraph::addEdge(const DirectedEdge &edge) edges.insert(edge); adj[edge.getFromNode()].push_back(edge.getToNode()); + adj[edge.getToNode()]; // insert key return true; } diff --git a/alib2data/src/graph/directed/AdjacencyMatrixDirectedGraph.cpp b/alib2data/src/graph/directed/AdjacencyMatrixDirectedGraph.cpp index 6bcdb3dc42..e0cfed03b5 100644 --- a/alib2data/src/graph/directed/AdjacencyMatrixDirectedGraph.cpp +++ b/alib2data/src/graph/directed/AdjacencyMatrixDirectedGraph.cpp @@ -116,6 +116,7 @@ bool AdjacencyMatrixDirectedGraph::addEdge(const DirectedEdge &edge) edges.insert(edge); adj[edge.getFromNode()][edge.getToNode()]++; + adj[edge.getToNode()]; // insert key return true; } diff --git a/alib2data/test-src/graph/GraphTest.cpp b/alib2data/test-src/graph/GraphTest.cpp index 5e1830cd97..0fcd966d54 100644 --- a/alib2data/test-src/graph/GraphTest.cpp +++ b/alib2data/test-src/graph/GraphTest.cpp @@ -66,6 +66,11 @@ void GraphTest::testHasEdge() RUN_TEST(testHasEdge_impl); } +void GraphTest::testAddEdgeValue() +{ + RUN_TEST(testAddEdgeValue_impl); +} + void GraphTest::testCopyConstruct_impl(graph::REPRESENTATION representation) { // Common @@ -604,6 +609,9 @@ void GraphTest::testHasEdge_impl(graph::REPRESENTATION representation) dg.addEdge(graph::DirectedEdge(n3, n4)); dg.addEdge(graph::DirectedEdge(n4, n1)); + CPPUNIT_ASSERT_EQUAL_INT(4, dg.getNodes().size()); + CPPUNIT_ASSERT_EQUAL_INT(5, dg.getEdges().size()); + CPPUNIT_ASSERT_EQUAL(true, dg.hasEdge(n1, n2)); CPPUNIT_ASSERT_EQUAL(true, dg.hasEdge(n2, n3)); CPPUNIT_ASSERT_EQUAL(true, dg.hasEdge(n3, n4)); @@ -622,6 +630,9 @@ void GraphTest::testHasEdge_impl(graph::REPRESENTATION representation) ug.addEdge(graph::UndirectedEdge(n3, n4)); ug.addEdge(graph::UndirectedEdge(n4, n1)); + CPPUNIT_ASSERT_EQUAL_INT(4, ug.getNodes().size()); + CPPUNIT_ASSERT_EQUAL_INT(5, ug.getEdges().size()); + CPPUNIT_ASSERT_EQUAL(true, ug.hasEdge(n1, n2)); CPPUNIT_ASSERT_EQUAL(true, ug.hasEdge(n2, n1)); CPPUNIT_ASSERT_EQUAL(true, ug.hasEdge(n2, n3)); @@ -637,3 +648,66 @@ void GraphTest::testHasEdge_impl(graph::REPRESENTATION representation) CPPUNIT_ASSERT_EQUAL(false, ug.hasEdge(n4, n2)); } +void GraphTest::testAddEdgeValue_impl(graph::REPRESENTATION representation) +{ + // Common + graph::Node n1("n1"); + graph::Node n2("n2"); + graph::Node n3("n3"); + graph::Node n4("n4"); + graph::Node n5("n5"); + graph::Node n6("n6"); + + // Directed + graph::DirectedEdge de1(n1, n2); + graph::DirectedEdge de2(n1, n3); + graph::DirectedEdge de3(n1, n4); + graph::DirectedEdge de4(n1, n5); + graph::DirectedEdge de5(n2, n5); + graph::DirectedEdge de6(n5, n6); + + graph::DirectedGraph dg(representation); + dg.addEdge(de1, 2); + dg.addEdge(de2, 2); + dg.addEdge(de3, 1); + dg.addEdge(de4, 6); + dg.addEdge(de5, 3); + dg.addEdge(de6, 2); + + CPPUNIT_ASSERT_EQUAL_INT(6, dg.getNodes().size()); + CPPUNIT_ASSERT_EQUAL_INT(6, dg.getEdges().size()); + + CPPUNIT_ASSERT_EQUAL(2, dg.getEdgeValue(de1)); + CPPUNIT_ASSERT_EQUAL(2, dg.getEdgeValue(de2)); + CPPUNIT_ASSERT_EQUAL(1, dg.getEdgeValue(de3)); + CPPUNIT_ASSERT_EQUAL(6, dg.getEdgeValue(de4)); + CPPUNIT_ASSERT_EQUAL(3, dg.getEdgeValue(de5)); + CPPUNIT_ASSERT_EQUAL(2, dg.getEdgeValue(de6)); + + // Undirected + graph::UndirectedEdge ue1(n1, n2); + graph::UndirectedEdge ue2(n1, n3); + graph::UndirectedEdge ue3(n1, n4); + graph::UndirectedEdge ue4(n1, n5); + graph::UndirectedEdge ue5(n2, n5); + graph::UndirectedEdge ue6(n5, n6); + + graph::UndirectedGraph ug(representation); + ug.addEdge(ue1, 2); + ug.addEdge(ue2, 2); + ug.addEdge(ue3, 1); + ug.addEdge(ue4, 6); + ug.addEdge(ue5, 3); + ug.addEdge(ue6, 2); + + CPPUNIT_ASSERT_EQUAL_INT(6, ug.getNodes().size()); + CPPUNIT_ASSERT_EQUAL_INT(6, ug.getEdges().size()); + + CPPUNIT_ASSERT_EQUAL(2, ug.getEdgeValue(ue1)); + CPPUNIT_ASSERT_EQUAL(2, ug.getEdgeValue(ue2)); + CPPUNIT_ASSERT_EQUAL(1, ug.getEdgeValue(ue3)); + CPPUNIT_ASSERT_EQUAL(6, ug.getEdgeValue(ue4)); + CPPUNIT_ASSERT_EQUAL(3, ug.getEdgeValue(ue5)); + CPPUNIT_ASSERT_EQUAL(2, ug.getEdgeValue(ue6)); +} + diff --git a/alib2data/test-src/graph/GraphTest.h b/alib2data/test-src/graph/GraphTest.h index 63ed55a4c3..50d9472e84 100644 --- a/alib2data/test-src/graph/GraphTest.h +++ b/alib2data/test-src/graph/GraphTest.h @@ -19,6 +19,7 @@ class GraphTest : public CppUnit::TestFixture CPPUNIT_TEST(testRemoveNode); CPPUNIT_TEST(testNeighborEdges); CPPUNIT_TEST(testHasEdge); + CPPUNIT_TEST(testAddEdgeValue); CPPUNIT_TEST_SUITE_END(); public: @@ -33,6 +34,7 @@ public: void testRemoveNode(); void testNeighborEdges(); void testHasEdge(); + void testAddEdgeValue(); void testCopyConstruct_impl(graph::REPRESENTATION representation); void testEqual_impl(graph::REPRESENTATION representation); @@ -45,6 +47,7 @@ public: void testRemoveNode_impl(graph::REPRESENTATION representation); void testNeighborEdges_impl(graph::REPRESENTATION representation); void testHasEdge_impl(graph::REPRESENTATION representation); + void testAddEdgeValue_impl(graph::REPRESENTATION representation); }; #endif // GRAPH_TEST_H_ -- GitLab