Skip to content

Commit

Permalink
multi solution test
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Jun 7, 2024
1 parent 1c86422 commit e0f9879
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions challenges/maze-solver/description.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Your task is to write a function `solve_maze` that takes a **2D vector of charac
- You can only move **up, down, left, or right**.
- Implement the function using appropriate control flow constructs **(loops, conditionals, and/or recursion)**.
- Ensure your solution **handles edge cases**, such as no available path or mazes with various sizes.
- If the maze is a dead-end, return an empty vector.
- If the maze has multiple solutions, return the shortest path.

## Example

Expand Down
93 changes: 93 additions & 0 deletions challenges/maze-solver/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,99 @@ mod tests {
);
}

#[test]
fn test_solve_maze_2() {
let maze = vec![
vec!['S', '.', '.', '#', '#'],
vec!['#', '#', '.', '#', '.'],
vec!['#', '.', '.', '.', '.'],
vec!['#', '#', '#', '#', '.'],
vec!['#', '#', '#', 'E', '.'],
];
let start = (0, 0);
let end = (4, 3);

let path = solve_maze(maze, start, end);
assert_eq!(
path,
vec![
(0, 0),
(0, 1),
(0, 2),
(1, 2),
(2, 2),
(2, 3),
(2, 4),
(3, 4),
(4, 4),
(4, 3)
]
);
}

#[test]
fn test_solve_maze_3() {
let maze = vec![
vec!['S', '.', '#', '#', '#'],
vec!['#', '.', '.', '.', '.'],
vec!['#', '#', '#', '#', '.'],
vec!['#', 'E', '.', '.', '.'],
vec!['#', '#', '#', '#', '#'],
];
let start = (0, 0);
let end = (3, 1);

let path = solve_maze(maze, start, end);
assert_eq!(
path,
vec![
(0, 0),
(0, 1),
(1, 1),
(1, 2),
(1, 3),
(1, 4),
(2, 4),
(3, 4),
(3, 3),
(3, 2),
(3, 1)
]
);
}

#[test]
fn test_solve_maze_4() {
let maze = vec![
vec!['S', '.', '.', '#', 'E'],
vec!['#', '#', '.', '#', '#'],
vec!['#', '.', '.', '.', '#'],
vec!['#', '#', '#', '.', '#'],
vec!['#', '#', '#', '#', '#'],
];
let start = (0, 0);
let end = (0, 4);

let path = solve_maze(maze, start, end);
assert_eq!(path, vec![]);
}

#[test]
fn test_multiple_solutions() {
let maze = vec![
vec!['S', '.', '.', '.', '.'],
vec!['.', '#', '#', '#', '.'],
vec!['.', '#', '#', '#', '.'],
vec!['.', '#', '#', '#', '.'],
vec!['.', 'E', '.', '.', '.'],
];
let start = (0, 0);
let end = (4, 1);

let path = solve_maze(maze, start, end);
assert_eq!(path, vec![(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (4, 1)]);
}

#[test]
fn test_no_path() {
let maze = vec![
Expand Down

0 comments on commit e0f9879

Please sign in to comment.