Add tests and add validity check to labyrinth dimensions.

This commit is contained in:
Manuel Friedli 2020-10-06 00:48:40 +02:00
parent 15f93310e4
commit ad0759b36f
3 changed files with 192 additions and 6 deletions

View file

@ -2,21 +2,22 @@ package ch.fritteli.labyrinth.model;
public enum Direction {
TOP,
RIGHT,
BOTTOM,
LEFT,
RIGHT;
LEFT;
public Direction invert() {
switch (this) {
case TOP:
return BOTTOM;
case LEFT:
return RIGHT;
case RIGHT:
return LEFT;
case BOTTOM:
return TOP;
case LEFT:
return RIGHT;
default:
throw new IllegalStateException("Programming error: Not all enum values covered in enum Direction#invert()!");
}
throw new IllegalStateException("Programming error: Not all enum values covered in enum Direction#invert()!");
}
}

View file

@ -28,10 +28,13 @@ public class Labyrinth {
}
public Labyrinth(final int width, final int height, final long randomSeed) {
if (width <= 0 || height <= 0) {
throw new IllegalArgumentException("widht and height must be >=1");
}
this.width = width;
this.height = height;
this.randomSeed = randomSeed;
this.random = new Random(this.randomSeed);
this.random = new Random(randomSeed);
this.field = new Tile[width][height];
this.start = new Position(0, 0);
this.end = new Position(this.width - 1, this.height - 1);

View file

@ -0,0 +1,182 @@
package ch.fritteli.labyrinth.model;
import io.vavr.control.Option;
import org.junit.jupiter.api.Test;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class TileTest {
@Test
void testConstruct() {
// arrange / act
final Tile sut = new Tile();
//assert
assertTrue(sut.hasWallAt(Direction.TOP));
assertTrue(sut.hasWallAt(Direction.RIGHT));
assertTrue(sut.hasWallAt(Direction.BOTTOM));
assertTrue(sut.hasWallAt(Direction.LEFT));
assertFalse(sut.isSolution());
}
@Test
void testDigFrom() {
// arrange
final Tile sut = new Tile();
// act
boolean result = sut.digFrom(Direction.TOP);
// assert
assertTrue(result);
assertFalse(sut.hasWallAt(Direction.TOP));
// act: can not dig from when already dug
result = sut.digFrom(Direction.BOTTOM);
// assert
assertFalse(result);
assertTrue(sut.hasWallAt(Direction.BOTTOM));
}
@Test
void testDigTo() {
// arrange
final Tile sut = new Tile();
// act / assert
assertTrue(sut.digTo(Direction.TOP));
assertTrue(sut.digTo(Direction.RIGHT));
assertTrue(sut.digTo(Direction.BOTTOM));
assertTrue(sut.digTo(Direction.LEFT));
// digging a second time does not succeed
assertFalse(sut.digTo(Direction.LEFT));
// assert
assertFalse(sut.hasWallAt(Direction.TOP));
assertFalse(sut.hasWallAt(Direction.RIGHT));
assertFalse(sut.hasWallAt(Direction.BOTTOM));
assertFalse(sut.hasWallAt(Direction.LEFT));
}
@Test
void testPreventDiggingToOrFrom() {
// arrange
final Tile sut = new Tile();
// act
sut.preventDiggingToOrFrom(Direction.LEFT);
// assert
assertFalse(sut.digTo(Direction.LEFT));
assertFalse(sut.digFrom(Direction.LEFT));
assertTrue(sut.hasWallAt(Direction.LEFT));
}
@Test
void testEnableDiggingToOrFrom() {
// arrange
final Tile sut = new Tile();
// act: fist, prevent digging
sut.preventDiggingToOrFrom(Direction.LEFT);
// then, re-enable digging
sut.enableDiggingToOrFrom(Direction.LEFT);
// also, enable it from a previously non-disabled direction
sut.enableDiggingToOrFrom(Direction.BOTTOM);
// assert
assertTrue(sut.digTo(Direction.LEFT));
assertFalse(sut.hasWallAt(Direction.LEFT));
}
@Test
void testUndigTo() {
// arrange
final Tile sut = new Tile();
sut.digTo(Direction.BOTTOM);
// act
sut.undigTo(Direction.BOTTOM);
// assert
assertTrue(sut.hasWallAt(Direction.BOTTOM));
}
@Test
void testSetSolution() {
// arrange
final Tile sut = new Tile();
// act
sut.setSolution();
// assert
assertTrue(sut.isSolution());
}
@Test
void testGetRandomAvailableDirection() {
// arrange
final Tile sut = new Tile();
// MyDummyRandom always returns 0
final MyDummyRandom dummyRandom = new MyDummyRandom();
// act
Option<Direction> result = sut.getRandomAvailableDirection(dummyRandom);
// assert
assertTrue(result.isDefined());
assertEquals(Direction.TOP, result.get());
// re-arrange
sut.preventDiggingToOrFrom(Direction.TOP);
// act
result = sut.getRandomAvailableDirection(dummyRandom);
// assert
assertTrue(result.isDefined());
assertEquals(Direction.RIGHT, result.get());
// re-arrange
sut.preventDiggingToOrFrom(Direction.RIGHT);
// act
result = sut.getRandomAvailableDirection(dummyRandom);
// assert
assertTrue(result.isDefined());
assertEquals(Direction.BOTTOM, result.get());
// re-arrange
sut.preventDiggingToOrFrom(Direction.BOTTOM);
// act
result = sut.getRandomAvailableDirection(dummyRandom);
// assert
assertTrue(result.isDefined());
assertEquals(Direction.LEFT, result.get());
// re-arrange
sut.preventDiggingToOrFrom(Direction.LEFT);
// act
result = sut.getRandomAvailableDirection(dummyRandom);
// assert
assertFalse(result.isDefined());
}
private static class MyDummyRandom extends Random {
@Override
protected int next(final int bits) {
return 0;
}
}
}