Skip to content

Commit

Permalink
Rec division TC
Browse files Browse the repository at this point in the history
  • Loading branch information
sadanandpai committed Mar 16, 2024
1 parent 19e2a0e commit 2e55d9f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {
drawHorizontalWall,
drawVerticalWall,
getRandomEvenNumber,
getRandomOddNumber,
} from '@/apps/path-finder/algorithms/maze-generator/recursive-division';
import { CellType } from '@/apps/path-finder/models/interfaces';

describe('Recursive Division', () => {
it('getRandomEvenNumber', () => {
Expand All @@ -25,4 +28,58 @@ describe('Recursive Division', () => {
expect(number % 2).toBe(1);
}
});

it('drawHorizontalWall', async () => {
const n = 10;
const grid = Array.from({ length: n }, () => Array(n).fill(CellType.clear));
const divisionPoint = 5;
const passagePoint = 3;
const start = 0;
const end = 6;

const updateCells = vi.fn();
await drawHorizontalWall(grid, {
updateCells,
divisionPoint,
passagePoint,
start,
end,
});

const calls = updateCells.mock.calls;
calls.slice(0, calls.length - 1).forEach((call) => {
const [_, __, cellType] = call;
expect(cellType).toBe(CellType.wall);
});

const [_, __, cellType] = calls.at(-1);
expect(cellType).toBeUndefined();
});

it('drawVerticalWall', async () => {
const n = 10;
const grid = Array.from({ length: n }, () => Array(n).fill(CellType.clear));
const divisionPoint = 5;
const passagePoint = 3;
const start = 0;
const end = 6;

const updateCells = vi.fn();
await drawVerticalWall(grid, {
updateCells,
divisionPoint,
passagePoint,
start,
end,
});

const calls = updateCells.mock.calls;
calls.slice(0, calls.length - 1).forEach((call) => {
const [_, __, cellType] = call;
expect(cellType).toBe(CellType.wall);
});

const [_, __, cellType] = calls.at(-1);
expect(cellType).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ export function getRandomOddNumber(min: number, max: number) {
return random % 2 === 1 ? random : random + 1;
}

async function drawHorizontalWall(
export async function drawHorizontalWall(
grid: CellType[][],
{ updateCells, divisionPoint, passagePoint, start, end }: DrawWallConfig
) {
for (let pos = start; pos <= end; pos++) {
await updateCells(grid, [{ row: divisionPoint, col: pos }], CellType.wall);
await updateCells(grid, { row: divisionPoint, col: pos }, CellType.wall);
}
await updateCells(grid, [{ row: divisionPoint, col: passagePoint }]);
await updateCells(grid, { row: divisionPoint, col: passagePoint });
}

async function drawVerticalWall(
export async function drawVerticalWall(
grid: CellType[][],
{ updateCells, divisionPoint, passagePoint, start, end }: DrawWallConfig
) {
for (let pos = start; pos <= end; pos++) {
await updateCells(grid, [{ row: pos, col: divisionPoint }], CellType.wall);
await updateCells(grid, { row: pos, col: divisionPoint }, CellType.wall);
}
await updateCells(grid, [{ row: passagePoint, col: divisionPoint }]);
await updateCells(grid, { row: passagePoint, col: divisionPoint });
}

export async function generateRecursiveDivisionMaze({
Expand Down
2 changes: 1 addition & 1 deletion src/apps/path-finder/components/controller/execution.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function Execution() {
<button
className={classes.play}
data-testid="player"
disabled={disabled}
disabled={disabled || !pathFinder}
data-tooltip="Play"
onClick={() => handlePlay()}
>
Expand Down
2 changes: 1 addition & 1 deletion src/apps/path-finder/components/controller/operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function Operations() {
data-testid="generate-maze"
onClick={() => mazeClickHandler()}
data-tooltip="Play"
disabled={disabled || maze === ''}
disabled={disabled || !maze}
>
<Play size={20} />
</button>
Expand Down

0 comments on commit 2e55d9f

Please sign in to comment.