Skip to content

Commit 35790b5

Browse files
author
Shawn Toubeau
committed
Adds maze reducer tests
1 parent a2bc6bd commit 35790b5

File tree

2 files changed

+162
-10
lines changed

2 files changed

+162
-10
lines changed

src/components/Maze/algorithms/A*.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
getCoord,
99
} from "./";
1010

11-
const heuristic = (a: Coord, b: Coord): number => {
11+
export const heuristic = (a: Coord, b: Coord): number => {
1212
// Manhattan distance formula
1313
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
1414

src/reducers/mazeReducer/mazeReducer.spec.ts

Lines changed: 161 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { mazeReducer } from "./mazeReducer";
2-
import { initialState } from "../../models/maze/initialState";
3-
import { CLEAR_GRID } from "../../actions/menuActions/menuActions";
2+
import { initialState, generateMaze } from "../../models/maze/initialState";
3+
import {
4+
CLEAR_GRID,
5+
UPDATE_GRID_SIZE,
6+
LOAD_MAZE,
7+
} from "../../actions/menuActions/menuActions";
48
import {
59
CHANGE_START,
610
CHANGE_END,
711
MAKE_WALL,
812
MAKE_EMPTY,
13+
PROGRESS_BFS,
14+
PROGRESS_DFS,
15+
PROGRESS_ASTAR,
916
} from "../../actions/mazeActions/mazeActions";
10-
import { Maze } from "../../models/maze";
17+
import { Maze, Coord } from "../../models/maze";
1118
import { SpaceTypes } from "../../components/Space/types";
19+
import { getCoord } from "../../components/Maze/algorithms";
20+
import { heuristic } from "../../components/Maze/algorithms/A*";
1221

1322
describe("Maze Reducer Tests", () => {
1423
it("Clear Grid Expected State", () => {
@@ -108,17 +117,160 @@ describe("Maze Reducer Tests", () => {
108117
expect(updatedState).toEqual(expectedState);
109118
});
110119

111-
// TODO: progressBFS
120+
it("Updates MazeInfo and bfsQueue", () => {
121+
const queue: Coord[] = [
122+
{ x: 0, y: 1 },
123+
{ x: 1, y: 0 },
124+
];
125+
const currCoord: Coord = { x: 0, y: 0 };
126+
const neighbors: Coord[] = [
127+
{ x: 0, y: 1 },
128+
{ x: 1, y: 0 },
129+
];
130+
131+
const action = {
132+
type: PROGRESS_BFS,
133+
payload: {
134+
queue: queue,
135+
coord: currCoord,
136+
neighbors: neighbors,
137+
},
138+
};
139+
140+
let newMaze = initialState.mazeInfo;
141+
// Sets current position to visited
142+
newMaze[currCoord.y][currCoord.x].visited = true;
143+
// Sets the parent of all the neighbors
144+
neighbors.forEach((neighbor) => {
145+
newMaze[neighbor.y][neighbor.x].parent = currCoord;
146+
});
112147

113-
// TODO: progressDFS
148+
const updatedState = mazeReducer(initialState, action);
149+
const expectedState: Maze = {
150+
...initialState,
151+
mazeInfo: newMaze,
152+
bfsQueue: queue,
153+
};
154+
expect(updatedState).toEqual(expectedState);
155+
});
114156

115-
// TODO: progressAstar
157+
it("Updates MazeInfo and dfsStack", () => {
158+
const stack: Coord[] = [
159+
{ x: 0, y: 1 },
160+
{ x: 1, y: 0 },
161+
];
162+
const currCoord: Coord = { x: 0, y: 0 };
163+
const neighbors: Coord[] = [
164+
{ x: 0, y: 1 },
165+
{ x: 1, y: 0 },
166+
];
116167

117-
// TODO: stopVis
168+
const action = {
169+
type: PROGRESS_DFS,
170+
payload: {
171+
stack: stack,
172+
coord: currCoord,
173+
neighbors: neighbors,
174+
},
175+
};
176+
177+
let newMaze = initialState.mazeInfo;
178+
// Sets current position to visited
179+
newMaze[currCoord.y][currCoord.x].visited = true;
180+
// Sets the parent of all the neighbors
181+
neighbors.forEach((neighbor) => {
182+
newMaze[neighbor.y][neighbor.x].parent = currCoord;
183+
});
184+
185+
const updatedState = mazeReducer(initialState, action);
186+
const expectedState: Maze = {
187+
...initialState,
188+
mazeInfo: newMaze,
189+
dfsStack: stack,
190+
};
191+
expect(updatedState).toEqual(expectedState);
192+
});
193+
194+
it("Updates MazeInfo and openSet and closedSet", () => {
195+
const currentSpace = { x: 0, y: 0 };
196+
const openSet: Coord[] = [];
197+
const closedSet: Coord[] = [currentSpace];
198+
let newMazeInfo = initialState.mazeInfo;
199+
const end = getCoord(initialState.mazeInfo, SpaceTypes.end);
200+
201+
// Update the G value of all the neighbors of the current space
202+
const neighbors = [
203+
{ x: 0, y: 1 },
204+
{ x: 1, y: 0 },
205+
];
206+
neighbors.forEach((neighbor) => {
207+
const tempG =
208+
initialState.mazeInfo[currentSpace.y][currentSpace.x].g +
209+
heuristic(neighbor, currentSpace);
210+
newMazeInfo[neighbor.y][neighbor.x].g = tempG;
211+
});
212+
213+
const action = {
214+
type: PROGRESS_ASTAR,
215+
payload: {
216+
openSet: openSet,
217+
closedSet: closedSet,
218+
newMazeInfo: newMazeInfo,
219+
end: end,
220+
},
221+
};
222+
223+
const updatedState = mazeReducer(initialState, action);
224+
const expectedState: Maze = {
225+
...initialState,
226+
mazeInfo: newMazeInfo,
227+
astarOpenSet: openSet,
228+
astarClosedSet: closedSet,
229+
};
230+
expect(updatedState).toEqual(expectedState);
231+
});
118232

119233
// TODO: randomizeWalls
120234

121-
// TODO: updateGridSize
235+
it("Changes the size of the gird", () => {
236+
const newHeight = 30;
237+
const newWidth = 30;
238+
239+
const action = {
240+
type: UPDATE_GRID_SIZE,
241+
payload: {
242+
cols: newWidth,
243+
rows: newHeight,
244+
},
245+
};
246+
247+
let newMaze = generateMaze(newWidth, newHeight, true);
248+
249+
const updatedState = mazeReducer(initialState, action);
250+
const expectedState: Maze = {
251+
...initialState,
252+
mazeInfo: newMaze,
253+
};
254+
255+
expect(updatedState).toEqual(expectedState);
256+
});
257+
258+
it("Loads a maze", () => {
259+
const loadedMaze = generateMaze(30, 30, true);
260+
261+
const action = {
262+
type: LOAD_MAZE,
263+
payload: {
264+
mazeInfo: loadedMaze,
265+
},
266+
};
122267

123-
// TODO: loadMaze
268+
const updatedState = mazeReducer(initialState, action);
269+
const expectedState: Maze = {
270+
...initialState,
271+
mazeInfo: loadedMaze,
272+
};
273+
274+
expect(updatedState).toEqual(expectedState);
275+
});
124276
});

0 commit comments

Comments
 (0)