-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathundirected-graph.test.ts
85 lines (75 loc) · 3.14 KB
/
undirected-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
import { UndirectedGraph } from './undirected-graph';
describe('UndirectedGraph', () => {
it('should create graph', () => {
const graph = new UndirectedGraph<number>();
expect(graph).toBeInstanceOf(UndirectedGraph);
expect(graph.isEmpy()).toBe(true);
});
it('should add vertex', () => {
const graph = new UndirectedGraph<number>();
expect(() => graph.addVertex('A', 1)).not.toThrow();
expect(graph.isEmpy()).toBe(false);
expect(graph.getVertexValue('A')).toBe(1);
expect(graph.neighbors('A')).toEqual([]);
});
it('should throw when adding an existing vertex', () => {
const graph = new UndirectedGraph<number>();
graph.addVertex('A', 1);
expect(() => graph.addVertex('A', 1)).toThrowError('Vertex with id A already exists');
});
it('should update vertex', () => {
const graph = new UndirectedGraph<number>();
graph.addVertex('A', 0);
expect(graph.getVertexValue('A')).toBe(0);
expect(() => graph.updateVertex('A', 1)).not.toThrow();
expect(graph.getVertexValue('A')).toBe(1);
});
it('should throw when updating a non-existing vertex', () => {
const graph = new UndirectedGraph<number>();
expect(() => graph.updateVertex('A', 1)).toThrowError('Vertex with id A does not exist');
});
it('should add edge', () => {
const graph = new UndirectedGraph<number>();
graph.addVertex('A', 1);
graph.addVertex('B', 2);
expect(() => graph.addEdge('A', 'B')).not.toThrow();
expect(graph.adjacent('A', 'B')).toBe(true);
expect(graph.adjacent('B', 'A')).toBe(true);
expect(graph.neighbors('A')).toEqual(['B']);
expect(graph.neighbors('B')).toEqual(['A']);
});
it('should throw when adding an edge to a non-existing vertex', () => {
const graph = new UndirectedGraph<number>();
expect(() => graph.addEdge('A', 'B')).toThrowError('Vertex with id A does not exist');
graph.addVertex('A', 1);
expect(() => graph.addEdge('A', 'B')).toThrowError('Vertex with id B does not exist');
});
it('should remove edge', () => {
const graph = new UndirectedGraph<number>();
graph.addVertex('A', 1);
graph.addVertex('B', 1);
graph.addEdge('A', 'B');
expect(() => graph.removeEdge('A', 'B')).not.toThrow();
expect(graph.adjacent('A', 'B')).toBe(false);
expect(graph.adjacent('B', 'A')).toBe(false);
expect(graph.neighbors('A')).toEqual([]);
expect(graph.neighbors('B')).toEqual([]);
});
it('should remove vertex', () => {
const graph = new UndirectedGraph<number>();
graph.addVertex('A', 1);
expect(() => graph.removeVertex('A')).not.toThrow();
expect(graph.isEmpy()).toBe(true);
});
it('should remove edges when removing vertex', () => {
const graph = new UndirectedGraph<number>();
graph.addVertex('A', 1);
graph.addVertex('B', 1);
graph.addEdge('A', 'B');
graph.removeVertex('A');
expect(() => graph.adjacent('A', 'B')).toThrowError('Vertex with id A does not exist');
expect(() => graph.adjacent('B', 'A')).toThrowError('Vertex with id A does not exist');
expect(() => graph.neighbors('A')).toThrowError('Vertex with id A does not exist');
expect(graph.neighbors('B')).toEqual([]);
});
});