Skip to content

Commit

Permalink
Added test for circular dependency check in tolopogical sort
Browse files Browse the repository at this point in the history
  • Loading branch information
mesmo committed Jul 12, 2018
1 parent 05e2a9c commit f730a9f
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions test/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,41 @@ const assert = require("assert");
const graph = require('../lib/node/index');

const data = [
{ name: 'A', requires: ['E', 'B'], toString: () => 'A' },
{ name: 'B', requires: ['D', 'C'], toString: () => 'B' },
{ name: 'C', requires: [], toString: () => 'C' },
{ name: 'D', requires: [], toString: () => 'D' },
{ name: 'E', requires: [], toString: () => 'E' }
{ name: 'A', requires: ['E', 'B'] },
{ name: 'B', requires: ['D', 'C'] },
{ name: 'C', requires: [] },
{ name: 'D', requires: [] },
{ name: 'E', requires: [] }
];

function dependencies(i) {
const circle = [
{ name: 'A', requires: ['B'] },
{ name: 'B', requires: ['C'] },
{ name: 'C', requires: ['A'] }
];

function dependencies1(i) {
return data.filter(j => i.requires.indexOf(j.name) !== -1);
}

function dependencies2(i) {
return circle.filter(j => i.requires.indexOf(j.name) !== -1);
}

describe("test/sort.js", function () {
it("topological", function () {
const sorted = graph.Sort.topological(data, dependencies, true);
it("topological/sort", function () {
const sorted1 = graph.Sort.topological(data, dependencies1, true);

assert.equal('C', sorted1[0].name);
assert.equal('D', sorted1[1].name);
assert.equal('B', sorted1[2].name);
assert.equal('E', sorted1[3].name);
assert.equal('A', sorted1[4].name);
});

assert.equal('C', sorted[0].name);
assert.equal('D', sorted[1].name);
assert.equal('B', sorted[2].name);
assert.equal('E', sorted[3].name);
assert.equal('A', sorted[4].name);
it("topological/circular", function () {
assert.throws(() => {
const sorted2 = graph.Sort.topological(circle, dependencies2, true);
}, Error, "Topologicalt sort: circular dependency detected");
});
});

0 comments on commit f730a9f

Please sign in to comment.