Skip to content

Commit

Permalink
Added tests using node costs for AStarFinder and DijkstraFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobello committed May 21, 2015
1 parent 5ef686d commit 3840d6e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
64 changes: 46 additions & 18 deletions test/PathTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@ var scenarios = require('./PathTestScenarios');
function pathTest(opt) {
var name = opt.name,
finder = opt.finder,
optimal = opt.optimal;
optimal = opt.optimal,
useCost = opt.useCost;


describe(name, function() {
var startX, startY, endX, endY, grid, expectedLength,
width, height, matrix, path, i, scen;
width, height, matrix, costs, path, i, scen;

var test = (function() {
var testId = 0;

return function(startX, startY, endX, endY, grid, expectedLength) {
return function(startX, startY, endX, endY, grid, expectedLength, expectedCostLength) {
it('should solve maze '+ ++testId, function() {
path = finder.findPath(startX, startY, endX, endY, grid);
if (optimal) {
if (useCost && expectedCostLength !== undefined) {
path.length.should.equal(expectedCostLength);
} else {
path.length.should.equal(expectedLength);
}
} else {
path[0].should.eql([startX, startY]);
path[path.length - 1].should.eql([endX, endY]);
Expand All @@ -35,16 +41,17 @@ function pathTest(opt) {
scen = scenarios[i];

matrix = scen.matrix;
costs = useCost ? scen.costs : undefined;
height = matrix.length;
width = matrix[0].length;

grid = new PF.Grid(width, height, matrix);
width = matrix[0].length;
grid = new PF.Grid(width, height, matrix, costs);

test(
scen.startX, scen.startY,
scen.endX, scen.endY,
grid,
scen.expectedLength
scen.expectedLength,
scen.expectedCostLength
);
}
});
Expand All @@ -61,52 +68,73 @@ function pathTests(tests) {
pathTests({
name: 'AStar',
finder: new PF.AStarFinder(),
optimal: true
optimal: true,
useCost: false
}, {
name: 'AStar Cost',
finder: new PF.AStarFinder(),
optimal: true,
useCost: true
}, {
name: 'BreadthFirst',
finder: new PF.BreadthFirstFinder(),
optimal: true
optimal: true,
useCost: false
}, {
name: 'Dijkstra',
finder: new PF.DijkstraFinder(),
optimal: true
optimal: true,
useCost: false
}, {
name: 'Dijkstra Cost',
finder: new PF.DijkstraFinder(),
optimal: true,
useCost: true
}, {
name: 'BiBreadthFirst',
finder: new PF.BiBreadthFirstFinder(),
optimal: true
optimal: true,
useCost: false
}, {
name: 'BiDijkstra',
finder: new PF.BiDijkstraFinder(),
optimal: true
optimal: true,
useCost: false
});

// finders NOT guaranteed to find the shortest path
pathTests({
name: 'BiAStar',
finder: new PF.BiAStarFinder(),
optimal: false
optimal: false,
useCost: false
}, {
name: 'BestFirst',
finder: new PF.BestFirstFinder(),
optimal: false
optimal: false,
useCost: false
}, {
name: 'BiBestFirst',
finder: new PF.BiBestFirstFinder(),
optimal: false
optimal: false,
useCost: false
}, {
name: 'IDAStar',
finder: new PF.IDAStarFinder(),
optimal: false
optimal: false,
useCost: false
}, {
name: 'JPFMoveDiagonallyIfAtMostOneObstacle',
finder: new PF.JumpPointFinder({
diagonalMovement: PF.DiagonalMovement.IfAtMostOneObstacle
}),
optimal: false
optimal: false,
useCost: false
}, {
name: 'JPFNeverMoveDiagonally',
finder: new PF.JumpPointFinder({
diagonalMovement: PF.DiagonalMovement.Never
}),
optimal: false
optimal: false,
useCost: false
});
18 changes: 18 additions & 0 deletions test/PathTestScenarios.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ module.exports = [
[1, 0]],
expectedLength: 3,
},
{
startX: 0,
startY: 0,
endX: 4,
endY: 4,
matrix: [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
costs: [[0, 0, 0, 0, 0],
[9, 9, 9, 9, 0],
[0, 0, 0, 0, 0],
[0, 9, 9, 9, 9],
[0, 0, 0, 0, 0]],
expectedLength: 9,
expectedCostLength: 17,
},
{
startX: 1,
startY: 1,
Expand Down

0 comments on commit 3840d6e

Please sign in to comment.