backend-infra-engineer: Pre-0.2.2 snapshot (2023)

This commit is contained in:
scawful
2023-12-29 22:43:40 -05:00
parent e7470bdfac
commit d94b7a3e81
174 changed files with 31731 additions and 4836 deletions

View File

@@ -1,4 +1,4 @@
# GoogleTest ------------------------------------------------------------------------------------
# GoogleTest ------------------------------------------------------------------
include(FetchContent)
FetchContent_Declare(
googletest
@@ -13,30 +13,52 @@ enable_testing()
add_executable(
yaze_test
yaze_test.cc
rom_test.cc
emu/cpu_test.cc
emu/spc700_test.cc
emu/ppu_test.cc
compression_test.cc
snes_palette_test.cc
room_object_test.cc
../src/cli/patch.cc
../src/cli/command_handler.cc
../src/app/rom.cc
../src/app/emu/cpu/cpu.cc
../src/app/emu/cpu/internal/instructions.cc
../src/app/emu/cpu/internal/addressing.cc
../src/app/emu/audio/internal/addressing.cc
../src/app/emu/audio/internal/instructions.cc
../src/app/emu/audio/apu.cc
../src/app/emu/video/ppu.cc
../src/app/emu/audio/dsp.cc
../src/app/emu/audio/spc700.cc
../src/app/gfx/bitmap.cc
../src/app/gfx/snes_tile.cc
../src/app/gfx/snes_palette.cc
../src/app/gfx/compression.cc
../src/app/core/common.cc
${ASAR_STATIC_SRC}
# ${ASAR_STATIC_SRC}
)
target_include_directories(
yaze_test PUBLIC
../src/
../src/lib/
../src/lib/asar/src/asar/
${SDL_INCLUDE_DIRS}
# ../src/lib/asar/src/asar/
${SDL2_INCLUDE_DIR}
${PNG_INCLUDE_DIRS}
)
target_link_libraries(
yaze_test
SDL2
${ABSL_TARGETS}
SDL2::SDL2
${PNG_LIBRARIES}
${GLEW_LIBRARIES}
${OPENGL_LIBRARIES}
${CMAKE_DL_LIBS}
asar-static
# asar-static
# snes_spc
ImGui
gmock_main
gmock
gtest_main

View File

@@ -1,67 +0,0 @@
#include <asar/interface-lib.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <array>
#include <cstdint>
#include <fstream>
#include <sstream>
#include <string>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "app/asm/script.h"
#include "app/core/constants.h"
#include "app/rom.h"
namespace yaze_test {
namespace asm_test {
using yaze::app::ROM;
using yaze::app::snes_asm::Script;
using ::testing::_;
using ::testing::ElementsAreArray;
using ::testing::Eq;
using ::testing::Return;
class MockScript : public Script {
public:
MOCK_METHOD(absl::Status, ApplyPatchToROM, (ROM & rom));
MOCK_METHOD(absl::Status, PatchOverworldMosaic,
(ROM & rom, char mosaic_tiles[yaze::app::core::kNumOverworldMaps],
int routine_offset, int hook_offset));
};
TEST(ASMTest, ApplyMosaicChangePatchOk) {
ROM rom;
MockScript script;
char mosaic_tiles[yaze::app::core::kNumOverworldMaps];
EXPECT_CALL(script, PatchOverworldMosaic(_, Eq(mosaic_tiles),
Eq(0x1301D0 + 0x138000), 0))
.WillOnce(Return(absl::OkStatus()));
EXPECT_CALL(script, ApplyPatchToROM(_)).WillOnce(Return(absl::OkStatus()));
EXPECT_THAT(
script.PatchOverworldMosaic(rom, mosaic_tiles, 0x1301D0 + 0x138000, 0),
absl::OkStatus());
EXPECT_THAT(script.ApplyPatchToROM(rom), absl::OkStatus());
}
TEST(ASMTest, NoPatchLoadedError) {
ROM rom;
MockScript script;
EXPECT_CALL(script, ApplyPatchToROM(_))
.WillOnce(Return(absl::InvalidArgumentError("No patch loaded!")));
EXPECT_THAT(script.ApplyPatchToROM(rom),
absl::InvalidArgumentError("No patch loaded!"));
}
} // namespace asm_test
} // namespace yaze_test

433
test/compression_test.cc Normal file
View File

@@ -0,0 +1,433 @@
#include "app/gfx/compression.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <array>
#include "absl/status/statusor.h"
#include "app/rom.h"
#define BUILD_HEADER(command, length) (command << 5) + (length - 1)
namespace yaze_test {
namespace gfx_test {
using yaze::app::ROM;
using yaze::app::gfx::lc_lz2::CompressionContext;
using yaze::app::gfx::lc_lz2::CompressionPiece;
using yaze::app::gfx::lc_lz2::CompressV2;
using yaze::app::gfx::lc_lz2::CompressV3;
using yaze::app::gfx::lc_lz2::DecompressV2;
using yaze::app::gfx::lc_lz2::kCommandByteFill;
using yaze::app::gfx::lc_lz2::kCommandDirectCopy;
using yaze::app::gfx::lc_lz2::kCommandIncreasingFill;
using yaze::app::gfx::lc_lz2::kCommandLongLength;
using yaze::app::gfx::lc_lz2::kCommandRepeatingBytes;
using yaze::app::gfx::lc_lz2::kCommandWordFill;
using ::testing::ElementsAre;
using ::testing::ElementsAreArray;
using ::testing::TypedEq;
namespace {
Bytes ExpectCompressOk(ROM& rom, uchar* in, int in_size) {
auto load_status = rom.LoadFromPointer(in, in_size);
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;
}
Bytes ExpectDecompressBytesOk(ROM& rom, Bytes& in) {
auto load_status = rom.LoadFromBytes(in);
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;
}
Bytes ExpectDecompressOk(ROM& rom, uchar* in, int in_size) {
auto load_status = rom.LoadFromPointer(in, in_size);
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, const 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<Bytes> 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));
}
Bytes ExpectCompressV3Ok(const std::vector<uint8_t>& uncompressed_data,
const std::vector<uint8_t>& expected_compressed_data) {
absl::StatusOr<Bytes> 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, CompressionDecompressionEmptyData) {
ROM rom;
uchar empty_input[0] = {};
auto comp_result = ExpectCompressOk(rom, empty_input, 0);
EXPECT_EQ(0, comp_result.size());
auto decomp_result = ExpectDecompressOk(rom, empty_input, 0);
EXPECT_EQ(0, decomp_result.size());
}
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;
auto new_piece = ExpectNewCompressionPieceOk(0x01, 0x01, "aaa", 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;
uchar single_set[5] = {0x2A, 0x2A, 0x2A, 0x2A, 0x2A};
uchar 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;
uchar single_word[6] = {0x2A, 0x01, 0x2A, 0x01, 0x2A, 0x01};
uchar 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;
uchar single_inc[3] = {0x01, 0x02, 0x03};
uchar 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;
uchar single_copy[4] = {0x03, 0x0A, 0x07, 0x14};
uchar 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;
// uchar buffer[3000];
// for (unsigned int i = 0; i < 3000; i += 2) {
// buffer[i] = 0x05;
// buffer[i + 1] = 0x06;
// }
// uchar 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;
uchar long_data[15] = {0x05, 0x06, 0x07, 0x08, 0x05, 0x06, 0x07, 0x08,
0x05, 0x06, 0x07, 0x08, 0x05, 0x06, 0x07};
uchar 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;
Bytes simple_copy_input = {BUILD_HEADER(0x00, 0x02), 0x2A, 0x45, 0xFF};
uchar 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;
uchar random1_i[11] = {BUILD_HEADER(0x01, 0x03),
0x2A,
BUILD_HEADER(0x00, 0x04),
0x01,
0x02,
0x03,
0x04,
BUILD_HEADER(0x02, 0x02),
0x0B,
0x16,
0xFF};
uchar 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 gfx_test
} // namespace yaze_test

View File

@@ -1,74 +0,0 @@
#include <asar/interface-lib.h>
#include <gmock/gmock.h>
#include <google/protobuf/repeated_field.h>
#include <gtest/gtest.h>
#include <array>
#include <cstdint>
#include <fstream>
#include <sstream>
#include <string>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "app/core/constants.h"
#include "app/delta/client.h"
#include "app/delta/service.h"
#include "app/rom.h"
#include "src/app/delta/delta.grpc.pb.h"
#include "src/app/delta/delta.pb.h"
namespace yaze_test {
namespace delta_test {
TEST(DeltaTest, InitRepoAndPushOk) {
yaze::app::delta::DeltaService service;
yaze::app::ROM rom;
Bytes test_bytes;
test_bytes.push_back(0x40);
EXPECT_TRUE(rom.LoadFromBytes(test_bytes).ok());
grpc::ServerContext* context;
InitRequest init_request;
auto repo = init_request.mutable_repo();
repo->set_project_name("test_repo");
Branch branch;
branch.set_branch_name("test_branch");
auto new_mutable_commits = branch.mutable_commits();
new_mutable_commits->Reserve(5);
for (int i = 0; i < 5; ++i) {
auto new_commit = new Commit();
new_commit->set_commit_id(i);
new_mutable_commits->Add();
new_mutable_commits->at(i) = *new_commit;
}
auto mutable_tree = repo->mutable_tree();
mutable_tree->Add();
mutable_tree->at(0) = branch;
InitResponse init_response;
auto init_status = service.Init(context, &init_request, &init_response);
EXPECT_TRUE(init_status.ok());
PushRequest request;
request.set_branch_name("test_branch");
request.set_repository_name("test_repo");
auto mutable_commits = request.mutable_commits();
mutable_commits->Reserve(5);
for (int i = 0; i < 5; ++i) {
auto new_commit = new Commit();
new_commit->set_commit_id(i * 2);
mutable_commits->Add();
mutable_commits->at(i) = *new_commit;
}
PushResponse reply;
auto status = service.Push(context, &request, &reply);
EXPECT_TRUE(status.ok());
auto repos = service.Repos();
auto result_branch = repos.at(0).tree();
std::cerr << result_branch.at(0).DebugString() << std::endl;
}
} // namespace delta_test
} // namespace yaze_test

4177
test/emu/cpu_test.cc Normal file

File diff suppressed because it is too large Load Diff

146
test/emu/ppu_test.cc Normal file
View File

@@ -0,0 +1,146 @@
#include "app/emu/video/ppu.h"
#include <gmock/gmock.h>
#include "app/emu/cpu/clock.h"
#include "app/emu/memory/memory.h"
#include "app/emu/memory/mock_memory.h"
namespace yaze {
namespace app {
namespace emu {
class MockPpu : public PpuInterface {
public:
MOCK_METHOD(void, Write, (uint16_t address, uint8_t data), (override));
MOCK_METHOD(uint8_t, Read, (uint16_t address), (const, override));
MOCK_METHOD(void, RenderFrame, (), (override));
MOCK_METHOD(void, RenderScanline, (), (override));
MOCK_METHOD(void, RenderBackground, (int layer), (override));
MOCK_METHOD(void, RenderSprites, (), (override));
MOCK_METHOD(void, Init, (), (override));
MOCK_METHOD(void, Reset, (), (override));
MOCK_METHOD(void, Update, (double deltaTime), (override));
MOCK_METHOD(void, UpdateClock, (double deltaTime), (override));
MOCK_METHOD(void, UpdateInternalState, (int cycles), (override));
MOCK_METHOD(const std::vector<uint8_t>&, GetFrameBuffer, (),
(const, override));
MOCK_METHOD(std::shared_ptr<gfx::Bitmap>, GetScreen, (), (const, override));
MOCK_METHOD(void, UpdateModeSettings, (), (override));
MOCK_METHOD(void, UpdateTileData, (), (override));
MOCK_METHOD(void, UpdateTileMapData, (), (override));
MOCK_METHOD(void, UpdatePaletteData, (), (override));
MOCK_METHOD(void, ApplyEffects, (), (override));
MOCK_METHOD(void, ComposeLayers, (), (override));
MOCK_METHOD(void, DisplayFrameBuffer, (), (override));
MOCK_METHOD(void, Notify, (uint32_t address, uint8_t data), (override));
std::vector<uint8_t> internalFrameBuffer;
std::vector<uint8_t> vram;
std::vector<SpriteAttributes> sprites;
std::vector<Tilemap> tilemaps;
BackgroundMode bgMode;
};
class PpuTest : public ::testing::Test {
protected:
MockMemory mock_memory;
MockClock mock_clock;
MockPpu mock_ppu;
PpuTest() {}
void SetUp() override {
ON_CALL(mock_ppu, Init()).WillByDefault([this]() {
mock_ppu.internalFrameBuffer.resize(256 * 240);
mock_ppu.vram.resize(0x10000);
});
ON_CALL(mock_ppu, Write(::testing::_, ::testing::_))
.WillByDefault([this](uint16_t address, uint8_t data) {
mock_ppu.vram[address] = data;
});
ON_CALL(mock_ppu, Read(::testing::_))
.WillByDefault(
[this](uint16_t address) { return mock_ppu.vram[address]; });
ON_CALL(mock_ppu, RenderScanline()).WillByDefault([this]() {
// Simulate scanline rendering logic...
});
ON_CALL(mock_ppu, GetFrameBuffer()).WillByDefault([this]() {
return mock_ppu.internalFrameBuffer;
});
// Additional ON_CALL setups as needed...
}
void TearDown() override {
// Common cleanup (if necessary)
}
const uint8_t testVRAMValue = 0xAB;
const uint16_t testVRAMAddress = 0x2000;
const std::vector<uint8_t> spriteData = {/* ... */};
const std::vector<uint8_t> bgData = {/* ... */};
const uint8_t testPaletteIndex = 3;
const uint16_t testTileIndex = 42;
};
// Test Initialization
TEST_F(PpuTest, InitializationSetsCorrectFrameBufferSize) {
// EXPECT_CALL(mock_ppu, Init()).Times(1);
// mock_ppu.Init();
// EXPECT_EQ(mock_ppu.GetFrameBuffer().size(), 256 * 240);
}
// Test State Reset
TEST_F(PpuTest, ResetClearsFrameBuffer) {
// EXPECT_CALL(mock_ppu, Reset()).Times(1);
// mock_ppu.Reset();
// auto frameBuffer = mock_ppu.GetFrameBuffer();
// EXPECT_TRUE(std::all_of(frameBuffer.begin(), frameBuffer.end(),
// [](uint8_t val) { return val == 0; }));
}
// Test Memory Interaction
TEST_F(PpuTest, ReadWriteVRAM) {
// uint16_t address = testVRAMAddress;
// uint8_t value = testVRAMValue;
// EXPECT_CALL(mock_ppu, Write(address, value)).Times(1);
// mock_ppu.Write(address, value);
// EXPECT_EQ(mock_ppu.Read(address), value);
}
// Test Rendering Mechanics
TEST_F(PpuTest, RenderScanlineUpdatesFrameBuffer) {
// Setup PPU with necessary background and sprite data
// Call RenderScanline and check if the framebuffer is updated correctly
}
// Test Mode and Register Handling
TEST_F(PpuTest, Mode0Rendering) {
// Set PPU to Mode0 and verify correct rendering behavior
}
// Test Interrupts and Counters
TEST_F(PpuTest, VBlankInterruptTriggered) {
// Simulate conditions for V-Blank and test if the interrupt is triggered
}
// Test Composite Rendering and Output
TEST_F(PpuTest, FrameComposition) {
// Setup various layers and sprites, call ComposeLayers, and verify the frame
// buffer
}
} // namespace emu
} // namespace app
} // namespace yaze

466
test/emu/spc700_test.cc Normal file
View File

@@ -0,0 +1,466 @@
#include "app/emu/audio/spc700.h"
#include <gmock/gmock-nice-strict.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace yaze {
namespace app {
namespace emu {
using testing::_;
using testing::Return;
class MockAudioRam : public AudioRam {
public:
MOCK_METHOD(void, reset, (), (override));
MOCK_METHOD(uint8_t, read, (uint16_t address), (const, override));
MOCK_METHOD(uint8_t&, mutable_read, (uint16_t address), (override));
MOCK_METHOD(void, write, (uint16_t address, uint8_t value), (override));
void SetupMemory(uint16_t address, const std::vector<uint8_t>& values) {
if (address > internal_audio_ram_.size()) {
internal_audio_ram_.resize(address + values.size());
}
int i = 0;
for (const auto& each : values) {
internal_audio_ram_[address + i] = each;
i++;
}
}
void SetUp() {
// internal_audio_ram_.resize(0x10000); // 64 K (0x10000)
// std::fill(internal_audio_ram_.begin(), internal_audio_ram_.end(), 0);
ON_CALL(*this, read(_)).WillByDefault([this](uint16_t address) {
return internal_audio_ram_[address];
});
ON_CALL(*this, mutable_read(_))
.WillByDefault([this](uint16_t address) -> uint8_t& {
return internal_audio_ram_[address];
});
ON_CALL(*this, write(_, _))
.WillByDefault([this](uint16_t address, uint8_t value) {
internal_audio_ram_[address] = value;
});
ON_CALL(*this, reset()).WillByDefault([this]() {
std::fill(internal_audio_ram_.begin(), internal_audio_ram_.end(), 0);
});
}
std::vector<uint8_t> internal_audio_ram_ = std::vector<uint8_t>(0x10000, 0);
};
class Spc700Test : public ::testing::Test {
public:
Spc700Test() = default;
void SetUp() override {
// Set up the mock
audioRAM.SetUp();
// Set the Spc700 to bank 01
spc700.PC = 0x0100;
}
testing::StrictMock<MockAudioRam> audioRAM;
Spc700 spc700{audioRAM};
};
// ========================================================
// 8-bit Move Memory to Register
TEST_F(Spc700Test, MOV_A_Immediate) {
// MOV A, imm
uint8_t opcode = 0xE8;
uint8_t immediate_value = 0x5A;
audioRAM.SetupMemory(0x0100, {opcode, immediate_value});
EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, immediate_value);
EXPECT_EQ(spc700.PSW.Z, 0);
EXPECT_EQ(spc700.PSW.N, 0);
}
TEST_F(Spc700Test, MOV_A_X) {
// MOV A, X
uint8_t opcode = 0x7D;
spc700.X = 0x5A;
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, spc700.X);
EXPECT_EQ(spc700.PSW.Z, 0);
EXPECT_EQ(spc700.PSW.N, 0);
}
TEST_F(Spc700Test, MOV_A_Y) {
// MOV A, Y
uint8_t opcode = 0xDD;
spc700.Y = 0x5A;
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, spc700.Y);
EXPECT_EQ(spc700.PSW.Z, 0);
EXPECT_EQ(spc700.PSW.N, 0);
}
TEST_F(Spc700Test, MOV_A_dp) {
// MOV A, dp
uint8_t opcode = 0xE4;
uint8_t dp_value = 0x5A;
audioRAM.SetupMemory(0x005A, {0x42});
audioRAM.SetupMemory(0x0100, {opcode, dp_value});
EXPECT_CALL(audioRAM, read(_))
.WillOnce(Return(dp_value))
.WillOnce(Return(0x42));
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, 0x42);
EXPECT_EQ(spc700.PSW.Z, 0);
EXPECT_EQ(spc700.PSW.N, 0);
}
TEST_F(Spc700Test, MOV_A_dp_plus_x) {
// MOV A, dp+X
uint8_t opcode = 0xF4;
uint8_t dp_value = 0x5A;
spc700.X = 0x01;
audioRAM.SetupMemory(0x005B, {0x42});
audioRAM.SetupMemory(0x0100, {opcode, dp_value});
EXPECT_CALL(audioRAM, read(_))
.WillOnce(Return(dp_value + spc700.X))
.WillOnce(Return(0x42));
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, 0x42);
EXPECT_EQ(spc700.PSW.Z, 0);
EXPECT_EQ(spc700.PSW.N, 0);
}
TEST_F(Spc700Test, MOV_A_dp_indirect_plus_y) {
// MOV A, [dp]+Y
uint8_t opcode = 0xF7;
uint8_t dp_value = 0x5A;
spc700.Y = 0x01;
audioRAM.SetupMemory(0x005A, {0x00, 0x42});
audioRAM.SetupMemory(0x0100, {opcode, dp_value});
audioRAM.SetupMemory(0x4201, {0x69});
EXPECT_CALL(audioRAM, read(_))
.WillOnce(Return(dp_value))
.WillOnce(Return(0x4200))
.WillOnce(Return(0x69));
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, 0x69);
EXPECT_EQ(spc700.PSW.Z, 0);
EXPECT_EQ(spc700.PSW.N, 0);
}
TEST_F(Spc700Test, MOV_A_dp_plus_x_indirect) {
// MOV A, [dp+X]
uint8_t opcode = 0xE7;
uint8_t dp_value = 0x5A;
spc700.X = 0x01;
audioRAM.SetupMemory(0x005B, {0x00, 0x42});
audioRAM.SetupMemory(0x0100, {opcode, dp_value});
audioRAM.SetupMemory(0x4200, {0x69});
EXPECT_CALL(audioRAM, read(_))
.WillOnce(Return(dp_value + 1))
.WillOnce(Return(0x4200))
.WillOnce(Return(0x69));
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, 0x69);
EXPECT_EQ(spc700.PSW.Z, 0);
EXPECT_EQ(spc700.PSW.N, 0);
}
TEST_F(Spc700Test, MOV_A_abs) {
// MOV A, !abs
uint8_t opcode = 0xE5;
uint16_t abs_addr = 0x1234;
uint8_t abs_value = 0x5A;
EXPECT_CALL(audioRAM, read(_))
.WillOnce(Return(abs_addr & 0xFF)) // Low byte
.WillOnce(Return(abs_addr >> 8)); // High byte
EXPECT_CALL(audioRAM, read(abs_addr)).WillOnce(Return(abs_value));
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, abs_value);
EXPECT_EQ(spc700.PSW.Z, 0);
EXPECT_EQ(spc700.PSW.N, 0);
}
// ============================================================================
// 8-bit Move Register to Memory
TEST_F(Spc700Test, MOV_Immediate) {
// MOV A, imm
uint8_t opcode = 0xE8;
uint8_t immediate_value = 0x5A;
EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, immediate_value);
EXPECT_EQ(spc700.PSW.Z, 0);
EXPECT_EQ(spc700.PSW.N, 0);
}
// ============================================================================
TEST_F(Spc700Test, NOP_DoesNothing) {
// NOP opcode
uint8_t opcode = 0x00;
uint16_t initialPC = spc700.PC;
spc700.ExecuteInstructions(opcode);
// PC should increment by 1, no other changes
EXPECT_EQ(spc700.PC, initialPC + 1);
// Add checks for other registers if needed
}
TEST_F(Spc700Test, ADC_A_Immediate) {
// ADC A, #imm
uint8_t opcode = 0x88;
uint8_t immediate_value = 0x10;
spc700.A = 0x01;
spc700.PSW.C = 1; // Assume carry is set
EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
spc700.ExecuteInstructions(opcode);
// Verify A, and flags
EXPECT_EQ(spc700.A, 0x12); // 0x01 + 0x10 + 1 (carry)
// Check for other flags (Z, C, etc.) based on the result
}
TEST_F(Spc700Test, BEQ_BranchesIfZeroFlagSet) {
// BEQ rel
uint8_t opcode = 0xF0;
int8_t offset = 0x05;
spc700.PSW.Z = 1; // Set Zero flag
EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(offset));
uint16_t initialPC = spc700.PC + 1;
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.PC, initialPC + offset);
}
TEST_F(Spc700Test, STA_Absolute) {
// STA !abs
uint8_t opcode = 0x85;
uint16_t abs_addr = 0x1234;
spc700.A = 0x80;
// Set up the mock to return the address for the absolute addressing
EXPECT_CALL(audioRAM, read(_))
.WillOnce(Return(abs_addr & 0xFF)) // Low byte
.WillOnce(Return(abs_addr >> 8)); // High byte
spc700.ExecuteInstructions(opcode);
}
TEST_F(Spc700Test, ExecuteADCWithImmediate) {
// ADC A, imm
uint8_t opcode = 0x88; // Replace with opcode for ADC A, imm
uint8_t immediate_value = 0x10;
spc700.A = 0x15;
EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, 0x25); // 0x15 + 0x10
EXPECT_EQ(spc700.PSW.Z, 0);
EXPECT_EQ(spc700.PSW.N, 0);
EXPECT_EQ(spc700.PSW.C, 0);
}
TEST_F(Spc700Test, ExecuteBRA) {
// BRA
uint8_t opcode = 0x2F;
int8_t offset = 0x05;
EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(offset));
// rel() moves the PC forward one after read
uint16_t initialPC = spc700.PC + 1;
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.PC, initialPC + offset);
}
TEST_F(Spc700Test, ReadFromAudioRAM) {
uint16_t address = 0x1234;
uint8_t expected_value = 0x5A;
EXPECT_CALL(audioRAM, read(address)).WillOnce(Return(expected_value));
uint8_t value = spc700.read(address);
EXPECT_EQ(value, expected_value);
}
TEST_F(Spc700Test, WriteToAudioRAM) {
uint16_t address = 0x1234;
uint8_t value = 0x5A;
EXPECT_CALL(audioRAM, write(address, value));
spc700.write(address, value);
}
TEST_F(Spc700Test, ExecuteANDWithImmediate) {
// AND A, imm
uint8_t opcode = 0x28;
uint8_t immediate_value = 0x0F;
spc700.A = 0x5A; // 0101 1010
EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, 0x0A); // 0101 1010 & 0000 1111 = 0000 1010
EXPECT_EQ(spc700.PSW.Z, 0);
EXPECT_EQ(spc700.PSW.N, 0);
}
TEST_F(Spc700Test, ExecuteORWithImmediate) {
// OR A, imm
uint8_t opcode = 0x08;
uint8_t immediate_value = 0x0F;
spc700.A = 0xA0; // 1010 0000
EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, 0xAF); // 1010 0000 | 0000 1111 = 1010 1111
EXPECT_EQ(spc700.PSW.Z, 0);
// EXPECT_EQ(spc700.PSW.N, 1);
}
TEST_F(Spc700Test, ExecuteEORWithImmediate) {
// EOR A, imm
uint8_t opcode = 0x48;
uint8_t immediate_value = 0x5A;
spc700.A = 0x5A; // 0101 1010
EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, 0x00); // 0101 1010 ^ 0101 1010 = 0000 0000
EXPECT_EQ(spc700.PSW.Z, 1);
EXPECT_EQ(spc700.PSW.N, 0);
}
TEST_F(Spc700Test, ExecuteINC) {
// INC A
uint8_t opcode = 0xBC;
spc700.A = 0xFF;
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, 0x00);
EXPECT_EQ(spc700.PSW.Z, 1);
EXPECT_EQ(spc700.PSW.N, 0);
}
TEST_F(Spc700Test, ExecuteDEC) {
// DEC A
uint8_t opcode = 0x9C;
spc700.A = 0x01;
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.A, 0x00);
EXPECT_EQ(spc700.PSW.Z, 1);
EXPECT_EQ(spc700.PSW.N, 0);
}
TEST_F(Spc700Test, ExecuteBNEWhenNotEqual) {
// BNE
uint8_t opcode = 0xD0;
int8_t offset = 0x05;
spc700.PSW.Z = 0;
EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(offset));
uint16_t initialPC = spc700.PC + 1;
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.PC, initialPC + offset);
}
TEST_F(Spc700Test, ExecuteBNEWhenEqual) {
// BNE
uint8_t opcode = 0xD0;
int8_t offset = 0x05;
spc700.PSW.Z = 1;
EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(offset));
uint16_t initialPC = spc700.PC;
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.PC, initialPC + 1); // +1 because of reading the offset
}
TEST_F(Spc700Test, ExecuteBEQWhenEqual) {
// BEQ
uint8_t opcode = 0xF0;
int8_t offset = 0x05;
spc700.PSW.Z = 1;
EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(offset));
uint16_t initialPC = spc700.PC + 1;
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.PC, initialPC + offset);
}
TEST_F(Spc700Test, ExecuteBEQWhenNotEqual) {
// BEQ
uint8_t opcode = 0xF0;
int8_t offset = 0x05;
spc700.PSW.Z = 0;
EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(offset));
uint16_t initialPC = spc700.PC;
spc700.ExecuteInstructions(opcode);
EXPECT_EQ(spc700.PC, initialPC + 1); // +1 because of reading the offset
}
TEST_F(Spc700Test, BootIplRomOk) {
// Boot the IPL ROM
// spc700.BootIplRom();
// EXPECT_EQ(spc700.PC, 0xFFC1 + 0x3F);
}
} // namespace emu
} // namespace app
} // namespace yaze

