Skip to content

Commit 8fe14d3

Browse files
committed
Updates the tests of bfs
1 parent c972cdc commit 8fe14d3

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

src/graphs/searching/bfs.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @private
2020
* @param {array} inputGraph The input matrix of the graph
21-
* @param {number} destination The destination
21+
* @param {array} destination The destination
2222
*/
2323
function init(inputGraph, destination) {
2424
graph = inputGraph;
@@ -27,6 +27,10 @@
2727
visited = {};
2828
}
2929

30+
/**
31+
* Adds a valid node to the queue
32+
* @param {array} node Node to be added to the queue
33+
*/
3034
function addNode(node) {
3135
if (visited[node] ||
3236
node[0] < 0 || node[1] < 0 ||
@@ -40,9 +44,8 @@
4044
/**
4145
* Process given node
4246
*
43-
* @param {number} destination The destionation, which should be reached
44-
* @param {number} current The current node
45-
* @param {number} node Neighbour node
47+
* @param {array} destination The destionation, which should be reached
48+
* @param {array} current The current node
4649
*/
4750
function processNode(destination, current) {
4851
if (destination.toString() === current.toString()) {
@@ -57,40 +60,40 @@
5760
}
5861

5962
/**
60-
* Validates the graph
63+
* Validates the params
6164
*
6265
* @param {array} graph A matrix representation of the graph
63-
* @param {number} source The source node
64-
* @param {number} destination The destination node
65-
* @returns {boolean} true/false depending whether the params are valid
66+
* @param {array} source The source node
67+
* @param {array} destination The destination node
6668
*/
6769
function validateParams(graph, source, destination) {
6870
if (!graph) {
69-
throw 'The graph should be represented as a matrix';
71+
throw new Error('The graph should be represented as a matrix');
7072
}
71-
if (!graph[0]) {
72-
throw 'The graph should be represented as ' +
73-
'a matrix, with size at least 1x1';
73+
if (graph[0] === undefined) {
74+
throw new Error('The graph should be represented as ' +
75+
'a matrix, with size at least 1x1');
7476
}
7577
var width = graph[0].length;
76-
for (var i = 0; i < graph.length; i += 1) {
77-
if (graph[i] !== width) {
78-
throw 'The graph should be represented as a matrix';
78+
for (var i = 1; i < graph.length; i += 1) {
79+
if (graph[i].length !== width) {
80+
throw new Error('The graph should be represented as a matrix');
7981
}
8082
}
8183
source.concat(destination).filter(function (c, i) {
8284
if (c < 0) {
83-
throw 'The source and destination coordinates should be above zero';
85+
throw new Error('The source and destination coordinates ' +
86+
'should be above zero');
8487
}
8588
if (i % 2 === 0) {
8689
if (c >= graph.length) {
87-
throw 'The source and destination coordinates ' +
88-
'should not be above graph\'s size';
90+
throw new Error('The source and destination coordinates ' +
91+
'should not be above graph\'s size');
8992
}
9093
} else {
9194
if (c >= graph[0].length) {
92-
throw 'The source and destination coordinates ' +
93-
'should not be above graph\'s size';
95+
throw new Error('The source and destination coordinates ' +
96+
'should not be above graph\'s size');
9497
}
9598
}
9699
});

test/graphs/searching/bfs.spec.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,35 @@ var bfs = require('../../../src/graphs/searching/bfs').breadthFirstSearch;
1313

1414
describe('BFS', function () {
1515

16-
var graph;
17-
1816
it('should work with incorrect input', function () {
1917
expect(function () {
2018
bfs(null, [1, 1], [1, 1]);
2119
}).toThrow();
22-
// expect(bfs(sampleGraph, [-1, -1], [0, 0])).toBe(false);
23-
// expect(bfs(sampleGraph, [0, -1], [-1, 0])).toBe(false);
24-
// expect(bfs(sampleGraph, [0, 0], [-1, 0])).toBe(false);
25-
// expect(bfs(sampleGraph, [0, 1000], [-1, 0])).toBe(false);
26-
// expect(bfs(sampleGraph, [100000, 1000], [-1, 0])).toBe(false);
27-
// expect(bfs(sampleGraph, [0, 0], [100, 100])).toBe(false);
28-
// expect(bfs(sampleGraph, [0, 0], [6, 6])).toBe(false);
20+
expect(function () {
21+
bfs(sampleGraph, [-1, -1], [0, 0]);
22+
}).toThrow();
23+
expect(function () {
24+
bfs(sampleGraph, [0, -1], [-1, 0]);
25+
}).toThrow();
26+
expect(function () {
27+
bfs(sampleGraph, [0, 0], [-1, 0]);
28+
}).toThrow();
29+
expect(function () {
30+
bfs(sampleGraph, [0, 1000], [-1, 0]);
31+
}).toThrow();
32+
expect(function () {
33+
bfs(sampleGraph, [100000, 1000], [-1, 0]);
34+
}).toThrow();
35+
expect(function () {
36+
bfs(sampleGraph, [0, 0], [100, 100]);
37+
}).toThrow();
38+
expect(function () {
39+
bfs(sampleGraph, [0, 0], [5, 5]);
40+
}).not.toThrow();
2941
});
3042

3143
it('should work with 1x1 matrix', function () {
32-
graph = [[1]];
44+
var graph = [[1]];
3345
expect(bfs(graph, [0, 0], [0, 0])).toBeTruthy();
3446
graph = [[0]];
3547
expect(bfs(graph, [0, 0], [0, 0])).toBeFalsy();

0 commit comments

Comments
 (0)