wip tests: build matrixtobytes
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
Debucquoy 2023-03-29 08:49:25 +02:00
parent 0188fd8420
commit 4a14c5d2e6
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
2 changed files with 88 additions and 5 deletions

View File

@ -6,7 +6,6 @@ import school_project.Utils.Bitwise;
import school_project.Vec2; import school_project.Vec2;
import java.io.*; import java.io.*;
import java.nio.file.Files;
import java.util.Arrays; import java.util.Arrays;
public class BinaryParser implements FileParser { 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 * @param level_data full data of the level without header and footer
* @return boolean matrix of the map * @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]; 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)); 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); 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 * @param shape bolean matrix where true are 1 and false are 0
* @return byte array with each element compiled for file format * @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; int width = shape[0].length , height = shape.length;
boolean[] list = new boolean[width * height]; boolean[] list = new boolean[width * height];
for (int x = 0; x < shape.length; x++) { 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 * @param matrix_data byte array of the data to export
* @return boolean Matrix of the data decompiled * @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]; boolean[][] ret = new boolean[matrix_height][matrix_width];
// Transforming the bit from matrix_data's byte into boolean array for better manipulation // 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 for (int i = 0; i < 8; i++) { // because 8 bit in a byte
b_array[index] = Bitwise.IsBitSetAt(b, i); b_array[index] = Bitwise.IsBitSetAt(b, i);
index++; index++;
if(index <= matrix_height * matrix_width)
break;
} }
} }
// Transforming b_array to a 2D matrix // Transforming b_array to a 2D matrix
for (int x = 0; x < matrix_height; x++) { for (int x = 0; x < matrix_height; x++) {
for (int y = 0; y < matrix_width; y++) { 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; return ret;

View File

@ -5,10 +5,23 @@ import school_project.Map;
import school_project.Piece; import school_project.Piece;
import school_project.Vec2; import school_project.Vec2;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class BinaryParserTest { 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 @Test
void getByteSizeForMap() { void getByteSizeForMap() {
boolean[][] map_shape = { boolean[][] map_shape = {
@ -40,4 +53,73 @@ class BinaryParserTest {
assertEquals(15, BinaryParser.getByteSizeForMap(map, false)); assertEquals(15, BinaryParser.getByteSizeForMap(map, false));
assertEquals(19, BinaryParser.getByteSizeForMap(map, true)); 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));
}
} }