summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/tests/WorldTest.java
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-21 10:36:18 +0100
committerPacien TRAN-GIRARD2015-11-21 10:36:18 +0100
commit655ac88f4e73b2df532a451aedf5a561ea1b0d2c (patch)
treeef6f914a465575f313e2b280bf0639d87a4cbd58 /src/ch/epfl/maze/tests/WorldTest.java
parent56279eb59ccdea48b18daa027a5095d861b4e2f4 (diff)
downloadmaze-solver-655ac88f4e73b2df532a451aedf5a561ea1b0d2c.tar.gz
Import project structure
Diffstat (limited to 'src/ch/epfl/maze/tests/WorldTest.java')
-rw-r--r--src/ch/epfl/maze/tests/WorldTest.java330
1 files changed, 330 insertions, 0 deletions
diff --git a/src/ch/epfl/maze/tests/WorldTest.java b/src/ch/epfl/maze/tests/WorldTest.java
new file mode 100644
index 0000000..6d551b1
--- /dev/null
+++ b/src/ch/epfl/maze/tests/WorldTest.java
@@ -0,0 +1,330 @@
1package ch.epfl.maze.tests;
2
3import static org.junit.Assert.assertArrayEquals;
4
5import java.util.Arrays;
6import java.util.List;
7
8import junit.framework.TestCase;
9
10import org.junit.Test;
11
12import ch.epfl.maze.physical.Animal;
13import ch.epfl.maze.physical.World;
14import ch.epfl.maze.util.Direction;
15import ch.epfl.maze.util.Vector2D;
16
17/**
18 * Test case for {@code World} implementation.
19 *
20 */
21
22public class WorldTest extends TestCase {
23
24 /* sample labyrinth */
25 private static final int[][] LABYRINTH_SAMPLE = {
26 {1, 1, 1, 1, 1, 3, 1},
27 {1, 0, 0, 0, 0, 0, 1},
28 {1, 2, 1, 1, 1, 1, 1}
29 };
30
31 /* constants labyrinth for testing getChoices(Vector2D) */
32 private static final int[][] LABYRINTH_STUCK = {
33 {1, 1, 1},
34 {1, 0, 1},
35 {1, 1, 1}
36 };
37 private static final int[][] LABYRINTH_CORRIDOR = {
38 {1, 1, 1, 1, 1, 1},
39 {1, 0, 1, 0, 0, 1},
40 {1, 0, 1, 1, 1, 1},
41 {1, 1, 1, 1, 1, 1}
42 };
43 private static final int[][] LABYRINTH_DOGHNUT = {
44 {1, 1, 1, 1, 1},
45 {1, 0, 0, 0, 1},
46 {1, 0, 1, 0, 1},
47 {1, 0, 0, 0, 1},
48 {1, 1, 1, 1, 1}
49 };
50 private static final int[][] LABYRINTH_SQUARE = {
51 {1, 1, 1, 1, 1},
52 {1, 0, 0, 0, 1},
53 {1, 0, 0, 0, 1},
54 {1, 0, 0, 0, 1},
55 {1, 1, 1, 1, 1}
56 };
57
58 /**
59 * Test case for {@code getTile(int x, int y)}.
60 */
61
62 @Test
63 public void testGetTile() {
64 World world = new ConcreteWorld(LABYRINTH_SAMPLE);
65
66 // checks if positions are reversed
67 assertTrue("You reversed the coordinates in your method !",
68 world.getTile(5, 1) != World.NOTHING);
69
70 // checks every position on the sample labyrinth
71 assertEquals(World.WALL, world.getTile(0, 0));
72 assertEquals(World.FREE, world.getTile(1, 1));
73 assertEquals(World.START, world.getTile(1, 2));
74 assertEquals(World.EXIT, world.getTile(5, 0));
75 assertEquals(World.NOTHING, world.getTile(0, 3));
76 }
77
78 /**
79 * Test case for {@code isFree(int x, int y)}.
80 */
81
82 @Test
83 public void testIsFree() {
84 World world = new ConcreteWorld(LABYRINTH_SAMPLE);
85
86 // checks FREE, START and EXIT positions
87 assertTrue("FREE tile should be free", world.isFree(3, 1));
88 assertTrue("START tile should be free", world.isFree(1, 2));
89 assertTrue("EXIT tile should be free", world.isFree(5, 0));
90
91 // checks WALL and NOTHING positions
92 assertFalse("WALL tile should NOT be free", world.isFree(0, 0));
93 assertFalse("NOTHING tile should NOT be free", world.isFree(0, 3));
94 }
95
96 /**
97 * Test case for {@code getWidth()}.
98 */
99
100 @Test
101 public void testGetWidth() {
102 World world = new ConcreteWorld(LABYRINTH_SAMPLE);
103
104 // checks width
105 assertEquals(7, world.getWidth());
106 }
107
108 /**
109 * Test case for {@code getHeight()}.
110 */
111
112 @Test
113 public void testGetHeight() {
114 World world = new ConcreteWorld(LABYRINTH_SAMPLE);
115
116 // checks height
117 assertEquals(3, world.getHeight());
118 }
119
120 /**
121 * Test case for {@code getStart()}.
122 */
123
124 @Test
125 public void testGetStart() {
126 World world = new ConcreteWorld(LABYRINTH_SAMPLE);
127
128 // checks starting position
129 assertEquals(new Vector2D(1, 2), world.getStart());
130 }
131
132 /**
133 * Test case for {@code getExit()}.
134 */
135
136 @Test
137 public void testGetExit() {
138 World world = new ConcreteWorld(LABYRINTH_SAMPLE);
139
140 // checks exiting position
141 assertEquals(new Vector2D(5, 0), world.getExit());
142 }
143
144 // ==========================================================
145
146 /**
147 * Test case for {@code getChoices(Vector2D position)}
148 * when there are 0 choices.
149 */
150
151 @Test
152 public void testZeroChoice() {
153 World world = new ConcreteWorld(LABYRINTH_STUCK);
154 Direction[] choices = world.getChoices(new Vector2D(1, 1));
155
156 // checks that the only choice is not to move
157 checkChoices(choices, new Direction[] { Direction.NONE });
158 }
159
160 /**
161 * Test case for {@code getChoices(Vector2D position)}
162 * when there is 1 choice.
163 */
164
165 @Test
166 public void testOneChoice() {
167 World world = new ConcreteWorld(LABYRINTH_CORRIDOR);
168 Direction[] choices;
169
170 // checks when the only choice is to move down
171 choices = world.getChoices(new Vector2D(1, 1));
172 checkChoices(choices, new Direction[] { Direction.DOWN });
173
174 // checks when the only choice is to move up
175 choices = world.getChoices(new Vector2D(1, 2));
176 checkChoices(choices, new Direction[] { Direction.UP });
177
178 // checks when the only choice is to move right
179 choices = world.getChoices(new Vector2D(3, 1));
180 checkChoices(choices, new Direction[] { Direction.RIGHT });
181
182 // checks when the only choice is to move left
183 choices = world.getChoices(new Vector2D(4, 1));
184 checkChoices(choices, new Direction[] { Direction.LEFT });
185 }
186
187 /**
188 * Test case for {@code getChoices(Vector2D position)}
189 * when there are 2 choices.
190 */
191
192 @Test
193 public void testTwoChoices() {
194 World world = new ConcreteWorld(LABYRINTH_DOGHNUT);
195 Direction[] choices;
196
197 // checks when the available choices are to move down and right
198 choices = world.getChoices(new Vector2D(1, 1));
199 checkChoices(choices,
200 new Direction[] { Direction.DOWN, Direction.RIGHT });
201
202 // checks when the available choices are to move down and up
203 choices = world.getChoices(new Vector2D(1, 2));
204 checkChoices(choices,
205 new Direction[] { Direction.DOWN, Direction.UP });
206
207 // checks when the available choices are to move up and right
208 choices = world.getChoices(new Vector2D(1, 3));
209 checkChoices(choices,
210 new Direction[] { Direction.UP, Direction.RIGHT });
211
212 // checks when the available choices are to move right and left
213 choices = world.getChoices(new Vector2D(2, 1));
214 checkChoices(choices,
215 new Direction[] { Direction.RIGHT, Direction.LEFT });
216
217 // checks when the available choices are to move down and left
218 choices = world.getChoices(new Vector2D(3, 1));
219 checkChoices(choices,
220 new Direction[] { Direction.DOWN, Direction.LEFT });
221
222 // checks when the available choices are to move up and left
223 choices = world.getChoices(new Vector2D(3, 3));