Move test dir to root

This commit is contained in:
scawful
2025-01-19 19:09:39 -05:00
parent de75cc6850
commit e4cc3b977a
18 changed files with 25 additions and 16 deletions

65
test/CMakeLists.txt Normal file
View File

@@ -0,0 +1,65 @@
set(YAZE_SRC_FILES "")
foreach (file
app/rom.cc
app/core/common.cc
${YAZE_APP_CORE_SRC}
${YAZE_APP_EMU_SRC}
${YAZE_APP_GFX_SRC}
${YAZE_APP_ZELDA3_SRC}
${YAZE_APP_EDITOR_SRC}
${YAZE_UTIL_SRC}
${YAZE_GUI_SRC})
list(APPEND YAZE_SRC_FILES ${CMAKE_SOURCE_DIR}/src/${file})
endforeach()
add_executable(
yaze_test
yaze_test.cc
rom_test.cc
gfx/compression_test.cc
gfx/snes_palette_test.cc
integration/test_editor.cc
zelda3/overworld_test.cc
zelda3/sprite_builder_test.cc
${ASAR_STATIC_SRC}
${IMGUI_SRC}
${IMGUI_TEST_ENGINE_SOURCES}
${YAZE_SRC_FILES}
)
target_include_directories(
yaze_test PUBLIC
app/
lib/
${CMAKE_SOURCE_DIR}/incl/
${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/src/lib/imgui_test_engine
${ASAR_INCLUDE_DIR}
${SDL2_INCLUDE_DIR}
${PNG_INCLUDE_DIRS}
${PROJECT_BINARY_DIR}
)
target_link_libraries(
yaze_test
SDL2::SDL2
asar-static
${ABSL_TARGETS}
${PNG_LIBRARIES}
${OPENGL_LIBRARIES}
${CMAKE_DL_LIBS}
yaze_c
ImGuiTestEngine
ImGui
gmock_main
gmock
gtest_main
gtest
)
target_compile_definitions(yaze_test PRIVATE "linux")
target_compile_definitions(yaze_test PRIVATE "stricmp=strcasecmp")
target_compile_definitions(yaze_test PRIVATE "IMGUI_ENABLE_TEST_ENGINE")
include(GoogleTest)
gtest_discover_tests(yaze_test)

48
test/core/testing.h Normal file
View File

@@ -0,0 +1,48 @@
#ifndef YAZE_TEST_CORE_TESTING_H
#define YAZE_TEST_CORE_TESTING_H
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#define EXPECT_OK(expr) EXPECT_EQ((expr), absl::OkStatus())
#define ASSERT_OK(expr) ASSERT_EQ((expr), absl::OkStatus())
#define ASSERT_OK_AND_ASSIGN(lhs, rexpr) \
if (auto rexpr_value = (rexpr); rexpr_value.ok()) { \
lhs = std::move(rexpr_value).value(); \
} else { \
FAIL() << "error: " << rexpr_value.status(); \
}
namespace yaze {
namespace test {
// StatusIs is a matcher that matches a status that has the same code and
// message as the expected status.
MATCHER_P(StatusIs, status, "") { return arg.code() == status; }
// Support for testing absl::StatusOr.
template <typename T>
::testing::AssertionResult IsOkAndHolds(const absl::StatusOr<T>& status_or,
const T& value) {
if (!status_or.ok()) {
return ::testing::AssertionFailure()
<< "Expected status to be OK, but got: " << status_or.status();
}
if (status_or.value() != value) {
return ::testing::AssertionFailure() << "Expected value to be " << value
<< ", but got: " << status_or.value();
}
return ::testing::AssertionSuccess();
}
MATCHER_P(IsOkAndHolds, value, "") { return IsOkAndHolds(arg, value); }
} // namespace test
} // namespace yaze
#endif // YAZE_TEST_CORE_TESTING_H

4199
test/emu/cpu_test.cc Normal file

File diff suppressed because it is too large Load Diff

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

@@ -0,0 +1,56 @@
#include "app/emu/video/ppu.h"
#include <gmock/gmock.h>
#include "test/mocks/mock_memory.h"
namespace yaze {
namespace test {
using yaze::emu::MockClock;
using yaze::emu::MockMemory;
using yaze::emu::BackgroundMode;
using yaze::emu::PpuInterface;
using yaze::emu::SpriteAttributes;
using yaze::emu::Tilemap;
/**
* @brief Mock Ppu class for testing
*/
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));
std::vector<uint8_t> internalFrameBuffer;
std::vector<uint8_t> vram;
std::vector<SpriteAttributes> sprites;
std::vector<Tilemap> tilemaps;
BackgroundMode bgMode;
};
/**
* \test Test fixture for PPU unit tests
*/
class PpuTest : public ::testing::Test {
protected:
MockMemory mock_memory;
MockClock mock_clock;
MockPpu mock_ppu;
PpuTest() {}
void SetUp() override {
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]; });
}
};
} // namespace test
} // namespace yaze

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

