Skip to content

Commit

Permalink
Add tests for shortesPaths function
Browse files Browse the repository at this point in the history
  • Loading branch information
mstou committed Sep 2, 2018
1 parent 4eba58f commit 3991b9c
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion test/alg/shortest-paths-tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var expect = require("../chai").expect,
Graph = require("../..").Graph;
Graph = require("../..").Graph,
alg = require("../..").alg;

module.exports = tests;

Expand Down Expand Up @@ -75,6 +76,29 @@ function tests(algorithm) {
});
}

// Test shortestPaths() function
describe("alg.shortestPaths", function() {
tests(alg.shortestPaths);

it("uses dijkstra if no weightFn is provided", function() {
var g = new Graph();
g.setPath(["a", "b", "c"]);
g.setEdge("b", "d", -10);

expect(alg.shortestPaths(g, "a")).to.eql(alg.dijkstra(g, "a"));
});

it("uses bellman-ford if the graph contains a negative edge", function() {
var g = new Graph();
g.setEdge("a", "b", 10);
g.setEdge("b", "c", 8);
g.setEdge("a", "d", -3);
g.setEdge("d", "c", 2);

expect(alg.shortestPaths(g, "a", weightFn(g))).to.eql(alg.bellmanFord(g, "a", weightFn(g)));
});
});

function weightFn(g) {
return function(e) {
return g.edge(e);
Expand Down

0 comments on commit 3991b9c

Please sign in to comment.