backend-infra-engineer: Release v0.3.0 snapshot
This commit is contained in:
431
test/gfx/compression_test.cc
Normal file
431
test/gfx/compression_test.cc
Normal file
@@ -0,0 +1,431 @@
|
||||
#include "app/gfx/compression.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <array>
|
||||
|
||||
#include "absl/status/statusor.h"
|
||||
#include "app/rom.h"
|
||||
|
||||
#define BUILD_HEADER(command, length) (command << 5) + (length - 1)
|
||||
|
||||
namespace yaze {
|
||||
namespace test {
|
||||
|
||||
using yaze::Rom;
|
||||
using yaze::gfx::lc_lz2::CompressionContext;
|
||||
using yaze::gfx::lc_lz2::CompressionPiece;
|
||||
using yaze::gfx::lc_lz2::CompressV2;
|
||||
using yaze::gfx::lc_lz2::CompressV3;
|
||||
using yaze::gfx::lc_lz2::DecompressV2;
|
||||
using yaze::gfx::lc_lz2::kCommandByteFill;
|
||||
using yaze::gfx::lc_lz2::kCommandDirectCopy;
|
||||
using yaze::gfx::lc_lz2::kCommandIncreasingFill;
|
||||
using yaze::gfx::lc_lz2::kCommandLongLength;
|
||||
using yaze::gfx::lc_lz2::kCommandRepeatingBytes;
|
||||
using yaze::gfx::lc_lz2::kCommandWordFill;
|
||||
|
||||
using ::testing::ElementsAre;
|
||||
using ::testing::ElementsAreArray;
|
||||
using ::testing::TypedEq;
|
||||
|
||||
namespace {
|
||||
|
||||
std::vector<uint8_t> ExpectCompressOk(Rom& rom, uint8_t* in, int in_size) {
|
||||
std::vector<uint8_t> data(in, in + in_size);
|
||||
auto load_status = rom.LoadFromData(data, false);
|
||||
EXPECT_TRUE(load_status.ok());
|
||||
auto compression_status = CompressV3(rom.vector(), 0, in_size);
|
||||
EXPECT_TRUE(compression_status.ok());
|
||||
auto compressed_bytes = std::move(*compression_status);
|
||||
return compressed_bytes;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ExpectDecompressBytesOk(Rom& rom,
|
||||
std::vector<uint8_t>& in) {
|
||||
auto load_status = rom.LoadFromData(in, false);
|
||||
EXPECT_TRUE(load_status.ok());
|
||||
auto decompression_status = DecompressV2(rom.data(), 0, in.size());
|
||||
EXPECT_TRUE(decompression_status.ok());
|
||||
auto decompressed_bytes = std::move(*decompression_status);
|
||||
return decompressed_bytes;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ExpectDecompressOk(Rom& rom, uint8_t* in, int in_size) {
|
||||
std::vector<uint8_t> data(in, in + in_size);
|
||||
auto load_status = rom.LoadFromData(data, false);
|
||||
EXPECT_TRUE(load_status.ok());
|
||||
auto decompression_status = DecompressV2(rom.data(), 0, in_size);
|
||||
EXPECT_TRUE(decompression_status.ok());
|
||||
auto decompressed_bytes = std::move(*decompression_status);
|
||||
return decompressed_bytes;
|
||||
}
|
||||
|
||||
std::shared_ptr<CompressionPiece> ExpectNewCompressionPieceOk(
|
||||
const char command, const int length, std::string& args,
|
||||
const int argument_length) {
|
||||
auto new_piece = std::make_shared<CompressionPiece>(command, length, args,
|
||||
argument_length);
|
||||
EXPECT_TRUE(new_piece != nullptr);
|
||||
return new_piece;
|
||||
}
|
||||
|
||||
// Helper function to assert compression quality.
|
||||
void AssertCompressionQuality(
|
||||
const std::vector<uint8_t>& uncompressed_data,
|
||||
const std::vector<uint8_t>& expected_compressed_data) {
|
||||
absl::StatusOr<std::vector<uint8_t>> result =
|
||||
CompressV3(uncompressed_data, 0, uncompressed_data.size(), 0, false);
|
||||
ASSERT_TRUE(result.ok());
|
||||
auto compressed_data = std::move(*result);
|
||||
EXPECT_THAT(compressed_data, ElementsAreArray(expected_compressed_data));
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ExpectCompressV3Ok(
|
||||
const std::vector<uint8_t>& uncompressed_data,
|
||||
const std::vector<uint8_t>& expected_compressed_data) {
|
||||
absl::StatusOr<std::vector<uint8_t>> result =
|
||||
CompressV3(uncompressed_data, 0, uncompressed_data.size(), 0, false);
|
||||
EXPECT_TRUE(result.ok());
|
||||
auto compressed_data = std::move(*result);
|
||||
return compressed_data;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> CreateRepeatedBetweenUncompressable(
|
||||
int leftUncompressedSize, int repeatedByteSize, int rightUncompressedSize) {
|
||||
std::vector<uint8_t> result(
|
||||
leftUncompressedSize + repeatedByteSize + rightUncompressedSize, 0);
|
||||
std::fill_n(result.begin() + leftUncompressedSize, repeatedByteSize, 0x00);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, TrivialRepeatedBytes) {
|
||||
AssertCompressionQuality({0x00, 0x00, 0x00}, {0x22, 0x00, 0xFF});
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, RepeatedBytesBetweenUncompressable) {
|
||||
AssertCompressionQuality({0x01, 0x00, 0x00, 0x00, 0x10},
|
||||
{0x04, 0x01, 0x00, 0x00, 0x00, 0x10, 0xFF});
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, RepeatedBytesBeforeUncompressable) {
|
||||
AssertCompressionQuality({0x00, 0x00, 0x00, 0x10},
|
||||
{0x22, 0x00, 0x00, 0x10, 0xFF});
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, RepeatedBytesAfterUncompressable) {
|
||||
AssertCompressionQuality({0x01, 0x00, 0x00, 0x00},
|
||||
{0x00, 0x01, 0x22, 0x00, 0xFF});
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, RepeatedBytesAfterUncompressableRepeated) {
|
||||
AssertCompressionQuality(
|
||||
{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02},
|
||||
{0x22, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x02, 0xFF});
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, RepeatedBytesBeforeUncompressableRepeated) {
|
||||
AssertCompressionQuality(
|
||||
{0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00},
|
||||
{0x04, 0x01, 0x00, 0x00, 0x00, 0x02, 0x22, 0x00, 0xFF});
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, NewDecompressionPieceOk) {
|
||||
char command = 1;
|
||||
int length = 1;
|
||||
char args[] = "aaa";
|
||||
int argument_length = 0x02;
|
||||
CompressionPiece old_piece;
|
||||
old_piece.command = command;
|
||||
old_piece.length = length;
|
||||
old_piece.argument = args;
|
||||
old_piece.argument_length = argument_length;
|
||||
old_piece.next = nullptr;
|
||||
|
||||
std::string new_args = "aaa";
|
||||
|
||||
auto new_piece = ExpectNewCompressionPieceOk(0x01, 0x01, new_args, 0x02);
|
||||
|
||||
EXPECT_EQ(old_piece.command, new_piece->command);
|
||||
EXPECT_EQ(old_piece.length, new_piece->length);
|
||||
ASSERT_EQ(old_piece.argument_length, new_piece->argument_length);
|
||||
for (int i = 0; i < old_piece.argument_length; ++i) {
|
||||
EXPECT_EQ(old_piece.argument[i], new_piece->argument[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Check why header built is off by one
|
||||
// 0x25 instead of 0x24
|
||||
TEST(LC_LZ2_CompressionTest, CompressionSingleSet) {
|
||||
Rom rom;
|
||||
uint8_t single_set[5] = {0x2A, 0x2A, 0x2A, 0x2A, 0x2A};
|
||||
uint8_t single_set_expected[3] = {BUILD_HEADER(1, 5), 0x2A, 0xFF};
|
||||
|
||||
auto comp_result = ExpectCompressOk(rom, single_set, 5);
|
||||
EXPECT_THAT(single_set_expected, ElementsAreArray(comp_result.data(), 3));
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, CompressionSingleWord) {
|
||||
Rom rom;
|
||||
uint8_t single_word[6] = {0x2A, 0x01, 0x2A, 0x01, 0x2A, 0x01};
|
||||
uint8_t single_word_expected[4] = {BUILD_HEADER(0x02, 0x06), 0x2A, 0x01, 0xFF};
|
||||
|
||||
auto comp_result = ExpectCompressOk(rom, single_word, 6);
|
||||
EXPECT_THAT(single_word_expected, ElementsAreArray(comp_result.data(), 4));
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, CompressionSingleIncrement) {
|
||||
Rom rom;
|
||||
uint8_t single_inc[3] = {0x01, 0x02, 0x03};
|
||||
uint8_t single_inc_expected[3] = {BUILD_HEADER(0x03, 0x03), 0x01, 0xFF};
|
||||
auto comp_result = ExpectCompressOk(rom, single_inc, 3);
|
||||
EXPECT_THAT(single_inc_expected, ElementsAreArray(comp_result.data(), 3));
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, CompressionSingleCopy) {
|
||||
Rom rom;
|
||||
uint8_t single_copy[4] = {0x03, 0x0A, 0x07, 0x14};
|
||||
uint8_t single_copy_expected[6] = {
|
||||
BUILD_HEADER(0x00, 0x04), 0x03, 0x0A, 0x07, 0x14, 0xFF};
|
||||
auto comp_result = ExpectCompressOk(rom, single_copy, 4);
|
||||
EXPECT_THAT(single_copy_expected, ElementsAreArray(comp_result.data(), 6));
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, CompressionSingleOverflowIncrement) {
|
||||
AssertCompressionQuality({0xFE, 0xFF, 0x00, 0x01},
|
||||
{BUILD_HEADER(0x03, 0x04), 0xFE, 0xFF});
|
||||
}
|
||||
/**
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, CompressionSingleCopyRepeat) {
|
||||
std::vector<uint8_t> single_copy_expected = {0x03, 0x0A, 0x07, 0x14,
|
||||
0x03, 0x0A, 0x07, 0x14};
|
||||
|
||||
auto comp_result = ExpectCompressV3Ok(
|
||||
single_copy_expected, {BUILD_HEADER(0x00, 0x04), 0x03, 0x0A, 0x07, 0x14,
|
||||
BUILD_HEADER(0x04, 0x04), 0x00, 0x00, 0xFF});
|
||||
EXPECT_THAT(single_copy_expected, ElementsAreArray(comp_result.data(), 6));
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, CompressionMixedRepeatIncrement) {
|
||||
AssertCompressionQuality(
|
||||
{0x05, 0x05, 0x05, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
|
||||
0x05, 0x02, 0x05, 0x02, 0x05, 0x02, 0x0A, 0x0B, 0x05, 0x02,
|
||||
0x05, 0x02, 0x05, 0x02, 0x08, 0x0A, 0x00, 0x05},
|
||||
{BUILD_HEADER(0x01, 0x04), 0x05, BUILD_HEADER(0x03, 0x06), 0x06,
|
||||
BUILD_HEADER(0x00, 0x01), 0x05, 0xFF});
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, CompressionMixedIncrementIntraCopyOffset) {
|
||||
// "Mixing, inc, alternate, intra copy"
|
||||
// compress start: 3, length: 21
|
||||
// compressed length: 9
|
||||
AssertCompressionQuality(
|
||||
{0x05, 0x05, 0x05, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
|
||||
0x05, 0x02, 0x05, 0x02, 0x05, 0x02, 0x0A, 0x0B, 0x05, 0x02,
|
||||
0x05, 0x02, 0x05, 0x02, 0x08, 0x0A, 0x00, 0x05},
|
||||
{BUILD_HEADER(0x03, 0x07), 0x05, BUILD_HEADER(0x02, 0x06), 0x05, 0x02,
|
||||
BUILD_HEADER(0x04, 0x08), 0x05, 0x00, 0xFF});
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, CompressionMixedIncrementIntraCopySource) {
|
||||
// "Mixing, inc, alternate, intra copy"
|
||||
// 0, 28
|
||||
// 16
|
||||
AssertCompressionQuality(
|
||||
{0x05, 0x05, 0x05, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
|
||||
0x05, 0x02, 0x05, 0x02, 0x05, 0x02, 0x0A, 0x0B, 0x05, 0x02,
|
||||
0x05, 0x02, 0x05, 0x02, 0x08, 0x0A, 0x00, 0x05},
|
||||
{BUILD_HEADER(0x01, 0x04), 0x05, BUILD_HEADER(0x03, 0x06), 0x06,
|
||||
BUILD_HEADER(0x02, 0x06), 0x05, 0x02, BUILD_HEADER(0x04, 0x08), 0x08,
|
||||
0x00, BUILD_HEADER(0x00, 0x04), 0x08, 0x0A, 0x00, 0x05, 0xFF});
|
||||
}
|
||||
|
||||
// Extended Header
|
||||
// 111CCCLL LLLLLLLL
|
||||
// CCC: Real command
|
||||
// LLLLLLLLLL: Length
|
||||
|
||||
// Normally you have 5 bits for the length, so the maximum value you can
|
||||
// represent is 31 (which outputs 32 bytes). With the long length, you get 5
|
||||
// more bits for the length, so the maximum value you can represent becomes
|
||||
// 1023, outputting 1024 bytes at a time.
|
||||
|
||||
void build_extended_header(uint8_t command, uint8_t length, uint8_t& byte1,
|
||||
uint8_t& byte2) {
|
||||
byte1 = command << 3;
|
||||
byte1 += (length - 1);
|
||||
byte1 += 0b11100000;
|
||||
byte2 = length >> 3;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> CreateRepeatedBetweenUncompressable(
|
||||
int leftUncompressedSize, int repeatedByteSize, int rightUncompressedSize) {
|
||||
std::vector<uint8_t> result(
|
||||
leftUncompressedSize + repeatedByteSize + rightUncompressedSize, 0);
|
||||
std::fill_n(result.begin() + leftUncompressedSize, repeatedByteSize, 0x00);
|
||||
return result;
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, LengthBorderCompression) {
|
||||
// "Length border compression"
|
||||
std::vector<uint8_t> result(42, 0);
|
||||
std::fill_n(result.begin(), 42, 0x05);
|
||||
AssertCompressionQuality(result, {BUILD_HEADER(0x04, 42), 0x05, 0x05, 0xFF});
|
||||
|
||||
// "Extended length, 400 repeat of 5"
|
||||
std::vector<uint8_t> result2(400, 0);
|
||||
std::fill_n(result2.begin(), 400, 0x05);
|
||||
uint8_t byte1;
|
||||
uint8_t byte2;
|
||||
build_extended_header(0x01, 42, byte1, byte2);
|
||||
AssertCompressionQuality(result2, {byte1, byte2, 0x05, 0x05, 0xFF});
|
||||
|
||||
// "Extended length, 1050 repeat of 5"
|
||||
std::vector<uint8_t> result3(1050, 0);
|
||||
std::fill_n(result3.begin(), 1050, 0x05);
|
||||
uint8_t byte3;
|
||||
uint8_t byte4;
|
||||
build_extended_header(0x04, 1050, byte3, byte4);
|
||||
AssertCompressionQuality(result3, {byte3, byte4, 0x05, 0x05, 0xFF});
|
||||
|
||||
// // "Extended length, 2050 repeat of 5"
|
||||
std::vector<uint8_t> result4(2050, 0);
|
||||
std::fill_n(result4.begin(), 2050, 0x05);
|
||||
uint8_t byte5;
|
||||
uint8_t byte6;
|
||||
build_extended_header(0x04, 2050, byte5, byte6);
|
||||
AssertCompressionQuality(result4, {byte5, byte6, 0x05, 0x05, 0xFF});
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, CompressionExtendedWordCopy) {
|
||||
// ROM rom;
|
||||
// uint8_t buffer[3000];
|
||||
// for (unsigned int i = 0; i < 3000; i += 2) {
|
||||
// buffer[i] = 0x05;
|
||||
// buffer[i + 1] = 0x06;
|
||||
// }
|
||||
// uint8_t hightlength_word_1050[] = {
|
||||
// 0b11101011, 0xFF, 0x05, 0x06, BUILD_HEADER(0x02, 0x1A), 0x05, 0x06,
|
||||
// 0xFF};
|
||||
|
||||
// // "Extended word copy"
|
||||
// auto comp_result = ExpectCompressOk(rom, buffer, 1050);
|
||||
// EXPECT_THAT(hightlength_word_1050, ElementsAreArray(comp_result.data(),
|
||||
// 8));
|
||||
|
||||
std::vector<uint8_t> buffer(3000, 0);
|
||||
std::fill_n(buffer.begin(), 3000, 0x05);
|
||||
for (unsigned int i = 0; i < 3000; i += 2) {
|
||||
buffer[i] = 0x05;
|
||||
buffer[i + 1] = 0x06;
|
||||
}
|
||||
|
||||
uint8_t byte1;
|
||||
uint8_t byte2;
|
||||
build_extended_header(0x02, 0x1A, byte1, byte2);
|
||||
AssertCompressionQuality(
|
||||
buffer, {0b11101011, 0xFF, 0x05, 0x06, byte1, byte2, 0x05, 0x06, 0xFF});
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, CompressionMixedPatterns) {
|
||||
AssertCompressionQuality(
|
||||
{0x05, 0x05, 0x05, 0x06, 0x07, 0x06, 0x07, 0x08, 0x09, 0x0A},
|
||||
{BUILD_HEADER(0x01, 0x03), 0x05, BUILD_HEADER(0x02, 0x04), 0x06, 0x07,
|
||||
BUILD_HEADER(0x03, 0x03), 0x08, 0xFF});
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, CompressionLongIntraCopy) {
|
||||
ROM rom;
|
||||
uint8_t long_data[15] = {0x05, 0x06, 0x07, 0x08, 0x05, 0x06, 0x07, 0x08,
|
||||
0x05, 0x06, 0x07, 0x08, 0x05, 0x06, 0x07};
|
||||
uint8_t long_expected[] = {BUILD_HEADER(0x00, 0x04), 0x05, 0x06, 0x07, 0x08,
|
||||
BUILD_HEADER(0x04, 0x0C), 0x00, 0x00, 0xFF};
|
||||
|
||||
auto comp_result = ExpectCompressOk(rom, long_data, 15);
|
||||
EXPECT_THAT(long_expected,
|
||||
ElementsAreArray(comp_result.data(), sizeof(long_expected)));
|
||||
}
|
||||
|
||||
*/
|
||||
// Tests for HandleDirectCopy
|
||||
|
||||
TEST(HandleDirectCopyTest, NotDirectCopyWithAccumulatedBytes) {
|
||||
CompressionContext context({0x01, 0x02, 0x03}, 0, 3);
|
||||
context.cmd_with_max = kCommandByteFill;
|
||||
context.comp_accumulator = 2;
|
||||
HandleDirectCopy(context);
|
||||
EXPECT_EQ(context.compressed_data.size(), 3);
|
||||
}
|
||||
|
||||
TEST(HandleDirectCopyTest, NotDirectCopyWithoutAccumulatedBytes) {
|
||||
CompressionContext context({0x01, 0x02, 0x03}, 0, 3);
|
||||
context.cmd_with_max = kCommandByteFill;
|
||||
HandleDirectCopy(context);
|
||||
EXPECT_EQ(context.compressed_data.size(), 2); // Header + 1 byte
|
||||
}
|
||||
|
||||
TEST(HandleDirectCopyTest, AccumulateBytesWithoutMax) {
|
||||
CompressionContext context({0x01, 0x02, 0x03}, 0, 3);
|
||||
context.cmd_with_max = kCommandDirectCopy;
|
||||
HandleDirectCopy(context);
|
||||
EXPECT_EQ(context.comp_accumulator, 1);
|
||||
EXPECT_EQ(context.compressed_data.size(), 0); // No data added yet
|
||||
}
|
||||
|
||||
// Tests for CheckIncByteV3
|
||||
|
||||
TEST(CheckIncByteV3Test, IncreasingSequence) {
|
||||
CompressionContext context({0x01, 0x02, 0x03}, 0, 3);
|
||||
CheckIncByteV3(context);
|
||||
EXPECT_EQ(context.current_cmd.data_size[kCommandIncreasingFill], 3);
|
||||
}
|
||||
|
||||
TEST(CheckIncByteV3Test, IncreasingSequenceSurroundedByIdenticalBytes) {
|
||||
CompressionContext context({0x01, 0x02, 0x03, 0x04, 0x01}, 1,
|
||||
3); // Start from index 1
|
||||
CheckIncByteV3(context);
|
||||
EXPECT_EQ(context.current_cmd.data_size[kCommandIncreasingFill],
|
||||
0); // Reset to prioritize direct copy
|
||||
}
|
||||
|
||||
TEST(CheckIncByteV3Test, NotAnIncreasingSequence) {
|
||||
CompressionContext context({0x01, 0x01, 0x03}, 0, 3);
|
||||
CheckIncByteV3(context);
|
||||
EXPECT_EQ(context.current_cmd.data_size[kCommandIncreasingFill],
|
||||
1); // Only one byte is detected
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, DecompressionValidCommand) {
|
||||
Rom rom;
|
||||
std::vector<uint8_t> simple_copy_input = {BUILD_HEADER(0x00, 0x02), 0x2A,
|
||||
0x45, 0xFF};
|
||||
uint8_t simple_copy_output[2] = {0x2A, 0x45};
|
||||
auto decomp_result = ExpectDecompressBytesOk(rom, simple_copy_input);
|
||||
EXPECT_THAT(simple_copy_output, ElementsAreArray(decomp_result.data(), 2));
|
||||
}
|
||||
|
||||
TEST(LC_LZ2_CompressionTest, DecompressionMixingCommand) {
|
||||
Rom rom;
|
||||
uint8_t random1_i[11] = {BUILD_HEADER(0x01, 0x03),
|
||||
0x2A,
|
||||
BUILD_HEADER(0x00, 0x04),
|
||||
0x01,
|
||||
0x02,
|
||||
0x03,
|
||||
0x04,
|
||||
BUILD_HEADER(0x02, 0x02),
|
||||
0x0B,
|
||||
0x16,
|
||||
0xFF};
|
||||
uint8_t random1_o[9] = {42, 42, 42, 1, 2, 3, 4, 11, 22};
|
||||
auto decomp_result = ExpectDecompressOk(rom, random1_i, 11);
|
||||
EXPECT_THAT(random1_o, ElementsAreArray(decomp_result.data(), 9));
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
} // namespace yaze
|
||||
199
test/gfx/snes_palette_test.cc
Normal file
199
test/gfx/snes_palette_test.cc
Normal file
@@ -0,0 +1,199 @@
|
||||
#include "app/gfx/snes_palette.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "app/gfx/snes_color.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace test {
|
||||
|
||||
using ::testing::ElementsAreArray;
|
||||
using yaze::gfx::ConvertRgbToSnes;
|
||||
using yaze::gfx::ConvertSnesToRgb;
|
||||
using yaze::gfx::Extract;
|
||||
|
||||
namespace {
|
||||
unsigned int test_convert(snes_color col) {
|
||||
unsigned int toret;
|
||||
toret = col.red << 16;
|
||||
toret += col.green << 8;
|
||||
toret += col.blue;
|
||||
return toret;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// SnesColor Tests
|
||||
TEST(SnesColorTest, DefaultConstructor) {
|
||||
yaze::gfx::SnesColor color;
|
||||
EXPECT_EQ(color.rgb().x, 0.0f);
|
||||
EXPECT_EQ(color.rgb().y, 0.0f);
|
||||
EXPECT_EQ(color.rgb().z, 0.0f);
|
||||
EXPECT_EQ(color.rgb().w, 0.0f);
|
||||
EXPECT_EQ(color.snes(), 0);
|
||||
}
|
||||
|
||||
TEST(SnesColorTest, RGBConstructor) {
|
||||
ImVec4 rgb(1.0f, 0.5f, 0.25f, 1.0f);
|
||||
yaze::gfx::SnesColor color(rgb);
|
||||
EXPECT_EQ(color.rgb().x, rgb.x);
|
||||
EXPECT_EQ(color.rgb().y, rgb.y);
|
||||
EXPECT_EQ(color.rgb().z, rgb.z);
|
||||
EXPECT_EQ(color.rgb().w, rgb.w);
|
||||
}
|
||||
|
||||
TEST(SnesColorTest, SNESConstructor) {
|
||||
uint16_t snes = 0x4210;
|
||||
yaze::gfx::SnesColor color(snes);
|
||||
EXPECT_EQ(color.snes(), snes);
|
||||
}
|
||||
|
||||
TEST(SnesColorTest, ConvertRgbToSnes) {
|
||||
snes_color color = {132, 132, 132};
|
||||
uint16_t snes = ConvertRgbToSnes(color);
|
||||
ASSERT_EQ(snes, 0x4210);
|
||||
}
|
||||
|
||||
TEST(SnesColorTest, ConvertSnestoRGB) {
|
||||
uint16_t snes = 0x4210;
|
||||
snes_color color = ConvertSnesToRgb(snes);
|
||||
ASSERT_EQ(color.red, 132);
|
||||
ASSERT_EQ(color.green, 132);
|
||||
ASSERT_EQ(color.blue, 132);
|
||||
}
|
||||
|
||||
TEST(SnesColorTest, ConvertSnesToRGB_Binary) {
|
||||
uint16_t red = 0b0000000000011111;
|
||||
uint16_t blue = 0b0111110000000000;
|
||||
uint16_t green = 0b0000001111100000;
|
||||
uint16_t purple = 0b0111110000011111;
|
||||
snes_color testcolor;
|
||||
|
||||
testcolor = ConvertSnesToRgb(red);
|
||||
ASSERT_EQ(0xFF0000, test_convert(testcolor));
|
||||
testcolor = ConvertSnesToRgb(green);
|
||||
ASSERT_EQ(0x00FF00, test_convert(testcolor));
|
||||
testcolor = ConvertSnesToRgb(blue);
|
||||
ASSERT_EQ(0x0000FF, test_convert(testcolor));
|
||||
testcolor = ConvertSnesToRgb(purple);
|
||||
ASSERT_EQ(0xFF00FF, test_convert(testcolor));
|
||||
}
|
||||
|
||||
TEST(SnesColorTest, Extraction) {
|
||||
// red, blue, green, purple
|
||||
char data[8] = {0x1F, 0x00, 0x00, 0x7C, static_cast<char>(0xE0),
|
||||
0x03, 0x1F, 0x7C};
|
||||
auto pal = Extract(data, 0, 4);
|
||||
ASSERT_EQ(4, pal.size());
|
||||
ASSERT_EQ(0xFF0000, test_convert(pal[0]));
|
||||
ASSERT_EQ(0x0000FF, test_convert(pal[1]));
|
||||
ASSERT_EQ(0x00FF00, test_convert(pal[2]));
|
||||
ASSERT_EQ(0xFF00FF, test_convert(pal[3]));
|
||||
}
|
||||
|
||||
TEST(SnesColorTest, Convert) {
|
||||
// red, blue, green, purple white
|
||||
char data[10] = {0x1F,
|
||||
0x00,
|
||||
0x00,
|
||||
0x7C,
|
||||
static_cast<char>(0xE0),
|
||||
0x03,
|
||||
0x1F,
|
||||
0x7C,
|
||||
static_cast<char>(0xFF),
|
||||
0x1F};
|
||||
auto pal = Extract(data, 0, 5);
|
||||
auto snes_string = yaze::gfx::Convert(pal);
|
||||
EXPECT_EQ(10, snes_string.size());
|
||||
EXPECT_THAT(data, ElementsAreArray(snes_string.data(), 10));
|
||||
}
|
||||
|
||||
// SnesPalette Tests
|
||||
TEST(SnesPaletteTest, DefaultConstructor) {
|
||||
yaze::gfx::SnesPalette palette;
|
||||
EXPECT_TRUE(palette.empty());
|
||||
EXPECT_EQ(palette.size(), 0);
|
||||
}
|
||||
|
||||
TEST(SnesPaletteTest, AddColor) {
|
||||
yaze::gfx::SnesPalette palette;
|
||||
yaze::gfx::SnesColor color;
|
||||
palette.AddColor(color);
|
||||
ASSERT_EQ(palette.size(), 1);
|
||||
}
|
||||
|
||||
TEST(SnesPaletteTest, AddMultipleColors) {
|
||||
yaze::gfx::SnesPalette palette;
|
||||
yaze::gfx::SnesColor color1(0x4210);
|
||||
yaze::gfx::SnesColor color2(0x7FFF);
|
||||
palette.AddColor(color1);
|
||||
palette.AddColor(color2);
|
||||
ASSERT_EQ(palette.size(), 2);
|
||||
}
|
||||
|
||||
TEST(SnesPaletteTest, UpdateColor) {
|
||||
yaze::gfx::SnesPalette palette;
|
||||
yaze::gfx::SnesColor color1(0x4210);
|
||||
yaze::gfx::SnesColor color2(0x7FFF);
|
||||
palette.AddColor(color1);
|
||||
palette.UpdateColor(0, color2);
|
||||
auto result = palette[0];
|
||||
ASSERT_EQ(result.snes(), 0x7FFF);
|
||||
}
|
||||
|
||||
TEST(SnesPaletteTest, SubPalette) {
|
||||
yaze::gfx::SnesPalette palette;
|
||||
yaze::gfx::SnesColor color1(0x4210);
|
||||
yaze::gfx::SnesColor color2(0x7FFF);
|
||||
yaze::gfx::SnesColor color3(0x1F1F);
|
||||
palette.AddColor(color1);
|
||||
palette.AddColor(color2);
|
||||
palette.AddColor(color3);
|
||||
|
||||
auto sub = palette.sub_palette(1, 3);
|
||||
ASSERT_EQ(sub.size(), 2);
|
||||
auto result = sub[0];
|
||||
ASSERT_EQ(result.snes(), 0x7FFF);
|
||||
}
|
||||
|
||||
TEST(SnesPaletteTest, VectorConstructor) {
|
||||
std::vector<yaze::gfx::SnesColor> colors = {yaze::gfx::SnesColor(0x4210),
|
||||
yaze::gfx::SnesColor(0x7FFF)};
|
||||
yaze::gfx::SnesPalette palette(colors);
|
||||
ASSERT_EQ(palette.size(), 2);
|
||||
}
|
||||
|
||||
TEST(SnesPaletteTest, Clear) {
|
||||
yaze::gfx::SnesPalette palette;
|
||||
yaze::gfx::SnesColor color(0x4210);
|
||||
palette.AddColor(color);
|
||||
ASSERT_EQ(palette.size(), 1);
|
||||
palette.clear();
|
||||
ASSERT_TRUE(palette.empty());
|
||||
}
|
||||
|
||||
TEST(SnesPaletteTest, Iterator) {
|
||||
yaze::gfx::SnesPalette palette;
|
||||
yaze::gfx::SnesColor color1(0x4210);
|
||||
yaze::gfx::SnesColor color2(0x7FFF);
|
||||
palette.AddColor(color1);
|
||||
palette.AddColor(color2);
|
||||
|
||||
int count = 0;
|
||||
for (const auto& color : palette) {
|
||||
EXPECT_TRUE(color.snes() == 0x4210 || color.snes() == 0x7FFF);
|
||||
count++;
|
||||
}
|
||||
EXPECT_EQ(count, 2);
|
||||
}
|
||||
|
||||
TEST(SnesPaletteTest, OperatorAccess) {
|
||||
yaze::gfx::SnesPalette palette;
|
||||
yaze::gfx::SnesColor color(0x4210);
|
||||
palette.AddColor(color);
|
||||
EXPECT_EQ(palette[0].snes(), 0x4210);
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
} // namespace yaze
|
||||
209
test/gfx/snes_tile_test.cc
Normal file
209
test/gfx/snes_tile_test.cc
Normal file
@@ -0,0 +1,209 @@
|
||||
#include "app/gfx/snes_tile.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "testing.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace test {
|
||||
|
||||
using ::testing::Eq;
|
||||
|
||||
TEST(SnesTileTest, UnpackBppTile) {
|
||||
// Test 1bpp tile unpacking
|
||||
std::vector<uint8_t> data1bpp = {0x80, 0x40, 0x20, 0x10,
|
||||
0x08, 0x04, 0x02, 0x01};
|
||||
auto tile1bpp = gfx::UnpackBppTile(data1bpp, 0, 1);
|
||||
EXPECT_EQ(tile1bpp.data[0], 1); // First pixel
|
||||
EXPECT_EQ(tile1bpp.data[7], 0); // Last pixel of first row
|
||||
EXPECT_EQ(tile1bpp.data[56], 0); // First pixel of last row
|
||||
EXPECT_EQ(tile1bpp.data[63], 1); // Last pixel
|
||||
|
||||
// Test 2bpp tile unpacking
|
||||
// Create test data where we know the expected results
|
||||
// For 2bpp: 16 bytes total (8 rows × 2 bytes per row)
|
||||
// Each row has 2 bytes: plane 0 byte, plane 1 byte
|
||||
// First pixel should be 3 (both bits set): plane0 bit7=1, plane1 bit7=1
|
||||
// Last pixel of first row should be 1: plane0 bit0=1, plane1 bit0=0
|
||||
std::vector<uint8_t> data2bpp = {
|
||||
0x81, 0x80, // Row 0: plane0=10000001, plane1=10000000
|
||||
0x00, 0x00, // Row 1: plane0=00000000, plane1=00000000
|
||||
0x00, 0x00, // Row 2: plane0=00000000, plane1=00000000
|
||||
0x00, 0x00, // Row 3: plane0=00000000, plane1=00000000
|
||||
0x00, 0x00, // Row 4: plane0=00000000, plane1=00000000
|
||||
0x00, 0x00, // Row 5: plane0=00000000, plane1=00000000
|
||||
0x00, 0x00, // Row 6: plane0=00000000, plane1=00000000
|
||||
0x01, 0x81 // Row 7: plane0=00000001, plane1=10000001
|
||||
};
|
||||
auto tile2bpp = gfx::UnpackBppTile(data2bpp, 0, 2);
|
||||
EXPECT_EQ(tile2bpp.data[0], 3); // First pixel: 1|1<<1 = 3
|
||||
EXPECT_EQ(tile2bpp.data[7], 1); // Last pixel of first row: 1|0<<1 = 1
|
||||
EXPECT_EQ(tile2bpp.data[56], 2); // First pixel of last row: 0|1<<1 = 2
|
||||
EXPECT_EQ(tile2bpp.data[63], 3); // Last pixel: 1|1<<1 = 3
|
||||
|
||||
// Test 4bpp tile unpacking
|
||||
// According to SnesLab: First planes 1&2 intertwined, then planes 3&4 intertwined
|
||||
// 32 bytes total: 16 bytes for planes 1&2, then 16 bytes for planes 3&4
|
||||
std::vector<uint8_t> data4bpp = {
|
||||
// Planes 1&2 intertwined (rows 0-7)
|
||||
0x81, 0x80, // Row 0: bp1=10000001, bp2=10000000
|
||||
0x00, 0x00, // Row 1: bp1=00000000, bp2=00000000
|
||||
0x00, 0x00, // Row 2: bp1=00000000, bp2=00000000
|
||||
0x00, 0x00, // Row 3: bp1=00000000, bp2=00000000
|
||||
0x00, 0x00, // Row 4: bp1=00000000, bp2=00000000
|
||||
0x00, 0x00, // Row 5: bp1=00000000, bp2=00000000
|
||||
0x00, 0x00, // Row 6: bp1=00000000, bp2=00000000
|
||||
0x01, 0x81, // Row 7: bp1=00000001, bp2=10000001
|
||||
// Planes 3&4 intertwined (rows 0-7)
|
||||
0x81, 0x80, // Row 0: bp3=10000001, bp4=10000000
|
||||
0x00, 0x00, // Row 1: bp3=00000000, bp4=00000000
|
||||
0x00, 0x00, // Row 2: bp3=00000000, bp4=00000000
|
||||
0x00, 0x00, // Row 3: bp3=00000000, bp4=00000000
|
||||
0x00, 0x00, // Row 4: bp3=00000000, bp4=00000000
|
||||
0x00, 0x00, // Row 5: bp3=00000000, bp4=00000000
|
||||
0x00, 0x00, // Row 6: bp3=00000000, bp4=00000000
|
||||
0x01, 0x81 // Row 7: bp3=00000001, bp4=10000001
|
||||
};
|
||||
auto tile4bpp = gfx::UnpackBppTile(data4bpp, 0, 4);
|
||||
EXPECT_EQ(tile4bpp.data[0], 0xF); // First pixel: 1|1<<1|1<<2|1<<3 = 15
|
||||
EXPECT_EQ(tile4bpp.data[7], 0x5); // Last pixel of first row: 1|0<<1|1<<2|0<<3 = 5
|
||||
EXPECT_EQ(tile4bpp.data[56], 0xA); // First pixel of last row: 0|1<<1|0<<2|1<<3 = 10
|
||||
EXPECT_EQ(tile4bpp.data[63], 0xF); // Last pixel: 1|1<<1|1<<2|1<<3 = 15
|
||||
}
|
||||
|
||||
TEST(SnesTileTest, PackBppTile) {
|
||||
// Test 1bpp tile packing
|
||||
snes_tile8 tile1bpp;
|
||||
std::fill(tile1bpp.data, tile1bpp.data + 64, 0);
|
||||
tile1bpp.data[0] = 1;
|
||||
tile1bpp.data[63] = 1;
|
||||
auto packed1bpp = gfx::PackBppTile(tile1bpp, 1);
|
||||
EXPECT_EQ(packed1bpp[0], 0x80); // First byte
|
||||
EXPECT_EQ(packed1bpp[7], 0x01); // Last byte
|
||||
|
||||
// Test 2bpp tile packing
|
||||
snes_tile8 tile2bpp;
|
||||
std::fill(tile2bpp.data, tile2bpp.data + 64, 0);
|
||||
tile2bpp.data[0] = 3;
|
||||
tile2bpp.data[7] = 1;
|
||||
tile2bpp.data[56] = 2;
|
||||
tile2bpp.data[63] = 3;
|
||||
auto packed2bpp = gfx::PackBppTile(tile2bpp, 2);
|
||||
EXPECT_EQ(packed2bpp[0], 0x81); // First byte of first plane: pixel0=3→0x80, pixel7=1→0x01
|
||||
EXPECT_EQ(packed2bpp[1], 0x80); // First byte of second plane: pixel0=3→0x80, pixel7=1→0x00
|
||||
EXPECT_EQ(packed2bpp[14], 0x01); // Last byte of first plane: pixel56=2→0x00, pixel63=3→0x01
|
||||
EXPECT_EQ(packed2bpp[15], 0x81); // Last byte of second plane: pixel56=2→0x80, pixel63=3→0x01
|
||||
}
|
||||
|
||||
TEST(SnesTileTest, ConvertBpp) {
|
||||
// Test 2bpp to 4bpp conversion
|
||||
std::vector<uint8_t> data2bpp = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04,
|
||||
0x02, 0x01, 0x01, 0x02, 0x04, 0x08,
|
||||
0x10, 0x20, 0x40, 0x80};
|
||||
auto converted4bpp = gfx::ConvertBpp(data2bpp, 2, 4);
|
||||
EXPECT_EQ(converted4bpp.size(), 32); // 4bpp tile is 32 bytes
|
||||
|
||||
// Test 4bpp to 2bpp conversion (using only colors 0-3 for valid 2bpp)
|
||||
std::vector<uint8_t> data4bpp = {
|
||||
// Planes 1&2 (rows 0-7) - create colors 0-3 only
|
||||
0x80, 0x80, 0x40, 0x00, 0x20, 0x40, 0x10, 0x80, // rows 0-3
|
||||
0x08, 0x00, 0x04, 0x40, 0x02, 0x80, 0x01, 0x00, // rows 4-7
|
||||
// Planes 3&4 (rows 0-7) - all zeros to ensure colors stay ≤ 3
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // rows 0-3
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // rows 4-7
|
||||
};
|
||||
auto converted2bpp = gfx::ConvertBpp(data4bpp, 4, 2);
|
||||
EXPECT_EQ(converted2bpp.size(), 16); // 2bpp tile is 16 bytes
|
||||
}
|
||||
|
||||
TEST(SnesTileTest, TileInfo) {
|
||||
// Test TileInfo construction and bit manipulation
|
||||
gfx::TileInfo info(0x123, 3, true, true, true);
|
||||
EXPECT_EQ(info.id_, 0x123);
|
||||
EXPECT_EQ(info.palette_, 3);
|
||||
EXPECT_TRUE(info.vertical_mirror_);
|
||||
EXPECT_TRUE(info.horizontal_mirror_);
|
||||
EXPECT_TRUE(info.over_);
|
||||
|
||||
// Test TileInfo from bytes
|
||||
gfx::TileInfo infoFromBytes(0x23, 0xED); // v=1, h=1, o=1, p=3, id=0x123
|
||||
EXPECT_EQ(infoFromBytes.id_, 0x123);
|
||||
EXPECT_EQ(infoFromBytes.palette_, 3);
|
||||
EXPECT_TRUE(infoFromBytes.vertical_mirror_);
|
||||
EXPECT_TRUE(infoFromBytes.horizontal_mirror_);
|
||||
EXPECT_TRUE(infoFromBytes.over_);
|
||||
|
||||
// Test TileInfo equality
|
||||
EXPECT_TRUE(info == infoFromBytes);
|
||||
}
|
||||
|
||||
TEST(SnesTileTest, TileInfoToWord) {
|
||||
gfx::TileInfo info(0x123, 3, true, true, true);
|
||||
uint16_t word = gfx::TileInfoToWord(info);
|
||||
|
||||
// Verify bit positions:
|
||||
// vhopppcc cccccccc
|
||||
EXPECT_EQ(word & 0x3FF, 0x123); // id (10 bits)
|
||||
EXPECT_TRUE(word & 0x8000); // vertical mirror
|
||||
EXPECT_TRUE(word & 0x4000); // horizontal mirror
|
||||
EXPECT_TRUE(word & 0x2000); // over
|
||||
EXPECT_EQ((word >> 10) & 0x07, 3); // palette (3 bits)
|
||||
}
|
||||
|
||||
TEST(SnesTileTest, WordToTileInfo) {
|
||||
uint16_t word = 0xED23; // v=1, h=1, o=1, p=3, id=0x123
|
||||
gfx::TileInfo info = gfx::WordToTileInfo(word);
|
||||
|
||||
EXPECT_EQ(info.id_, 0x123);
|
||||
EXPECT_EQ(info.palette_, 3);
|
||||
EXPECT_TRUE(info.vertical_mirror_);
|
||||
EXPECT_TRUE(info.horizontal_mirror_);
|
||||
EXPECT_TRUE(info.over_);
|
||||
}
|
||||
|
||||
TEST(SnesTileTest, Tile32) {
|
||||
// Test Tile32 construction and operations
|
||||
gfx::Tile32 tile32(0x1234, 0x5678, 0x9ABC, 0xDEF0);
|
||||
EXPECT_EQ(tile32.tile0_, 0x1234);
|
||||
EXPECT_EQ(tile32.tile1_, 0x5678);
|
||||
EXPECT_EQ(tile32.tile2_, 0x9ABC);
|
||||
EXPECT_EQ(tile32.tile3_, 0xDEF0);
|
||||
|
||||
// Test packed value
|
||||
uint64_t packed = tile32.GetPackedValue();
|
||||
EXPECT_EQ(packed, 0xDEF09ABC56781234);
|
||||
|
||||
// Test from packed value
|
||||
gfx::Tile32 tile32FromPacked(packed);
|
||||
EXPECT_EQ(tile32FromPacked.tile0_, 0x1234);
|
||||
EXPECT_EQ(tile32FromPacked.tile1_, 0x5678);
|
||||
EXPECT_EQ(tile32FromPacked.tile2_, 0x9ABC);
|
||||
EXPECT_EQ(tile32FromPacked.tile3_, 0xDEF0);
|
||||
|
||||
// Test equality
|
||||
EXPECT_TRUE(tile32 == tile32FromPacked);
|
||||
}
|
||||
|
||||
TEST(SnesTileTest, Tile16) {
|
||||
// Test Tile16 construction and operations
|
||||
gfx::TileInfo info0(0x123, 3, true, true, true);
|
||||
gfx::TileInfo info1(0x456, 2, false, true, false);
|
||||
gfx::TileInfo info2(0x789, 1, true, false, true);
|
||||
gfx::TileInfo info3(0xABC, 0, false, false, false);
|
||||
|
||||
gfx::Tile16 tile16(info0, info1, info2, info3);
|
||||
EXPECT_TRUE(tile16.tile0_ == info0);
|
||||
EXPECT_TRUE(tile16.tile1_ == info1);
|
||||
EXPECT_TRUE(tile16.tile2_ == info2);
|
||||
EXPECT_TRUE(tile16.tile3_ == info3);
|
||||
|
||||
// Test array access
|
||||
EXPECT_TRUE(tile16.tiles_info[0] == info0);
|
||||
EXPECT_TRUE(tile16.tiles_info[1] == info1);
|
||||
EXPECT_TRUE(tile16.tiles_info[2] == info2);
|
||||
EXPECT_TRUE(tile16.tiles_info[3] == info3);
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
} // namespace yaze
|
||||
Reference in New Issue
Block a user