Skip to content

Commit 942504f

Browse files
committed
Update dfs tests
1 parent b6ddbcf commit 942504f

File tree

1 file changed

+25
-46
lines changed

1 file changed

+25
-46
lines changed

test/graphs/searching/dfs.spec.js

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,37 @@
11
'use strict';
22

3-
var sampleGraph = [[1, 1, 1, 0, 0, 0],
4-
[0, 1, 1, 1, 0, 0],
5-
[1, 0, 1, 1, 1, 0],
6-
[0, 1, 0, 1, 1, 0],
7-
[0, 1, 0, 1, 1, 0],
8-
[0, 1, 0, 1, 1, 0],
9-
[0, 0, 1, 1, 1, 1],
10-
[0, 0, 0, 0, 1, 1]];
11-
12-
var dfs = require('../../../src/graphs/searching/dfs').depthFirstSearch;
3+
var dfs = require('../../../src/graphs/searching/dfs').dfs;
134

145
describe('dfs', function () {
156

16-
it('should work with incorrect input', function () {
17-
expect(function () {
18-
dfs(null, [1, 1], [1, 1]);
19-
}).toThrow();
20-
expect(function () {
21-
dfs(sampleGraph, [-1, -1], [0, 0]);
22-
}).toThrow();
23-
expect(function () {
24-
dfs(sampleGraph, [0, -1], [-1, 0]);
25-
}).toThrow();
26-
expect(function () {
27-
dfs(sampleGraph, [0, 0], [-1, 0]);
28-
}).toThrow();
29-
expect(function () {
30-
dfs(sampleGraph, [0, 1000], [-1, 0]);
31-
}).toThrow();
32-
expect(function () {
33-
dfs(sampleGraph, [100000, 1000], [-1, 0]);
34-
}).toThrow();
35-
expect(function () {
36-
dfs(sampleGraph, [0, 0], [100, 100]);
37-
}).toThrow();
38-
expect(function () {
39-
dfs(sampleGraph, [0, 0], [5, 5]);
40-
}).not.toThrow();
7+
it('should work with empty graph', function () {
8+
expect(dfs([[]])).toBeTruthy();
419
});
4210

43-
it('should work with 1x1 matrix', function () {
44-
var graph = [[1]];
45-
expect(dfs(graph, [0, 0], [0, 0])).toBeTruthy();
46-
graph = [[0]];
47-
expect(dfs(graph, [0, 0], [0, 0])).toBeFalsy();
11+
it('should always find a path between node and itself', function () {
12+
expect(dfs([[0]]), 0, 0).toBeTruthy();
4813
});
4914

50-
it('should work in the general case', function () {
51-
expect(dfs(sampleGraph, [0, 0], [1, 1])).toBeTruthy();
52-
expect(dfs(sampleGraph, [0, 0], [6, 5])).toBeTruthy();
53-
expect(dfs(sampleGraph, [0, 0], [0, 5])).toBeFalsy();
54-
expect(dfs(sampleGraph, [1, 1], [6, 5])).toBeTruthy();
55-
expect(dfs(sampleGraph, [1, 1], [0, 5])).toBeFalsy();
15+
it('should always find a path between two directly connected nodes',
16+
function () {
17+
expect(dfs([[0, 1], [1, 0]], 0, 1)).toBeTruthy();
18+
expect(dfs([[0, 1], [1, 0]], 1, 0)).toBeTruthy();
19+
});
20+
21+
it('should always find a path between two directly connected' +
22+
'connected nodes in a directed graph', function () {
23+
expect(dfs([[0, 0], [1, 0]], 1, 0)).toBeTruthy();
5624
});
5725

26+
it('should always find a path between two indirectly connected nodes',
27+
function () {
28+
expect(dfs([[0, 1, 0], [0, 0, 1], [0, 0, 0]], 0, 2)).toBeTruthy();
29+
});
30+
31+
it('should not find a path between two nodes, which are not connected',
32+
function () {
33+
expect(dfs([[0, 0], [1, 0]], 0, 1)).toBeFalsy();
34+
expect(dfs([[0, 0, 0], [0, 0, 1], [0, 0, 0]], 0, 2)).toBeFalsy();
35+
});
36+
5837
});

0 commit comments

Comments
 (0)