Skip to content

Commit 9280db2

Browse files
committed
Add tests for topological sort
1 parent 60f2983 commit 9280db2

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var ts = require('../../../src/graphs/others/topological-sort').topologicalSort;
2+
3+
describe('Topological sort', function () {
4+
'use strict';
5+
it('should be defined', function () {
6+
expect(typeof ts).toBe('function');
7+
});
8+
9+
it('should work with empty graphs', function () {
10+
expect(ts({})).toEqual([]);
11+
});
12+
13+
it('should give the proper topological order', function () {
14+
expect(ts({ v1: [] })).toEqual(['v1']);
15+
var graph = {
16+
v1: ['v2'],
17+
v2: ['v3'],
18+
v3: []
19+
};
20+
expect(ts(graph)).toEqual(['v1', 'v2', 'v3']);
21+
graph = {
22+
v1: ['v2', 'v5'],
23+
v2: [],
24+
v3: ['v1', 'v2', 'v4', 'v5'],
25+
v4: [],
26+
v5: []
27+
};
28+
expect(ts(graph)).toEqual(['v3', 'v4', 'v1', 'v5', 'v2']);
29+
});
30+
31+
it('should throw an error on cycle', function () {
32+
function runTs() {
33+
ts({
34+
v1: ['v2'],
35+
v2: ['v1']
36+
});
37+
}
38+
expect(runTs).toThrow();
39+
});
40+
});

0 commit comments

Comments
 (0)