From 5f10131d374baefff862c036d286d21cd8170f06 Mon Sep 17 00:00:00 2001 From: ngittlen Date: Thu, 17 Oct 2019 22:16:27 -0400 Subject: [PATCH 1/2] Added tests for the AStar algorithm, alligned all names with the naming convention and added errors and return value to the AStar algorithm --- src/_PathFinder_/AStar/AStar.test.js | 122 ++++++++++++++++++ .../AStart => _PathFinder_/AStar}/index.js | 19 ++- 2 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 src/_PathFinder_/AStar/AStar.test.js rename src/{PathFinder/AStart => _PathFinder_/AStar}/index.js (94%) diff --git a/src/_PathFinder_/AStar/AStar.test.js b/src/_PathFinder_/AStar/AStar.test.js new file mode 100644 index 00000000..836479c2 --- /dev/null +++ b/src/_PathFinder_/AStar/AStar.test.js @@ -0,0 +1,122 @@ +const { AStar } = require('.'); + +describe('A*', () => { + describe('Completes grid successfully', () => { + it('A*', () => { + const inputGrid = [ + [1, 1, 1, 0, 0, 0], + [1, 0, 1, 1, 1, 1], + [1, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1], + ]; + const ROW = inputGrid.length; + const COL = inputGrid[0].length; + const start = { + i: 0, + j: 0, + }; + const end = { + i: 3, + j: 5, + }; + const completedPath = [[3, 5], [3, 4], [3, 3], [3, 2], [3, 1], [2, 0], [1, 0], [0, 0]]; + expect(AStar(start, end, ROW, COL, inputGrid)).toEqual(completedPath); + }); + }); + + describe('Completes large grid successfully', () => { + it('A*', () => { + const inputGrid = [ + [1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1], + [1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1], + [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1], + [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], + [1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1], + [0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1], + [0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0], + [0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], + ]; + const ROW = inputGrid.length; + const COL = inputGrid[0].length; + const start = { + i: 0, + j: 0, + }; + const end = { + i: 8, + j: 11, + }; + const completedPath = [[8, 11], [8, 10], [7, 9], [6, 8], [5, 9], [5, 10], + [4, 11], [3, 11], [2, 11], [1, 11], [0, 10], [1, 9], [0, 8], [1, 7], + [1, 6], [2, 5], [2, 4], [2, 3], [2, 2], [2, 1], [1, 0], [0, 0]]; + expect(AStar(start, end, ROW, COL, inputGrid)).toEqual(completedPath); + }); + }); + + describe('Cannot complete grid successfully', () => { + it('A*', () => { + const inputGrid = [ + [1, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0], + [1, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1], + ]; + const ROW = inputGrid.length; + const COL = inputGrid[0].length; + const start = { + i: 0, + j: 0, + }; + const end = { + i: 3, + j: 5, + }; + expect(() => { AStar(start, end, ROW, COL, inputGrid); }).toThrowError('Error: Endpoint cannot be reached'); + }); + }); + + describe('Endpoint out of grid bounds', () => { + it('A*', () => { + const inputGrid = [ + [1, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0], + [1, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1], + ]; + const ROW = inputGrid.length; + const COL = inputGrid[0].length; + const start = { + i: 0, + j: 0, + }; + const end = { + i: 5, + j: 5, + }; + expect(() => { AStar(start, end, ROW, COL, inputGrid); }).toThrowError('Error: Endpoint outside grid bounds'); + }); + }); + + describe('Endpoint value is zero', () => { + it('A*', () => { + const inputGrid = [ + [1, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0], + [1, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1], + ]; + const ROW = inputGrid.length; + const COL = inputGrid[0].length; + const start = { + i: 0, + j: 0, + }; + const end = { + i: 1, + j: 3, + }; + expect(() => { AStar(start, end, ROW, COL, inputGrid); }).toThrowError('Error: Endpoint is unreachable'); + }); + }); +}); diff --git a/src/PathFinder/AStart/index.js b/src/_PathFinder_/AStar/index.js similarity index 94% rename from src/PathFinder/AStart/index.js rename to src/_PathFinder_/AStar/index.js index 9bf72bb0..24c2a073 100644 --- a/src/PathFinder/AStart/index.js +++ b/src/_PathFinder_/AStar/index.js @@ -9,6 +9,15 @@ function AStar(s, e, row, col, inputGrid) { const Col = col; const start = s; const end = e; + const path = []; + + if (end.i > inputGrid.length || end.j > inputGrid[0].length) { + throw new Error('Error: Endpoint outside grid bounds'); + } + + if (inputGrid[end.i][end.j] === 0) { + throw new Error('Error: Endpoint is unreachable'); + } function cell() { this.cellValue = null; @@ -53,7 +62,6 @@ function AStar(s, e, row, col, inputGrid) { let i = endRow; let j = endCol; - const path = []; while (!(i === startRow && j === startCol)) { path.push([i, j]); @@ -64,10 +72,6 @@ function AStar(s, e, row, col, inputGrid) { j = nextJ; } path.push([i, j]); - - for (let i = 0; i < path.length; i += 1) { - console.log(path[i]); - } }; const neighbourExplorer = (i, j, parentI, parentJ, openList, openListMap, @@ -173,7 +177,10 @@ function AStar(s, e, row, col, inputGrid) { } return true; }; - search(); + if (!search()) { + throw new Error('Error: Endpoint cannot be reached'); + } + return path; } From a2273b7a55ec29110a1a0f83c1e638eb81774a93 Mon Sep 17 00:00:00 2001 From: ngittlen Date: Thu, 17 Oct 2019 22:39:43 -0400 Subject: [PATCH 2/2] Fixed egde case with checking grid bounds --- src/_PathFinder_/AStar/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_PathFinder_/AStar/index.js b/src/_PathFinder_/AStar/index.js index 24c2a073..7e0ad35f 100644 --- a/src/_PathFinder_/AStar/index.js +++ b/src/_PathFinder_/AStar/index.js @@ -11,7 +11,7 @@ function AStar(s, e, row, col, inputGrid) { const end = e; const path = []; - if (end.i > inputGrid.length || end.j > inputGrid[0].length) { + if (end.i >= inputGrid.length || end.j >= inputGrid[0].length) { throw new Error('Error: Endpoint outside grid bounds'); }