Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60f2983

Browse files
committed
Add basic tests for dijkstra
1 parent 911d563 commit 60f2983

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/graphs/shortest-path/dijkstra.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
current = unvisited.extract();
114114
}
115115
if (distance[dest]) {
116-
return distance[dest].distance || Infinity;
116+
return distance[dest].distance;
117117
}
118118
return Infinity;
119119
};

test/graphs/shortest-path/dijkstra.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,15 @@ describe('dijkstra', function () {
1212
expect(dijkstra(0, 0, [])).toBe(Infinity);
1313
});
1414

15+
it('should work when the src and dest are the same', function () {
16+
expect(dijkstra(0, 0, [[0]])).toBe(0);
17+
});
18+
19+
it('should work when there\'s no path', function () {
20+
expect(dijkstra(0, 1, [[0, Infinity], [Infinity, 0]])).toBe(Infinity);
21+
});
22+
23+
it('should find the shortest path', function () {
24+
expect(dijkstra(0, 2, [[0, 1, 4], [1, 0, 1], [4, 1, 0]])).toBe(2);
25+
});
1526
});

0 commit comments

Comments
 (0)