@@ -0,0 +1,474 @@
#include "app/emu/audio/spc700.h"
#include <gmock/gmock-nice-strict.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace yaze {
namespace test {
using testing::_;
using testing::Return;
using yaze::emu::ApuCallbacks;
using yaze::emu::AudioRam;
using yaze::emu::Spc700;
/**
* @brief MockAudioRam is a mock class for the AudioRam class.
*/
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);
};
/**
* \test Spc700Test is a test fixture for the Spc700 class.
*/
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;
ApuCallbacks callbacks_;
Spc700 spc700{callbacks_};
};
// ========================================================
// 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 test
} // namespace yaze

View File

@@ -0,0 +1,430 @@
#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 {
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, uchar* 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, uchar* 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;
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;
std::vector<uint8_t> 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 test
} // namespace yaze

View File

@@ -0,0 +1,95 @@
#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
TEST(SnesPaletteTest, AddColor) {
yaze::gfx::SnesPalette palette;
yaze::gfx::SnesColor color;
palette.AddColor(color);
ASSERT_EQ(palette.size(), 1);
}
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));
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,100 @@
#include "test/integration/test_editor.h"
#include <SDL.h>
#include "app/core/controller.h"
#include "app/core/platform/renderer.h"
#include "app/gui/style.h"
#include "imgui/backends/imgui_impl_sdl2.h"
#include "imgui/backends/imgui_impl_sdlrenderer2.h"
#include "imgui/imgui.h"
#include "imgui_test_engine/imgui_te_context.h"
#include "imgui_test_engine/imgui_te_engine.h"
#include "imgui_test_engine/imgui_te_imconfig.h"
#include "imgui_test_engine/imgui_te_ui.h"
namespace yaze {
namespace test {
namespace integration {
absl::Status TestEditor::Update() {
ImGui::NewFrame();
if (ImGui::Begin("My Window")) {
ImGui::Text("Hello, world!");
ImGui::Button("My Button");
}
static bool show_demo_window = true;
ImGuiTestEngine_ShowTestEngineWindows(engine_, &show_demo_window);
ImGui::End();
return absl::OkStatus();
}
void TestEditor::RegisterTests(ImGuiTestEngine* engine) {
engine_ = engine;
ImGuiTest* test = IM_REGISTER_TEST(engine, "demo_test", "test1");
test->TestFunc = [](ImGuiTestContext* ctx) {
ctx->SetRef("My Window");
ctx->ItemClick("My Button");
ctx->ItemCheck("Node/Checkbox");
};
}
int RunIntegrationTest() {
yaze::test::integration::TestEditor test_editor;
yaze::core::Controller controller;
controller.init_test_editor(&test_editor);
if (!controller.CreateWindow().ok()) {
return EXIT_FAILURE;
}
if (!controller.CreateRenderer().ok()) {
return EXIT_FAILURE;
}
IMGUI_CHECKVERSION();
ImGui::CreateContext();
// Initialize Test Engine
ImGuiTestEngine* engine = ImGuiTestEngine_CreateContext();
ImGuiTestEngineIO& test_io = ImGuiTestEngine_GetIO(engine);
test_io.ConfigVerboseLevel = ImGuiTestVerboseLevel_Info;
test_io.ConfigVerboseLevelOnError = ImGuiTestVerboseLevel_Debug;
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
// Initialize ImGui for SDL
ImGui_ImplSDL2_InitForSDLRenderer(
controller.window(), yaze::core::Renderer::GetInstance().renderer());
ImGui_ImplSDLRenderer2_Init(yaze::core::Renderer::GetInstance().renderer());
test_editor.RegisterTests(engine);
ImGuiTestEngine_Start(engine, ImGui::GetCurrentContext());
controller.set_active(true);
// Set the default style
yaze::gui::ColorsYaze();
// Build a new ImGui frame
ImGui_ImplSDLRenderer2_NewFrame();
ImGui_ImplSDL2_NewFrame();
while (controller.IsActive()) {
controller.OnInput();
if (const auto status = controller.OnTestLoad(); !status.ok()) {
return EXIT_FAILURE;
}
controller.DoRender();
}
ImGuiTestEngine_Stop(engine);
controller.OnExit();
return EXIT_SUCCESS;
}
} // namespace integration
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,53 @@
#ifndef YAZE_TEST_INTEGRATION_TEST_EDITOR_H
#define YAZE_TEST_INTEGRATION_TEST_EDITOR_H
#include "app/editor/editor.h"
#include "imgui/imgui.h"
#include "imgui_test_engine/imgui_te_context.h"
#include "imgui_test_engine/imgui_te_engine.h"
namespace yaze {
namespace test {
namespace integration {
class TestEditor : public yaze::editor::Editor {
public:
TestEditor() = default;
~TestEditor() = default;
absl::Status Cut() override {
return absl::UnimplementedError("Not implemented");
}
absl::Status Copy() override {
return absl::UnimplementedError("Not implemented");
}
absl::Status Paste() override {
return absl::UnimplementedError("Not implemented");
}
absl::Status Undo() override {
return absl::UnimplementedError("Not implemented");
}
absl::Status Redo() override {
return absl::UnimplementedError("Not implemented");
}
absl::Status Find() override {
return absl::UnimplementedError("Not implemented");
}
absl::Status Update() override;
void RegisterTests(ImGuiTestEngine* engine);
private:
ImGuiTestEngine* engine_;
};
int RunIntegrationTest();
} // namespace integration
} // namespace test
} // namespace yaze
#endif // YAZE_TEST_INTEGRATION_TEST_EDITOR_H

211
test/mocks/mock_memory.h Normal file
View File

@@ -0,0 +1,211 @@
#ifndef YAZE_TEST_MOCK_MOCK_MEMORY_H
#define YAZE_TEST_MOCK_MOCK_MEMORY_H
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "app/emu/memory/memory.h"
namespace yaze {
namespace emu {
/**
* @class MockMemory
* @brief A mock implementation of the Memory class.
*
* This class is used for testing purposes and provides a mock implementation of
* the Memory class. It allows for reading and writing bytes, words, and longs
* to memory, as well as pushing and popping values onto and from the stack. It
* also provides methods for setting and getting the stack pointer, initializing
* memory with ROM data, and clearing memory.
*
* The mock memory is represented by a vector of uint8_t values, where each
* element represents a byte of memory. The memory can be accessed using the []
* operator, and its contents can be set using the SetMemoryContents() method.
*
* @note This class is intended for testing purposes only and should not be used
* in production code.
*/
class MockMemory : public Memory {
public:
MOCK_CONST_METHOD1(ReadByte, uint8_t(uint32_t address));
MOCK_CONST_METHOD1(ReadWord, uint16_t(uint32_t address));
MOCK_CONST_METHOD1(ReadWordLong, uint32_t(uint32_t address));
MOCK_METHOD(std::vector<uint8_t>, ReadByteVector,
(uint32_t address, uint16_t length), (const, override));
MOCK_METHOD2(WriteByte, void(uint32_t address, uint8_t value));
MOCK_METHOD2(WriteWord, void(uint32_t address, uint16_t value));
MOCK_METHOD2(WriteLong, void(uint32_t address, uint32_t value));
MOCK_METHOD1(PushByte, void(uint8_t value));
MOCK_METHOD0(PopByte, uint8_t());
MOCK_METHOD1(PushWord, void(uint16_t value));
MOCK_METHOD0(PopWord, uint16_t());
MOCK_METHOD1(PushLong, void(uint32_t value));
MOCK_METHOD0(PopLong, uint32_t());
MOCK_CONST_METHOD0(SP, uint16_t());
MOCK_METHOD1(SetSP, void(uint16_t value));
MOCK_METHOD1(SetMemory, void(const std::vector<uint8_t>& data));
MOCK_METHOD1(LoadData, void(const std::vector<uint8_t>& data));
MOCK_METHOD0(ClearMemory, void());
MOCK_CONST_METHOD1(at, uint8_t(int i));
uint8_t operator[](int i) const override { return memory_[i]; }
MOCK_METHOD0(init_hdma_request, void());
MOCK_METHOD0(run_hdma_request, void());
MOCK_METHOD1(set_hdma_run_requested, void(bool value));
MOCK_METHOD1(set_hdma_init_requested, void(bool value));
MOCK_CONST_METHOD0(hdma_init_requested, bool());
MOCK_CONST_METHOD0(hdma_run_requested, bool());
MOCK_METHOD1(set_pal_timing, void(bool value));
MOCK_CONST_METHOD0(pal_timing, bool());
MOCK_CONST_METHOD0(h_pos, uint16_t());
MOCK_CONST_METHOD0(v_pos, uint16_t());
MOCK_METHOD1(set_h_pos, void(uint16_t value));
MOCK_METHOD1(set_v_pos, void(uint16_t value));
MOCK_METHOD1(set_open_bus, void(uint8_t value));
MOCK_CONST_METHOD0(open_bus, uint8_t());
void SetMemoryContents(const std::vector<uint8_t>& data) {
if (data.size() > memory_.size()) {
memory_.resize(data.size());
}
std::copy(data.begin(), data.end(), memory_.begin());
}
void SetMemoryContents(const std::vector<uint16_t>& data) {
if (data.size() > memory_.size()) {
memory_.resize(data.size());
}
int i = 0;
for (const auto& each : data) {
memory_[i] = each & 0xFF;
memory_[i + 1] = (each >> 8) & 0xFF;
i += 2;
}
}
void InsertMemory(const uint64_t address, const std::vector<uint8_t>& data) {
if (address > memory_.size()) {
memory_.resize(address + data.size());
}
int i = 0;
for (const auto& each : data) {
memory_[address + i] = each;
i++;
}
}
// 16MB = 0x1000000
// 02MB = 0x200000
void Initialize(const std::vector<uint8_t>& romData) {
// 16 MB, simplifying the memory layout for testing
memory_.resize(0x1000000);
// Clear memory
std::fill(memory_.begin(), memory_.end(), 0);
// Load ROM data into mock memory
size_t romSize = romData.size();
size_t romAddress = 0;
const size_t ROM_CHUNK_SIZE = 0x8000; // 32 KB
for (size_t bank = 0x00; bank <= 0xBF; bank += 0x80) {
for (size_t offset = 0x8000; offset <= 0xFFFF; offset += ROM_CHUNK_SIZE) {
if (romAddress < romSize) {
std::copy(romData.begin() + romAddress,
romData.begin() + romAddress + ROM_CHUNK_SIZE,
memory_.begin() + (bank << 16) + offset);
romAddress += ROM_CHUNK_SIZE;
}
}
}
}
void Init() {
ON_CALL(*this, ReadByte(::testing::_))
.WillByDefault(
[this](uint32_t address) { return memory_.at(address); });
ON_CALL(*this, ReadWord(::testing::_))
.WillByDefault([this](uint32_t address) {
return static_cast<uint16_t>(memory_.at(address)) |
(static_cast<uint16_t>(memory_.at(address + 1)) << 8);
});
ON_CALL(*this, ReadWordLong(::testing::_))
.WillByDefault([this](uint32_t address) {
return static_cast<uint32_t>(memory_.at(address)) |
(static_cast<uint32_t>(memory_.at(address + 1)) << 8) |
(static_cast<uint32_t>(memory_.at(address + 2)) << 16);
});
ON_CALL(*this, ReadByteVector(::testing::_, ::testing::_))
.WillByDefault([this](uint32_t address, uint16_t length) {
std::vector<uint8_t> data;
for (int i = 0; i < length; i++) {
data.push_back(memory_.at(address + i));
}
return data;
});
ON_CALL(*this, WriteByte(::testing::_, ::testing::_))
.WillByDefault([this](uint32_t address, uint8_t value) {
memory_[address] = value;
});
ON_CALL(*this, WriteWord(::testing::_, ::testing::_))
.WillByDefault([this](uint32_t address, uint16_t value) {
memory_[address] = value & 0xFF;
memory_[address + 1] = (value >> 8) & 0xFF;
});
ON_CALL(*this, PushByte(::testing::_)).WillByDefault([this](uint8_t value) {
memory_.at(SP_--) = value;
});
ON_CALL(*this, PopByte()).WillByDefault([this]() {
uint8_t value = memory_.at(SP_);
this->SetSP(SP_ + 1);
return value;
});
ON_CALL(*this, PushWord(::testing::_))
.WillByDefault([this](uint16_t value) {
memory_.at(SP_) = value & 0xFF;
memory_.at(SP_ + 1) = (value >> 8) & 0xFF;
this->SetSP(SP_ - 2);
});
ON_CALL(*this, PopWord()).WillByDefault([this]() {
uint16_t value = static_cast<uint16_t>(memory_.at(SP_)) |
(static_cast<uint16_t>(memory_.at(SP_ + 1)) << 8);
this->SetSP(SP_ + 2);
return value;
});
ON_CALL(*this, PushLong(::testing::_))
.WillByDefault([this](uint32_t value) {
memory_.at(SP_) = value & 0xFF;
memory_.at(SP_ + 1) = (value >> 8) & 0xFF;
memory_.at(SP_ + 2) = (value >> 16) & 0xFF;
});
ON_CALL(*this, PopLong()).WillByDefault([this]() {
uint32_t value = static_cast<uint32_t>(memory_.at(SP_)) |
(static_cast<uint32_t>(memory_.at(SP_ + 1)) << 8) |
(static_cast<uint32_t>(memory_.at(SP_ + 2)) << 16);
this->SetSP(SP_ + 3);
return value;
});
ON_CALL(*this, SP()).WillByDefault([this]() { return SP_; });
ON_CALL(*this, SetSP(::testing::_)).WillByDefault([this](uint16_t value) {
SP_ = value;
});
ON_CALL(*this, ClearMemory()).WillByDefault([this]() {
memory_.resize(64000, 0x00);
});
}
std::vector<uint8_t> memory_;
uint16_t SP_ = 0x01FF;
};
} // namespace emu
} // namespace yaze
#endif // YAZE_TEST_MOCK_MOCK_MEMORY_H

207
test/rom_test.cc Normal file
View File

@@ -0,0 +1,207 @@
#include "app/rom.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "test/core/testing.h"
namespace yaze {
namespace test {
using ::testing::_;
using ::testing::Return;
const static std::vector<uint8_t> kMockRomData = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
};
class MockRom : public Rom {
public:
MOCK_METHOD(absl::Status, WriteHelper, (const WriteAction&), (override));
MOCK_METHOD2(ReadHelper, absl::Status(uint8_t&, int));
MOCK_METHOD2(ReadHelper, absl::Status(uint16_t&, int));
MOCK_METHOD2(ReadHelper, absl::Status(std::vector<uint8_t>&, int));
MOCK_METHOD(absl::StatusOr<uint8_t>, ReadByte, (int));
MOCK_METHOD(absl::StatusOr<uint16_t>, ReadWord, (int));
MOCK_METHOD(absl::StatusOr<uint32_t>, ReadLong, (int));
};
class RomTest : public ::testing::Test {
protected:
Rom rom_;
};
TEST_F(RomTest, Uninitialized) {
EXPECT_EQ(rom_.size(), 0);
EXPECT_EQ(rom_.data(), nullptr);
}
TEST_F(RomTest, LoadFromFile) {
#if defined(__linux__)
GTEST_SKIP();
#endif
EXPECT_OK(rom_.LoadFromFile("zelda3.sfc"));
EXPECT_EQ(rom_.size(), 0x200000);
EXPECT_NE(rom_.data(), nullptr);
}
TEST_F(RomTest, LoadFromFileInvalid) {
EXPECT_THAT(rom_.LoadFromFile("invalid.sfc"),
StatusIs(absl::StatusCode::kNotFound));
EXPECT_EQ(rom_.size(), 0);
EXPECT_EQ(rom_.data(), nullptr);
}
TEST_F(RomTest, LoadFromFileEmpty) {
EXPECT_THAT(rom_.LoadFromFile(""),
StatusIs(absl::StatusCode::kInvalidArgument));
}
TEST_F(RomTest, ReadByteOk) {
EXPECT_OK(rom_.LoadFromData(kMockRomData, false));
for (size_t i = 0; i < kMockRomData.size(); ++i) {
uint8_t byte;
ASSERT_OK_AND_ASSIGN(byte, rom_.ReadByte(i));
EXPECT_EQ(byte, kMockRomData[i]);
}
}
TEST_F(RomTest, ReadByteInvalid) {
EXPECT_THAT(rom_.ReadByte(0).status(),
StatusIs(absl::StatusCode::kFailedPrecondition));
}
TEST_F(RomTest, ReadWordOk) {
EXPECT_OK(rom_.LoadFromData(kMockRomData, false));
for (size_t i = 0; i < kMockRomData.size(); i += 2) {
// Little endian
EXPECT_THAT(
rom_.ReadWord(i),
IsOkAndHolds<uint16_t>((kMockRomData[i]) | kMockRomData[i + 1] << 8));
}
}
TEST_F(RomTest, ReadWordInvalid) {
EXPECT_THAT(rom_.ReadWord(0).status(),
StatusIs(absl::StatusCode::kFailedPrecondition));
}
TEST_F(RomTest, ReadLongOk) {
EXPECT_OK(rom_.LoadFromData(kMockRomData, false));
for (size_t i = 0; i < kMockRomData.size(); i += 4) {
// Little endian
EXPECT_THAT(rom_.ReadLong(i),
IsOkAndHolds<uint32_t>((kMockRomData[i]) | kMockRomData[i] |
kMockRomData[i + 1] << 8 |
kMockRomData[i + 2] << 16));
}
}
TEST_F(RomTest, ReadBytesOk) {
EXPECT_OK(rom_.LoadFromData(kMockRomData, false));
std::vector<uint8_t> bytes;
ASSERT_OK_AND_ASSIGN(bytes, rom_.ReadByteVector(0, kMockRomData.size()));
EXPECT_THAT(bytes, ::testing::ContainerEq(kMockRomData));
}
TEST_F(RomTest, ReadBytesOutOfRange) {
EXPECT_OK(rom_.LoadFromData(kMockRomData, false));
std::vector<uint8_t> bytes;
EXPECT_THAT(rom_.ReadByteVector(kMockRomData.size() + 1, 1).status(),
StatusIs(absl::StatusCode::kOutOfRange));
}
TEST_F(RomTest, WriteByteOk) {
EXPECT_OK(rom_.LoadFromData(kMockRomData, false));
for (size_t i = 0; i < kMockRomData.size(); ++i) {
EXPECT_OK(rom_.WriteByte(i, 0xFF));
uint8_t byte;
ASSERT_OK_AND_ASSIGN(byte, rom_.ReadByte(i));
EXPECT_EQ(byte, 0xFF);
}
}
TEST_F(RomTest, WriteWordOk) {
EXPECT_OK(rom_.LoadFromData(kMockRomData, false));
for (size_t i = 0; i < kMockRomData.size(); i += 2) {
EXPECT_OK(rom_.WriteWord(i, 0xFFFF));
uint16_t word;
ASSERT_OK_AND_ASSIGN(word, rom_.ReadWord(i));
EXPECT_EQ(word, 0xFFFF);
}
}
TEST_F(RomTest, WriteLongOk) {
EXPECT_OK(rom_.LoadFromData(kMockRomData, false));
for (size_t i = 0; i < kMockRomData.size(); i += 4) {
EXPECT_OK(rom_.WriteLong(i, 0xFFFFFF));
uint32_t word;
ASSERT_OK_AND_ASSIGN(word, rom_.ReadLong(i));
EXPECT_EQ(word, 0xFFFFFF);
}
}
TEST_F(RomTest, WriteTransactionSuccess) {
MockRom mock_rom;
EXPECT_OK(mock_rom.LoadFromData(kMockRomData, false));
EXPECT_CALL(mock_rom, WriteHelper(_))
.WillRepeatedly(Return(absl::OkStatus()));
EXPECT_OK(mock_rom.WriteTransaction(
Rom::WriteAction{0x1000, uint8_t{0xFF}},
Rom::WriteAction{0x1001, uint16_t{0xABCD}},
Rom::WriteAction{0x1002, std::vector<uint8_t>{0x12, 0x34}}));
}
TEST_F(RomTest, WriteTransactionFailure) {
MockRom mock_rom;
EXPECT_OK(mock_rom.LoadFromData(kMockRomData, false));
EXPECT_CALL(mock_rom, WriteHelper(_))
.WillOnce(Return(absl::OkStatus()))
.WillOnce(Return(absl::InternalError("Write failed")));
EXPECT_EQ(
mock_rom.WriteTransaction(Rom::WriteAction{0x1000, uint8_t{0xFF}},
Rom::WriteAction{0x1001, uint16_t{0xABCD}}),
absl::InternalError("Write failed"));
}
TEST_F(RomTest, ReadTransactionSuccess) {
MockRom mock_rom;
EXPECT_OK(mock_rom.LoadFromData(kMockRomData, false));
uint8_t byte_val;
uint16_t word_val;
EXPECT_OK(mock_rom.ReadTransaction(byte_val, 0x0000, word_val, 0x0001));
EXPECT_EQ(byte_val, 0x00);
EXPECT_EQ(word_val, 0x0201);
}
TEST_F(RomTest, ReadTransactionFailure) {
MockRom mock_rom;
EXPECT_OK(mock_rom.LoadFromData(kMockRomData, false));
uint8_t byte_val;
EXPECT_EQ(mock_rom.ReadTransaction(byte_val, 0x1000),
absl::FailedPreconditionError("Offset out of range"));
}
} // namespace test
} // namespace yaze

26
test/yaze_test.cc Normal file
View File

@@ -0,0 +1,26 @@
#define SDL_MAIN_HANDLED
#include <gtest/gtest.h>
#include "absl/debugging/failure_signal_handler.h"
#include "absl/debugging/symbolize.h"
#include "test/integration/test_editor.h"
int main(int argc, char* argv[]) {
absl::InitializeSymbolizer(argv[0]);
absl::FailureSignalHandlerOptions options;
absl::InstallFailureSignalHandler(options);
if (argc > 1 && std::string(argv[1]) == "integration") {
return yaze::test::integration::RunIntegrationTest();
} else if (argc > 1 && std::string(argv[1]) == "room_object") {
::testing::InitGoogleTest(&argc, argv);
if (!RUN_ALL_TESTS()) {
return yaze::test::integration::RunIntegrationTest();
}
}
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,33 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "app/rom.h"
#include "app/zelda3/dungeon/room.h"
namespace yaze {
namespace test {
class DungeonRoomTest : public ::testing::Test, public SharedRom {
protected:
void SetUp() override {
// Skip tests on Linux for automated github builds
#if defined(__linux__)
GTEST_SKIP();
#else
if (!rom()->LoadFromFile("./zelda3.sfc").ok()) {
GTEST_SKIP_("Failed to load test ROM");
}
#endif
}
void TearDown() override {}
};
TEST_F(DungeonRoomTest, SingleRoomLoadOk) {
zelda3::Room test_room(/*room_id=*/0);
test_room.LoadHeader();
// Do some assertions based on the output in ZS
test_room.LoadRoomFromROM();
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,54 @@
#include <gtest/gtest.h>
#include "app/editor/message/message_data.h"
#include "app/editor/message/message_editor.h"
#include "test/core/testing.h"
namespace yaze {
namespace test {
class MessageTest : public ::testing::Test, public SharedRom {
protected:
void SetUp() override {
#if defined(__linux__)
GTEST_SKIP();
#endif
}
void TearDown() override {}
editor::MessageEditor message_editor_;
std::vector<editor::DictionaryEntry> dictionary_;
};
TEST_F(MessageTest, LoadMessagesFromRomOk) {
EXPECT_OK(rom()->LoadFromFile("zelda3.sfc"));
EXPECT_OK(message_editor_.Initialize());
}
/**
* @test Verify that a single message can be loaded from the ROM.
*
* @details The message is loaded from the ROM and the message is parsed.
*
* Message #1 at address 0x0E000B
RawString:
[S:00][3][][:75][:44][CH2I]
Parsed:
[S:##]A
[3]give
[2]give >[CH2I]
Message ID: 2
Raw: [S:00][3][][:75][:44][CH2I]
Parsed: [S:00][3][][:75][:44][CH2I]
Raw Bytes: 7A 00 76 88 8A 75 88 44 68
Parsed Bytes: 7A 00 76 88 8A 75 88 44 68
*/
TEST_F(MessageTest, VerifySingleMessageFromRomOk) {
// TODO - Implement this test
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,47 @@
#include "app/zelda3/overworld/overworld.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "app/rom.h"
#include "app/zelda3/overworld/overworld.h"
#include "app/zelda3/overworld/overworld_map.h"
#include "test/core/testing.h"
namespace yaze {
namespace test {
class OverworldTest : public ::testing::Test, public SharedRom {
protected:
void SetUp() override {
// Skip tests on Linux for automated github builds
#if defined(__linux__)
GTEST_SKIP();
#endif
}
void TearDown() override {}
zelda3::Overworld overworld_{*rom()};
};
TEST_F(OverworldTest, OverworldLoadNoRomDataError) {
Rom rom;
EXPECT_THAT(overworld_.Load(rom),
StatusIs(absl::StatusCode::kInvalidArgument));
}
TEST_F(OverworldTest, OverworldLoadRomDataOk) {
/**
EXPECT_OK(rom()->LoadFromFile("zelda3.sfc"));
ASSERT_OK_AND_ASSIGN(auto gfx_data,
LoadAllGraphicsData(*rom(), true));
auto status = overworld_.Load(*rom());
EXPECT_TRUE(status.ok());
EXPECT_EQ(overworld_.overworld_maps().size(), zelda3::kNumOverworldMaps);
EXPECT_EQ(overworld_.tiles16().size(), zelda3::kNumTile16Individual);
*/
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,64 @@
#include "app/zelda3/sprite/sprite_builder.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace yaze {
namespace test {
using namespace yaze::zelda3;
class SpriteBuilderTest : public ::testing::Test {
protected:
void SetUp() override {
// Create a new sprite
SpriteBuilder sprite = SpriteBuilder::Create("Puffstool")
.SetProperty("NbrTiles", 2)
.SetProperty("Health", 10)
.SetProperty("Harmless", false);
// Create an anonymous global action for the sprite to run before each
// action
SpriteAction globalAction = SpriteAction::Create().AddInstruction(
SpriteInstruction::BehaveAsBarrier());
// Create an action for the SprAction::LocalJumpTable
SpriteAction walkAction =
SpriteAction::Create("Walk")
.AddInstruction(SpriteInstruction::PlayAnimation(0, 6, 10))
.AddInstruction(SpriteInstruction::ApplySpeedTowardsPlayer(2))
.AddInstruction(SpriteInstruction::MoveXyz())
.AddInstruction(SpriteInstruction::BounceFromTileCollision())
.AddCustomInstruction("JSL $0DBB7C"); // Custom ASM
// Link to the idle action. If the action does not exist, build will fail
walkAction.SetNextAction("IdleAction");
// Idle action which jumps to a fn. If the fn does not exist, build will
// fail
SpriteAction idleAction =
SpriteAction::Create("IdleAction")
.AddInstruction(SpriteInstruction::JumpToFunction("IdleFn"));
idleAction.SetNextAction("Walk");
// Build the function that the idle action jumps to
SpriteAction idleFunction = SpriteAction::Create("IdleFn").AddInstruction(
SpriteInstruction::MoveXyz());
// Add actions and functions to sprite
sprite.SetGlobalAction(globalAction);
sprite.AddAction(idleAction); // 0x00
sprite.AddAction(walkAction); // 0x01
sprite.AddFunction(idleFunction); // Local
}
void TearDown() override {}
SpriteBuilder sprite;
};
TEST_F(SpriteBuilderTest, BuildSpritePropertiesOk) {
EXPECT_THAT(sprite.BuildProperties(), testing::HasSubstr(R"(!SPRID = $00
!NbrTiles = $00
!Harmless = $00
)"));
}
} // namespace test
} // namespace yaze