Fixed graph reading in wrong

This commit is contained in:
TriantaTV 2023-09-15 17:01:51 -05:00
parent 9dcfef9dd5
commit 259e26c257
2 changed files with 49 additions and 11 deletions

View File

@ -6,7 +6,6 @@
#include <queue>
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) {

View File

@ -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();