Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for the A* algorithm and cleaned up A* directories #100

Merged
merged 2 commits into from
Oct 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions src/_PathFinder_/AStar/AStar.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
19 changes: 13 additions & 6 deletions src/PathFinder/AStart/index.js → src/_PathFinder_/AStar/index.js
Original file line number Diff line number Diff line change
@@ -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;
}