View File

@@ -1,315 +0,0 @@
#include "app/rom.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <array>
#include "absl/status/statusor.h"
#define BUILD_HEADER(command, length) (command << 5) + (length - 1)
namespace yaze_test {
namespace rom_test {
using yaze::app::CompressionPiece;
using yaze::app::ROM;
using ::testing::ElementsAreArray;
using ::testing::TypedEq;
namespace {
Bytes ExpectCompressOk(ROM& rom, uchar* in, int in_size) {
auto load_status = rom.LoadFromPointer(in, in_size);
EXPECT_TRUE(load_status.ok());
auto compression_status = rom.Compress(0, in_size);
EXPECT_TRUE(compression_status.ok());
auto compressed_bytes = std::move(*compression_status);
return compressed_bytes;
}
Bytes ExpectDecompressBytesOk(ROM& rom, Bytes& in) {
auto load_status = rom.LoadFromBytes(in);
EXPECT_TRUE(load_status.ok());
auto decompression_status = rom.Decompress(0, in.size());
EXPECT_TRUE(decompression_status.ok());
auto decompressed_bytes = std::move(*decompression_status);
return decompressed_bytes;
}
Bytes ExpectDecompressOk(ROM& rom, uchar* in, int in_size) {
auto load_status = rom.LoadFromPointer(in, in_size);
EXPECT_TRUE(load_status.ok());
auto decompression_status = rom.Decompress(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, const 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;
}
} // namespace
TEST(ROMTest, 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;
auto new_piece = ExpectNewCompressionPieceOk(0x01, 0x01, "aaa", 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]);
}
}
TEST(ROMTest, DecompressionValidCommand) {
ROM rom;
Bytes simple_copy_input = {BUILD_HEADER(0x00, 0x02), 0x2A, 0x45, 0xFF};
uchar 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(ROMTest, DecompressionMixingCommand) {
ROM rom;
uchar random1_i[11] = {BUILD_HEADER(0x01, 0x03),
0x2A,
BUILD_HEADER(0x00, 0x04),
0x01,
0x02,
0x03,
0x04,
BUILD_HEADER(0x02, 0x02),
0x0B,
0x16,
0xFF};
uchar 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));
}
TEST(ROMTest, CompressionSingleSet) {
ROM rom;
uchar single_set[5] = {0x2A, 0x2A, 0x2A, 0x2A, 0x2A};
uchar 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(ROMTest, CompressionSingleWord) {
ROM rom;
uchar single_word[6] = {0x2A, 0x01, 0x2A, 0x01, 0x2A, 0x01};
uchar 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(ROMTest, CompressionSingleIncrement) {
ROM rom;
uchar single_inc[3] = {0x01, 0x02, 0x03};
uchar 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(ROMTest, CompressionSingleCopy) {
ROM rom;
uchar single_copy[4] = {0x03, 0x0A, 0x07, 0x14};
uchar 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));
}
/* Hiding tests until I figure out a better PR to address the bug
TEST(ROMTest, CompressionSingleCopyRepeat) {
ROM rom;
uchar single_copy_repeat[8] = {0x03, 0x0A, 0x07, 0x14, 0x03, 10, 0x07, 0x14};
uchar single_copy_repeat_expected[9] = {
BUILD_HEADER(0x00, 0x04), 0x03, 0x0A, 0x07, 0x14,
BUILD_HEADER(0x04, 0x04), 0x00, 0x00, 0xFF};
auto comp_result = ExpectCompressOk(rom, single_copy_repeat, 8);
EXPECT_THAT(single_copy_repeat_expected,
ElementsAreArray(comp_result.data(), 9));
}
TEST(ROMTest, CompressionSingleOverflowIncrement) {
ROM rom;
uchar overflow_inc[4] = {0xFE, 0xFF, 0x00, 0x01};
uchar overflow_inc_expected[3] = {BUILD_HEADER(0x03, 0x04), 0xFE, 0xFF};
auto comp_result = ExpectCompressOk(rom, overflow_inc, 4);
EXPECT_THAT(overflow_inc_expected, ElementsAreArray(comp_result.data(), 3));
}
TEST(ROMTest, CompressionMixedRepeatIncrement) {
ROM rom;
uchar to_compress_string[28] = {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};
uchar repeat_and_inc_copy_expected[7] = {BUILD_HEADER(0x01, 0x04),
0x05,
BUILD_HEADER(0x03, 0x06),
0x06,
BUILD_HEADER(0x00, 0x01),
0x05,
0xFF};
// Mixing, repeat, inc, trailing copy
auto comp_result = ExpectCompressOk(rom, to_compress_string, 28);
EXPECT_THAT(repeat_and_inc_copy_expected,
ElementsAreArray(comp_result.data(), 7));
}
*/
TEST(ROMTest, CompressionMixedIncrementIntraCopyOffset) {
ROM rom;
uchar to_compress_string[] = {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};
uchar inc_word_intra_copy_expected[] = {BUILD_HEADER(0x03, 0x07),
0x05,
BUILD_HEADER(0x02, 0x06),
0x05,
0x02,
BUILD_HEADER(0x04, 0x08),
0x05,
0x00,
0xFF};
// "Mixing, inc, alternate, intra copy"
// compress start: 3, length: 21
// compressed length: 9
auto comp_result = ExpectCompressOk(rom, to_compress_string + 3, 21);
EXPECT_THAT(inc_word_intra_copy_expected,
ElementsAreArray(comp_result.data(), 9));
}
TEST(ROMTest, CompressionMixedIncrementIntraCopySource) {
ROM rom;
uchar to_compress_string[] = {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};
uchar all_expected[] = {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};
// "Mixing, inc, alternate, intra copy"
// 0, 28
// 16
auto comp_result = ExpectCompressOk(rom, to_compress_string, 28);
EXPECT_THAT(all_expected, ElementsAreArray(comp_result.data(), 16));
}
TEST(ROMTest, LengthBorderCompression) {
ROM rom;
uchar buffer[3000];
for (unsigned int i = 0; i < 3000; i++) buffer[i] = 0x05;
uchar extended_lenght_expected_42[] = {0b11100100, 0x29, 0x05, 0xFF};
uchar extended_lenght_expected_400[] = {0b11100101, 0x8F, 0x05, 0xFF};
uchar extended_lenght_expected_1050[] = {
0b11100111, 0xFF, 0x05, BUILD_HEADER(0x01, 0x1A), 0x05, 0xFF};
uchar extended_lenght_expected_2050[] = {
0b11100111, 0xFF, 0x05, 0b11100111, 0xFF, 0x05, BUILD_HEADER(0x01, 0x02),
0x05, 0xFF};
// "Extended lenght, 42 repeat of 5"
auto comp_result = ExpectCompressOk(rom, buffer, 42);
EXPECT_THAT(extended_lenght_expected_42,
ElementsAreArray(comp_result.data(), 4));
// "Extended lenght, 400 repeat of 5"
comp_result = ExpectCompressOk(rom, buffer, 400);
EXPECT_THAT(extended_lenght_expected_400,
ElementsAreArray(comp_result.data(), 4));
// "Extended lenght, 1050 repeat of 5"
comp_result = ExpectCompressOk(rom, buffer, 1050);
EXPECT_THAT(extended_lenght_expected_1050,
ElementsAreArray(comp_result.data(), 6));
// "Extended lenght, 2050 repeat of 5"
comp_result = ExpectCompressOk(rom, buffer, 2050);
EXPECT_THAT(extended_lenght_expected_2050,
ElementsAreArray(comp_result.data(), 9));
}
TEST(ROMTest, CompressionExtendedWordCopy) {
ROM rom;
uchar buffer[3000];
for (unsigned int i = 0; i < 3000; i += 2) {
buffer[i] = 0x05;
buffer[i + 1] = 0x06;
}
uchar hightlenght_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(hightlenght_word_1050, ElementsAreArray(comp_result.data(), 8));
}
/* Extended Header Command is currently unimplemented
TEST(ROMTest, ExtendedHeaderDecompress) {
ROM rom;
Bytes extendedcmd_i = {0b11100100, 0x8F, 0x2A, 0xFF};
uchar extendedcmd_o[50];
for (int i = 0; i < 50; ++i) {
extendedcmd_o[i] = 0x2A;
}
auto decomp_result = ExpectDecompressBytesOk(rom, extendedcmd_i);
ASSERT_THAT(extendedcmd_o, ElementsAreArray(decomp_result.data(), 50));
}
TEST(ROMTest, ExtendedHeaderDecompress2) {
ROM rom;
Bytes extendedcmd_i = {0b11100101, 0x8F, 0x2A, 0xFF};
uchar extendedcmd_o[50];
for (int i = 0; i < 50; i++) {
extendedcmd_o[i] = 0x2A;
}
auto data = ExpectDecompressBytesOk(rom, extendedcmd_i);
for (int i = 0; i < 50; i++) {
ASSERT_EQ(extendedcmd_o[i], data[i]);
}
}
*/
} // namespace rom_test
} // namespace yaze_test

