-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphtest.cpp
More file actions
253 lines (214 loc) · 7.58 KB
/
graphtest.cpp
File metadata and controls
253 lines (214 loc) · 7.58 KB
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
/**
* Testing BST - Binary Search Tree functions
*
* @author Yusuf Pisan
* @date 19 Oct 2019
*/
#include "graph.h"
#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// global value for testing
// NOLINTNEXTLINE
stringstream globalSS;
// need to reset SS before calling this
void vertexPrinter(const string &s) { globalSS << s; }
// need to reset SS before calling this
void edgePrinter(const string &from, const string &to, int weight) {
globalSS << "[" << from << to << " " << weight << "]";
}
// convert a map to a string so we can compare it
template <typename K, typename L>
static string map2string(const map<K, L> &mp) {
stringstream out;
for (auto &p : mp) {
out << "[" << p.first << ":" << p.second << "]";
}
return out.str();
}
void testGraphBasic() {
Graph g;
assert(g.add("a") && "add vertex a");
assert(g.add("b") && "add vertex b");
assert(g.add("c") && "add vertex c");
assert(g.add("d") && "add vertex d");
assert(g.add("e") && "add vertex e");
assert(!g.add("b") && "b added twice");
assert(g.connect("a", "b", 10) && "connect a b");
assert(!g.connect("a", "b", 50) && "duplicate connect a b");
assert(!g.connect("a", "a", 1) && "connect a to itself");
g.connect("a", "d", 40);
g.connect("a", "c", 20);
assert((g.verticesSize() == 5) && "graph number of vertices");
assert((g.edgesSize() == 3) && "graph number of edges");
assert((g.vertexDegree("a") == 3) && "vertex number of edges");
assert((g.vertexDegree("c") == 0) && "no outgoing edges c");
assert((g.vertexDegree("xxx") == -1) && "no edges for xxx");
assert(!g.contains("xxx") && "xxx not in graph");
assert(g.contains("a") && "a in graph");
// check that they are sorted based on edge end label
assert(g.getEdgesAsString("a") == "b(10),c(20),d(40)");
// disconnect non-existent edge/vertex
assert(!g.disconnect("a", "e") && "disconnecting non-existent vertex");
assert((g.edgesSize() == 3) && "disconnected nonexisting");
assert(g.disconnect("a", "c") && "a-c disconnect");
assert((g.edgesSize() == 2) && "number of edges after disconnect");
assert((g.vertexDegree("a") == 2) && "a has 2 edges");
assert(g.getEdgesAsString("a") == "b(10),d(40)" && "removing middle edge");
}
void testGraph0DFS() {
cout << "testGraph0DFS" << endl;
Graph g;
if (!g.readFile("graph0.txt")) {
return;
}
assert(g.contains("A") && "a in graph");
assert(g.contains("B") && "b in graph");
assert(g.contains("C") && "c in graph");
assert(g.getEdgesAsString("A") == "B(1),C(8)");
assert(g.getEdgesAsString("B") == "C(3)");
assert(g.getEdgesAsString("C").empty());
g.dfs("A", vertexPrinter);
assert(globalSS.str() == "ABC" && "starting from A");
globalSS.str("");
g.dfs("B", vertexPrinter);
assert(globalSS.str() == "BC" && "starting from B");
globalSS.str("");
g.dfs("C", vertexPrinter);
assert(globalSS.str() == "C" && "starting from C");
globalSS.str("");
g.dfs("X", vertexPrinter);
assert(globalSS.str().empty() && "starting from X");
}
void testGraph0BFS() {
cout << "testGraph0BFS" << endl;
Graph g;
if (!g.readFile("graph0.txt")) {
return;
}
globalSS.str("");
g.bfs("A", vertexPrinter);
assert(globalSS.str() == "ABC" && "starting from A");
globalSS.str("");
g.bfs("B", vertexPrinter);
assert(globalSS.str() == "BC" && "starting from B");
globalSS.str("");
g.bfs("C", vertexPrinter);
assert(globalSS.str() == "C" && "starting from C");
globalSS.str("");
g.bfs("X", vertexPrinter);
assert(globalSS.str().empty() && "starting from X");
}
void testGraph0Dijkstra() {
cout << "testGraph0Dijkstra" << endl;
Graph g;
if (!g.readFile("graph0.txt")) {
return;
}
map<string, int> weights;
map<string, string> previous;
tie(weights, previous) = g.dijkstra("A");
// cout << "Dijkstra(A) weights is " << map2string(weights) << endl;
assert(map2string(weights) == "[B:1][C:4]" && "Dijkstra(A) weights");
// cout << "Dijkstra(A) previous is " << map2string(previous) << endl;
assert(map2string(previous) == "[B:A][C:B]" && "Dijkstra(A) previous");
tie(weights, previous) = g.dijkstra("B");
assert(map2string(weights) == "[C:3]" && "Dijkstra(B) weights");
assert(map2string(previous) == "[C:B]" && "Dijkstra(B) previous");
tie(weights, previous) = g.dijkstra("X");
assert(map2string(weights).empty() && "Dijkstra(C) weights");
assert(map2string(previous).empty() && "Dijkstra(C) previous");
}
void testGraph0NotDirected() {
cout << "testGraph0NotDirected" << endl;
bool isDirectional = false;
Graph g(isDirectional);
if (!g.readFile("graph0.txt")) {
return;
}
globalSS.str("");
g.bfs("A", vertexPrinter);
assert(globalSS.str() == "ABC" && "starting from A");
globalSS.str("");
g.dfs("B", vertexPrinter);
assert(globalSS.str() == "BAC" && "starting from B");
globalSS.str("");
g.dfs("C", vertexPrinter);
assert(globalSS.str() == "CAB" && "starting from C");
globalSS.str("");
g.dfs("X", vertexPrinter);
assert(globalSS.str().empty() && "starting from X");
map<string, int> weights;
map<string, string> previous;
tie(weights, previous) = g.dijkstra("A");
// cout << "Dijkstra(A) weights is " << map2string(weights) << endl;
assert(map2string(weights) == "[B:1][C:4]" && "Dijkstra(A) weights");
// cout << "Dijkstra(A) previous is " << map2string(previous) << endl;
assert(map2string(previous) == "[B:A][C:B]" && "Dijkstra(A) previous");
tie(weights, previous) = g.dijkstra("B");
assert(map2string(weights) == "[A:1][C:3]" && "Dijkstra(B) weights");
assert(map2string(previous) == "[A:B][C:B]" && "Dijkstra(B) previous");
tie(weights, previous) = g.dijkstra("X");
assert(map2string(weights).empty() && "Dijkstra(C) weights");
assert(map2string(previous).empty() && "Dijkstra(C) previous");
globalSS.str("");
int mstLength = g.mstPrim("A", edgePrinter);
assert(mstLength == 4 && "mst A is 4");
assert(globalSS.str() == "[AB 1][BC 3]" && "mst A is [AB 1][BC 3]");
globalSS.str("");
mstLength = g.mstPrim("B", edgePrinter);
assert(mstLength == 4 && "mst 4 is 4");
assert(globalSS.str() == "[BA 1][BC 3]");
globalSS.str("");
mstLength = g.mstPrim("C", edgePrinter);
assert(mstLength == 4 && "mst C is 4");
assert(globalSS.str() == "[CB 3][BA 1]");
globalSS.str("");
mstLength = g.mstPrim("X", edgePrinter);
assert(mstLength == -1 && "mst X is -1");
assert(globalSS.str().empty() && "mst for vertex not found");
// bonus Kruskal
globalSS.str("");
mstLength = g.mstKruskal(edgePrinter);
assert(mstLength == 4 && "mstKruskal is 4");
// cout << globalSS.str() << endl;
// [AB 1][BC 3]
}
void testGraph1() {
cout << "testGraph1" << endl;
Graph g;
if (!g.readFile("graph1.txt")) {
return;
}
globalSS.str("");
g.dfs("A", vertexPrinter);
assert(globalSS.str() == "ABCDEFGH" && "dfs starting from A");
globalSS.str("");
g.bfs("A", vertexPrinter);
assert(globalSS.str() == "ABHCGDEF" && "bfs starting from A");
globalSS.str("");
g.dfs("B", vertexPrinter);
assert(globalSS.str() == "BCDEFG" && "dfs starting from B");
globalSS.str("");
g.bfs("B", vertexPrinter);
assert(globalSS.str() == "BCDEFG" && "bfs starting from B");
map<string, int> weights;
map<string, string> previous;
auto p = g.dijkstra("A");
weights = p.first;
previous = p.second;
assert(map2string(weights) == "[B:1][C:2][D:3][E:4][F:5][G:4][H:3]" &&
"Dijkstra(B) weights");
assert(map2string(previous) == "[B:A][C:B][D:C][E:D][F:E][G:H][H:A]" &&
"Dijkstra(B) previous");
}
void testAll() {
testGraphBasic();
testGraph0DFS();
testGraph0BFS();
testGraph0Dijkstra();
testGraph0NotDirected();
testGraph1();
}