-
Notifications
You must be signed in to change notification settings - Fork 5
/
gtest_hw2_edges.cpp
294 lines (237 loc) · 8.17 KB
/
gtest_hw2_edges.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <gtest/gtest.h>
#include <fstream>
#include "CME212/Util.hpp"
#include "CME212/Point.hpp"
#include "Graph.hpp"
class GraphPointFixture : public ::testing::Test {
protected:
//Define types
using GraphType = Graph<int,int>;
using NodeType = typename GraphType::node_type;
using EdgeType = typename GraphType::edge_type;
//Set up Graph and Points
GraphType graph;
Point p1, p2;
virtual void SetUp() {
p1 = Point(CME212::random(), CME212::random(), CME212::random());
p2 = Point(CME212::random(), CME212::random(), CME212::random());
}
};
// test Edge Size
TEST_F(GraphPointFixture, NodeSize){
EXPECT_LE( sizeof(EdgeType), 32 ) << "edge size > 32 bytes";
}
// Test add edge
TEST_F(GraphPointFixture, AddEdge){
//Inserting Node
graph.add_node(p1);
graph.add_node(p2);
//Graph has 2 Nodes
EXPECT_EQ(graph.num_nodes(),2) << "Graph does not have 2 Nodes";
//Inserting Edge
EdgeType e = graph.add_edge(graph.node(0), graph.node(1));
//Graph has 1 Edge
EXPECT_EQ(graph.num_edges(),1) << "Graph does not have 1 Edge";
//Getting Edge
EXPECT_EQ(e, graph.edge(0)) << "Edge equality or get error ";
}
class SingleEdgeFixture : public ::testing::Test {
protected:
//Define types
using GraphType = Graph<int,int>;
using NodeType = typename GraphType::node_type;
using EdgeType = typename GraphType::edge_type;
//Set up Graph and Points
GraphType graph;
std::vector<Point> points;
Point p1, p2;
EdgeType e;
virtual void SetUp() {
p1 = Point(CME212::random(), CME212::random(), CME212::random());
p2 = Point(CME212::random(), CME212::random(), CME212::random());
graph.add_node(p1);
graph.add_node(p2);
graph.add_edge(graph.node(0), graph.node(1));
e = graph.edge(0);
}
};
// Test has_edge
TEST_F(SingleEdgeFixture, HasEdge){
//Graph has_edge e
EXPECT_TRUE(graph.has_edge(e.node1(), e.node2())) << "Graph does not have edge e";
//Graph has_edge e transpose
EXPECT_TRUE(graph.has_edge(e.node2(), e.node1())) << "Graph does not have edge e transpose";
}
// Test nodes of edge
TEST_F(SingleEdgeFixture, EdgeCheck){
//Edge nodes check out
EXPECT_TRUE(
(e.node1() == graph.node(0) && e.node2() == graph.node(1)) ||
(e.node1() == graph.node(1) && e.node2() == graph.node(0))
) << "Edge nodes do not check out";
}
// Test edge removal
TEST_F(SingleEdgeFixture, RemoveEdge){
//remove_edge return value is true
EXPECT_TRUE(graph.remove_edge(e.node1(), e.node2())) << "remove_edge return value is false";
//Graph has 0 edges
EXPECT_EQ(graph.num_edges(),0) << "Graph does not have 0 edges";
//Graph !has_edge
EXPECT_FALSE(graph.has_edge(graph.node(0), graph.node(1))) << "Graph has_edge that was removed";
//remove_edge return value is false
EXPECT_FALSE(graph.remove_edge(graph.node(0), graph.node(1))) << "remove_edge return value is true";
}
// Test add_edge twice after removal
TEST_F(SingleEdgeFixture, AddEdgeAfter){
//remove edge
graph.remove_edge(e.node1(), e.node2());
// Inserting Edge
graph.add_edge(graph.node(0), graph.node(1));
//Inserting Edge Again
graph.add_edge(graph.node(0), graph.node(1));
//Graph has 1 Edge
EXPECT_EQ(graph.num_edges(),1) << "Graph adds duplicate edges";
}
// Test Edges after node removal
TEST_F(SingleEdgeFixture, EdgeAfterNodeRemove){
//Removing Node ...
EXPECT_TRUE(graph.remove_node(graph.node(1))) << "remove_node is false";
//Graph has 1 node
EXPECT_EQ(graph.num_nodes(),1) << "Graph does not have 1 node";
//Edge removed b/c of Node
EXPECT_EQ(graph.num_edges(),0) << "Edge not removed after node removal";
}
// Test Clearing
TEST_F(SingleEdgeFixture,Clearing){
//Clear
graph.clear();
//Graph has no edges
EXPECT_EQ(graph.num_edges(),0) << "Graph has edges after clearing";
}
// Test Many Edges
TEST_F(GraphPointFixture, HundredEdges){
EdgeType e;
//Add 100 Nodes
for (int k = 0; k < 100; ++k) {
graph.add_node(Point(CME212::random(), CME212::random(), CME212::random()));
}
// Adding 100 Edges
for (unsigned k = 0; k < 100; ++k) {
unsigned n1, n2;
do {
n1 = (unsigned) CME212::random(0, graph.num_nodes());
n2 = (unsigned) CME212::random(0, graph.num_nodes());
} while (n1 == n2 || graph.has_edge(graph.node(n1), graph.node(n2)));
e = graph.add_edge(graph.node(n1), graph.node(n2));
if (k == 43) {
//Graph has_edge e
EXPECT_TRUE(graph.has_edge(e.node1(), e.node2())) << "Graph does not have edge e";
EXPECT_TRUE(
(e.node1() == graph.node(n1) && e.node2() == graph.node(n2)) ||
(e.node1() == graph.node(n2) && e.node2() == graph.node(n1))
) << "Edge nodes do not check out" ;
}
}
//100 Nodes, 100 Edges
EXPECT_EQ(graph.num_edges(),100) << "graph does not have 100 Nodes";
EXPECT_EQ(graph.num_nodes(),100) << "graph does not have 100 Edges";
}
class HundredEdgeFixture : public ::testing::Test {
protected:
//Define types
using GraphType = Graph<int,int>;
using NodeType = typename GraphType::node_type;
using EdgeType = typename GraphType::edge_type;
//Set up Graph and Points
GraphType graph;
EdgeType e;
virtual void SetUp() {
//Add 100 Nodes
for (int k = 0; k < 100; ++k) {
graph.add_node(Point(CME212::random(), CME212::random(), CME212::random()));
}
// Adding 100 Edges
for (unsigned k = 0; k < 100; ++k) {
unsigned n1, n2;
do {
n1 = (unsigned) CME212::random(0, graph.num_nodes());
n2 = (unsigned) CME212::random(0, graph.num_nodes());
} while (n1 == n2 || graph.has_edge(graph.node(n1), graph.node(n2)));
e = graph.add_edge(graph.node(n1), graph.node(n2));
}
}
};
// Test multiple edge removal
TEST_F(HundredEdgeFixture, RepeatedRemoveEdge){
// Remove 50 Edges
for (unsigned k = 0; k < 50; ++k) {
unsigned n1, n2;
do {
n1 = (unsigned) CME212::random(0, graph.num_nodes());
n2 = (unsigned) CME212::random(0, graph.num_nodes());
} while (!graph.has_edge(graph.node(n1), graph.node(n2)));
graph.remove_edge(graph.node(n1), graph.node(n2));
if (k == 23){
// Graph !has_edge after remove
EXPECT_FALSE(graph.has_edge(graph.node(n1), graph.node(n2))) << "Graph has_edge after remove";
}
}
//Removed 50 Edges
EXPECT_EQ(graph.num_edges(), 50) << "Did not remove 50 Edges";
// Count edges the long way
unsigned count_edges = 0;
for (unsigned k = 0; k < graph.num_nodes(); ++k) {
for (unsigned j = k+1; j < graph.num_nodes(); ++j) {
if (graph.has_edge(graph.node(k), graph.node(j)))
++count_edges;
}
}
//Edge count agrees
EXPECT_EQ(graph.num_edges(), count_edges) << "Edge count does not agree";
}
// Test repeated remove node
TEST_F(HundredEdgeFixture, RepeatedRemoveNode){
// Remove 50 Nodes...
for (unsigned k = 0; k < 50; ++k) {
unsigned n = (unsigned) CME212::random(0, graph.num_nodes());
graph.remove_node(graph.node(n));
}
// removed 50 Nodes
EXPECT_EQ(graph.num_nodes(), 50) << "Did not remove 50 Nodes";
// Count edges the long way
unsigned count_edges = 0;
for (unsigned k = 0; k < graph.num_nodes(); ++k) {
for (unsigned j = k+1; j < graph.num_nodes(); ++j) {
if (graph.has_edge(graph.node(k), graph.node(j)))
++count_edges;
}
}
//Edge count agrees
EXPECT_EQ(count_edges, graph.num_edges()) << "Edge count does not agree";
}
class TwoGraphFixture : public ::testing::Test {
protected:
//Define types
using GraphType = Graph<int,int>;
using NodeType = typename GraphType::node_type;
using EdgeType = typename GraphType::edge_type;
//Set up Graph and Nodes
GraphType g, g2;
};
// Test comparisons
TEST_F(TwoGraphFixture, CompareEdges){
//Adding 10 Nodes to Graph1 and Graph2...
for (unsigned k = 0; k < 10; ++k) {
Point p(CME212::random(), CME212::random(), CME212::random());
g.add_node(p);
g2.add_node(p);
}
EdgeType e1 = g.add_edge(g.node(3), g.node(4));
EdgeType e2 = g2.add_edge(g2.node(3), g2.node(4));
//E1-E1 Edge comparison ==
EXPECT_EQ(e1,e1) << "E1-E1 Edge comparison == error";
//G1-G2 Edge comparison !=
EXPECT_NE(e1, e2) << "G1-G2 Edge comparison != error";
//G1-G2 Edge comparison < >
EXPECT_TRUE(e1 < e2 || e2 < e1) << "G1-G2 Edge comparison < > error";
}