-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopologicalSortSpec.js
executable file
·94 lines (76 loc) · 2.84 KB
/
topologicalSortSpec.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
describe('Testing topologicalSort service', function() {
var topologicalSort;
beforeEach(module('plunker'));
beforeEach(inject(function(_topologicalSort_) {
topologicalSort = _topologicalSort_;
}));
it('should exist', function () {
expect(!!topologicalSort).toBe(true);
});
describe("result of a possible topological sort", function () {
var elements, result;
beforeEach(function () {
var a = {name:"A thing", require:{}};
var b = {name:"B", require:{}};
var c = {name:"C", require:{}};
var d = {name:"D", require:{}};
var e = {name:"E", require:{}};
var f = {name:"F", require:{}};
b.require[a.name] = true;
c.require[b.name] = true;
d.require[b.name] = true;
d.require[f.name] = true;
e.require[d.name] = true;
elements = [a, b, c, d, e, f];
result = topologicalSort(elements);
});
it("should have a vertices hash table with the 6 vertices", function () {
angular.forEach(["A thing", "B", "C", "F", "D", "E"], function(v) {
expect(result.vertices[v]).toBeDefined();
});
});
it("should have the correct adjacent list for each vertice", function () {
expect(result.vertices["A thing"].adj).toContain("B");
expect(result.vertices["B"].adj).toContain("C");
expect(result.vertices["B"].adj).toContain("D");
expect(result.vertices["D"].adj).toContain("E");
expect(result.vertices["F"].adj).toContain("D");
expect(result.vertices["C"].adj.length).toBe(0);
expect(result.vertices["E"].adj.length).toBe(0);
});
it("should contains the 6 elements in order array", function () {
angular.forEach(["A thing", "B", "C", "F", "D", "E"], function(v) {
expect(result.order).toContain(v);
});
});
it("should have a correct order of elements", function () {
expect(result.order.indexOf("A thing")).toBeLessThan(result.order.indexOf("B"));
expect(result.order.indexOf("B")).toBeLessThan(result.order.indexOf("C"));
expect(result.order.indexOf("B")).toBeLessThan(result.order.indexOf("D"));
expect(result.order.indexOf("F")).toBeLessThan(result.order.indexOf("D"));
expect(result.order.indexOf("D")).toBeLessThan(result.order.indexOf("E"));
});
});
describe("result of an impossible topological sort", function () {
var elements, result;
beforeEach(function () {
var a = {name:"A thing", require:{}};
var b = {name:"B", require:{}};
var c = {name:"C", require:{}};
var d = {name:"D", require:{}};
var e = {name:"E", require:{}};
var f = {name:"F", require:{}};
b.require[a.name] = true;
c.require[b.name] = true;
d.require[b.name] = true;
d.require[f.name] = true;
e.require[d.name] = true;
a.require[d.name] = true; // cycle created
elements = [a, b, c, d, e, f];
result = topologicalSort(elements);
});
it("should have an error", function () {
expect(result.error.length).toBeGreaterThan(0);
});
});
});