From 3840d6e60b88a71c38ee5c356542b4a5883f3444 Mon Sep 17 00:00:00 2001 From: Paul Robello Date: Thu, 21 May 2015 09:50:40 -0700 Subject: [PATCH] Added tests using node costs for AStarFinder and DijkstraFinder --- test/PathTest.js | 64 ++++++++++++++++++++++++++++----------- test/PathTestScenarios.js | 18 +++++++++++ 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/test/PathTest.js b/test/PathTest.js index 3cc4de68..47a8171f 100644 --- a/test/PathTest.js +++ b/test/PathTest.js @@ -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]); @@ -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 ); } }); @@ -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 }); diff --git a/test/PathTestScenarios.js b/test/PathTestScenarios.js index 63476f61..71b5b1cf 100644 --- a/test/PathTestScenarios.js +++ b/test/PathTestScenarios.js @@ -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,