From 259e26c25715ce032b9db7290f7697aaeddcd794 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Fri, 15 Sep 2023 17:01:51 -0500 Subject: [PATCH] Fixed graph reading in wrong --- src/algorithm.cpp | 9 +++++---- test/test.cpp | 51 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/algorithm.cpp b/src/algorithm.cpp index 39cd76d..4e48fcf 100644 --- a/src/algorithm.cpp +++ b/src/algorithm.cpp @@ -6,7 +6,6 @@ #include Graph ReadInGraph(std::string fileLocation) { - std::cout << fileLocation << std::endl; std::ifstream graphFile(fileLocation); if (!graphFile.is_open()) { std::cerr << "Error opening file, check filename" << std::endl; @@ -14,14 +13,17 @@ Graph ReadInGraph(std::string fileLocation) { } Graph newGraph; graphFile >> newGraph.nodeCount; - newGraph.vertices.resize(newGraph.nodeCount + 1); + newGraph.vertices.resize(newGraph.nodeCount); std::string line; int source, destination, weight; + std::getline(graphFile, line); while (std::getline(graphFile, line)) { std::istringstream iss(line); iss >> source; + source--; do { iss >> destination >> weight; + destination--; newGraph.vertices.at(source).nodeNumber = source; newGraph.AddEdge(source, destination, weight); } while (!iss.eof()); @@ -31,8 +33,7 @@ Graph ReadInGraph(std::string fileLocation) { } Graph::Graph(void) { - nodeCount = -1; - + nodeCount = 0; } Edge::Edge(Vertex* destination, int weight) { diff --git a/test/test.cpp b/test/test.cpp index ebb78dd..b648774 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -7,20 +7,57 @@ void tearDown() { ; } Graph testGraph; -void test_AllVerticesAdded(void) -{ +void test_GraphReadsIn(void) { testGraph = ReadInGraph("test/input/graphPosLittle"); TEST_ASSERT_EQUAL_INT(10, testGraph.nodeCount); } -void test_AllEdgesAdded(void) -{ - TEST_ASSERT_EQUAL_INT(4, 2+3); +void test_AllVerticesAdded(void) { + for (int i = 0; i < 10; i++) { + TEST_ASSERT_EQUAL_INT(i, testGraph.vertices.at(i).nodeNumber); + } } -int main(void) -{ +void test_AllEdgesAdded(void) { + // Edges for vertex 1 + TEST_ASSERT_EQUAL_INT(7, testGraph.vertices.at(0).edges.at(0).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(2, testGraph.vertices.at(0).edges.at(1).destination->nodeNumber + 1); + // Edges for vertex 2 + TEST_ASSERT_EQUAL_INT(7, testGraph.vertices.at(1).edges.at(0).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(5, testGraph.vertices.at(1).edges.at(1).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(1, testGraph.vertices.at(1).edges.at(2).destination->nodeNumber + 1); + // Edges for vertex 3 + TEST_ASSERT_EQUAL_INT(6, testGraph.vertices.at(2).edges.at(0).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(1, testGraph.vertices.at(2).edges.at(1).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(5, testGraph.vertices.at(2).edges.at(2).destination->nodeNumber + 1); + // Edges for vertex 4 + TEST_ASSERT_EQUAL_INT(8, testGraph.vertices.at(3).edges.at(0).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(10, testGraph.vertices.at(3).edges.at(1).destination->nodeNumber + 1); + // Edges for vertex 5 + TEST_ASSERT_EQUAL_INT(3, testGraph.vertices.at(4).edges.at(0).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(8, testGraph.vertices.at(4).edges.at(1).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(7, testGraph.vertices.at(4).edges.at(2).destination->nodeNumber + 1); + // Edges for vertex 6 + TEST_ASSERT_EQUAL_INT(3, testGraph.vertices.at(5).edges.at(0).destination->nodeNumber + 1); + // Edges for vertex 7 + TEST_ASSERT_EQUAL_INT(2, testGraph.vertices.at(6).edges.at(0).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(9, testGraph.vertices.at(6).edges.at(1).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(1, testGraph.vertices.at(6).edges.at(2).destination->nodeNumber + 1); + // Edges for vertex 8 + TEST_ASSERT_EQUAL_INT(5, testGraph.vertices.at(7).edges.at(0).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(4, testGraph.vertices.at(7).edges.at(1).destination->nodeNumber + 1); + // Edges for vertex 9 + TEST_ASSERT_EQUAL_INT(6, testGraph.vertices.at(8).edges.at(0).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(10, testGraph.vertices.at(8).edges.at(1).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(7, testGraph.vertices.at(8).edges.at(2).destination->nodeNumber + 1); + // Edges for vertex 10 + TEST_ASSERT_EQUAL_INT(4, testGraph.vertices.at(9).edges.at(0).destination->nodeNumber + 1); + TEST_ASSERT_EQUAL_INT(9, testGraph.vertices.at(9).edges.at(1).destination->nodeNumber + 1); +} + +int main(void) { UNITY_BEGIN(); + RUN_TEST(test_GraphReadsIn); RUN_TEST(test_AllVerticesAdded); RUN_TEST(test_AllEdgesAdded); return UNITY_END();