-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.test.ts
396 lines (332 loc) · 13.1 KB
/
graph.test.ts
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import IGraph from "../../../../src/app/types/IGraph";
import UndirectedGraph from "../../../../src/app/data-structures/Graph/UndirectedGraph";
import DirectedGraph from "../../../../src/app/data-structures/Graph/DirectedGraph";
import { createGraph } from "../../../../src/app/data-structures/Graph/_helpers/createGraph";
import { EnumGraphType } from "../../../../src/app/types/EnumGraphType";
import IsAlreadyExistsException from "../../../../src/app/exceptions/IsAlreadyExistsException";
import IsNotFoundException from "../../../../src/app/exceptions/IsNotFoundException";
describe.each([EnumGraphType.DIRECTED, EnumGraphType.UNDIRECTED])(
"%s graph",
(graphType: EnumGraphType) => {
describe("method weight", () => {
const graph: IGraph<string> = createGraph(graphType);
graph
.addVertex("Mike")
.addVertex("Bob")
.addVertex("Lisa")
.addEdge("Mike", "Bob", 5)
.addEdge("Bob", "Lisa", 10)
.addEdge("Mike", "Lisa", 20);
it("should return correct weight value", () => {
const weight = graph.weight();
const expectedWeight = 35;
expect(weight).toBe(expectedWeight);
});
});
describe("method verticesCount", () => {
describe("in empty graph", () => {
const graph: IGraph<string> = createGraph(graphType);
it("should return correct vertices count", () => {
expect(graph.verticesCount()).toBe(0);
});
});
describe("in non empty graph", () => {
const graph: IGraph<string> = createGraph(graphType);
graph.addVertex("Mike").addVertex("Bob").addVertex("Lisa");
it("should return correct vertices count", () => {
expect(graph.verticesCount()).toBe(3);
});
});
});
describe("method edgesCount", () => {
describe("in empty graph", () => {
const graph: IGraph<string> = createGraph(graphType);
it("should return correct vertices count", () => {
expect(graph.edgesCount()).toBe(0);
});
});
if (graphType === EnumGraphType.UNDIRECTED) {
describe("in non-empty graph", () => {
const graph: IGraph<string> = new UndirectedGraph();
graph
.addVertex("Mike")
.addVertex("Bob")
.addVertex("Lisa")
.addEdge("Mike", "Bob")
.addEdge("Mike", "Lisa")
.addEdge("Bob", "Lisa");
it("should return correct vertices count", () => {
expect(graph.edgesCount()).toBe(3);
});
});
}
if (graphType === EnumGraphType.DIRECTED) {
describe("in non-empty graph", () => {
const graph: IGraph<string> = new DirectedGraph();
graph
.addVertex("Mike")
.addVertex("Bob")
.addVertex("Lisa")
.addEdge("Mike", "Bob")
.addEdge("Mike", "Lisa")
.addEdge("Lisa", "Mike");
it("should return correct vertices count", () => {
expect(graph.edgesCount()).toBe(3);
});
});
}
});
describe("method vertices", () => {
describe("in empty graph", () => {
const graph: IGraph<string> = createGraph(graphType);
it("should return empty array of vertices", () => {
expect(graph.vertices()).toEqual([]);
});
});
describe("in non empty graph", () => {
const graph: IGraph<string> = createGraph(graphType);
graph.addVertex("Mike").addVertex("Bob").addVertex("Lisa");
it("should return correct array of vertices", () => {
expect(graph.vertices()).toEqual(["Mike", "Bob", "Lisa"]);
});
});
});
describe("method addVertex", () => {
it("should correct add vertex", () => {
const graph: IGraph<string> = createGraph(graphType);
graph.addVertex("Mike").addVertex("Bob").addVertex("Lisa");
const vertices = graph.vertices();
const expectedVertices = ["Mike", "Bob", "Lisa"];
expect(vertices).toEqual(expectedVertices);
});
it("should throw when try to add an existed vertex", () => {
const graph: IGraph<string> = createGraph(graphType);
graph.addVertex("Mike");
expect(() => {
graph.addVertex("Mike");
}).toThrowError(IsAlreadyExistsException);
});
});
describe("method addEdge", () => {
describe("should throw when try to add edge between not existed vertices", () => {
const graph: IGraph<string> = createGraph(graphType);
graph.addVertex("Mike").addVertex("Bob");
it("when first node does not exist", () => {
expect(() => {
graph.addEdge("NOT_EXISTED_NODE", "Bob");
}).toThrowError(IsNotFoundException);
});
it("when second node does not exist", () => {
expect(() => {
graph.addEdge("Mike", "NOT_EXISTED_NODE");
}).toThrowError(IsNotFoundException);
});
});
if (graphType === EnumGraphType.UNDIRECTED) {
describe("should correct add edge between two existed vertices", () => {
const graph: IGraph<string> = new UndirectedGraph();
graph.addVertex("Mike").addVertex("Bob").addEdge("Mike", "Bob");
it("should add second node to first node neighbors", () => {
expect(graph.getVertexNeighbors("Mike")).toContain("Bob");
});
it("should add first node to second node neighbors", () => {
expect(graph.getVertexNeighbors("Bob")).toContain("Mike");
});
});
describe("should override an existed edge and its weight", () => {
const graph: IGraph<string> = new UndirectedGraph();
graph
.addVertex("Mike")
.addVertex("Bob")
.addEdge("Mike", "Bob", 5)
.addEdge("Mike", "Bob", 10);
describe("should not multiple edges", () => {
it("should not add vertex to first edge", () => {
expect(graph.getVertexNeighbors("Mike")).toEqual(["Bob"]);
});
it("should not add vertex to second edge", () => {
expect(graph.getVertexNeighbors("Bob")).toEqual(["Mike"]);
});
});
it("should override edge weight", () => {
expect(graph.getEdgeWeight("Mike", "Bob")).toBe(10);
});
});
}
if (graphType === EnumGraphType.DIRECTED) {
describe("should correct add edge between two existed vertices", () => {
const graph: IGraph<string> = new DirectedGraph();
graph.addVertex("Mike").addVertex("Bob").addEdge("Mike", "Bob");
it("should add second node to first node neighbors", () => {
expect(graph.getVertexNeighbors("Mike")).toContain("Bob");
});
it("should not add first node to second node neighbors", () => {
expect(graph.getVertexNeighbors("Bob")).not.toContain("Mike");
});
});
describe("should override an existed edge and its weight", () => {
const graph: IGraph<string> = new DirectedGraph();
graph
.addVertex("Mike")
.addVertex("Bob")
.addEdge("Mike", "Bob", 5)
.addEdge("Bob", "Mike", 15)
.addEdge("Mike", "Bob", 10);
describe("should not multiple edges", () => {
it("should not add vertex to first edge", () => {
expect(graph.getVertexNeighbors("Mike")).toEqual(["Bob"]);
});
it("should not add vertex to second edge", () => {
expect(graph.getVertexNeighbors("Bob")).toEqual(["Mike"]);
});
});
it("should override edge weight", () => {
expect(graph.getEdgeWeight("Mike", "Bob")).toBe(10);
});
it("should not override back edge weight", () => {
expect(graph.getEdgeWeight("Bob", "Mike")).toBe(15);
});
});
}
});
describe("method removeVertex", () => {
it("should throw when try to delete not existed vertex", () => {
const graph: IGraph<string> = createGraph(graphType);
expect(() => {
graph.removeVertex("NOT_EXISTED_VERTEX");
}).toThrowError(IsNotFoundException);
});
if (graphType === EnumGraphType.UNDIRECTED) {
describe("should cascade remove", () => {
const graph: IGraph<string> = new UndirectedGraph();
graph
.addVertex("Mike")
.addVertex("Bob")
.addVertex("John")
.addVertex("Lisa")
.addEdge("Mike", "Bob")
.addEdge("Mike", "Lisa")
.addEdge("John", "Lisa")
.addEdge("Bob", "John");
graph.removeVertex("Bob");
it("should remove edges", () => {
const hasEdge = graph.hasEdge("Mike", "Bob");
expect(hasEdge).toBe(false);
});
it("should remove related vertexes neighbors", () => {
const mikeNeighbors = graph.getVertexNeighbors("Mike");
const lisaNeighbors = graph.getVertexNeighbors("Lisa");
const johnNeighbors = graph.getVertexNeighbors("John");
expect(mikeNeighbors).toEqual(["Lisa"]);
expect(lisaNeighbors).toEqual(["Mike", "John"]);
expect(johnNeighbors).toEqual(["Lisa"]);
});
});
}
if (graphType === EnumGraphType.DIRECTED) {
describe("should cascade remove", () => {
const graph: IGraph<string> = new DirectedGraph();
graph
.addVertex("Mike")
.addVertex("Bob")
.addVertex("John")
.addVertex("Lisa")
.addEdge("Mike", "Bob")
.addEdge("Mike", "Lisa")
.addEdge("John", "Lisa")
.addEdge("Bob", "John");
graph.removeVertex("Bob");
it("should remove edges", () => {
const hasEdge = graph.hasEdge("Mike", "Bob");
expect(hasEdge).toBe(false);
});
it("should remove related vertices neighbors", () => {
const mikeNeighbors = graph.getVertexNeighbors("Mike");
const lisaNeighbors = graph.getVertexNeighbors("Lisa");
const johnNeighbors = graph.getVertexNeighbors("John");
expect(mikeNeighbors).toEqual(["Lisa"]);
expect(lisaNeighbors).toEqual([]);
expect(johnNeighbors).toEqual(["Lisa"]);
});
});
}
});
describe("method removeEdge", () => {
it("should throw when try to delete not existed edge", () => {
const graph: IGraph<string> = createGraph(graphType);
expect(() => {
graph.removeEdge("NOT_EXISTED_VERTEX", "NOT_EXISTED_VERTEX");
}).toThrowError(IsNotFoundException);
});
if (graphType === EnumGraphType.UNDIRECTED) {
describe("should delete correct", () => {
const graph: IGraph<string> = new UndirectedGraph();
graph
.addVertex("Mike")
.addVertex("Bob")
.addVertex("Lisa")
.addEdge("Mike", "Bob")
.addEdge("Bob", "Lisa")
.addEdge("Mike", "Lisa");
graph.removeEdge("Mike", "Lisa");
it("should delete correct", () => {
expect(graph.hasEdge("Mike", "Lisa")).toBe(false);
});
});
}
if (graphType === EnumGraphType.DIRECTED) {
describe("should delete correct", () => {
const graph: IGraph<string> = new DirectedGraph();
graph
.addVertex("Mike")
.addVertex("Bob")
.addVertex("Lisa")
.addEdge("Mike", "Bob")
.addEdge("Bob", "Lisa")
.addEdge("Mike", "Lisa")
.addEdge("Lisa", "Mike");
graph.removeEdge("Mike", "Lisa");
it("should delete edge from-to", () => {
expect(graph.hasEdge("Mike", "Lisa")).toBe(false);
});
it("should not delete edge to-from", () => {
expect(graph.hasEdge("Lisa", "Mike")).toBe(true);
});
});
}
});
describe("method getVertexNeighbors", () => {
it("should throw when vertex does not exist", () => {
const graph: IGraph<number> = createGraph(graphType);
graph.addVertex(1).addVertex(2).addEdge(1, 2);
expect(() => {
graph.getVertexNeighbors(0);
}).toThrowError();
});
if (graphType === EnumGraphType.UNDIRECTED) {
it("should return correct neighbors", () => {
const graph: IGraph<number> = new UndirectedGraph();
graph
.addVertex(1)
.addVertex(2)
.addVertex(3)
.addEdge(1, 2)
.addEdge(2, 3);
expect(graph.getVertexNeighbors(2)).toEqual([1, 3]);
});
}
if (graphType === EnumGraphType.DIRECTED) {
it("should return correct neighbors", () => {
const graph: IGraph<number> = new DirectedGraph();
graph
.addVertex(1)
.addVertex(2)
.addVertex(3)
.addEdge(1, 2)
.addEdge(2, 3);
expect(graph.getVertexNeighbors(2)).toEqual([3]);
});
}
});
}
);