From c7511827fbc5f1b940900dae7b04cb33b2c81957 Mon Sep 17 00:00:00 2001 From: Debucquoy Date: Wed, 29 Mar 2023 22:32:38 +0200 Subject: [PATCH] Extraction Tests --- .../school_project/Parsers/BinaryParser.java | 10 +-- .../Parsers/BinaryParserTest.java | 69 +++++++++++++++---- 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/school_project/Parsers/BinaryParser.java b/app/src/main/java/school_project/Parsers/BinaryParser.java index 019daf7..7e672c5 100644 --- a/app/src/main/java/school_project/Parsers/BinaryParser.java +++ b/app/src/main/java/school_project/Parsers/BinaryParser.java @@ -68,7 +68,7 @@ public class BinaryParser implements FileParser { * @return Level data as an array of byte * @throws IOException Expected if we can't read the file */ - private static byte[] ExtractLevelData(FileInputStream fileStream) throws IOException { + static byte[] ExtractLevelData(InputStream fileStream) throws IOException { byte[] bytes = fileStream.readAllBytes(); @@ -80,7 +80,7 @@ public class BinaryParser implements FileParser { } } - for (int i = start_position; i < bytes.length; i++) { + for (int i = start_position; i < bytes.length - 2; i++) { if(bytes[i] == 83 && bytes[i+1] == 77 && bytes[i+2] == 69){ // SME end_position = i; break; @@ -97,11 +97,11 @@ public class BinaryParser implements FileParser { * @param saved_data Should extract saved data and included it in the pieces * @return array of Piece from level data */ - private static Piece[] ExtractPiecesFromLevelData(byte[] levelData, boolean saved_data) { + static Piece[] ExtractPiecesFromLevelData(byte[] levelData, boolean saved_data) { byte map_width = levelData[0], map_height = levelData[1]; - byte piece_count = levelData[3 + map_width * map_height / 8 + (map_height * map_width % 8 != 0 ? 1 : 0)]; + 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, 4 + map_width * map_height / 8 + (map_height * map_width % 8 != 0 ? 1 : 0), levelData.length - 1); + 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; int piece_offset = 0; for (int piece_index = 0; piece_index < piece_count; piece_index++) { diff --git a/app/src/test/java/school_project/Parsers/BinaryParserTest.java b/app/src/test/java/school_project/Parsers/BinaryParserTest.java index defc63a..4f52c12 100644 --- a/app/src/test/java/school_project/Parsers/BinaryParserTest.java +++ b/app/src/test/java/school_project/Parsers/BinaryParserTest.java @@ -5,6 +5,8 @@ import school_project.Map; import school_project.Piece; import school_project.Vec2; +import java.io.ByteArrayInputStream; +import java.io.IOException; import java.util.Arrays; import static org.junit.jupiter.api.Assertions.*; @@ -13,13 +15,13 @@ class BinaryParserTest { static byte[] file_data = { 'S', 'M', 'S', - 6, 4, (byte) 0x31, (byte) 0xEC, (byte) 0xF3, (byte) 0xFC, + 6, 5, (byte) 0x31, (byte) 0xEC, (byte) 0xF3, (byte) 0xFC, 4, (byte) 0x22, (byte) 0x70, (byte) 0x33, (byte) 0x99, (byte) 0x80, (byte) 0x32, (byte) 0x7C, (byte) 0x33, (byte) 0xDB, (byte) 0x80, - 'S', 'M', 'S' + 'S', 'M', 'E' }; @Test @@ -54,19 +56,6 @@ class BinaryParserTest { assertEquals(19, BinaryParser.getByteSizeForMap(map, true)); } -// @Test -// void ExtractLevelData(){ -// boolean[][] expected_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}, -// }; -// -// boolean[][] map = BinaryParser.ExtractMapFromLevelData(file_data); -// assertArrayEquals(expected_map_shape, map); -// } @Test void BuildMatrixFromByte_map(){ @@ -122,4 +111,54 @@ class BinaryParserTest { }; assertArrayEquals(piece4_shape, BinaryParser.BuildMatrixFromBytes(3, 3, piece4_data)); } + + @Test + void ExtractLevelData() throws IOException { + boolean[][] expected_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}, + }; + + byte[] level_data = BinaryParser.ExtractLevelData(new ByteArrayInputStream(file_data)); + boolean[][] map = BinaryParser.ExtractMapFromLevelData(level_data); + + assertArrayEquals(expected_map_shape, map); + } + + @Test + void ExtractPiecesFronLevelDataTest() throws IOException { + boolean[][] piece1_shape = { + {false, true}, + {true, true}, + }; + boolean[][] piece2_shape = { + {true, false, false}, + {true, true, false}, + {false, true, true}, + }; + boolean[][] piece3_shape = { + {false, true, true}, + {true, true, true}, + }; + boolean[][] piece4_shape = { + {true, true, false}, + {true, true, false}, + {true, true, true}, + }; + boolean[][][] pieces_shapes = { + piece1_shape, + piece2_shape, + piece3_shape, + piece4_shape + }; + byte[] level_data = BinaryParser.ExtractLevelData(new ByteArrayInputStream(file_data)); + Piece[] pieces = BinaryParser.ExtractPiecesFromLevelData(level_data, false); + + for (int i = 0; i < pieces_shapes.length; i++) { + assertArrayEquals(pieces_shapes[i], pieces[i].getShape()); + } + } } \ No newline at end of file