forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphVertex.test.js
More file actions
103 lines (79 loc) · 3.03 KB
/
GraphVertex.test.js
File metadata and controls
103 lines (79 loc) · 3.03 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
import GraphVertex from '../GraphVertex';
import GraphEdge from '../GraphEdge';
describe('GraphVertex', () => {
it('should throw an error when trying to create vertex without value', () => {
let vertex = null;
function createEmptyVertex() {
vertex = new GraphVertex();
}
expect(vertex).toBeNull();
expect(createEmptyVertex).toThrow();
});
it('should create graph vertex', () => {
const vertex = new GraphVertex('A');
expect(vertex).toBeDefined();
expect(vertex.value).toBe('A');
expect(vertex.toString()).toBe('A');
expect(vertex.getKey()).toBe('A');
expect(vertex.edges.toString()).toBe('');
expect(vertex.getEdges()).toEqual([]);
});
it('should add edges to vertex and check if it exists', () => {
const vertexA = new GraphVertex('A');
const vertexB = new GraphVertex('B');
const edgeAB = new GraphEdge(vertexA, vertexB);
vertexA.addEdge(edgeAB);
expect(vertexA.hasEdge(edgeAB)).toBeTruthy();
expect(vertexB.hasEdge(edgeAB)).toBeFalsy();
expect(vertexA.getEdges().length).toBe(1);
expect(vertexA.getEdges()[0].toString()).toBe('A_B');
});
it('should return vertex neighbors in case if current node is start one', () => {
const vertexA = new GraphVertex('A');
const vertexB = new GraphVertex('B');
const vertexC = new GraphVertex('C');
const edgeAB = new GraphEdge(vertexA, vertexB);
const edgeAC = new GraphEdge(vertexA, vertexC);
vertexA
.addEdge(edgeAB)
.addEdge(edgeAC);
expect(vertexB.getNeighbors()).toEqual([]);
const neighbors = vertexA.getNeighbors();
expect(neighbors.length).toBe(2);
expect(neighbors[0]).toEqual(vertexB);
expect(neighbors[1]).toEqual(vertexC);
});
it('should return vertex neighbors in case if current node is end one', () => {
const vertexA = new GraphVertex('A');
const vertexB = new GraphVertex('B');
const vertexC = new GraphVertex('C');
const edgeBA = new GraphEdge(vertexB, vertexA);
const edgeCA = new GraphEdge(vertexC, vertexA);
vertexA
.addEdge(edgeBA)
.addEdge(edgeCA);
expect(vertexB.getNeighbors()).toEqual([]);
const neighbors = vertexA.getNeighbors();
expect(neighbors.length).toBe(2);
expect(neighbors[0]).toEqual(vertexB);
expect(neighbors[1]).toEqual(vertexC);
});
it('should check if vertex has specific neighbor', () => {
const vertexA = new GraphVertex('A');
const vertexB = new GraphVertex('B');
const vertexC = new GraphVertex('C');
const edgeAB = new GraphEdge(vertexA, vertexB);
vertexA.addEdge(edgeAB);
expect(vertexA.hasNeighbor(vertexB)).toBeTruthy();
expect(vertexA.hasNeighbor(vertexC)).toBeFalsy();
});
it('should edge by vertex', () => {
const vertexA = new GraphVertex('A');
const vertexB = new GraphVertex('B');
const vertexC = new GraphVertex('C');
const edgeAB = new GraphEdge(vertexA, vertexB);
vertexA.addEdge(edgeAB);
expect(vertexA.findEdge(vertexB)).toEqual(edgeAB);
expect(vertexA.findEdge(vertexC)).toBeNull();
});
});