summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/tests/WorldTest.java
blob: dccd1abe88e7a2ef1d4f4fddf588272cfa8374c1 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package ch.epfl.maze.tests;

import ch.epfl.maze.physical.Animal;
import ch.epfl.maze.physical.World;
import ch.epfl.maze.util.Direction;
import ch.epfl.maze.util.Vector2D;
import junit.framework.TestCase;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertArrayEquals;

/**
 * Test case for {@code World} implementation.
 */

public class WorldTest extends TestCase {

    /* sample labyrinth */
    private static final int[][] LABYRINTH_SAMPLE = {
            {1, 1, 1, 1, 1, 3, 1},
            {1, 0, 0, 0, 0, 0, 1},
            {1, 2, 1, 1, 1, 1, 1}
    };

    /* constants labyrinth for testing getChoices(Vector2D) */
    private static final int[][] LABYRINTH_STUCK = {
            {1, 1, 1},
            {1, 0, 1},
            {1, 1, 1}
    };
    private static final int[][] LABYRINTH_CORRIDOR = {
            {1, 1, 1, 1, 1, 1},
            {1, 0, 1, 0, 0, 1},
            {1, 0, 1, 1, 1, 1},
            {1, 1, 1, 1, 1, 1}
    };
    private static final int[][] LABYRINTH_DOGHNUT = {
            {1, 1, 1, 1, 1},
            {1, 0, 0, 0, 1},
            {1, 0, 1, 0, 1},
            {1, 0, 0, 0, 1},
            {1, 1, 1, 1, 1}
    };
    private static final int[][] LABYRINTH_SQUARE = {
            {1, 1, 1, 1, 1},
            {1, 0, 0, 0, 1},
            {1, 0, 0, 0, 1},
            {1, 0, 0, 0, 1},
            {1, 1, 1, 1, 1}
    };

    /**
     * Test case for {@code getTile(int x, int y)}.
     */

    @Test
    public void testGetTile() {
        World world = new ConcreteWorld(LABYRINTH_SAMPLE);

        // checks if positions are reversed
        assertTrue("You reversed the coordinates in your method !",
                world.getTile(5, 1) != World.NOTHING);

        // checks every position on the sample labyrinth
        assertEquals(World.WALL, world.getTile(0, 0));
        assertEquals(World.FREE, world.getTile(1, 1));
        assertEquals(World.START, world.getTile(1, 2));
        assertEquals(World.EXIT, world.getTile(5, 0));
        assertEquals(World.NOTHING, world.getTile(0, 3));
    }

    /**
     * Test case for {@code isFree(int x, int y)}.
     */

    @Test
    public void testIsFree() {
        World world = new ConcreteWorld(LABYRINTH_SAMPLE);

        // checks FREE, START and EXIT positions
        assertTrue("FREE tile should be free", world.isFree(3, 1));
        assertTrue("START tile should be free", world.isFree(1, 2));
        assertTrue("EXIT tile should be free", world.isFree(5, 0));

        // checks WALL and NOTHING positions
        assertFalse("WALL tile should NOT be free", world.isFree(0, 0));
        assertFalse("NOTHING tile should NOT be free", world.isFree(0, 3));
    }

    /**
     * Test case for {@code getWidth()}.
     */

    @Test
    public void testGetWidth() {
        World world = new ConcreteWorld(LABYRINTH_SAMPLE);

        // checks width
        assertEquals(7, world.getWidth());
    }

    /**
     * Test case for {@code getHeight()}.
     */

    @Test
    public void testGetHeight() {
        World world = new ConcreteWorld(LABYRINTH_SAMPLE);

        // checks height
        assertEquals(3, world.getHeight());
    }

    /**
     * Test case for {@code getStart()}.
     */

    @Test
    public void testGetStart() {
        World world = new ConcreteWorld(LABYRINTH_SAMPLE);

        // checks starting position
        assertEquals(new Vector2D(1, 2), world.getStart());
    }

    /**
     * Test case for {@code getExit()}.
     */

    @Test
    public void testGetExit() {
        World world = new ConcreteWorld(LABYRINTH_SAMPLE);

        // checks exiting position
        assertEquals(new Vector2D(5, 0), world.getExit());
    }

    // ==========================================================

    /**
     * Test case for {@code getChoices(Vector2D position)}
     * when there are 0 choices.
     */

    @Test
    public void testZeroChoice() {
        World world = new ConcreteWorld(LABYRINTH_STUCK);
        Direction[] choices = world.getChoices(new Vector2D(1, 1));

        // checks that the only choice is not to move
        checkChoices(choices, new Direction[]{Direction.NONE});
    }

    /**
     * Test case for {@code getChoices(Vector2D position)}
     * when there is 1 choice.
     */

