Extraction Tests
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 22:32:38 +02:00 committed by Anthony Debucquoy
parent 3ce26796a8
commit c7511827fb
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
2 changed files with 59 additions and 20 deletions

View File

@ -68,7 +68,7 @@ public class BinaryParser implements FileParser {
* @return Level data as an array of byte * @return Level data as an array of byte
* @throws IOException Expected if we can't read the file * @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(); 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 if(bytes[i] == 83 && bytes[i+1] == 77 && bytes[i+2] == 69){ // SME
end_position = i; end_position = i;
break; break;
@ -97,11 +97,11 @@ public class BinaryParser implements FileParser {
* @param saved_data Should extract saved data and included it in the pieces * @param saved_data Should extract saved data and included it in the pieces
* @return array of Piece from level data * @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 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]; 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; byte[] pieces_positions = saved_data ? Arrays.copyOfRange(levelData, levelData.length - 1 - piece_count,levelData.length - 1): null;
int piece_offset = 0; int piece_offset = 0;
for (int piece_index = 0; piece_index < piece_count; piece_index++) { for (int piece_index = 0; piece_index < piece_count; piece_index++) {

View File

@ -5,6 +5,8 @@ import school_project.Map;
import school_project.Piece; import school_project.Piece;
import school_project.Vec2; import school_project.Vec2;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -13,13 +15,13 @@ class BinaryParserTest {
static byte[] file_data = { static byte[] file_data = {
'S', 'M', 'S', 'S', 'M', 'S',
6, 4, (byte) 0x31, (byte) 0xEC, (byte) 0xF3, (byte) 0xFC, 6, 5, (byte) 0x31, (byte) 0xEC, (byte) 0xF3, (byte) 0xFC,
4, 4,
(byte) 0x22, (byte) 0x70, (byte) 0x22, (byte) 0x70,
(byte) 0x33, (byte) 0x99, (byte) 0x80, (byte) 0x33, (byte) 0x99, (byte) 0x80,
(byte) 0x32, (byte) 0x7C, (byte) 0x32, (byte) 0x7C,
(byte) 0x33, (byte) 0xDB, (byte) 0x80, (byte) 0x33, (byte) 0xDB, (byte) 0x80,
'S', 'M', 'S' 'S', 'M', 'E'
}; };
@Test @Test
@ -54,19 +56,6 @@ class BinaryParserTest {
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 @Test
void BuildMatrixFromByte_map(){ void BuildMatrixFromByte_map(){
@ -122,4 +111,54 @@ class BinaryParserTest {
}; };
assertArrayEquals(piece4_shape, BinaryParser.BuildMatrixFromBytes(3, 3, piece4_data)); 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());
}
}
} }