-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestGraph.cpp
194 lines (158 loc) · 5.44 KB
/
TestGraph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "FaspTightCut/graph.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <random>
using ::testing::ElementsAre;
using ::testing::UnorderedElementsAre;
namespace {
template <typename T>
class GraphTest : public ::testing::Test {
public:
using EdgeType = typename Graph::Graph<T>::Edge;
};
using GraphTypes = ::testing::Types<int, int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t>;
TYPED_TEST_SUITE(GraphTest, GraphTypes,);
template <typename VT>
void testCreateEmpty(Graph::Graph<VT> &g) {
ASSERT_EQ(g.getNumOfVertices(), 0);
ASSERT_EQ(g.getNumOfEdges(), 0);
ASSERT_EQ(g.getVertices().size(), 0);
ASSERT_EQ(g.getEdges().size(), 0);
}
TYPED_TEST(GraphTest, createEmptyGraph) {
Graph::Graph<TypeParam> gm;
testCreateEmpty(gm);
}
template <typename VT>
void testCreate(Graph::Graph<VT> &g) {
// Tested methods
g.addVertex(1);
g.addVertex(2);
g.addVertex(3);
g.addEdge({1, 2});
g.addEdge({2, 3});
g.addEdge({3, 1});
g.addEdge({1, 3});
ASSERT_EQ(g.getNumOfVertices(), 3);
ASSERT_EQ(g.getNumOfEdges(), 4);
ASSERT_EQ(g.getVertices().size(), 3);
ASSERT_EQ(g.getEdges().size(), 4);
ASSERT_TRUE(g.hasEdge({1, 2}));
ASSERT_TRUE(g.hasEdge({2, 3}));
ASSERT_TRUE(g.hasEdge({3, 1}));
ASSERT_TRUE(g.hasEdge({1, 3}));
ASSERT_FALSE(g.hasEdge({2, 1}));
ASSERT_FALSE(g.hasEdge({3, 2}));
ASSERT_FALSE(g.hasEdge({1, 4}));
ASSERT_FALSE(g.hasEdge({4, 1}));
}
TYPED_TEST(GraphTest, createGraph) {
Graph::Graph<TypeParam> gm;
testCreate(gm);
}
template <typename VT>
void testRemoveVertex(Graph::Graph<VT> &g) {
g.addVertex(1);
g.addVertex(2);
g.addVertex(3);
g.addEdge({1, 2});
g.addEdge({2, 3});
g.addEdge({3, 1});
g.addEdge({1, 3});
ASSERT_EQ(g.getNumOfVertices(), 3);
ASSERT_EQ(g.getNumOfEdges(), 4);
ASSERT_TRUE(g.hasVertex(2));
// Tested method
g.removeVertex(2);
ASSERT_FALSE(g.hasVertex(2));
ASSERT_EQ(g.getVertices().size(), 2);
ASSERT_EQ(g.getEdges().size(), 2);
ASSERT_FALSE(g.hasEdge({1, 2}));
ASSERT_FALSE(g.hasEdge({2, 3}));
ASSERT_TRUE(g.hasEdge({3, 1}));
ASSERT_TRUE(g.hasEdge({1, 3}));
ASSERT_FALSE(g.hasEdge({2, 1}));
ASSERT_FALSE(g.hasEdge({3, 2}));
}
TYPED_TEST(GraphTest, removeVertex) {
Graph::Graph<TypeParam> gm;
testRemoveVertex(gm);
}
template <typename VT>
void testGraphInfo(Graph::Graph<VT> &g) {
g.addVertex(1);
g.addVertex(2);
// add/remove vertex
ASSERT_FALSE(g.hasVertex(3));
ASSERT_EQ(g.getNumOfVertices(), 2);
g.addVertex(3);
ASSERT_TRUE(g.hasVertex(3));
ASSERT_EQ(g.getNumOfVertices(), 3);
g.removeVertex(3);
ASSERT_FALSE(g.hasVertex(3));
ASSERT_EQ(g.getNumOfVertices(), 2);
// add/remove edge
ASSERT_FALSE(g.hasEdge({1, 2}));
ASSERT_EQ(g.getNumOfEdges(), 0);
g.addEdge({1, 2});
ASSERT_TRUE(g.hasEdge({1, 2}));
ASSERT_EQ(g.getNumOfEdges(), 1);
g.removeEdge({1, 2});
ASSERT_FALSE(g.hasEdge({1, 2}));
ASSERT_EQ(g.getNumOfEdges(), 0);
}
TYPED_TEST(GraphTest, graphInfo) {
Graph::Graph<TypeParam> gm;
testGraphInfo(gm);
}
template <typename VT>
void testGraphGetters(Graph::Graph<VT> &g) {
using Edge = typename Graph::Graph<VT>::Edge;
g.addVertex(1);
g.addVertex(2);
g.addVertex(4);
// we cannot assume the order of elements
ASSERT_THAT(g.getVertices(), UnorderedElementsAre(1, 2, 4));
g.addEdge({1, 2});
g.addEdge({1, 4});
g.addEdge({4, 2});
g.addEdge({2, 1});
// we cannot assume the order of elements
ASSERT_THAT(g.getEdges(), UnorderedElementsAre(Edge{1, 2}, Edge{1, 4}, Edge{4, 2}, Edge{2, 1}));
}
TYPED_TEST(GraphTest, graphGetters) {
Graph::Graph<TypeParam> gm;
testGraphGetters(gm);
}
// Testing asserts - these tests can be run only in debug mode
#ifndef NDEBUG
template <typename VT>
void testGraphAsserts(Graph::Graph<VT> &g) {
//Add same vertex twice
g.addVertex(2);
ASSERT_DEATH(g.addVertex(2), "Vertex already exists!");
// Remove same vertex twice
g.removeVertex(2);
ASSERT_DEATH(g.removeVertex(2), "Vertex does not exists!");
// Add edge - vertices not existing
ASSERT_DEATH(g.addEdge({1, 3}), "Source vertex does not exists!");
g.addVertex(1);
ASSERT_DEATH(g.addEdge({1, 3}), "Destination vertex does not exists!");
// Remove edge - vertices not existing
ASSERT_DEATH(g.removeEdge({2, 3}), "Source vertex does not exists!");
g.addVertex(2);
ASSERT_DEATH(g.removeEdge({2, 3}), "Destination vertex does not exists!");
// Get in/out vetices
ASSERT_DEATH(g.getInVertices(5), "Vertex does not exists!");
ASSERT_DEATH(g.getOutVertices(6), "Vertex does not exists!");
}
TYPED_TEST(GraphTest, graphAsserts) {
Graph::Graph<TypeParam> gm;
testGraphAsserts(gm);
}
#endif
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}