From ad0759b36fcac4a6b9f48fc205f463ab52cec6e6 Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Tue, 6 Oct 2020 00:48:40 +0200 Subject: [PATCH] Add tests and add validity check to labyrinth dimensions. --- .../fritteli/labyrinth/model/Direction.java | 11 +- .../fritteli/labyrinth/model/Labyrinth.java | 5 +- .../ch/fritteli/labyrinth/model/TileTest.java | 182 ++++++++++++++++++ 3 files changed, 192 insertions(+), 6 deletions(-) create mode 100644 src/test/java/ch/fritteli/labyrinth/model/TileTest.java diff --git a/src/main/java/ch/fritteli/labyrinth/model/Direction.java b/src/main/java/ch/fritteli/labyrinth/model/Direction.java index 69b5de3..ec8a8c2 100644 --- a/src/main/java/ch/fritteli/labyrinth/model/Direction.java +++ b/src/main/java/ch/fritteli/labyrinth/model/Direction.java @@ -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()!"); } } diff --git a/src/main/java/ch/fritteli/labyrinth/model/Labyrinth.java b/src/main/java/ch/fritteli/labyrinth/model/Labyrinth.java index 55dc3b4..1cc6b35 100644 --- a/src/main/java/ch/fritteli/labyrinth/model/Labyrinth.java +++ b/src/main/java/ch/fritteli/labyrinth/model/Labyrinth.java @@ -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); diff --git a/src/test/java/ch/fritteli/labyrinth/model/TileTest.java b/src/test/java/ch/fritteli/labyrinth/model/TileTest.java new file mode 100644 index 0000000..6b3b744 --- /dev/null +++ b/src/test/java/ch/fritteli/labyrinth/model/TileTest.java @@ -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 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; + } + } +}