    @Test
    public void testOneChoice() {
        World world = new ConcreteWorld(LABYRINTH_CORRIDOR);
        Direction[] choices;

        // checks when the only choice is to move down
        choices = world.getChoices(new Vector2D(1, 1));
        checkChoices(choices, new Direction[]{Direction.DOWN});

        // checks when the only choice is to move up
        choices = world.getChoices(new Vector2D(1, 2));
        checkChoices(choices, new Direction[]{Direction.UP});

        // checks when the only choice is to move right
        choices = world.getChoices(new Vector2D(3, 1));
        checkChoices(choices, new Direction[]{Direction.RIGHT});

        // checks when the only choice is to move left
        choices = world.getChoices(new Vector2D(4, 1));
        checkChoices(choices, new Direction[]{Direction.LEFT});
    }

    /**
     * Test case for {@code getChoices(Vector2D position)}
     * when there are 2 choices.
     */

    @Test
    public void testTwoChoices() {
        World world = new ConcreteWorld(LABYRINTH_DOGHNUT);
        Direction[] choices;

        // checks when the available choices are to move down and right
        choices = world.getChoices(new Vector2D(1, 1));
        checkChoices(choices,
                new Direction[]{Direction.DOWN, Direction.RIGHT});

        // checks when the available choices are to move down and up
        choices = world.getChoices(new Vector2D(1, 2));
        checkChoices(choices,
                new Direction[]{Direction.DOWN, Direction.UP});

        // checks when the available choices are to move up and right
        choices = world.getChoices(new Vector2D(1, 3));
        checkChoices(choices,
                new Direction[]{Direction.UP, Direction.RIGHT});

        // checks when the available choices are to move right and left
        choices = world.getChoices(new Vector2D(2, 1));
        checkChoices(choices,
                new Direction[]{Direction.RIGHT, Direction.LEFT});

        // checks when the available choices are to move down and left
        choices = world.getChoices(new Vector2D(3, 1));
        checkChoices(choices,
                new Direction[]{Direction.DOWN, Direction.LEFT});

        // checks when the available choices are to move up and left
        choices = world.getChoices(new Vector2D(3, 3));
        checkChoices(choices,
                new Direction[]{Direction.UP, Direction.LEFT});
    }

    /**
     * Test case for {@code getChoices(Vector2D position)}
     * when there are 3 choices.
     */

    @Test
    public void testThreeChoices() {
        World world = new ConcreteWorld(LABYRINTH_SQUARE);
        Direction[] choices;

        // checks when the available choices are to move right, left, and down
        choices = world.getChoices(new Vector2D(2, 1));
        checkChoices(choices,
                new Direction[]{Direction.RIGHT, Direction.LEFT, Direction.DOWN});

        // checks when the available choices are to move right, left, and up
        choices = world.getChoices(new Vector2D(2, 3));
        checkChoices(choices,
                new Direction[]{Direction.RIGHT, Direction.LEFT, Direction.UP});

        // checks when the available choices are to move down, up, and right
        choices = world.getChoices(new Vector2D(1, 2));
        checkChoices(choices,
                new Direction[]{Direction.DOWN, Direction.UP, Direction.RIGHT});

        // checks when the available choices are to move down, up, and left
        choices = world.getChoices(new Vector2D(3, 2));
        checkChoices(choices,
                new Direction[]{Direction.DOWN, Direction.UP, Direction.LEFT});
    }

    /**
     * Test case for {@code getChoices(Vector2D position)}
     * when there are 4 choices.
     */

    @Test
    public void testFourChoices() {
        World world = new ConcreteWorld(LABYRINTH_SQUARE);
        Direction[] choices;

        // checks when all the directions are available
        choices = world.getChoices(new Vector2D(2, 2));
        checkChoices(choices,
                new Direction[]{Direction.DOWN, Direction.UP, Direction.RIGHT, Direction.LEFT});
    }

    // ==========================================================

    /**
     * Checks if there are the same choices in {@code result} as in {@code
     * expected}
     *
     * @param result   The choices computed with method {@code getChoices(position)}
     * @param expected The choices expected to be present
     */

    private void checkChoices(Direction[] result, Direction[] expected) {
        // checks that array was initialized
        assertNotNull(result);

        // checks whether array has correct length and values
        Arrays.sort(result);
        Arrays.sort(expected);
        assertArrayEquals(expected, result);
    }

    /**
     * Mock class that makes {@code World} concrete.
     */

    private final class ConcreteWorld extends World {

        /**
         * Creates a concrete instance of the {@code World} class.
         *
         * @param labyrinth Actual maze
         */

        public ConcreteWorld(int[][] labyrinth) {
            super(labyrinth);
        }

        @Override
        public boolean isSolved() {
            return false;
        }

        @Override
        public void reset() {
            // do nothing
        }

        @Override
        public List<Animal> getAnimals() {
            return null;
        }
    }
}