-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgraph-traversal.spec.js
208 lines (184 loc) · 6.18 KB
/
graph-traversal.spec.js
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
const chai = require('chai');
const expect = chai.expect;
const Node = require('../graphGenerator');
const BFS = require('../breadthFirstSearch');
const DFS = require('../depthFirstSearch');
describe('Graph Generator', () => {
let A;
let B;
let C;
let D;
let E;
let F;
beforeEach(() => {
A = new Node("A", "Joe");
B = new Node("B", "Jon");
C = new Node("C", "Ray");
D = new Node("D", "JSON");
E = new Node("E", "Marifel");
F = new Node("F", "Nigel");
});
it('should be a function that exists', () => {
expect(Node).to.exist;
expect(Node).to.be.a('function');
});
it('Creating a new Node should return an object', () => {
expect(A).to.exist;
expect(A).to.be.an('object');
});
it('Node should have a property `name`', () => {
expect(A.name).to.exist;
expect(B.name).to.exist;
expect(C.name).to.exist;
expect(A.name).to.be.an('String');
expect(B.name).to.be.an('String');
expect(C.name).to.be.an('String');
expect(A.name).to.equal('A');
expect(B.name).to.equal('B');
expect(C.name).to.equal('C');
});
it('Node should have a property `value`', () => {
expect(A.value).to.exist;
expect(B.value).to.exist;
expect(C.value).to.exist;
expect(A.value).to.equal('Joe');
expect(B.value).to.equal('Jon');
expect(C.value).to.equal('Ray');
});
it('Node should have a property `neighbors`', () => {
expect(A.neighbors).to.exist;
expect(B.neighbors).to.exist;
expect(C.neighbors).to.exist;
expect(D.neighbors).to.exist;
expect(E.neighbors).to.exist;
expect(F.neighbors).to.exist;
});
it('Node property `neighbors` should be initialized to an empty array', () => {
expect(A.neighbors).to.be.an('Array');
expect(B.neighbors).to.be.an('Array');
expect(C.neighbors).to.be.an('Array');
expect(A.neighbors).to.deep.equal([]);
expect(B.neighbors).to.deep.equal([]);
expect(C.neighbors).to.deep.equal([]);
});
it('Node should have a method `addNeighbors`', () => {
expect(A.addNeighbors()).to.exist;
expect(B.addNeighbors()).to.exist;
expect(C.addNeighbors()).to.exist;
});
it('Node method `addNeighbors` should return an array', () => {
expect(A.addNeighbors([])).to.be.an('Array');
expect(B.addNeighbors([])).to.be.an('Array');
expect(C.addNeighbors([])).to.be.an('Array');
});
it('Node method `addNeighbors` should return an array of Nodes', () => {
A.addNeighbors([B,C])
expect(A.neighbors[0].name).to.equal('B');
expect(A.neighbors[0].value).to.equal('Jon');
expect(A.neighbors[1].name).to.equal('C');
expect(A.neighbors[1].value).to.equal('Ray');
A.addNeighbors([D,E])
expect(A.neighbors[0].name).to.equal('B');
expect(A.neighbors[0].value).to.equal('Jon');
expect(A.neighbors[1].name).to.equal('C');
expect(A.neighbors[1].value).to.equal('Ray');
expect(A.neighbors[2].name).to.equal('D');
expect(A.neighbors[2].value).to.equal('JSON');
expect(A.neighbors[3].name).to.equal('E');
expect(A.neighbors[3].value).to.equal('Marifel');
});
it('Node should have a method `getNeighbors`', () => {
expect(A.getNeighbors()).to.exist;
expect(B.getNeighbors()).to.exist;
expect(C.getNeighbors()).to.exist;
expect(A.getNeighbors()).to.be.an('Array');
expect(B.getNeighbors()).to.be.an('Array');
expect(C.getNeighbors()).to.be.an('Array');
expect(A.getNeighbors()).to.deep.equal([]);
expect(B.getNeighbors()).to.deep.equal([]);
expect(B.getNeighbors()).to.deep.equal([]);
});
it('Node `neighbors` should refernce other neighbors', () => {
A.addNeighbors([B, C]);
B.addNeighbors([D, E]);
C.addNeighbors([F]);
expect(A.neighbors[0].name).to.equal('B');
expect(A.neighbors[0].value).to.equal('Jon');
expect(A.neighbors[0].neighbors[0].name).to.equal('D');
expect(A.neighbors[1].name).to.equal('C');
expect(A.neighbors[1].value).to.equal('Ray');
expect(A.neighbors[1].neighbors[0].name).to.equal('F');
expect(B.neighbors[0].name).to.equal('D');
expect(B.neighbors[0].value).to.equal('JSON');
expect(B.neighbors[0].neighbors).to.deep.equal([]);
expect(B.neighbors[1].name).to.equal('E');
expect(B.neighbors[1].value).to.equal('Marifel');
expect(B.neighbors[1].neighbors).to.deep.equal([]);
});
});
describe('Depth First Search', () => {
let A;
let B;
let C;
let D;
let E;
let F;
beforeEach(() => {
A = new Node("A", "Joe");
B = new Node("B", "Jon");
C = new Node("C", "Ray");
D = new Node("D", "JSON");
E = new Node("E", "Marifel");
F = new Node("F", "Nigel");
A.addNeighbors([B, C]);
B.addNeighbors([D, E]);
C.addNeighbors([F]);
});
it('should be a function that exists', () => {
expect(DFS).to.exist;
expect(DFS).to.be.a('function');
});
it('should return the node with the value of `searchFor` stored in its `name` property', () => {
expect(DFS(A, "D").value).to.equal("JSON");
expect(DFS(A, "D").name).to.equal("D");
expect(DFS(A, "F").value).to.equal("Nigel");
expect(DFS(A, "F").name).to.equal("F");
expect(DFS(B, "E").value).to.equal("Marifel");
expect(DFS(A, "E").name).to.equal("E");
});
it('should return false if it cant find the value in the graph', () => {
expect(DFS(F, "Joe")).to.equal(false);
expect(DFS(E, "Joe")).to.equal(false);
});
});
describe('Breadth First Search', () => {
let A;
let B;
let C;
let D;
let E;
let F;
beforeEach(() => {
A = new Node("A", "Joe");
B = new Node("B", "Jon");
C = new Node("C", "Ray");
D = new Node("D", "JSON");
E = new Node("E", "Marifel");
F = new Node("F", "Nigel");
A.addNeighbors([B, C]);
B.addNeighbors([D, E]);
C.addNeighbors([F]);
});
it('should be a function that exists', () => {
expect(BFS).to.exist;
expect(BFS).to.be.a('function');
});
it('should return the traversal path from the starting point all the way to the end', () => {
expect(BFS(A)).to.deep.equal(["A","B","C","D", "E", "F"]);
expect(BFS(B)).to.deep.equal(["B","D","E"]);
expect(BFS(C)).to.deep.equal(["C","F"]);
expect(BFS(D)).to.deep.equal(["D"]);
expect(BFS(E)).to.deep.equal(["E"]);
expect(BFS(F)).to.deep.equal(["F"]);
});
});