From 4a14c5d2e6455cfa7c0a999e31437092196cd43d Mon Sep 17 00:00:00 2001 From: Debucquoy Date: Wed, 29 Mar 2023 08:49:25 +0200 Subject: [PATCH] wip tests: build matrixtobytes --- .../school_project/Parsers/BinaryParser.java | 11 +-- .../Parsers/BinaryParserTest.java | 82 +++++++++++++++++++ 2 files changed, 88 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/school_project/Parsers/BinaryParser.java b/app/src/main/java/school_project/Parsers/BinaryParser.java index 8c78677..9e2f64e 100644 --- a/app/src/main/java/school_project/Parsers/BinaryParser.java +++ b/app/src/main/java/school_project/Parsers/BinaryParser.java @@ -6,7 +6,6 @@ import school_project.Utils.Bitwise; import school_project.Vec2; import java.io.*; -import java.nio.file.Files; import java.util.Arrays; public class BinaryParser implements FileParser { @@ -129,7 +128,7 @@ public class BinaryParser implements FileParser { * @param level_data full data of the level without header and footer * @return boolean matrix of the map */ - private static boolean[][] ExtractMapFromLevelData(byte[] level_data){ + static boolean[][] ExtractMapFromLevelData(byte[] level_data){ int map_width = level_data[0], map_height = level_data[1]; byte[] map_data = Arrays.copyOfRange(level_data, 2, 2 + map_width * map_height / 8 + (map_height * map_width % 8 != 0 ? 1 : 0)); return BuildMatrixFromBytes(map_width, map_height, map_data); @@ -140,7 +139,7 @@ public class BinaryParser implements FileParser { * @param shape bolean matrix where true are 1 and false are 0 * @return byte array with each element compiled for file format */ - private static byte[] BuildByteFromMatrix(boolean[][] shape){ + static byte[] BuildByteFromMatrix(boolean[][] shape){ int width = shape[0].length , height = shape.length; boolean[] list = new boolean[width * height]; for (int x = 0; x < shape.length; x++) { @@ -169,7 +168,7 @@ public class BinaryParser implements FileParser { * @param matrix_data byte array of the data to export * @return boolean Matrix of the data decompiled */ - private static boolean[][] BuildMatrixFromBytes(int matrix_width, int matrix_height, byte[] matrix_data){ + static boolean[][] BuildMatrixFromBytes(int matrix_width, int matrix_height, byte[] matrix_data){ boolean[][] ret = new boolean[matrix_height][matrix_width]; // Transforming the bit from matrix_data's byte into boolean array for better manipulation @@ -179,13 +178,15 @@ public class BinaryParser implements FileParser { for (int i = 0; i < 8; i++) { // because 8 bit in a byte b_array[index] = Bitwise.IsBitSetAt(b, i); index++; + if(index <= matrix_height * matrix_width) + break; } } // Transforming b_array to a 2D matrix for (int x = 0; x < matrix_height; x++) { for (int y = 0; y < matrix_width; y++) { - ret[x][y] = b_array[y + x * 8]; + ret[x][y] = b_array[y + x * matrix_width]; } } return ret; diff --git a/app/src/test/java/school_project/Parsers/BinaryParserTest.java b/app/src/test/java/school_project/Parsers/BinaryParserTest.java index 7243485..a8db7a9 100644 --- a/app/src/test/java/school_project/Parsers/BinaryParserTest.java +++ b/app/src/test/java/school_project/Parsers/BinaryParserTest.java @@ -5,10 +5,23 @@ import school_project.Map; import school_project.Piece; import school_project.Vec2; +import java.util.Arrays; + import static org.junit.jupiter.api.Assertions.*; class BinaryParserTest { + static byte[] file_data = { + 'S', 'M', 'S', + 6, 4, (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' + }; + @Test void getByteSizeForMap() { boolean[][] map_shape = { @@ -40,4 +53,73 @@ class BinaryParserTest { assertEquals(15, BinaryParser.getByteSizeForMap(map, false)); 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(){ + 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}, + }; + assertEquals(map_shape, BinaryParser.BuildMatrixFromBytes(6, 5, map_data)); + } + + @Test + void BuildMatrixFromByte_piece1(){ + byte[] piece1_data = Arrays.copyOfRange(file_data, 11, 12); + boolean[][] piece1_shape = { + {false, true}, + {true, true}, + }; + assertArrayEquals(piece1_shape, BinaryParser.BuildMatrixFromBytes(2, 2, piece1_data)); + } + + @Test + void BuildMatrixFromByte_piece2(){ + byte[] piece2_data = Arrays.copyOfRange(file_data, 13, 15); + boolean[][] piece2_shape = { + {true, false, false}, + {true, true, false}, + {false, true, true}, + }; + assertArrayEquals(piece2_shape, BinaryParser.BuildMatrixFromBytes(3, 3, piece2_data)); + } + + @Test + void BuildMatrixFromByte_piece3(){ + byte[] piece3_data = Arrays.copyOfRange(file_data, 16, 17); + boolean[][] piece3_shape = { + {false, true, true}, + {true, true, false}, + }; + assertArrayEquals(piece3_shape, BinaryParser.BuildMatrixFromBytes(3, 2, piece3_data)); + } + + @Test + void BuildMatrixFromByte_piece4(){ + byte[] piece4_data = Arrays.copyOfRange(file_data, 18, 20); + boolean[][] piece4_shape = { + {true, true, false}, + {true, true, false}, + {true, true, true}, + }; + assertArrayEquals(piece4_shape, BinaryParser.BuildMatrixFromBytes(3, 3, piece4_data)); + } } \ No newline at end of file