diff --git a/app/build.gradle b/app/build.gradle index 3f8d793..b253582 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -19,7 +19,9 @@ repositories { dependencies { // Use JUnit Jupiter for testing. - testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1' + testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.2' + testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.2' // This dependency is used by the application. implementation 'com.google.guava:guava:31.1-jre' diff --git a/app/src/main/java/school_project/Parsers/BinaryParser.java b/app/src/main/java/school_project/Parsers/BinaryParser.java index 7e672c5..98e32a1 100644 --- a/app/src/main/java/school_project/Parsers/BinaryParser.java +++ b/app/src/main/java/school_project/Parsers/BinaryParser.java @@ -102,7 +102,7 @@ public class BinaryParser implements FileParser { byte piece_count = levelData[2 + map_width * map_height / 8 + (map_height * map_width % 8 != 0 ? 1 : 0)]; Piece[] ret = new Piece[piece_count]; byte[] pieces_data = Arrays.copyOfRange(levelData, 3 + map_width * map_height / 8 + (map_height * map_width % 8 != 0 ? 1 : 0), levelData.length); - byte[] pieces_positions = saved_data ? Arrays.copyOfRange(levelData, levelData.length - 1 - piece_count,levelData.length - 1): null; + byte[] pieces_positions = saved_data ? Arrays.copyOfRange(levelData, levelData.length - piece_count*2,levelData.length ): null; int piece_offset = 0; for (int piece_index = 0; piece_index < piece_count; piece_index++) { Vec2 _piece_size = Bitwise.ByteToNible(pieces_data[piece_index + piece_offset]); @@ -140,21 +140,22 @@ public class BinaryParser implements FileParser { * @return byte array with each element compiled for file format */ static byte[] BuildByteFromMatrix(boolean[][] shape){ - // TODO: 3/27/23 Problem with this function to investigate int width = shape[0].length , height = shape.length; - boolean[] list = new boolean[width * height]; + boolean[] b_list = new boolean[width * height]; for (int x = 0; x < shape.length; x++) { for (int y = 0; y < shape[x].length; y++) { - list[x * shape[x].length + y] = shape[x][y]; + b_list[x * shape[x].length + y] = shape[x][y]; } } - byte[] ret = new byte[width * height / 8 + width * height % 8 == 0 ? 0 : 1 ]; + byte[] ret = new byte[width * height / 8 + (width * height % 8 == 0 ? 0 : 1)]; for (int i = 0; i < ret.length; i++) { - byte b = 0; - for (int b_count = i * 8; b_count < (i * 8 + 8) % (i * 8 - ret.length); b_count++) { - b = (byte) ((b << 1) | (list[b_count] ? 1 : 0)); + byte current_byte = 0; + boolean[] current_byte_data = Arrays.copyOfRange(b_list, i * 8, i * 8 + 8); + for (boolean curr_data: current_byte_data) { + current_byte = (byte) (current_byte << 1); + current_byte = (byte) (current_byte | (curr_data ? 1 : 0)); } - ret[i] = b; + ret[i] = current_byte; } return ret; } diff --git a/app/src/main/java/school_project/Piece.java b/app/src/main/java/school_project/Piece.java index 8d9c0e7..51df463 100644 --- a/app/src/main/java/school_project/Piece.java +++ b/app/src/main/java/school_project/Piece.java @@ -24,10 +24,6 @@ public class Piece extends Shape{ } public void setPosition(Vec2 position){ - if (linked_map == null) { - return; - } - this.Position = position; } diff --git a/app/src/main/java/school_project/Vec2.java b/app/src/main/java/school_project/Vec2.java index 79d4fd9..05867cc 100644 --- a/app/src/main/java/school_project/Vec2.java +++ b/app/src/main/java/school_project/Vec2.java @@ -1,10 +1,12 @@ package school_project; +import java.io.Serializable; + /** * This is used to represent a position/vector/... any ensemble of 2 elements that have to work together in * a plan. This way we can use some basic operations over them. */ -public class Vec2 { +public class Vec2 implements Serializable { public int x, y; public Vec2() { @@ -16,4 +18,12 @@ public class Vec2 { this.x = x; this.y = y; } + + @Override + public boolean equals(Object obj) { + if (obj instanceof Vec2 vec) { + return this.x == vec.x && this.y == vec.y; + } + return false; + } } diff --git a/app/src/test/java/school_project/Parsers/BinaryParserTest.java b/app/src/test/java/school_project/Parsers/BinaryParserTest.java index 4f52c12..ea28f6c 100644 --- a/app/src/test/java/school_project/Parsers/BinaryParserTest.java +++ b/app/src/test/java/school_project/Parsers/BinaryParserTest.java @@ -57,6 +57,19 @@ class BinaryParserTest { } + @Test + void BuildByteFromMatrix(){ + byte[] map_data = Arrays.copyOfRange(file_data, 5, 9); + boolean[][] map_shape = { + {false, false, true, true, false, false}, + {false, true, true, true, true, false}, + {true, true, false, false, true, true}, + {true, true, false, false, true, true}, + {true, true, true, true, true, true}, + }; + assertArrayEquals(map_data, BinaryParser.BuildByteFromMatrix(map_shape)); + } + @Test void BuildMatrixFromByte_map(){ byte[] map_data = Arrays.copyOfRange(file_data, 5, 9); diff --git a/app/src/test/java/school_project/Parsers/FileParserFactoryTest.java b/app/src/test/java/school_project/Parsers/FileParserFactoryTest.java index 7a08e67..e703db6 100644 --- a/app/src/test/java/school_project/Parsers/FileParserFactoryTest.java +++ b/app/src/test/java/school_project/Parsers/FileParserFactoryTest.java @@ -1,94 +1,103 @@ package school_project.Parsers; import org.junit.jupiter.api.*; +import org.junit.jupiter.api.io.TempDir; import school_project.Map; import school_project.Piece; +import school_project.Vec2; import java.io.File; import java.io.IOException; +import java.nio.file.Path; import static org.junit.jupiter.api.Assertions.*; -@TestMethodOrder(value = MethodOrderer.OrderAnnotation.class) class FileParserFactoryTest { static Map generateMapTest(){ boolean[][] map_shape = { - {true, true, true, true}, - {true, false, true, true}, - {true, true, false, true}, - {true, true, false, false}, + {false, false, true, true, false, false}, + {false, true, true, true, true, false}, + {true, true, false, false, true, true}, + {true, true, false, false, true, true}, + {true, true, true, true, true, true}, }; boolean[][] piece1_shape = { - {true, true}, - {true, false}, - {true, true}, + {false, true}, {true, true}, }; boolean[][] piece2_shape = { - {true, true}, + {true, false, false}, + {true, true, false}, + {false, true, true}, }; boolean[][] piece3_shape = { - {true, true}, - {false, true}, + {false, true, true}, + {true, true, true}, }; - Piece[] pieces = { new Piece(piece1_shape), new Piece(piece2_shape), new Piece(piece3_shape) }; + boolean[][] piece4_shape = { + {true, true, false}, + {true, true, false}, + {true, true, true}, + }; + Piece[] pieces = { new Piece(piece1_shape), new Piece(piece2_shape), new Piece(piece3_shape), new Piece(piece4_shape) }; Map map = new Map(map_shape); map.addPiece(pieces); + + pieces[0].setPosition(new Vec2(1, 0)); + pieces[1].setPosition(new Vec2(3, 0)); + pieces[2].setPosition(new Vec2(3, 3)); + pieces[3].setPosition(new Vec2(0, 2)); return map; } @Test - void saveFileFromMap_Binary() throws IOException { + void saveLoadFileFromMap_Binary(@TempDir Path tmpFolder) throws IOException { Map map = generateMapTest(); - FileParserFactory.saveFileFromMap(new File("TestBinaryLevel.level"), map); - } + FileParserFactory.saveFileFromMap(tmpFolder.resolve("TestBinaryLevel.level").toFile(), map); - @Test - void saveFileFromMap_save_Binary() throws IOException { - Map map = generateMapTest(); - FileParserFactory.saveFileFromMap(new File("TestBinarySave.slevel"), map); - } - @Test - void saveFileFromMap_Serialized() throws IOException { - Map map = generateMapTest(); - FileParserFactory.saveFileFromMap(new File("TestSerializedLevel.serialized"), map); - } - - @Test - void saveFileFromMap_save_Serialized() throws IOException{ - Map map = generateMapTest(); - FileParserFactory.saveFileFromMap(new File("TestSerializedSave.sserialized"), map); - } - - @Test - void loadMapFromFile_Binary() throws IOException { - Map map = generateMapTest(); - Map testMap = FileParserFactory.loadMapFromFile(new File("src/test/resources/saveFileExamples/TestBinaryLevel.level")); + Map testMap = FileParserFactory.loadMapFromFile(tmpFolder.resolve("TestBinaryLevel.level").toFile()); assertArrayEquals(map.getCleanedMap().getShape(), testMap.getShape()); - assertTrue(map.getCleanedMap().getPieces().equals(testMap.getPieces())); + for (int i = 0; i < map.getPieces().size(); i++) { + assertArrayEquals(map.getPieces().get(i).getShape(), testMap.getPieces().get(i).getShape()); + } } @Test - void loadMapFromFile_saved_Binary() throws IOException { + void saveLoadFileFromMap_save_Binary(@TempDir Path tmpFolder) throws IOException { Map map = generateMapTest(); - Map testMap = FileParserFactory.loadMapFromFile(new File("src/test/resources/saveFileExamples/TestBinarySave.slevel")); + FileParserFactory.saveFileFromMap(tmpFolder.resolve("TestBinarySave.slevel").toFile(), map); + + Map testMap = FileParserFactory.loadMapFromFile(tmpFolder.resolve("TestBinarySave.slevel").toFile()); assertArrayEquals(map.getShape(), testMap.getShape()); - assert(map.getPieces().equals(testMap.getPieces())); + for (int i = 0; i < map.getPieces().size(); i++) { + assertArrayEquals(map.getPieces().get(i).getShape(), testMap.getPieces().get(i).getShape()); + assertEquals(map.getPieces().get(i).getPosition(), testMap.getPieces().get(i).getPosition()); + } } @Test - void loadMapFromFile_Serialized() throws IOException { + void saveLoadFileFromMap_Serialized(@TempDir Path tmpFolder) throws IOException { Map map = generateMapTest(); - Map testMap = FileParserFactory.loadMapFromFile(new File("src/test/resources/saveFileExamples/TestSerializedLevel.serialized")); - assertArrayEquals(map.getCleanedMap().getShape(), testMap.getShape()); - assert(map.getCleanedMap().getPieces().equals(testMap.getPieces())); + FileParserFactory.saveFileFromMap( tmpFolder.resolve("TestSerializedLevel.serialized").toFile(), map); + + Map testMap = FileParserFactory.loadMapFromFile( tmpFolder.resolve("TestSerializedLevel.serialized").toFile()); + assertArrayEquals(map.getShape(), testMap.getShape()); + for (int i = 0; i < map.getPieces().size(); i++) { + assertArrayEquals(map.getPieces().get(i).getShape(), testMap.getPieces().get(i).getShape()); + } + } @Test - void loadMapFromFile_saved_Serialized() throws IOException { + void saveLoadFileFromMap_save_Serialized(@TempDir Path tmpFolder) throws IOException{ Map map = generateMapTest(); - Map testMap = FileParserFactory.loadMapFromFile(new File("src/test/resources/saveFileExamples/TestSerializedSave.sserialized")); + FileParserFactory.saveFileFromMap(tmpFolder.resolve("TestSerializedSave.sserialized").toFile(), map); + + Map testMap = FileParserFactory.loadMapFromFile(tmpFolder.resolve("TestSerializedSave.sserialized").toFile()); assertArrayEquals(map.getShape(), testMap.getShape()); - assertTrue(map.getPieces().equals(testMap.getPieces())); + for (int i = 0; i < map.getPieces().size(); i++) { + assertArrayEquals(map.getPieces().get(i).getShape(), testMap.getPieces().get(i).getShape()); + assertEquals(map.getPieces().get(i).getPosition(), testMap.getPieces().get(i).getPosition()); + } } -} \ No newline at end of file +}