From 3bf438ffb92dadeb19e42140549bcad098eeb8d2 Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Wed, 18 Dec 2024 01:03:53 +0100 Subject: [PATCH] Add and fix tests for the Serializers. --- .../maze/generator/algorithm/Wilson.java | 15 ++++++ .../v1/SerializerDeserializerV1Test.java | 6 +++ .../v2/SerializerDeserializerV2Test.java | 46 +++++++++++++++++++ .../v3/SerializerDeserializerV3Test.java | 40 ++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 src/test/java/ch/fritteli/maze/generator/serialization/v2/SerializerDeserializerV2Test.java create mode 100644 src/test/java/ch/fritteli/maze/generator/serialization/v3/SerializerDeserializerV3Test.java diff --git a/src/main/java/ch/fritteli/maze/generator/algorithm/Wilson.java b/src/main/java/ch/fritteli/maze/generator/algorithm/Wilson.java index e53acce..af55d7e 100644 --- a/src/main/java/ch/fritteli/maze/generator/algorithm/Wilson.java +++ b/src/main/java/ch/fritteli/maze/generator/algorithm/Wilson.java @@ -86,6 +86,21 @@ public class Wilson extends AbstractMazeGeneratorAlgorithm { direction = determineDirectionForDigging(maze.getEnd()); t = maze.getEndTile(); this.digTo(t, direction); + // seal all walls, mark all as visited + for (int x = 0; x < maze.getWidth(); x++) { + for (int y = 0; y < maze.getHeight(); y++) { + maze.getTileAt(x, y).forEach(tile -> { + Stream.of(Direction.values()) + .forEach(d -> { + if (tile.hasWallAt(d)) { + tile.preventDiggingToOrFrom(d); + } else { + tile.digFrom(d); + } + }); + }); + } + } } @Nullable diff --git a/src/test/java/ch/fritteli/maze/generator/serialization/v1/SerializerDeserializerV1Test.java b/src/test/java/ch/fritteli/maze/generator/serialization/v1/SerializerDeserializerV1Test.java index 2fd819b..4d8a392 100644 --- a/src/test/java/ch/fritteli/maze/generator/serialization/v1/SerializerDeserializerV1Test.java +++ b/src/test/java/ch/fritteli/maze/generator/serialization/v1/SerializerDeserializerV1Test.java @@ -13,6 +13,8 @@ class SerializerDeserializerV1Test { new RandomDepthFirst(expected).run(); final byte[] bytes = SerializerDeserializerV1.serialize(expected); final Maze result = SerializerDeserializerV1.deserialize(bytes); + assertThat(result.getAlgorithm()).isNull(); + expected.setAlgorithm(null); assertThat(result).isEqualTo(expected); } @@ -22,6 +24,8 @@ class SerializerDeserializerV1Test { new RandomDepthFirst(expected).run(); final byte[] bytes = SerializerDeserializerV1.serialize(expected); final Maze result = SerializerDeserializerV1.deserialize(bytes); + assertThat(result.getAlgorithm()).isNull(); + expected.setAlgorithm(null); assertThat(result).isEqualTo(expected); } @@ -31,6 +35,8 @@ class SerializerDeserializerV1Test { new RandomDepthFirst(expected).run(); final byte[] bytes = SerializerDeserializerV1.serialize(expected); final Maze result = SerializerDeserializerV1.deserialize(bytes); + assertThat(result.getAlgorithm()).isNull(); + expected.setAlgorithm(null); assertThat(result).isEqualTo(expected); } } diff --git a/src/test/java/ch/fritteli/maze/generator/serialization/v2/SerializerDeserializerV2Test.java b/src/test/java/ch/fritteli/maze/generator/serialization/v2/SerializerDeserializerV2Test.java new file mode 100644 index 0000000..c4898c7 --- /dev/null +++ b/src/test/java/ch/fritteli/maze/generator/serialization/v2/SerializerDeserializerV2Test.java @@ -0,0 +1,46 @@ +package ch.fritteli.maze.generator.serialization.v2; + +import ch.fritteli.maze.generator.algorithm.RandomDepthFirst; +import ch.fritteli.maze.generator.algorithm.Wilson; +import ch.fritteli.maze.generator.model.Maze; +import ch.fritteli.maze.generator.model.Position; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +class SerializerDeserializerV2Test { + @Test + void testSerializeDeserializeTiny() throws IOException { + final Maze expected = new Maze(2, 2, 255, new Position(0, 0), new Position(1, 1)); + new RandomDepthFirst(expected).run(); + final byte[] bytes = SerializerDeserializerV2.serialize(expected); + final Maze result = SerializerDeserializerV2.deserialize(bytes); + assertThat(result.getAlgorithm()).isNull(); + expected.setAlgorithm(null); + assertThat(result).isEqualTo(expected); + } + + @Test + void testSerializeDeserializeMedium() throws IOException { + final Maze expected = new Maze(20, 20, -271828182846L); + new Wilson(expected).run(); + final byte[] bytes = SerializerDeserializerV2.serialize(expected); + final Maze result = SerializerDeserializerV2.deserialize(bytes); + assertThat(result.getAlgorithm()).isNull(); + expected.setAlgorithm(null); + assertThat(result).isEqualTo(expected); + } + + @Test + void testSerializeDeserializeLarge() throws IOException { + final Maze expected = new Maze(200, 320, 3141592653589793238L); + new Wilson(expected).run(); + final byte[] bytes = SerializerDeserializerV2.serialize(expected); + final Maze result = SerializerDeserializerV2.deserialize(bytes); + assertThat(result.getAlgorithm()).isNull(); + expected.setAlgorithm(null); + assertThat(result).isEqualTo(expected); + } +} diff --git a/src/test/java/ch/fritteli/maze/generator/serialization/v3/SerializerDeserializerV3Test.java b/src/test/java/ch/fritteli/maze/generator/serialization/v3/SerializerDeserializerV3Test.java new file mode 100644 index 0000000..fde2008 --- /dev/null +++ b/src/test/java/ch/fritteli/maze/generator/serialization/v3/SerializerDeserializerV3Test.java @@ -0,0 +1,40 @@ +package ch.fritteli.maze.generator.serialization.v3; + +import ch.fritteli.maze.generator.algorithm.RandomDepthFirst; +import ch.fritteli.maze.generator.algorithm.Wilson; +import ch.fritteli.maze.generator.model.Maze; +import ch.fritteli.maze.generator.model.Position; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +class SerializerDeserializerV3Test { + @Test + void testSerializeDeserializeTiny() throws IOException { + final Maze expected = new Maze(2, 2, 255, new Position(0, 0), new Position(1, 1)); + new RandomDepthFirst(expected).run(); + final byte[] bytes = SerializerDeserializerV3.serialize(expected); + final Maze result = SerializerDeserializerV3.deserialize(bytes); + assertThat(result).isEqualTo(expected); + } + + @Test + void testSerializeDeserializeMedium() throws IOException { + final Maze expected = new Maze(20, 20, -271828182846L); + new Wilson(expected).run(); + final byte[] bytes = SerializerDeserializerV3.serialize(expected); + final Maze result = SerializerDeserializerV3.deserialize(bytes); + assertThat(result).isEqualTo(expected); + } + + @Test + void testSerializeDeserializeLarge() throws IOException { + final Maze expected = new Maze(200, 320, 3141592653589793238L); + new Wilson(expected).run(); + final byte[] bytes = SerializerDeserializerV3.serialize(expected); + final Maze result = SerializerDeserializerV3.deserialize(bytes); + assertThat(result).isEqualTo(expected); + } +}