Skip to content

Commit 72f5ba5

Browse files
authored
Merge pull request #100 from ngittlen/master
Added tests for the A* algorithm and cleaned up A* directories
2 parents 77e1f25 + a2273b7 commit 72f5ba5

File tree

2 files changed

+135
-6
lines changed

2 files changed

+135
-6
lines changed

src/_PathFinder_/AStar/AStar.test.js

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
const { AStar } = require('.');
2+
3+
describe('A*', () => {
4+
describe('Completes grid successfully', () => {
5+
it('A*', () => {
6+
const inputGrid = [
7+
[1, 1, 1, 0, 0, 0],
8+
[1, 0, 1, 1, 1, 1],
9+
[1, 0, 0, 0, 0, 0],
10+
[1, 1, 1, 1, 1, 1],
11+
];
12+
const ROW = inputGrid.length;
13+
const COL = inputGrid[0].length;
14+
const start = {
15+
i: 0,
16+
j: 0,
17+
};
18+
const end = {
19+
i: 3,
20+
j: 5,
21+
};
22+
const completedPath = [[3, 5], [3, 4], [3, 3], [3, 2], [3, 1], [2, 0], [1, 0], [0, 0]];
23+
expect(AStar(start, end, ROW, COL, inputGrid)).toEqual(completedPath);
24+
});
25+
});
26+
27+
describe('Completes large grid successfully', () => {
28+
it('A*', () => {
29+
const inputGrid = [
30+
[1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1],
31+
[1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1],
32+
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1],
33+
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
34+
[1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1],
35+
[0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1],
36+
[0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0],
37+
[0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0],
38+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
39+
];
40+
const ROW = inputGrid.length;
41+
const COL = inputGrid[0].length;
42+
const start = {
43+
i: 0,
44+
j: 0,
45+
};
46+
const end = {
47+
i: 8,
48+
j: 11,
49+
};
50+
const completedPath = [[8, 11], [8, 10], [7, 9], [6, 8], [5, 9], [5, 10],
51+
[4, 11], [3, 11], [2, 11], [1, 11], [0, 10], [1, 9], [0, 8], [1, 7],
52+
[1, 6], [2, 5], [2, 4], [2, 3], [2, 2], [2, 1], [1, 0], [0, 0]];
53+
expect(AStar(start, end, ROW, COL, inputGrid)).toEqual(completedPath);
54+
});
55+
});
56+
57+
describe('Cannot complete grid successfully', () => {
58+
it('A*', () => {
59+
const inputGrid = [
60+
[1, 1, 1, 0, 0, 0],
61+
[0, 0, 0, 0, 0, 0],
62+
[1, 0, 0, 0, 0, 0],
63+
[1, 1, 1, 1, 1, 1],
64+
];
65+
const ROW = inputGrid.length;
66+
const COL = inputGrid[0].length;
67+
const start = {
68+
i: 0,
69+
j: 0,
70+
};
71+
const end = {
72+
i: 3,
73+
j: 5,
74+
};
75+
expect(() => { AStar(start, end, ROW, COL, inputGrid); }).toThrowError('Error: Endpoint cannot be reached');
76+
});
77+
});
78+
79+
describe('Endpoint out of grid bounds', () => {
80+
it('A*', () => {
81+
const inputGrid = [
82+
[1, 1, 1, 0, 0, 0],
83+
[0, 0, 0, 0, 0, 0],
84+
[1, 0, 0, 0, 0, 0],
85+
[1, 1, 1, 1, 1, 1],
86+
];
87+
const ROW = inputGrid.length;
88+
const COL = inputGrid[0].length;
89+
const start = {
90+
i: 0,
91+
j: 0,
92+
};
93+
const end = {
94+
i: 5,
95+
j: 5,
96+
};
97+
expect(() => { AStar(start, end, ROW, COL, inputGrid); }).toThrowError('Error: Endpoint outside grid bounds');
98+
});
99+
});
100+
101+
describe('Endpoint value is zero', () => {
102+
it('A*', () => {
103+
const inputGrid = [
104+
[1, 1, 1, 0, 0, 0],
105+
[0, 0, 0, 0, 0, 0],
106+
[1, 0, 0, 0, 0, 0],
107+
[1, 1, 1, 1, 1, 1],
108+
];
109+
const ROW = inputGrid.length;
110+
const COL = inputGrid[0].length;
111+
const start = {
112+
i: 0,
113+
j: 0,
114+
};
115+
const end = {
116+
i: 1,
117+
j: 3,
118+
};
119+
expect(() => { AStar(start, end, ROW, COL, inputGrid); }).toThrowError('Error: Endpoint is unreachable');
120+
});
121+
});
122+
});

src/PathFinder/AStart/index.js src/_PathFinder_/AStar/index.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ function AStar(s, e, row, col, inputGrid) {
99
const Col = col;
1010
const start = s;
1111
const end = e;
12+
const path = [];
13+
14+
if (end.i >= inputGrid.length || end.j >= inputGrid[0].length) {
15+
throw new Error('Error: Endpoint outside grid bounds');
16+
}
17+
18+
if (inputGrid[end.i][end.j] === 0) {
19+
throw new Error('Error: Endpoint is unreachable');
20+
}
1221

1322
function cell() {
1423
this.cellValue = null;
@@ -53,7 +62,6 @@ function AStar(s, e, row, col, inputGrid) {
5362

5463
let i = endRow;
5564
let j = endCol;
56-
const path = [];
5765

5866
while (!(i === startRow && j === startCol)) {
5967
path.push([i, j]);
@@ -64,10 +72,6 @@ function AStar(s, e, row, col, inputGrid) {
6472
j = nextJ;
6573
}
6674
path.push([i, j]);
67-
68-
for (let i = 0; i < path.length; i += 1) {
69-
console.log(path[i]);
70-
}
7175
};
7276

7377
const neighbourExplorer = (i, j, parentI, parentJ, openList, openListMap,
@@ -173,7 +177,10 @@ function AStar(s, e, row, col, inputGrid) {
173177
}
174178
return true;
175179
};
176-
search();
180+
if (!search()) {
181+
throw new Error('Error: Endpoint cannot be reached');
182+
}
183+
return path;
177184
}
178185

179186

0 commit comments

Comments
 (0)