Skip to content

Commit

Permalink
add unittests for VertexST and remove the topological order attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Aymane Lotfi committed Apr 11, 2024
1 parent 360ee39 commit 6085b03
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 22 deletions.
13 changes: 1 addition & 12 deletions graph_utils/VertexST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ VertexST::VertexST() {
this->id = -1;
this->id_in_graph = -1;
this->time = -1;
this->topological_position = -1;
}

VertexST::VertexST(const int& id_in_graph, const int& time) {
this->id = this->lastId;
this->id_in_graph = id_in_graph;
this->time = time;
this->topological_position = -1;
this->lastId++;
}

Expand All @@ -24,7 +22,6 @@ VertexST::VertexST(const VertexST& vertex) {
this->id = vertex.id;
this->id_in_graph = vertex.id_in_graph;
this->time = vertex.time;
this->topological_position = vertex.topological_position;
this->adjacency_list_out = vertex.adjacency_list_out;
this->adjacency_list_out_cost = vertex.adjacency_list_out_cost;
this->adjacency_list_out_time = vertex.adjacency_list_out_time;
Expand All @@ -35,7 +32,6 @@ VertexST& VertexST::operator=(const VertexST& vertex) {
this->id = vertex.id;
this->id_in_graph = vertex.id_in_graph;
this->time = vertex.time;
this->topological_position = vertex.topological_position;
this->adjacency_list_out = vertex.adjacency_list_out;
this->adjacency_list_out_cost = vertex.adjacency_list_out_cost;
this->adjacency_list_out_time = vertex.adjacency_list_out_time;
Expand Down Expand Up @@ -85,9 +81,6 @@ const int& VertexST::get_time() const {
return this->time;
}

const int& VertexST::get_topological_position() const {
return this->topological_position;
}

const unordered_map<int, vector<int>>& VertexST::get_adjacency_list_out() const {
return this->adjacency_list_out;
Expand All @@ -105,13 +98,9 @@ const unordered_map<int, vector<int>>& VertexST::get_adjacency_list_in() const {
return this->adjacency_list_in;
}

void VertexST::set_topological_position(const int& pos) {
this->topological_position = pos;
}

const string VertexST::toString() const {
string str;
str.append(to_string(this->id) + "\t(" + to_string(this->id_in_graph) + "," + to_string(this->time) + ")\t" + to_string(this->topological_position) + "\t\t" +
str.append(to_string(this->id) + "\t(" + to_string(this->id_in_graph) + "," + to_string(this->time) + ")\t" + "\t\t" +
to_string(this->adjacency_list_in.size()) + "\t" + to_string(this->adjacency_list_out.size()) + "\n");
return str;
}
8 changes: 0 additions & 8 deletions graph_utils/VertexST.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ class VertexST {
*/
int time;

/**
* position in the topological order
*/
int topological_position;

/**
* arc = (this,id) key = id, value = vector of arcs to go from this to id
*/
Expand Down Expand Up @@ -112,14 +107,11 @@ class VertexST {
const int& get_id() const;
const int& get_id_in_graph() const;
const int& get_time() const;
const int& get_topological_position() const;
const unordered_map<int, vector<int>>& get_adjacency_list_out() const;
const unordered_map<int, vector<int>>& get_adjacency_list_out_cost() const;
const unordered_map<int, vector<int>>& get_adjacency_list_out_time() const;
const unordered_map<int, vector<int>>& get_adjacency_list_in() const;

void set_topological_position(const int& pos);

const string toString() const;

};
Expand Down
2 changes: 0 additions & 2 deletions tests/Graph_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ TEST(Graph, GetVertex) {
graph.add_vertex("B");
EXPECT_EQ(graph.get_vertex(0).get_name(), "A");
EXPECT_EQ(graph.get_vertex("B").get_id(), 1);
// Expect exceptions for non-existing IDs/names
EXPECT_DEATH(graph.get_vertex(10), "");
}

TEST(Graph, GetArcs) {
Expand Down
75 changes: 75 additions & 0 deletions tests/VertexST_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <gtest/gtest.h>
#include "../graph_utils/VertexST.h"

TEST(VertexSTTest, Constructor) {
// Test default constructor
VertexST v1;
EXPECT_EQ(v1.get_id(), -1);
EXPECT_EQ(v1.get_id_in_graph(), -1);
EXPECT_EQ(v1.get_time(), -1);

// Test constructor with parameters
VertexST v2(10, 5);
EXPECT_EQ(v2.get_id(), 0);
EXPECT_EQ(v2.get_id_in_graph(), 10);
EXPECT_EQ(v2.get_time(), 5);
}

TEST(VertexSTTest, CopyConstructor) {
VertexST v1(10, 5);
v1.add_neighbour_out(1, 2);

// Create a copy of v1
VertexST v2(v1);

// Verify that the copies are equal
EXPECT_EQ(v2.get_id(), v1.get_id());
EXPECT_EQ(v2.get_id_in_graph(), v1.get_id_in_graph());
EXPECT_EQ(v2.get_time(), v1.get_time());
EXPECT_EQ(v2.get_adjacency_list_out(), v1.get_adjacency_list_out());
}

TEST(VertexSTTest, AssignmentOperator) {
VertexST v1(10, 5);
v1.add_neighbour_out(1, 2);

// Create a new vertex
VertexST v2;

// Assign v1 to v2
v2 = v1;

// Verify that the assignment was successful
EXPECT_EQ(v2.get_id(), v1.get_id());
EXPECT_EQ(v2.get_id_in_graph(), v1.get_id_in_graph());
EXPECT_EQ(v2.get_time(), v1.get_time());
EXPECT_EQ(v2.get_adjacency_list_out(), v1.get_adjacency_list_out());
}

TEST(VertexSTTest, AddNeighbourOut) {
VertexST v;
v.add_neighbour_out(1, 2);
v.add_neighbour_out(1, 3);
v.add_neighbour_out(2, 4);

// Verify that the neighbours were added correctly
EXPECT_EQ(v.get_adjacency_list_out().size(), 2);
EXPECT_TRUE(v.get_adjacency_list_out().count(1) > 0);
EXPECT_TRUE(v.get_adjacency_list_out().count(2) > 0);
EXPECT_EQ(v.get_adjacency_list_out().at(1).size(), 2);
EXPECT_EQ(v.get_adjacency_list_out().at(2).size(), 1);
}

TEST(VertexSTTest, AddNeighbourIn) {
VertexST v;
v.add_neighbour_in(1, 2);
v.add_neighbour_in(1, 3);
v.add_neighbour_in(2, 4);

// Verify that the neighbours were added correctly
EXPECT_EQ(v.get_adjacency_list_in().size(), 2);
EXPECT_TRUE(v.get_adjacency_list_in().count(1) > 0);
EXPECT_TRUE(v.get_adjacency_list_in().count(2) > 0);
EXPECT_EQ(v.get_adjacency_list_in().at(1).size(), 2);
EXPECT_EQ(v.get_adjacency_list_in().at(2).size(), 1);
}

0 comments on commit 6085b03

Please sign in to comment.