Repository navigation
Expand file tree
/
Copy pathMazeRecursionTest.java
More file actions
166 lines (141 loc) · 6.92 KB
/
Copy pathMazeRecursionTest.java
File metadata and controls
166 lines (141 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.thealgorithms.backtracking;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
/**
* @author onglipwei
* @create 2022-08-03 5:17 AM
*/
public class MazeRecursionTest {
@Test
public void testSmallMazeWithExplicitCoordinates() {
int[][] firstStrategyMaze = {{0, 0}, {0, 0}};
int[][] secondStrategyMaze = {{0, 0}, {0, 0}};
assertArrayEquals(new int[][] {{2, 0}, {2, 2}}, MazeRecursion.solveMazeUsingFirstStrategy(firstStrategyMaze, 0, 0, 1, 1));
assertArrayEquals(new int[][] {{2, 2}, {0, 2}}, MazeRecursion.solveMazeUsingSecondStrategy(secondStrategyMaze, 0, 0, 1, 1));
}
@Test
public void testLegacyMethodsReturnNullWhenFixedGoalIsOutsideMaze() {
assertNull(MazeRecursion.solveMazeUsingFirstStrategy(new int[1][1]));
assertNull(MazeRecursion.solveMazeUsingSecondStrategy(new int[1][1]));
assertNull(MazeRecursion.solveMazeUsingFirstStrategy(new int[2][2]));
assertNull(MazeRecursion.solveMazeUsingSecondStrategy(new int[2][2]));
}
@Test
public void testLegacyMethodsReturnNullWhenFixedGoalRowOrColumnIsOutsideMaze() {
assertNull(MazeRecursion.solveMazeUsingFirstStrategy(new int[7][5]));
assertNull(MazeRecursion.solveMazeUsingSecondStrategy(new int[6][6]));
}
@Test
public void testLegacyMethodsReturnNullWhenNoPathExists() {
int[][] firstStrategyMaze = new int[7][6];
int[][] secondStrategyMaze = new int[7][6];
firstStrategyMaze[1][1] = 1;
secondStrategyMaze[1][1] = 1;
assertNull(MazeRecursion.solveMazeUsingFirstStrategy(firstStrategyMaze));
assertNull(MazeRecursion.solveMazeUsingSecondStrategy(secondStrategyMaze));
}
@Test
public void testLegacyMethodsAcceptAlreadyMarkedGoal() {
int[][] firstStrategyMaze = new int[7][6];
int[][] secondStrategyMaze = new int[7][6];
firstStrategyMaze[6][5] = 2;
secondStrategyMaze[6][5] = 2;
assertSame(firstStrategyMaze, MazeRecursion.solveMazeUsingFirstStrategy(firstStrategyMaze));
assertSame(secondStrategyMaze, MazeRecursion.solveMazeUsingSecondStrategy(secondStrategyMaze));
}
@Test
public void testNoPathExistsWithExplicitCoordinates() {
int[][] maze = {{0, 1}, {1, 0}};
assertNull(MazeRecursion.solveMazeUsingFirstStrategy(maze, 0, 0, 1, 1));
}
@Test
public void testExplicitCoordinatesMustBeInsideMaze() {
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(new int[2][2], 0, 0, 2, 1));
}
@Test
public void testEachStartAndGoalCoordinateMustBeInsideMaze() {
int[][] maze = new int[2][2];
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(maze, -1, 0, 1, 1));
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(maze, 2, 0, 1, 1));
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(maze, 0, -1, 1, 1));
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(maze, 0, 2, 1, 1));
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(maze, 0, 0, -1, 1));
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(maze, 0, 0, 2, 1));
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(maze, 0, 0, 1, -1));
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(maze, 0, 0, 1, 2));
}
@Test
public void testMazeMustNotBeNullOrEmpty() {
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(null));
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(new int[0][0]));
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(new int[][] {null}));
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(new int[][] {new int[0]}));
}
@Test
public void testMazeMustBeRectangular() {
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(new int[][] {{0, 0}, {0}}, 0, 0, 1, 0));
}
@Test
public void testBlockedStartAndGoalHaveNoSolution() {
assertNull(MazeRecursion.solveMazeUsingFirstStrategy(new int[][] {{1, 0}, {0, 0}}, 0, 0, 1, 1));
assertNull(MazeRecursion.solveMazeUsingSecondStrategy(new int[][] {{0, 0}, {0, 1}}, 0, 0, 1, 1));
}
@Test
public void testOpenSingleCellMazeIsAlreadySolved() {
assertArrayEquals(new int[][] {{2}}, MazeRecursion.solveMazeUsingFirstStrategy(new int[][] {{0}}, 0, 0, 0, 0));
assertArrayEquals(new int[][] {{2}}, MazeRecursion.solveMazeUsingSecondStrategy(new int[][] {{0}}, 0, 0, 0, 0));
}
@Test
public void testSolveMazeUsingFirstAndSecondStrategy() {
int[][] map = new int[8][7];
int[][] map2 = new int[8][7];
// We use 1 to indicate walls
// Set the ceiling and floor to 1
for (int i = 0; i < 7; i++) {
map[0][i] = 1;
map[7][i] = 1;
}
// Set the left and right wall to 1
for (int i = 0; i < 8; i++) {
map[i][0] = 1;
map[i][6] = 1;
}
// Set obstacles
map[3][1] = 1;
map[3][2] = 1;
// Clone the original map for the second pathfinding strategy
for (int i = 0; i < map.length; i++) {
System.arraycopy(map[i], 0, map2[i], 0, map[i].length);
}
// Solve the maze using the first strategy
int[][] solvedMap1 = MazeRecursion.solveMazeUsingFirstStrategy(map);
// Solve the maze using the second strategy
int[][] solvedMap2 = MazeRecursion.solveMazeUsingSecondStrategy(map2);
int[][] expectedMap1 = new int[][] {
{1, 1, 1, 1, 1, 1, 1},
{1, 2, 0, 0, 0, 0, 1},
{1, 2, 2, 2, 0, 0, 1},
{1, 1, 1, 2, 0, 0, 1},
{1, 0, 0, 2, 0, 0, 1},
{1, 0, 0, 2, 0, 0, 1},
{1, 0, 0, 2, 2, 2, 1},
{1, 1, 1, 1, 1, 1, 1},
};
int[][] expectedMap2 = new int[][] {
{1, 1, 1, 1, 1, 1, 1},
{1, 2, 2, 2, 2, 2, 1},
{1, 0, 0, 0, 0, 2, 1},
{1, 1, 1, 0, 0, 2, 1},
{1, 0, 0, 0, 0, 2, 1},
{1, 0, 0, 0, 0, 2, 1},
{1, 0, 0, 0, 0, 2, 1},
{1, 1, 1, 1, 1, 1, 1},
};
// Assert the results
assertArrayEquals(expectedMap1, solvedMap1);
assertArrayEquals(expectedMap2, solvedMap2);
}
}