25
test/room_object_test.cc Normal file
View File

@@ -0,0 +1,25 @@
#include "app/zelda3/dungeon/room_object.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "app/emu/cpu/cpu.h"
#include "app/emu/memory/memory.h"
#include "app/emu/memory/mock_memory.h"
#include "app/emu/video/ppu.h"
#include "app/gfx/bitmap.h"
#include "app/rom.h"
namespace yaze {
namespace test {
TEST(DungeonObjectTest, RenderObjectsAsBitmaps) {
app::ROM rom;
// rom.LoadFromFile("/Users/scawful/Code/yaze/build/bin/zelda3.sfc"));
// EXPECT_EQ(rom_status, absl::Status::ok());
app::zelda3::dungeon::DungeonObjectRenderer renderer;
}
} // namespace test
} // namespace yaze

106
test/snes_palette_test.cc Normal file
View File

@@ -0,0 +1,106 @@
#include "app/gfx/snes_palette.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace yaze_test {
namespace gfx_test {
using ::testing::ElementsAreArray;
using yaze::app::gfx::ConvertRGBtoSNES;
using yaze::app::gfx::ConvertSNEStoRGB;
using yaze::app::gfx::Extract;
using yaze::app::gfx::snes_color;
using yaze::app::gfx::snes_palette;
using yaze::app::gfx::SNESPalette;
namespace {
unsigned int test_convert(yaze::app::gfx::snes_color col) {
unsigned int toret;
toret = col.red << 16;
toret += col.green << 8;
toret += col.blue;
return toret;
}
} // namespace
TEST(SNESPaletteTest, AddColor) {
yaze::app::gfx::SNESPalette palette;
yaze::app::gfx::SNESColor color;
palette.AddColor(color);
ASSERT_EQ(palette.size(), 1);
}
TEST(SNESPaletteTest, GetColorOutOfBounds) {
yaze::app::gfx::SNESPalette palette;
std::vector<yaze::app::gfx::SNESColor> colors(5);
palette.Create(colors);
// Now try to get a color at an out-of-bounds index
ASSERT_THROW(palette.GetColor(10), std::exception);
ASSERT_THROW(palette[10], std::exception);
}
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 = Convert(pal);
EXPECT_EQ(10, snes_string.size());
EXPECT_THAT(data, ElementsAreArray(snes_string.data(), 10));
}
} // namespace gfx_test
} // namespace yaze_test