backend-infra-engineer: Release v0.3.2 snapshot

This commit is contained in:
scawful
2025-10-17 12:10:25 -04:00
parent 4371618a9b
commit 3d71417f62
857 changed files with 174954 additions and 45626 deletions

View File

@@ -1,158 +1,163 @@
# This file defines the yaze test suites and configures test discovery with labels.
set(YAZE_SRC_FILES "")
foreach (file
app/rom.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()
if(YAZE_BUILD_TESTS)
add_executable(
yaze_test
yaze_test.cc
rom_test.cc
test_editor.cc
hex_test.cc
core/asar_wrapper_test.cc
gfx/snes_tile_test.cc
gfx/compression_test.cc
gfx/snes_palette_test.cc
zelda3/message_test.cc
zelda3/overworld_test.cc
zelda3/overworld_integration_test.cc
zelda3/comprehensive_integration_test.cc
zelda3/dungeon_integration_test.cc
zelda3/dungeon_object_renderer_integration_test.cc
zelda3/dungeon_object_renderer_mock_test.cc
zelda3/dungeon_editor_system_integration_test.cc
zelda3/sprite_builder_test.cc
zelda3/sprite_position_test.cc
emu/cpu_test.cc
emu/ppu_test.cc
emu/spc700_test.cc
emu/audio/apu_test.cc
emu/audio/ipl_handshake_test.cc
integration/dungeon_editor_test.cc
dungeon_component_unit_test.cc
integration/asar_integration_test.cc
integration/asar_rom_test.cc
editor/tile16_editor_test.cc
zelda3/object_parser_test.cc
zelda3/object_parser_structs_test.cc
zelda3/test_dungeon_objects.cc
)
# Helper function to create and configure a test executable for a suite of tests.
# This function adds the executable, links common dependencies, discovers the
# tests using gtest_discover_tests, and assigns a label to all discovered tests.
function(yaze_add_test_suite suite_name label is_gui_test)
set(sources ${ARGN})
add_executable(${suite_name} yaze_test.cc ${sources})
# Add vanilla value extraction utility (only for local development with ROM access)
if(NOT YAZE_MINIMAL_BUILD AND YAZE_ENABLE_ROM_TESTS)
add_executable(
extract_vanilla_values
zelda3/extract_vanilla_values.cc
${YAZE_SRC_FILES}
)
target_include_directories(
extract_vanilla_values PUBLIC
${CMAKE_SOURCE_DIR}/src/app/
${CMAKE_SOURCE_DIR}/src/lib/
${CMAKE_SOURCE_DIR}/incl/
${CMAKE_SOURCE_DIR}/src/
${CMAKE_SOURCE_DIR}/src/lib/imgui_test_engine
${CMAKE_SOURCE_DIR}/src/lib/asar/src
${CMAKE_SOURCE_DIR}/src/lib/asar/src/asar
${CMAKE_SOURCE_DIR}/src/lib/asar/src/asar-dll-bindings/c
${SDL2_INCLUDE_DIR}
${PNG_INCLUDE_DIRS}
${PROJECT_BINARY_DIR}
)
target_link_libraries(
extract_vanilla_values
${SDL_TARGETS}
asar-static
${ABSL_TARGETS}
${PNG_LIBRARIES}
${OPENGL_LIBRARIES}
${CMAKE_DL_LIBS}
)
# Conditionally link yaze_c only when library is built
if(YAZE_BUILD_LIB)
target_link_libraries(extract_vanilla_values yaze_c)
endif()
endif()
target_include_directories(
yaze_test PUBLIC
${CMAKE_SOURCE_DIR}/src/app/
${CMAKE_SOURCE_DIR}/src/lib/
${CMAKE_SOURCE_DIR}/incl/
${CMAKE_SOURCE_DIR}/src/
${CMAKE_SOURCE_DIR}/test/
${CMAKE_SOURCE_DIR}/src/lib/imgui_test_engine
${CMAKE_SOURCE_DIR}/src/lib/asar/src
${CMAKE_SOURCE_DIR}/src/lib/asar/src/asar
${CMAKE_SOURCE_DIR}/src/lib/asar/src/asar-dll-bindings/c
${SDL2_INCLUDE_DIR}
${PNG_INCLUDE_DIRS}
${PROJECT_BINARY_DIR}
)
target_link_libraries(
yaze_test
${SDL_TARGETS}
asar-static
${ABSL_TARGETS}
${PNG_LIBRARIES}
${OPENGL_LIBRARIES}
${CMAKE_DL_LIBS}
ImGui
gmock_main
gmock
gtest_main
gtest
)
# Link core library for essential functionality (BPS, ASAR, etc.)
if(YAZE_BUILD_LIB)
target_link_libraries(yaze_test yaze_core)
endif()
# Conditionally link ImGuiTestEngine only when UI tests are enabled
if(YAZE_ENABLE_UI_TESTS)
target_link_libraries(yaze_test ${IMGUI_TEST_ENGINE_TARGET})
target_compile_definitions(yaze_test PRIVATE ${IMGUI_TEST_ENGINE_DEFINITIONS})
endif()
# ROM Testing Configuration
if(YAZE_ENABLE_ROM_TESTS)
target_compile_definitions(yaze_test PRIVATE
YAZE_ENABLE_ROM_TESTS=1
YAZE_TEST_ROM_PATH="${YAZE_TEST_ROM_PATH}"
target_include_directories(${suite_name} PUBLIC
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/incl
${CMAKE_SOURCE_DIR}/test
${CMAKE_SOURCE_DIR}/src/lib
${CMAKE_SOURCE_DIR}/src/lib/imgui
${CMAKE_SOURCE_DIR}/src/lib/imgui/backends
${CMAKE_SOURCE_DIR}/src/lib/imgui_test_engine
${CMAKE_SOURCE_DIR}/src/lib/SDL/include
${CMAKE_SOURCE_DIR}/third_party/json/include
${CMAKE_BINARY_DIR}/src/lib/SDL/include
${PROJECT_BINARY_DIR}
)
target_link_libraries(${suite_name} PRIVATE
yaze_test_support
gmock_main
gtest_main
absl::failure_signal_handler
absl::flags
absl::flags_parse
ImGui
${SDL_TARGETS}
)
# Link ImGui Test Engine for GUI tests (always available when tests are built)
if(is_gui_test AND TARGET ImGuiTestEngine)
target_link_libraries(${suite_name} PRIVATE ImGuiTestEngine)
target_compile_definitions(${suite_name} PRIVATE
IMGUI_ENABLE_TEST_ENGINE=1
IMGUI_TEST_ENGINE_ENABLE_COROUTINE_STDTHREAD_IMPL=1
YAZE_GUI_TEST_TARGET=1) # Mark this as a GUI test target
endif()
if(WIN32)
message(STATUS "Configuring Windows stack size for ${suite_name} to 16MB")
if(MSVC)
target_link_options(${suite_name} PRIVATE /STACK:16777216)
# Force whole-archive linking for protobuf to ensure all symbols are included
if(YAZE_WITH_GRPC AND MSVC AND YAZE_PROTOBUF_WHOLEARCHIVE_TARGETS)
foreach(_yaze_proto_target IN LISTS YAZE_PROTOBUF_WHOLEARCHIVE_TARGETS)
target_link_options(${suite_name} PRIVATE /WHOLEARCHIVE:$<TARGET_FILE:${_yaze_proto_target}>)
endforeach()
endif()
else()
target_link_options(${suite_name} PRIVATE -Wl,--stack,16777216)
endif()
endif()
include(GoogleTest)
if(WIN32)
gtest_discover_tests(${suite_name}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DISCOVERY_TIMEOUT 60
PROPERTIES LABELS "${label}"
)
else()
gtest_discover_tests(${suite_name}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
PROPERTIES LABELS "${label}"
)
endif()
endfunction()
# --- Stable Test Suite (Valid Contracts) ---
set(STABLE_TEST_SOURCES
test_editor.cc
test_utils.cc
# Unit Tests
unit/core/asar_wrapper_test.cc
unit/core/hex_test.cc
unit/cli/resource_catalog_test.cc
unit/rom/rom_test.cc
unit/gfx/snes_tile_test.cc
unit/gfx/compression_test.cc
unit/gfx/snes_palette_test.cc
unit/snes_color_test.cc
unit/gui/tile_selector_widget_test.cc
unit/gui/canvas_automation_api_test.cc
unit/zelda3/overworld_test.cc
unit/zelda3/object_parser_test.cc
unit/zelda3/object_parser_structs_test.cc
unit/zelda3/sprite_builder_test.cc
unit/zelda3/dungeon_component_unit_test.cc
unit/zelda3/dungeon/room_object_encoding_test.cc
unit/zelda3/dungeon/room_manipulation_test.cc
../src/cli/service/resources/resource_catalog.cc
cli/service/resources/command_context_test.cc
# Integration Tests
integration/asar_integration_test.cc
integration/dungeon_editor_test.cc
integration/dungeon_editor_v2_test.cc
integration/editor/tile16_editor_test.cc
integration/editor/editor_integration_test.cc
integration/zelda3/overworld_integration_test.cc
integration/zelda3/dungeon_editor_system_integration_test.cc
integration/zelda3/room_integration_test.cc
integration/zelda3/dungeon_object_rendering_tests.cc
integration/zelda3/dungeon_room_test.cc
integration/zelda3/sprite_position_test.cc
integration/zelda3/message_test.cc
integration/palette_manager_test.cc
)
yaze_add_test_suite(yaze_test_stable "stable" OFF ${STABLE_TEST_SOURCES})
# --- ROM Dependent Test Suite ---
if(YAZE_ENABLE_ROM_TESTS)
set(ROM_DEPENDENT_TEST_SOURCES
integration/asar_rom_test.cc
e2e/rom_dependent/e2e_rom_test.cc
e2e/zscustomoverworld/zscustomoverworld_upgrade_test.cc
)
yaze_add_test_suite(yaze_test_rom_dependent "rom_dependent" OFF ${ROM_DEPENDENT_TEST_SOURCES})
target_compile_definitions(yaze_test_rom_dependent PRIVATE
YAZE_ENABLE_ROM_TESTS=1
YAZE_TEST_ROM_PATH="${YAZE_TEST_ROM_PATH}"
)
endif()
# --- Experimental & GUI Test Suites ---
# GUI tests always available when tests are built (uses ImGui Test Engine)
set(GUI_TEST_SOURCES
test_utils.cc
e2e/framework_smoke_test.cc
e2e/dungeon_editor_smoke_test.cc
e2e/canvas_selection_test.cc
integration/ai/ai_gui_controller_test.cc
)
yaze_add_test_suite(yaze_test_gui "gui;experimental" ON ${GUI_TEST_SOURCES})
# Add a single test entry to run the entire GUI suite headlessly
add_test(
NAME headless_gui_suite
COMMAND $<TARGET_FILE:yaze_test_gui> -nogui
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
set_tests_properties(headless_gui_suite PROPERTIES LABELS "headless_gui;experimental")
set(EXPERIMENTAL_TEST_SOURCES
test_utils.cc
integration/ai/test_ai_tile_placement.cc
integration/ai/test_gemini_vision.cc
)
yaze_add_test_suite(yaze_test_experimental "experimental" OFF ${EXPERIMENTAL_TEST_SOURCES})
# --- Benchmark Test Suite ---
set(BENCHMARK_TEST_SOURCES
benchmarks/gfx_optimization_benchmarks.cc
)
yaze_add_test_suite(yaze_test_benchmark "benchmark" OFF ${BENCHMARK_TEST_SOURCES})
endif()
# ImGui Test Engine definitions are now handled conditionally above
# Platform-specific definitions
if(UNIX AND NOT APPLE)
target_compile_definitions(yaze_test PRIVATE "linux" "stricmp=strcasecmp")
elseif(APPLE)
target_compile_definitions(yaze_test PRIVATE "MACOS" "stricmp=strcasecmp")
elseif(WIN32)
target_compile_definitions(yaze_test PRIVATE "WINDOWS")
endif()
include(GoogleTest)
# Configure test discovery with efficient labeling for CI/CD
include(GoogleTest)
# Discover all tests with default properties
gtest_discover_tests(yaze_test)
# Add test labels using a simpler approach
# Note: Test names might have prefixes, we'll use regex patterns for CI

195
test/README.md Normal file
View File

@@ -0,0 +1,195 @@
# yaze Test Suite
This directory contains the comprehensive test suite for YAZE, organized for optimal AI agent testing and development workflow.
## Directory Structure
```
test/
├── unit/ # Unit tests for individual components
│ ├── core/ # Core functionality tests
│ ├── rom/ # ROM handling tests
│ ├── gfx/ # Graphics system tests
│ └── zelda3/ # Zelda 3 specific tests
├── integration/ # Integration tests
│ ├── editor/ # Editor integration tests
│ ├── asar_integration_test.cc
│ ├── asar_rom_test.cc
│ └── dungeon_editor_test.cc
├── e2e/ # End-to-end tests
│ ├── rom_dependent/ # ROM-dependent E2E tests
│ └── zscustomoverworld/ # ZSCustomOverworld upgrade tests
├── deprecated/ # Outdated tests (for cleanup)
│ └── emu/ # Deprecated emulator tests
├── mocks/ # Mock objects for testing
├── assets/ # Test assets and patches
└── yaze_test.cc # Enhanced test runner
```
## Test Categories
### Unit Tests (`unit/`)
- **Core**: ASAR wrapper, hex utilities, core functionality
- **ROM**: ROM loading, saving, validation
- **Graphics**: SNES tiles, palettes, compression
- **Zelda3**: Message system, overworld, objects, sprites
### Integration Tests (`integration/`)
- **Editor**: Tile editor, dungeon editor integration
- **ASAR**: ASAR integration and ROM patching
- **Dungeon**: Dungeon editor system integration
### End-to-End Tests (`e2e/`)
- **ROM Dependent**: Complete ROM editing workflow validation
- **ZSCustomOverworld**: Version upgrade testing (vanilla → v2 → v3)
## Enhanced Test Runner
The `yaze_test` executable now supports comprehensive argument handling for AI agents:
### Usage Examples
```bash
# Run all tests
./yaze_test
# Run specific test categories
./yaze_test --unit --verbose
./yaze_test --integration
./yaze_test --e2e --rom-path my_rom.sfc
./yaze_test --zscustomoverworld --verbose
# Run specific test patterns
./yaze_test RomTest.*
./yaze_test *ZSCustomOverworld*
# Skip ROM-dependent tests
./yaze_test --skip-rom-tests
# Enable UI tests
./yaze_test --enable-ui-tests
```
### Test Modes
- `--unit`: Unit tests only
- `--integration`: Integration tests only
- `--e2e`: End-to-end tests only
- `--rom-dependent`: ROM-dependent tests only
- `--zscustomoverworld`: ZSCustomOverworld tests only
- `--core`: Core functionality tests
- `--graphics`: Graphics tests
- `--editor`: Editor tests
- `--deprecated`: Deprecated tests (for cleanup)
### Options
- `--rom-path PATH`: Specify ROM path for testing
- `--skip-rom-tests`: Skip tests requiring ROM files
- `--enable-ui-tests`: Enable UI tests (requires display)
- `--verbose`: Enable verbose output
- `--help`: Show help message
## E2E ROM Testing
The E2E ROM test suite (`e2e/rom_dependent/e2e_rom_test.cc`) provides comprehensive validation of the complete ROM editing workflow:
1. **Load vanilla ROM**
2. **Apply various edits** (overworld, dungeon, graphics, etc.)
3. **Save changes**
4. **Reload ROM and verify edits persist**
5. **Verify no data corruption occurred**
### Test Cases
- `BasicROMLoadSave`: Basic ROM loading and saving
- `OverworldEditWorkflow`: Complete overworld editing workflow
- `DungeonEditWorkflow`: Complete dungeon editing workflow
- `TransactionSystem`: Multi-edit transaction validation
- `CorruptionDetection`: ROM corruption detection
- `LargeScaleEditing`: Large-scale editing without corruption
## ZSCustomOverworld Upgrade Testing
The ZSCustomOverworld test suite (`e2e/zscustomoverworld/zscustomoverworld_upgrade_test.cc`) validates version upgrades:
### Supported Upgrades
- **Vanilla → v2**: Basic upgrade with main palettes
- **v2 → v3**: Advanced upgrade with expanded features
- **Vanilla → v3**: Direct upgrade to latest version
### Test Cases
- `VanillaBaseline`: Validate vanilla ROM baseline
- `VanillaToV2Upgrade`: Test vanilla to v2 upgrade
- `V2ToV3Upgrade`: Test v2 to v3 upgrade
- `VanillaToV3Upgrade`: Test direct vanilla to v3 upgrade
- `AddressValidation`: Validate version-specific addresses
- `SaveCompatibility`: Test save compatibility between versions
- `FeatureToggle`: Test feature enablement/disablement
- `DataIntegrity`: Test data integrity during upgrades
### Version-Specific Features
#### Vanilla
- Basic overworld functionality
- Standard message IDs, area graphics, palettes
#### v2
- Main palettes support
- Expanded message ID table
#### v3
- Area-specific background colors
- Subscreen overlays
- Animated GFX
- Custom tile GFX groups
- Mosaic effects
## Environment Variables
- `YAZE_TEST_ROM_PATH`: Path to test ROM file
- `YAZE_SKIP_ROM_TESTS`: Skip ROM-dependent tests
- `YAZE_ENABLE_UI_TESTS`: Enable UI tests
- `YAZE_VERBOSE_TESTS`: Enable verbose test output
## CI/CD Integration
Tests are automatically labeled for CI/CD:
- `unit`: Fast unit tests
- `integration`: Medium-speed integration tests
- `e2e`: Slow end-to-end tests
- `rom`: ROM-dependent tests
- `zscustomoverworld`: ZSCustomOverworld specific tests
- `core`: Core functionality tests
- `graphics`: Graphics tests
- `editor`: Editor tests
- `deprecated`: Deprecated tests
## Deprecated Tests
The `deprecated/` directory contains outdated tests that no longer pass after the large refactor:
- **EMU tests**: CPU, PPU, SPC700, APU tests that are no longer compatible
- These tests are kept for reference but should not be run in CI/CD
## Best Practices
1. **Use appropriate test categories** for new tests
2. **Add comprehensive E2E tests** for new features
3. **Test upgrade paths** for ZSCustomOverworld features
4. **Validate data integrity** in all ROM operations
5. **Use descriptive test names** for AI agent clarity
6. **Include verbose output** for debugging
## AI Agent Testing
The enhanced test runner is specifically designed for AI agent testing:
- **Clear argument structure** for easy automation
- **Comprehensive help system** for understanding capabilities
- **Verbose output** for debugging and validation
- **Flexible test filtering** for targeted testing
- **Environment variable support** for configuration

View File

@@ -0,0 +1,445 @@
#include <gtest/gtest.h>
#include <chrono>
#include <vector>
#include <random>
#include "app/gfx/core/bitmap.h"
#include "app/gfx/resource/arena.h"
#include "app/gfx/resource/memory_pool.h"
#include "app/gfx/render/atlas_renderer.h"
#include "app/gfx/debug/performance/performance_profiler.h"
#include "app/gfx/debug/performance/performance_dashboard.h"
namespace yaze {
namespace gfx {
class GraphicsOptimizationBenchmarks : public ::testing::Test {
protected:
void SetUp() override {
// Initialize graphics systems
Arena::Get();
MemoryPool::Get();
PerformanceProfiler::Get().Clear();
}
void TearDown() override {
// Cleanup
PerformanceProfiler::Get().Clear();
}
// Helper methods for creating test data
std::vector<uint8_t> CreateTestBitmapData(int width, int height) {
std::vector<uint8_t> data(width * height);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 15); // 4-bit color indices
for (auto& pixel : data) {
pixel = static_cast<uint8_t>(dis(gen));
}
return data;
}
SnesPalette CreateTestPalette() {
SnesPalette palette;
for (int i = 0; i < 16; ++i) {
palette.AddColor(SnesColor(i * 16, i * 16, i * 16));
}
return palette;
}
};
// Benchmark palette lookup optimization
TEST_F(GraphicsOptimizationBenchmarks, PaletteLookupPerformance) {
const int kIterations = 10000;
const int kBitmapSize = 128;
auto test_data = CreateTestBitmapData(kBitmapSize, kBitmapSize);
auto test_palette = CreateTestPalette();
Bitmap bitmap(kBitmapSize, kBitmapSize, 8, test_data, test_palette);
// Benchmark palette lookup
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < kIterations; ++i) {
SnesColor test_color(i % 16, (i + 1) % 16, (i + 2) % 16);
uint8_t index = bitmap.FindColorIndex(test_color);
(void)index; // Prevent optimization
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
double avg_time_us = static_cast<double>(duration.count()) / kIterations;
// Verify optimization is working (should be < 1μs per lookup)
EXPECT_LT(avg_time_us, 1.0) << "Palette lookup should be optimized to < 1μs";
std::cout << "Palette lookup average time: " << avg_time_us << " μs" << std::endl;
}
// Benchmark dirty region tracking
TEST_F(GraphicsOptimizationBenchmarks, DirtyRegionTrackingPerformance) {
const int kBitmapSize = 256;
const int kPixelUpdates = 1000;
auto test_data = CreateTestBitmapData(kBitmapSize, kBitmapSize);
auto test_palette = CreateTestPalette();
Bitmap bitmap(kBitmapSize, kBitmapSize, 8, test_data, test_palette);
// Benchmark pixel updates with dirty region tracking
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < kPixelUpdates; ++i) {
int x = i % kBitmapSize;
int y = (i * 7) % kBitmapSize; // Spread updates across bitmap
SnesColor color(i % 16, (i + 1) % 16, (i + 2) % 16);
bitmap.SetPixel(x, y, color);
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
double avg_time_us = static_cast<double>(duration.count()) / kPixelUpdates;
// Verify dirty region tracking is efficient
EXPECT_LT(avg_time_us, 10.0) << "Pixel updates should be < 10μs with dirty region tracking";
std::cout << "Pixel update average time: " << avg_time_us << " μs" << std::endl;
}
// Benchmark memory pool allocation
TEST_F(GraphicsOptimizationBenchmarks, MemoryPoolAllocationPerformance) {
const int kAllocations = 10000;
const size_t kAllocationSize = 1024; // 1KB blocks
auto& memory_pool = MemoryPool::Get();
std::vector<void*> allocations;
allocations.reserve(kAllocations);
// Benchmark allocations
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < kAllocations; ++i) {
void* ptr = memory_pool.Allocate(kAllocationSize);
allocations.push_back(ptr);
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
double avg_time_us = static_cast<double>(duration.count()) / kAllocations;
// Verify memory pool is faster than system malloc
EXPECT_LT(avg_time_us, 1.0) << "Memory pool allocation should be < 1μs";
std::cout << "Memory pool allocation average time: " << avg_time_us << " μs" << std::endl;
// Benchmark deallocations
start = std::chrono::high_resolution_clock::now();
for (void* ptr : allocations) {
memory_pool.Deallocate(ptr);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
avg_time_us = static_cast<double>(duration.count()) / kAllocations;
EXPECT_LT(avg_time_us, 1.0) << "Memory pool deallocation should be < 1μs";
std::cout << "Memory pool deallocation average time: " << avg_time_us << " μs" << std::endl;
}
// Benchmark batch texture updates
TEST_F(GraphicsOptimizationBenchmarks, BatchTextureUpdatePerformance) {
const int kTextureUpdates = 100;
const int kBitmapSize = 64;
auto test_data = CreateTestBitmapData(kBitmapSize, kBitmapSize);
auto test_palette = CreateTestPalette();
std::vector<Bitmap> bitmaps;
bitmaps.reserve(kTextureUpdates);
// Create test bitmaps
for (int i = 0; i < kTextureUpdates; ++i) {
bitmaps.emplace_back(kBitmapSize, kBitmapSize, 8, test_data, test_palette);
}
auto& arena = Arena::Get();
// Benchmark individual texture updates
auto start = std::chrono::high_resolution_clock::now();
for (auto& bitmap : bitmaps) {
gfx::Arena::Get().QueueTextureCommand(
gfx::Arena::TextureCommandType::UPDATE, &bitmap);
}
auto end = std::chrono::high_resolution_clock::now();
auto individual_duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// Benchmark batch texture updates
start = std::chrono::high_resolution_clock::now();
for (auto& bitmap : bitmaps) {
gfx::Arena::Get().QueueTextureCommand(
gfx::Arena::TextureCommandType::UPDATE, &bitmap);
}
gfx::Arena::Get().ProcessTextureQueue(nullptr); // Process all at once
end = std::chrono::high_resolution_clock::now();
auto batch_duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// Verify batch updates are faster
double individual_avg = static_cast<double>(individual_duration.count()) / kTextureUpdates;
double batch_avg = static_cast<double>(batch_duration.count()) / kTextureUpdates;
EXPECT_LT(batch_avg, individual_avg) << "Batch updates should be faster than individual updates";
std::cout << "Individual texture update average: " << individual_avg << " μs" << std::endl;
std::cout << "Batch texture update average: " << batch_avg << " μs" << std::endl;
std::cout << "Speedup: " << (individual_avg / batch_avg) << "x" << std::endl;
}
// Benchmark atlas rendering
TEST_F(GraphicsOptimizationBenchmarks, AtlasRenderingPerformance) {
const int kBitmaps = 50;
const int kBitmapSize = 32;
auto test_data = CreateTestBitmapData(kBitmapSize, kBitmapSize);
auto test_palette = CreateTestPalette();
std::vector<Bitmap> bitmaps;
bitmaps.reserve(kBitmaps);
// Create test bitmaps
for (int i = 0; i < kBitmaps; ++i) {
bitmaps.emplace_back(kBitmapSize, kBitmapSize, 8, test_data, test_palette);
}
auto& atlas_renderer = AtlasRenderer::Get();
atlas_renderer.Initialize(nullptr, 512); // Initialize with 512x512 atlas
// Add bitmaps to atlas
std::vector<int> atlas_ids;
for (auto& bitmap : bitmaps) {
int atlas_id = atlas_renderer.AddBitmap(bitmap);
if (atlas_id >= 0) {
atlas_ids.push_back(atlas_id);
}
}
// Create render commands
std::vector<RenderCommand> render_commands;
for (size_t i = 0; i < atlas_ids.size(); ++i) {
render_commands.emplace_back(atlas_ids[i], i * 10.0f, i * 10.0f);
}
// Benchmark atlas rendering
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000; ++i) {
atlas_renderer.RenderBatch(render_commands);
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
double avg_time_us = static_cast<double>(duration.count()) / 1000.0;
// Verify atlas rendering is efficient
EXPECT_LT(avg_time_us, 100.0) << "Atlas rendering should be < 100μs per batch";
std::cout << "Atlas rendering average time: " << avg_time_us << " μs per batch" << std::endl;
// Get atlas statistics
auto stats = atlas_renderer.GetStats();
std::cout << "Atlas utilization: " << stats.utilization_percent << "%" << std::endl;
}
// Benchmark performance profiler overhead
TEST_F(GraphicsOptimizationBenchmarks, PerformanceProfilerOverhead) {
const int kOperations = 100000;
auto& profiler = PerformanceProfiler::Get();
// Benchmark operations without profiling
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < kOperations; ++i) {
// Simulate some work
volatile int result = i * i;
(void)result;
}
auto end = std::chrono::high_resolution_clock::now();
auto no_profiling_duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// Benchmark operations with profiling
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < kOperations; ++i) {
profiler.StartTimer("test_operation");
// Simulate some work
volatile int result = i * i;
(void)result;
profiler.EndTimer("test_operation");
}
end = std::chrono::high_resolution_clock::now();
auto with_profiling_duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// Calculate profiling overhead
double no_profiling_avg = static_cast<double>(no_profiling_duration.count()) / kOperations;
double with_profiling_avg = static_cast<double>(with_profiling_duration.count()) / kOperations;
double overhead = with_profiling_avg - no_profiling_avg;
// Verify profiling overhead is minimal
EXPECT_LT(overhead, 1.0) << "Profiling overhead should be < 1μs per operation";
std::cout << "No profiling average: " << no_profiling_avg << " μs" << std::endl;
std::cout << "With profiling average: " << with_profiling_avg << " μs" << std::endl;
std::cout << "Profiling overhead: " << overhead << " μs" << std::endl;
}
// Benchmark atlas rendering performance
TEST_F(GraphicsOptimizationBenchmarks, AtlasRenderingPerformance2) {
const int kNumTiles = 100;
const int kTileSize = 16;
auto& atlas_renderer = AtlasRenderer::Get();
auto& profiler = PerformanceProfiler::Get();
// Create test tiles
std::vector<Bitmap> test_tiles;
std::vector<int> atlas_ids;
for (int i = 0; i < kNumTiles; ++i) {
auto tile_data = CreateTestBitmapData(kTileSize, kTileSize);
auto tile_palette = CreateTestPalette();
test_tiles.emplace_back(kTileSize, kTileSize, 8, tile_data, tile_palette);
// Add to atlas
int atlas_id = atlas_renderer.AddBitmap(test_tiles.back());
if (atlas_id >= 0) {
atlas_ids.push_back(atlas_id);
}
}
// Benchmark individual tile rendering
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < kNumTiles; ++i) {
if (i < atlas_ids.size()) {
atlas_renderer.RenderBitmap(atlas_ids[i], i * 20.0f, 0.0f);
}
}
auto end = std::chrono::high_resolution_clock::now();
auto individual_duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// Benchmark batch rendering
std::vector<RenderCommand> render_commands;
for (size_t i = 0; i < atlas_ids.size(); ++i) {
render_commands.emplace_back(atlas_ids[i], i * 20.0f, 100.0f);
}
start = std::chrono::high_resolution_clock::now();
atlas_renderer.RenderBatch(render_commands);
end = std::chrono::high_resolution_clock::now();
auto batch_duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// Verify batch rendering is faster
EXPECT_LT(batch_duration.count(), individual_duration.count())
<< "Batch rendering should be faster than individual rendering";
// Get atlas statistics
auto stats = atlas_renderer.GetStats();
EXPECT_GT(stats.total_entries, 0) << "Atlas should contain entries";
EXPECT_GT(stats.used_entries, 0) << "Atlas should have used entries";
std::cout << "Individual rendering: " << individual_duration.count() << " μs" << std::endl;
std::cout << "Batch rendering: " << batch_duration.count() << " μs" << std::endl;
std::cout << "Atlas entries: " << stats.used_entries << "/" << stats.total_entries << std::endl;
std::cout << "Atlas utilization: " << stats.utilization_percent << "%" << std::endl;
}
// Integration test for overall performance
TEST_F(GraphicsOptimizationBenchmarks, OverallPerformanceIntegration) {
const int kGraphicsSheets = 10;
const int kTilesPerSheet = 100;
const int kTileSize = 16;
auto& memory_pool = MemoryPool::Get();
auto& arena = Arena::Get();
auto& profiler = PerformanceProfiler::Get();
// Simulate loading graphics sheets
auto start = std::chrono::high_resolution_clock::now();
std::vector<Bitmap> graphics_sheets;
for (int sheet = 0; sheet < kGraphicsSheets; ++sheet) {
auto sheet_data = CreateTestBitmapData(kTileSize * 10, kTileSize * 10);
auto sheet_palette = CreateTestPalette();
graphics_sheets.emplace_back(kTileSize * 10, kTileSize * 10, 8, sheet_data, sheet_palette);
}
auto end = std::chrono::high_resolution_clock::now();
auto load_duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// Simulate tile operations
start = std::chrono::high_resolution_clock::now();
for (int sheet = 0; sheet < kGraphicsSheets; ++sheet) {
for (int tile = 0; tile < kTilesPerSheet; ++tile) {
int x = (tile % 10) * kTileSize;
int y = (tile / 10) * kTileSize;
SnesColor color(tile % 16, (tile + 1) % 16, (tile + 2) % 16);
graphics_sheets[sheet].SetPixel(x, y, color);
}
}
end = std::chrono::high_resolution_clock::now();
auto tile_duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// Simulate batch texture updates
start = std::chrono::high_resolution_clock::now();
for (auto& sheet : graphics_sheets) {
arena.QueueTextureCommand(gfx::Arena::TextureCommandType::UPDATE, &sheet);
}
arena.ProcessTextureQueue(nullptr);
end = std::chrono::high_resolution_clock::now();
auto batch_duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// Verify overall performance
double load_time_ms = static_cast<double>(load_duration.count()) / 1000.0;
double tile_time_ms = static_cast<double>(tile_duration.count()) / 1000.0;
double batch_time_ms = static_cast<double>(batch_duration.count()) / 1000.0;
EXPECT_LT(load_time_ms, 100.0) << "Graphics sheet loading should be < 100ms";
EXPECT_LT(tile_time_ms, 50.0) << "Tile operations should be < 50ms";
EXPECT_LT(batch_time_ms, 10.0) << "Batch updates should be < 10ms";
std::cout << "Graphics sheet loading: " << load_time_ms << " ms" << std::endl;
std::cout << "Tile operations: " << tile_time_ms << " ms" << std::endl;
std::cout << "Batch updates: " << batch_time_ms << " ms" << std::endl;
// Get performance summary
auto summary = PerformanceDashboard::Get().GetSummary();
std::cout << "Optimization score: " << summary.optimization_score << "/100" << std::endl;
std::cout << "Status: " << summary.status_message << std::endl;
}
} // namespace gfx
} // namespace yaze

View File

@@ -0,0 +1,295 @@
#include "cli/service/resources/command_context.h"
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "app/rom.h"
#include "mocks/mock_rom.h"
namespace yaze {
namespace cli {
namespace resources {
class CommandContextTest : public ::testing::Test {
protected:
void SetUp() override {
// Initialize mock ROM for testing
std::vector<uint8_t> test_data(1024, 0); // 1KB of empty data
auto status = mock_rom_.SetTestData(test_data);
ASSERT_TRUE(status.ok());
}
yaze::test::MockRom mock_rom_;
};
TEST_F(CommandContextTest, LoadsRomFromConfig) {
CommandContext::Config config;
config.use_mock_rom = true;
CommandContext context(config);
auto rom_or = context.GetRom();
ASSERT_TRUE(rom_or.ok());
EXPECT_TRUE(rom_or.value()->is_loaded());
}
TEST_F(CommandContextTest, UsesExternalRomContext) {
CommandContext::Config config;
config.external_rom_context = &mock_rom_;
CommandContext context(config);
auto rom_or = context.GetRom();
ASSERT_TRUE(rom_or.ok());
EXPECT_EQ(rom_or.value(), &mock_rom_);
}
TEST_F(CommandContextTest, LoadsRomFromPath) {
CommandContext::Config config;
config.rom_path = "test_rom.sfc"; // This would need a real ROM file
CommandContext context(config);
// This test would need a real ROM file to pass
// For now, we expect it to fail gracefully
auto rom_or = context.GetRom();
EXPECT_FALSE(rom_or.ok());
}
TEST_F(CommandContextTest, EnsuresLabelsLoaded) {
CommandContext::Config config;
config.use_mock_rom = true;
CommandContext context(config);
auto rom_or = context.GetRom();
ASSERT_TRUE(rom_or.ok());
auto status = context.EnsureLabelsLoaded(rom_or.value());
EXPECT_TRUE(status.ok());
}
TEST_F(CommandContextTest, GetFormatReturnsConfigFormat) {
CommandContext::Config config;
config.format = "text";
CommandContext context(config);
EXPECT_EQ(context.GetFormat(), "text");
}
TEST_F(CommandContextTest, IsVerboseReturnsConfigVerbose) {
CommandContext::Config config;
config.verbose = true;
CommandContext context(config);
EXPECT_TRUE(context.IsVerbose());
}
// ArgumentParser Tests
class ArgumentParserTest : public ::testing::Test {
protected:
void SetUp() override {}
};
TEST_F(ArgumentParserTest, ParsesStringArguments) {
std::vector<std::string> args = {"--type=dungeon", "--format", "json"};
ArgumentParser parser(args);
EXPECT_EQ(parser.GetString("type").value(), "dungeon");
EXPECT_EQ(parser.GetString("format").value(), "json");
}
TEST_F(ArgumentParserTest, ParsesIntArguments) {
std::vector<std::string> args = {"--room=0x12", "--count", "42"};
ArgumentParser parser(args);
auto room_or = parser.GetInt("room");
ASSERT_TRUE(room_or.ok());
EXPECT_EQ(room_or.value(), 0x12);
auto count_or = parser.GetInt("count");
ASSERT_TRUE(count_or.ok());
EXPECT_EQ(count_or.value(), 42);
}
TEST_F(ArgumentParserTest, ParsesHexArguments) {
std::vector<std::string> args = {"--address=0x1234", "--value", "0xFF"};
ArgumentParser parser(args);
auto addr_or = parser.GetHex("address");
ASSERT_TRUE(addr_or.ok());
EXPECT_EQ(addr_or.value(), 0x1234);
auto value_or = parser.GetHex("value");
ASSERT_TRUE(value_or.ok());
EXPECT_EQ(value_or.value(), 0xFF);
}
TEST_F(ArgumentParserTest, DetectsFlags) {
std::vector<std::string> args = {"--verbose", "--debug", "--format=json"};
ArgumentParser parser(args);
EXPECT_TRUE(parser.HasFlag("verbose"));
EXPECT_TRUE(parser.HasFlag("debug"));
EXPECT_FALSE(parser.HasFlag("format")); // format is a value, not a flag
}
TEST_F(ArgumentParserTest, GetsPositionalArguments) {
std::vector<std::string> args = {"command", "--flag", "value", "positional1", "positional2"};
ArgumentParser parser(args);
auto positional = parser.GetPositional();
EXPECT_THAT(positional, ::testing::ElementsAre("command", "positional1", "positional2"));
}
TEST_F(ArgumentParserTest, ValidatesRequiredArguments) {
std::vector<std::string> args = {"--type=dungeon"};
ArgumentParser parser(args);
auto status = parser.RequireArgs({"type"});
EXPECT_TRUE(status.ok());
status = parser.RequireArgs({"type", "missing"});
EXPECT_FALSE(status.ok());
}
TEST_F(ArgumentParserTest, HandlesMissingArguments) {
std::vector<std::string> args = {"--type=dungeon"};
ArgumentParser parser(args);
auto missing = parser.GetString("missing");
EXPECT_FALSE(missing.has_value());
auto int_missing = parser.GetInt("missing");
EXPECT_FALSE(int_missing.ok());
}
// OutputFormatter Tests
class OutputFormatterTest : public ::testing::Test {
protected:
void SetUp() override {}
};
TEST_F(OutputFormatterTest, CreatesFromString) {
auto json_formatter = OutputFormatter::FromString("json");
ASSERT_TRUE(json_formatter.ok());
EXPECT_TRUE(json_formatter.value().IsJson());
auto text_formatter = OutputFormatter::FromString("text");
ASSERT_TRUE(text_formatter.ok());
EXPECT_TRUE(text_formatter.value().IsText());
auto invalid_formatter = OutputFormatter::FromString("invalid");
EXPECT_FALSE(invalid_formatter.ok());
}
TEST_F(OutputFormatterTest, GeneratesValidJson) {
auto formatter_or = OutputFormatter::FromString("json");
ASSERT_TRUE(formatter_or.ok());
auto formatter = std::move(formatter_or.value());
formatter.BeginObject("Test");
formatter.AddField("string_field", "value");
formatter.AddField("int_field", 42);
formatter.AddField("bool_field", true);
formatter.AddHexField("hex_field", 0x1234, 4);
formatter.BeginArray("array_field");
formatter.AddArrayItem("item1");
formatter.AddArrayItem("item2");
formatter.EndArray();
formatter.EndObject();
std::string output = formatter.GetOutput();
EXPECT_THAT(output, ::testing::HasSubstr("\"string_field\": \"value\""));
EXPECT_THAT(output, ::testing::HasSubstr("\"int_field\": 42"));
EXPECT_THAT(output, ::testing::HasSubstr("\"bool_field\": true"));
EXPECT_THAT(output, ::testing::HasSubstr("\"hex_field\": \"0x1234\""));
EXPECT_THAT(output, ::testing::HasSubstr("\"array_field\": ["));
EXPECT_THAT(output, ::testing::HasSubstr("\"item1\""));
EXPECT_THAT(output, ::testing::HasSubstr("\"item2\""));
}
TEST_F(OutputFormatterTest, GeneratesValidText) {
auto formatter_or = OutputFormatter::FromString("text");
ASSERT_TRUE(formatter_or.ok());
auto formatter = std::move(formatter_or.value());
formatter.BeginObject("Test Object");
formatter.AddField("string_field", "value");
formatter.AddField("int_field", 42);
formatter.AddField("bool_field", true);
formatter.AddHexField("hex_field", 0x1234, 4);
formatter.BeginArray("array_field");
formatter.AddArrayItem("item1");
formatter.AddArrayItem("item2");
formatter.EndArray();
formatter.EndObject();
std::string output = formatter.GetOutput();
EXPECT_THAT(output, ::testing::HasSubstr("=== Test Object ==="));
EXPECT_THAT(output, ::testing::HasSubstr("string_field : value"));
EXPECT_THAT(output, ::testing::HasSubstr("int_field : 42"));
EXPECT_THAT(output, ::testing::HasSubstr("bool_field : yes"));
EXPECT_THAT(output, ::testing::HasSubstr("hex_field : 0x1234"));
EXPECT_THAT(output, ::testing::HasSubstr("array_field:"));
EXPECT_THAT(output, ::testing::HasSubstr("- item1"));
EXPECT_THAT(output, ::testing::HasSubstr("- item2"));
}
TEST_F(OutputFormatterTest, EscapesJsonStrings) {
auto formatter_or = OutputFormatter::FromString("json");
ASSERT_TRUE(formatter_or.ok());
auto formatter = std::move(formatter_or.value());
formatter.BeginObject("Test");
formatter.AddField("quotes", "He said \"Hello\"");
formatter.AddField("newlines", "Line1\nLine2");
formatter.AddField("backslashes", "Path\\to\\file");
formatter.EndObject();
std::string output = formatter.GetOutput();
EXPECT_THAT(output, ::testing::HasSubstr("\\\""));
EXPECT_THAT(output, ::testing::HasSubstr("\\n"));
EXPECT_THAT(output, ::testing::HasSubstr("\\\\"));
}
TEST_F(OutputFormatterTest, HandlesEmptyObjects) {
auto formatter_or = OutputFormatter::FromString("json");
ASSERT_TRUE(formatter_or.ok());
auto formatter = std::move(formatter_or.value());
formatter.BeginObject("Empty");
formatter.EndObject();
std::string output = formatter.GetOutput();
EXPECT_THAT(output, ::testing::HasSubstr("{}"));
}
TEST_F(OutputFormatterTest, HandlesEmptyArrays) {
auto formatter_or = OutputFormatter::FromString("json");
ASSERT_TRUE(formatter_or.ok());
auto formatter = std::move(formatter_or.value());
formatter.BeginObject("Test");
formatter.BeginArray("empty_array");
formatter.EndArray();
formatter.EndObject();
std::string output = formatter.GetOutput();
EXPECT_THAT(output, ::testing::HasSubstr("\"empty_array\": []"));
}
} // namespace resources
} // namespace cli
} // namespace yaze

176
test/e2e/README.md Normal file
View File

@@ -0,0 +1,176 @@
# End-to-End (E2E) Tests
This directory contains E2E tests using ImGui Test Engine to validate complete user workflows.
## Active Tests
### ✅ Working Tests
1. **framework_smoke_test.cc** - Basic framework validation
2. **canvas_selection_test.cc** - Canvas selection and copy/paste workflow
3. **dungeon_editor_smoke_test.cc** - Dungeon editor UI navigation and interaction
4. **overworld/overworld_e2e_test.cc** - Overworld editor workflows
5. **rom_dependent/e2e_rom_test.cc** - ROM-dependent functionality tests
6. **zscustomoverworld/zscustomoverworld_upgrade_test.cc** - ZSCustomOverworld upgrade tests
### 📝 Dungeon Editor Smoke Test
**File**: `dungeon_editor_smoke_test.cc`
**Status**: ✅ Working and registered
Tests complete dungeon editor workflow:
- ROM loading
- Editor window opening
- Room selection (0x00, 0x01, 0x02)
- Canvas interaction
- Tab navigation (Object Selector, Room Graphics, Object Editor, Entrances)
- Mode button verification (Select, Insert, Edit)
- Detailed logging at each step
## Running Tests
### All E2E Tests (GUI Mode)
```bash
./build/bin/yaze_test --show-gui
```
### Specific Test Category
```bash
./build/bin/yaze_test --show-gui --gtest_filter="E2ETest*"
```
### Dungeon Editor Test Only
```bash
./build/bin/yaze_test --show-gui --gtest_filter="*DungeonEditorSmokeTest"
```
## Test Development
### Creating New Tests
Follow the pattern in `dungeon_editor_smoke_test.cc`:
```cpp
#include "e2e/my_new_test.h"
#include "test_utils.h"
#include "app/core/controller.h"
void E2ETest_MyNewTest(ImGuiTestContext* ctx) {
// Load ROM
yaze::test::gui::LoadRomInTest(ctx, "zelda3.sfc");
// Open editor
yaze::test::gui::OpenEditorInTest(ctx, "My Editor");
// Test interactions with logging
ctx->LogInfo("Starting test...");
ctx->WindowFocus("My Editor");
ctx->ItemClick("MyButton");
ctx->LogInfo("Test completed");
}
```
### Register in yaze_test.cc
```cpp
#include "e2e/my_new_test.h"
// In RunGuiMode():
ImGuiTest* my_test = IM_REGISTER_TEST(engine, "E2ETest", "MyNewTest");
my_test->TestFunc = E2ETest_MyNewTest;
my_test->UserData = &controller;
```
### ImGui Test Engine API
Key methods available:
- `ctx->WindowFocus("WindowName")` - Focus a window
- `ctx->SetRef("WindowName")` - Set reference window for relative queries
- `ctx->ItemClick("ButtonName")` - Click an item
- `ctx->ItemExists("ItemName")` - Check if item exists
- `ctx->LogInfo("message", ...)` - Log information
- `ctx->LogWarning("message", ...)` - Log warning
- `ctx->LogError("message", ...)` - Log error
- `ctx->Yield()` - Yield to allow UI to update
Full API: `src/lib/imgui_test_engine/imgui_te_engine.h`
## Test Logging
Tests log detailed information during execution. View logs:
- In GUI mode: Check ImGui Test Engine window
- In CI mode: Check console output
- Look for lines starting with date/time stamps
Example log output:
```
2025-10-04 14:03:38 INFO: === Starting Dungeon Editor E2E Test ===
2025-10-04 14:03:38 INFO: Loading ROM...
2025-10-04 14:03:38 INFO: ROM loaded successfully
2025-10-04 14:03:38 INFO: Opening Dungeon Editor...
```
## Test Infrastructure
### File Organization
```
test/e2e/
├── README.md (this file)
├── framework_smoke_test.{cc,h}
├── canvas_selection_test.{cc,h}
├── dungeon_editor_smoke_test.{cc,h} ← Latest dungeon test
├── overworld/
│ └── overworld_e2e_test.cc
├── rom_dependent/
│ └── e2e_rom_test.cc
└── zscustomoverworld/
└── zscustomoverworld_upgrade_test.cc
```
### Helper Functions
Available in `test_utils.h`:
- `yaze::test::gui::LoadRomInTest(ctx, "zelda3.sfc")` - Load ROM for testing
- `yaze::test::gui::OpenEditorInTest(ctx, "Editor Name")` - Open an editor window
## Future Test Ideas
Potential tests to add:
- [ ] Object placement workflow
- [ ] Object property editing
- [ ] Layer visibility toggling
- [ ] Save workflow validation
- [ ] Sprite editor workflows
- [ ] Palette editor workflows
- [ ] Music editor workflows
## Troubleshooting
### Test Crashes in GUI Mode
- Ensure ROM exists at `assets/zelda3.sfc`
- Check logs for specific error messages
- Try running without `--show-gui` first
### Tests Not Found
- Verify test is registered in `yaze_test.cc`
- Check that files are added to CMakeLists.txt
- Rebuild: `make -C build yaze_test`
### ImGui Items Not Found
- Use `ctx->ItemExists("ItemName")` to check availability
- Ensure window is focused with `ctx->WindowFocus()`
- Check actual widget IDs in source code (look for `##` suffixes)
## References
- **ImGui Test Engine**: `src/lib/imgui_test_engine/`
- **Test Registration**: `test/yaze_test.cc`
- **Test Utilities**: `test/test_utils.h`
- **Working Examples**: See existing tests in this directory
## Status
**Current State**: E2E testing infrastructure is working with 6+ active tests.
**Test Coverage**: Basic workflows covered; opportunity for expansion.
**Stability**: Tests run reliably in both GUI and CI modes.

View File

@@ -0,0 +1,76 @@
#define IMGUI_DEFINE_MATH_OPERATORS
#include "e2e/canvas_selection_test.h"
#include "app/controller.h"
#include "test_utils.h"
void E2ETest_CanvasSelectionTest(ImGuiTestContext* ctx)
{
yaze::test::gui::LoadRomInTest(ctx, "zelda3.sfc");
yaze::Controller* controller = (yaze::Controller*)ctx->Test->UserData;
yaze::zelda3::Overworld* overworld = controller->overworld();
// 1. Open the Overworld Editor
yaze::test::gui::OpenEditorInTest(ctx, "Overworld Editor");
// 2. Find the canvas
ctx->WindowFocus("Overworld Editor");
ctx->ItemClick("##Canvas");
// 3. Get the original tile data
// We'll check the 2x2 tile area at the paste location (600, 300)
// The tile at (600, 300) is at (75, 37) in tile coordinates.
// The overworld map is 128x128 tiles.
uint16_t orig_tile1 = overworld->GetTile(75, 37);
uint16_t orig_tile2 = overworld->GetTile(76, 37);
uint16_t orig_tile3 = overworld->GetTile(75, 38);
uint16_t orig_tile4 = overworld->GetTile(76, 38);
// 4. Perform a rectangle selection that crosses a 512px boundary
// The canvas is 1024x1024, with the top-left at (0,0).
// We'll select a 2x2 tile area from (510, 256) to (514, 258).
// This will cross the 512px boundary.
ctx->MouseMoveToPos(ImVec2(510, 256));
ctx->MouseDown(0);
ctx->MouseMoveToPos(ImVec2(514, 258));
ctx->MouseUp(0);
// 5. Copy the selection
ctx->KeyDown(ImGuiKey_LeftCtrl);
ctx->KeyPress(ImGuiKey_C);
ctx->KeyUp(ImGuiKey_LeftCtrl);
// 6. Paste the selection
ctx->MouseMoveToPos(ImVec2(600, 300));
ctx->KeyDown(ImGuiKey_LeftCtrl);
ctx->KeyPress(ImGuiKey_V);
ctx->KeyUp(ImGuiKey_LeftCtrl);
// 7. Verify that the pasted tiles are correct
uint16_t new_tile1 = overworld->GetTile(75, 37);
uint16_t new_tile2 = overworld->GetTile(76, 37);
uint16_t new_tile3 = overworld->GetTile(75, 38);
uint16_t new_tile4 = overworld->GetTile(76, 38);
// The bug is that the selection wraps around, so the pasted tiles are incorrect.
// We expect the new tiles to be different from the original tiles.
IM_CHECK_NE(orig_tile1, new_tile1);
IM_CHECK_NE(orig_tile2, new_tile2);
IM_CHECK_NE(orig_tile3, new_tile3);
IM_CHECK_NE(orig_tile4, new_tile4);
// We also expect the pasted tiles to be the same as the selected tiles.
// The selected tiles are at (63, 32) and (64, 32), (63, 33) and (64, 33).
uint16_t selected_tile1 = overworld->GetTile(63, 32);
uint16_t selected_tile2 = overworld->GetTile(64, 32);
uint16_t selected_tile3 = overworld->GetTile(63, 33);
uint16_t selected_tile4 = overworld->GetTile(64, 33);
IM_CHECK_EQ(new_tile1, selected_tile1);
IM_CHECK_EQ(new_tile2, selected_tile2);
IM_CHECK_EQ(new_tile3, selected_tile3);
IM_CHECK_EQ(new_tile4, selected_tile4);
ctx->LogInfo("Original tiles: %d, %d, %d, %d", orig_tile1, orig_tile2, orig_tile3, orig_tile4);
ctx->LogInfo("Selected tiles: %d, %d, %d, %d", selected_tile1, selected_tile2, selected_tile3, selected_tile4);
ctx->LogInfo("New tiles: %d, %d, %d, %d", new_tile1, new_tile2, new_tile3, new_tile4);
}

View File

@@ -0,0 +1,8 @@
#ifndef YAZE_TEST_E2E_CANVAS_SELECTION_TEST_H
#define YAZE_TEST_E2E_CANVAS_SELECTION_TEST_H
#include "imgui_test_engine/imgui_te_context.h"
void E2ETest_CanvasSelectionTest(ImGuiTestContext* ctx);
#endif // YAZE_TEST_E2E_CANVAS_SELECTION_TEST_H

View File

@@ -0,0 +1,124 @@
#include "e2e/dungeon_editor_smoke_test.h"
#include "test_utils.h"
#include "app/controller.h"
#include "imgui_test_engine/imgui_te_context.h"
/**
* @brief Quick smoke test for DungeonEditorV2
*
* Tests the card-based architecture:
* - Independent windows (cards) can be opened/closed
* - Room cards function correctly
* - Basic navigation works
*/
void E2ETest_DungeonEditorV2SmokeTest(ImGuiTestContext* ctx)
{
ctx->LogInfo("=== Starting DungeonEditorV2 Smoke Test ===");
// Load ROM first
ctx->LogInfo("Loading ROM...");
yaze::test::gui::LoadRomInTest(ctx, "zelda3.sfc");
ctx->LogInfo("ROM loaded successfully");
// Open the Dungeon Editor
ctx->LogInfo("Opening Dungeon Editor...");
yaze::test::gui::OpenEditorInTest(ctx, "Dungeon");
ctx->LogInfo("Dungeon Editor opened");
// Test 1: Control Panel Access
ctx->LogInfo("--- Test 1: Control Panel ---");
if (ctx->WindowInfo("Dungeon Controls").Window != nullptr) {
ctx->WindowFocus("Dungeon Controls");
ctx->LogInfo("Dungeon Controls panel is visible");
} else {
ctx->LogWarning("Dungeon Controls panel not visible - may be minimized");
}
// Test 2: Open Room Selector Card
ctx->LogInfo("--- Test 2: Room Selector Card ---");
if (ctx->WindowInfo("Dungeon Controls").Window != nullptr) {
ctx->SetRef("Dungeon Controls");
ctx->ItemClick("Rooms"); // Toggle checkbox
ctx->Yield();
ctx->LogInfo("Toggled Room Selector visibility");
}
// Test 3: Open Room Matrix Card
ctx->LogInfo("--- Test 3: Room Matrix Card ---");
if (ctx->WindowInfo("Dungeon Controls").Window != nullptr) {
ctx->SetRef("Dungeon Controls");
ctx->ItemClick("Matrix"); // Toggle checkbox
ctx->Yield();
ctx->LogInfo("Toggled Room Matrix visibility");
}
// Test 4: Open a Room Card
ctx->LogInfo("--- Test 4: Room Card ---");
// Try to open room 0 by clicking in room selector
if (ctx->WindowInfo("Room Selector").Window != nullptr) {
ctx->SetRef("Room Selector");
// Look for selectable room items
if (ctx->ItemExists("Room 0x00")) {
ctx->ItemDoubleClick("Room 0x00");
ctx->Yield(2);
ctx->LogInfo("Opened Room 0x00 card");
// Verify room card exists
if (ctx->WindowInfo("Room 0x00").Window != nullptr) {
ctx->LogInfo("Room 0x00 card successfully opened");
ctx->SetRef("Room 0x00");
// Test 5: Per-Room Layer Controls
ctx->LogInfo("--- Test 5: Per-Room Layer Controls ---");
if (ctx->ItemExists("Show BG1")) {
ctx->LogInfo("Found per-room BG1 control");
// Toggle it
ctx->ItemClick("Show BG1");
ctx->Yield();
ctx->ItemClick("Show BG1"); // Toggle back
ctx->Yield();
ctx->LogInfo("Per-room layer controls functional");
}
} else {
ctx->LogWarning("Room card did not open");
}
} else {
ctx->LogWarning("Room 0x00 not found in selector");
}
} else {
ctx->LogWarning("Room Selector card not visible");
}
// Test 6: Object Editor Card
ctx->LogInfo("--- Test 6: Object Editor Card ---");
if (ctx->WindowInfo("Dungeon Controls").Window != nullptr) {
ctx->SetRef("Dungeon Controls");
ctx->ItemClick("Objects"); // Toggle checkbox
ctx->Yield();
ctx->LogInfo("Toggled Object Editor visibility");
}
// Test 7: Palette Editor Card
ctx->LogInfo("--- Test 7: Palette Editor Card ---");
if (ctx->WindowInfo("Dungeon Controls").Window != nullptr) {
ctx->SetRef("Dungeon Controls");
ctx->ItemClick("Palette"); // Toggle checkbox
ctx->Yield();
ctx->LogInfo("Toggled Palette Editor visibility");
}
// Test 8: Independent Cards can be closed
ctx->LogInfo("--- Test 8: Close Independent Cards ---");
// Close room card if it's open
if (ctx->WindowInfo("Room 0x00").Window != nullptr) {
ctx->WindowClose("Room 0x00");
ctx->Yield();
ctx->LogInfo("Closed Room 0x00 card");
}
// Final verification
ctx->LogInfo("=== DungeonEditorV2 Smoke Test Completed Successfully ===");
ctx->LogInfo("Card-based architecture is functional");
ctx->LogInfo("Independent windows can be opened and closed");
ctx->LogInfo("Per-room settings are accessible");
}

View File

@@ -0,0 +1,18 @@
#ifndef YAZE_TEST_E2E_DUNGEON_EDITOR_SMOKE_TEST_H
#define YAZE_TEST_E2E_DUNGEON_EDITOR_SMOKE_TEST_H
#include "imgui_test_engine/imgui_te_context.h"
/**
* @brief Quick smoke test for DungeonEditorV2 card-based UI
*
* Tests basic functionality:
* - Opening dungeon editor
* - Opening independent cards (Rooms, Matrix, Objects, etc.)
* - Opening room cards
* - Basic interaction with canvas
*/
void E2ETest_DungeonEditorV2SmokeTest(ImGuiTestContext* ctx);
#endif // YAZE_TEST_E2E_DUNGEON_EDITOR_SMOKE_TEST_H

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,16 @@
#include "e2e/framework_smoke_test.h"
#include "test_utils.h"
#include "imgui.h"
#include "imgui_test_engine/imgui_te_context.h"
// Smoke test for the E2E testing framework.
// This test is run by the `test-gui` command.
// It opens a window, clicks a button, and verifies that the button was clicked.
// The GUI for this test is rendered in `test/yaze_test.cc`.
void E2ETest_FrameworkSmokeTest(ImGuiTestContext* ctx)
{
yaze::test::gui::LoadRomInTest(ctx, "zelda3.sfc");
ctx->SetRef("Hello World Window");
ctx->ItemClick("Button");
ctx->ItemCheck("Clicked 1 times");
}

View File

@@ -0,0 +1,8 @@
#ifndef YAZE_TEST_E2E_FRAMEWORK_SMOKE_TEST_H
#define YAZE_TEST_E2E_FRAMEWORK_SMOKE_TEST_H
#include "imgui_test_engine/imgui_te_context.h"
void E2ETest_FrameworkSmokeTest(ImGuiTestContext* ctx);
#endif // YAZE_TEST_E2E_FRAMEWORK_SMOKE_TEST_H

View File

@@ -0,0 +1,426 @@
#include <gtest/gtest.h>
#include <filesystem>
#include <memory>
#include <vector>
#include <string>
#include "app/rom.h"
#include "zelda3/overworld/overworld.h"
#include "zelda3/overworld/overworld_map.h"
#include "testing.h"
namespace yaze {
namespace test {
/**
* @brief Comprehensive End-to-End Overworld Test Suite
*
* This test suite validates the complete overworld editing workflow:
* 1. Load vanilla ROM and extract golden data
* 2. Apply ZSCustomOverworld ASM patches
* 3. Make various edits to overworld data
* 4. Validate edits are correctly saved and loaded
* 5. Compare before/after states using golden data
* 6. Test integration with existing test infrastructure
*/
class OverworldE2ETest : public ::testing::Test {
protected:
void SetUp() override {
// Skip tests if ROM is not available
if (getenv("YAZE_SKIP_ROM_TESTS")) {
GTEST_SKIP() << "ROM tests disabled";
}
// Get ROM path from environment or use default
const char* rom_path_env = getenv("YAZE_TEST_ROM_PATH");
vanilla_rom_path_ = rom_path_env ? rom_path_env : "zelda3.sfc";
if (!std::filesystem::exists(vanilla_rom_path_)) {
GTEST_SKIP() << "Test ROM not found: " << vanilla_rom_path_;
}
// Create test ROM copies
vanilla_test_path_ = "test_vanilla_e2e.sfc";
edited_test_path_ = "test_edited_e2e.sfc";
golden_data_path_ = "golden_data_e2e.h";
// Copy vanilla ROM for testing
std::filesystem::copy_file(vanilla_rom_path_, vanilla_test_path_);
}
void TearDown() override {
// Clean up test files
std::vector<std::string> test_files = {
vanilla_test_path_, edited_test_path_, golden_data_path_
};
for (const auto& file : test_files) {
if (std::filesystem::exists(file)) {
std::filesystem::remove(file);
}
}
}
// Helper to extract golden data from ROM
absl::Status ExtractGoldenData(const std::string& rom_path,
const std::string& output_path) {
// Run the golden data extractor
std::string command = "./overworld_golden_data_extractor " + rom_path + " " + output_path;
int result = system(command.c_str());
if (result != 0) {
return absl::InternalError("Failed to extract golden data");
}
return absl::OkStatus();
}
// Helper to validate ROM against golden data
bool ValidateROMAgainstGoldenData(Rom& rom, const std::string& /* golden_data_path */) {
// This would load the generated golden data header and compare values
// For now, we'll do basic validation
// Check basic ROM properties
if (rom.title().empty()) return false;
if (rom.size() < 1024*1024) return false; // At least 1MB
// Check ASM version
auto asm_version = rom.ReadByte(0x140145);
if (!asm_version.ok()) return false;
return true;
}
std::string vanilla_rom_path_;
std::string vanilla_test_path_;
std::string edited_test_path_;
std::string golden_data_path_;
};
// Test 1: Extract golden data from vanilla ROM
TEST_F(OverworldE2ETest, ExtractVanillaGoldenData) {
std::unique_ptr<Rom> rom = std::make_unique<Rom>();
ASSERT_OK(rom->LoadFromFile(vanilla_test_path_));
// Extract golden data
ASSERT_OK(ExtractGoldenData(vanilla_test_path_, golden_data_path_));
// Verify golden data file was created
EXPECT_TRUE(std::filesystem::exists(golden_data_path_));
// Validate ROM against golden data
EXPECT_TRUE(ValidateROMAgainstGoldenData(*rom, golden_data_path_));
}
// Test 2: Load and validate vanilla overworld data
TEST_F(OverworldE2ETest, LoadVanillaOverworldData) {
std::unique_ptr<Rom> rom = std::make_unique<Rom>();
ASSERT_OK(rom->LoadFromFile(vanilla_test_path_));
zelda3::Overworld overworld(rom.get());
auto status = overworld.Load(rom.get());
ASSERT_TRUE(status.ok());
// Validate basic overworld structure
EXPECT_TRUE(overworld.is_loaded());
const auto& maps = overworld.overworld_maps();
EXPECT_EQ(maps.size(), 160);
// Validate that we have a vanilla ROM (ASM version 0xFF)
auto asm_version = rom->ReadByte(0x140145);
ASSERT_TRUE(asm_version.ok());
EXPECT_EQ(*asm_version, 0xFF);
// Validate expansion flags for vanilla
EXPECT_FALSE(overworld.expanded_tile16());
EXPECT_FALSE(overworld.expanded_tile32());
// Validate data structures
const auto& entrances = overworld.entrances();
const auto& exits = overworld.exits();
const auto& holes = overworld.holes();
const auto& items = overworld.all_items();
EXPECT_EQ(entrances.size(), 129);
EXPECT_EQ(exits->size(), 0x4F);
EXPECT_EQ(holes.size(), 0x13);
EXPECT_GE(items.size(), 0);
// Validate sprite data (3 game states)
const auto& sprites = overworld.all_sprites();
EXPECT_EQ(sprites.size(), 3);
}
// Test 3: Apply ZSCustomOverworld v3 ASM and validate changes
TEST_F(OverworldE2ETest, ApplyZSCustomOverworldV3) {
std::unique_ptr<Rom> rom = std::make_unique<Rom>();
ASSERT_OK(rom->LoadFromFile(vanilla_test_path_));
// Apply ZSCustomOverworld v3 ASM
// This would typically be done through the editor, but we can simulate it
ASSERT_OK(rom->WriteByte(0x140145, 0x03)); // Set ASM version to v3
// Enable v3 features
ASSERT_OK(rom->WriteByte(0x140146, 0x01)); // Enable main palettes
ASSERT_OK(rom->WriteByte(0x140147, 0x01)); // Enable area-specific BG
ASSERT_OK(rom->WriteByte(0x140148, 0x01)); // Enable subscreen overlay
ASSERT_OK(rom->WriteByte(0x140149, 0x01)); // Enable animated GFX
ASSERT_OK(rom->WriteByte(0x14014A, 0x01)); // Enable custom tile GFX groups
ASSERT_OK(rom->WriteByte(0x14014B, 0x01)); // Enable mosaic
// Save the modified ROM
ASSERT_OK(rom->SaveToFile(Rom::SaveSettings{.filename = edited_test_path_}));
// Reload and validate
std::unique_ptr<Rom> reloaded_rom = std::make_unique<Rom>();
ASSERT_OK(reloaded_rom->LoadFromFile(edited_test_path_));
// Validate ASM version was applied
auto asm_version = reloaded_rom->ReadByte(0x140145);
ASSERT_TRUE(asm_version.ok());
EXPECT_EQ(*asm_version, 0x03);
// Validate feature flags
auto main_palettes = reloaded_rom->ReadByte(0x140146);
auto area_bg = reloaded_rom->ReadByte(0x140147);
auto subscreen_overlay = reloaded_rom->ReadByte(0x140148);
auto animated_gfx = reloaded_rom->ReadByte(0x140149);
auto custom_tiles = reloaded_rom->ReadByte(0x14014A);
auto mosaic = reloaded_rom->ReadByte(0x14014B);
ASSERT_TRUE(main_palettes.ok());
ASSERT_TRUE(area_bg.ok());
ASSERT_TRUE(subscreen_overlay.ok());
ASSERT_TRUE(animated_gfx.ok());
ASSERT_TRUE(custom_tiles.ok());
ASSERT_TRUE(mosaic.ok());
EXPECT_EQ(*main_palettes, 0x01);
EXPECT_EQ(*area_bg, 0x01);
EXPECT_EQ(*subscreen_overlay, 0x01);
EXPECT_EQ(*animated_gfx, 0x01);
EXPECT_EQ(*custom_tiles, 0x01);
EXPECT_EQ(*mosaic, 0x01);
// Load overworld and validate v3 features are detected
zelda3::Overworld overworld(reloaded_rom.get());
auto status = overworld.Load(reloaded_rom.get());
ASSERT_TRUE(status.ok());
// v3 should have expanded features available
EXPECT_TRUE(overworld.expanded_tile16());
EXPECT_TRUE(overworld.expanded_tile32());
}
// Test 4: Make overworld edits and validate persistence
TEST_F(OverworldE2ETest, OverworldEditPersistence) {
std::unique_ptr<Rom> rom = std::make_unique<Rom>();
ASSERT_OK(rom->LoadFromFile(vanilla_test_path_));
// Load overworld
zelda3::Overworld overworld(rom.get());
auto status = overworld.Load(rom.get());
ASSERT_TRUE(status.ok());
// Make some edits to overworld maps
auto* map0 = overworld.mutable_overworld_map(0);
uint8_t original_gfx = map0->area_graphics();
uint8_t original_palette = map0->main_palette();
// Change graphics and palette
map0->set_area_graphics(0x01);
map0->set_main_palette(0x02);
// Save the changes
auto save_maps_status = overworld.SaveOverworldMaps();
ASSERT_TRUE(save_maps_status.ok());
auto save_props_status = overworld.SaveMapProperties();
ASSERT_TRUE(save_props_status.ok());
// Save ROM
ASSERT_OK(rom->SaveToFile(Rom::SaveSettings{.filename = edited_test_path_}));
// Reload ROM and validate changes persisted
std::unique_ptr<Rom> reloaded_rom = std::make_unique<Rom>();
ASSERT_OK(reloaded_rom->LoadFromFile(edited_test_path_));
zelda3::Overworld reloaded_overworld(reloaded_rom.get());
ASSERT_OK(reloaded_overworld.Load(reloaded_rom.get()));
const auto& reloaded_map0 = reloaded_overworld.overworld_map(0);
EXPECT_EQ(reloaded_map0->area_graphics(), 0x01);
EXPECT_EQ(reloaded_map0->main_palette(), 0x02);
}
// Test 5: Validate coordinate calculations match ZScream exactly
TEST_F(OverworldE2ETest, CoordinateCalculationValidation) {
std::unique_ptr<Rom> rom = std::make_unique<Rom>();
ASSERT_OK(rom->LoadFromFile(vanilla_test_path_));
zelda3::Overworld overworld(rom.get());
ASSERT_OK(overworld.Load(rom.get()));
const auto& entrances = overworld.entrances();
EXPECT_EQ(entrances.size(), 129);
// Test coordinate calculation for first 10 entrances
for (int i = 0; i < std::min(10, static_cast<int>(entrances.size())); i++) {
const auto& entrance = entrances[i];
// ZScream coordinate calculation logic
uint16_t map_pos = entrance.map_pos_;
uint16_t map_id = entrance.map_id_;
int position = map_pos >> 1;
int x_coord = position % 64;
int y_coord = position >> 6;
int expected_x = (x_coord * 16) + (((map_id % 64) - (((map_id % 64) / 8) * 8)) * 512);
int expected_y = (y_coord * 16) + (((map_id % 64) / 8) * 512);
EXPECT_EQ(entrance.x_, expected_x) << "Entrance " << i << " X coordinate mismatch";
EXPECT_EQ(entrance.y_, expected_y) << "Entrance " << i << " Y coordinate mismatch";
}
// Test hole coordinate calculation with 0x400 offset
const auto& holes = overworld.holes();
EXPECT_EQ(holes.size(), 0x13);
for (int i = 0; i < std::min(5, static_cast<int>(holes.size())); i++) {
const auto& hole = holes[i];
// ZScream hole coordinate calculation with 0x400 offset
uint16_t map_pos = hole.map_pos_;
uint16_t map_id = hole.map_id_;
int position = map_pos >> 1;
int x_coord = position % 64;
int y_coord = position >> 6;
int expected_x = (x_coord * 16) + (((map_id % 64) - (((map_id % 64) / 8) * 8)) * 512);
int expected_y = (y_coord * 16) + (((map_id % 64) / 8) * 512);
EXPECT_EQ(hole.x_, expected_x) << "Hole " << i << " X coordinate mismatch";
EXPECT_EQ(hole.y_, expected_y) << "Hole " << i << " Y coordinate mismatch";
EXPECT_TRUE(hole.is_hole_) << "Hole " << i << " should be marked as hole";
}
}
// Test 6: Comprehensive before/after validation
TEST_F(OverworldE2ETest, BeforeAfterValidation) {
// Extract golden data from vanilla ROM
ASSERT_OK(ExtractGoldenData(vanilla_test_path_, golden_data_path_));
// Load vanilla ROM and make some changes
std::unique_ptr<Rom> vanilla_rom = std::make_unique<Rom>();
ASSERT_OK(vanilla_rom->LoadFromFile(vanilla_test_path_));
// Store some original values for comparison
auto original_asm_version = vanilla_rom->ReadByte(0x140145);
auto original_graphics_0 = vanilla_rom->ReadByte(0x7C9C); // First map graphics
auto original_palette_0 = vanilla_rom->ReadByte(0x7D1C); // First map palette
ASSERT_TRUE(original_asm_version.ok());
ASSERT_TRUE(original_graphics_0.ok());
ASSERT_TRUE(original_palette_0.ok());
// Make changes
auto write1 = vanilla_rom->WriteByte(0x140145, 0x03); // Apply v3 ASM
ASSERT_TRUE(write1.ok());
auto write2 = vanilla_rom->WriteByte(0x7C9C, 0x01); // Change first map graphics
ASSERT_TRUE(write2.ok());
auto write3 = vanilla_rom->WriteByte(0x7D1C, 0x02); // Change first map palette
ASSERT_TRUE(write3.ok());
// Save modified ROM
ASSERT_OK(vanilla_rom->SaveToFile(Rom::SaveSettings{.filename = edited_test_path_}));
// Reload and validate changes
std::unique_ptr<Rom> modified_rom = std::make_unique<Rom>();
ASSERT_OK(modified_rom->LoadFromFile(edited_test_path_));
auto modified_asm_version = modified_rom->ReadByte(0x140145);
auto modified_graphics_0 = modified_rom->ReadByte(0x7C9C);
auto modified_palette_0 = modified_rom->ReadByte(0x7D1C);
ASSERT_TRUE(modified_asm_version.ok());
ASSERT_TRUE(modified_graphics_0.ok());
ASSERT_TRUE(modified_palette_0.ok());
// Validate changes were applied
EXPECT_EQ(*modified_asm_version, 0x03);
EXPECT_EQ(*modified_graphics_0, 0x01);
EXPECT_EQ(*modified_palette_0, 0x02);
// Validate original values were different
EXPECT_NE(*original_asm_version, *modified_asm_version);
EXPECT_NE(*original_graphics_0, *modified_graphics_0);
EXPECT_NE(*original_palette_0, *modified_palette_0);
}
// Test 7: Integration with RomDependentTestSuite
TEST_F(OverworldE2ETest, RomDependentTestSuiteIntegration) {
std::unique_ptr<Rom> rom = std::make_unique<Rom>();
ASSERT_OK(rom->LoadFromFile(vanilla_test_path_));
// Test that our overworld loading works with RomDependentTestSuite patterns
zelda3::Overworld overworld(rom.get());
auto status = overworld.Load(rom.get());
ASSERT_TRUE(status.ok());
// Validate ROM-dependent features work correctly
EXPECT_TRUE(overworld.is_loaded());
const auto& maps = overworld.overworld_maps();
EXPECT_EQ(maps.size(), 160);
// Test that we can access the same data structures as RomDependentTestSuite
for (int i = 0; i < std::min(10, static_cast<int>(maps.size())); i++) {
const auto& map = maps[i];
// Verify map properties are accessible
EXPECT_GE(map.area_graphics(), 0);
EXPECT_GE(map.main_palette(), 0);
EXPECT_GE(map.area_size(), zelda3::AreaSizeEnum::SmallArea);
EXPECT_LE(map.area_size(), zelda3::AreaSizeEnum::TallArea);
}
// Test that sprite data is accessible (matches RomDependentTestSuite expectations)
const auto& sprites = overworld.all_sprites();
EXPECT_EQ(sprites.size(), 3); // Three game states
// Test that item data is accessible
const auto& items = overworld.all_items();
EXPECT_GE(items.size(), 0);
// Test that entrance/exit data is accessible
const auto& entrances = overworld.entrances();
const auto& exits = overworld.exits();
EXPECT_EQ(entrances.size(), 129);
EXPECT_EQ(exits->size(), 0x4F);
}
// Test 8: Performance and stability testing
TEST_F(OverworldE2ETest, PerformanceAndStability) {
std::unique_ptr<Rom> rom = std::make_unique<Rom>();
ASSERT_OK(rom->LoadFromFile(vanilla_test_path_));
// Test multiple load/unload cycles
for (int cycle = 0; cycle < 5; cycle++) {
zelda3::Overworld overworld(rom.get());
auto status = overworld.Load(rom.get());
ASSERT_TRUE(status.ok()) << "Load failed on cycle " << cycle;
// Validate basic structure
const auto& maps = overworld.overworld_maps();
EXPECT_EQ(maps.size(), 160) << "Map count mismatch on cycle " << cycle;
const auto& entrances = overworld.entrances();
EXPECT_EQ(entrances.size(), 129) << "Entrance count mismatch on cycle " << cycle;
const auto& exits = overworld.exits();
EXPECT_EQ(exits->size(), 0x4F) << "Exit count mismatch on cycle " << cycle;
}
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,272 @@
#include <gtest/gtest.h>
#include <filesystem>
#include <fstream>
#include <memory>
#include <vector>
#include <string>
#include "app/rom.h"
#include "app/transaction.h"
#include "testing.h"
namespace yaze {
namespace test {
/**
* @brief Comprehensive End-to-End ROM testing suite
*
* This test suite validates the complete ROM editing workflow:
* 1. Load vanilla ROM
* 2. Apply various edits (ROM data, graphics, etc.)
* 3. Save changes
* 4. Reload ROM and verify edits persist
* 5. Verify no data corruption occurred
*/
class E2ERomDependentTest : public ::testing::Test {
protected:
void SetUp() override {
// Skip tests if ROM is not available
if (getenv("YAZE_SKIP_ROM_TESTS")) {
GTEST_SKIP() << "ROM tests disabled";
}
// Get ROM path from environment or use default
const char* rom_path_env = getenv("YAZE_TEST_ROM_PATH");
vanilla_rom_path_ = rom_path_env ? rom_path_env : "zelda3.sfc";
if (!std::filesystem::exists(vanilla_rom_path_)) {
GTEST_SKIP() << "Test ROM not found: " << vanilla_rom_path_;
}
// Create test ROM copies
test_rom_path_ = "test_rom_edit.sfc";
backup_rom_path_ = "test_rom_backup.sfc";
// Copy vanilla ROM for testing
std::filesystem::copy_file(vanilla_rom_path_, test_rom_path_);
std::filesystem::copy_file(vanilla_rom_path_, backup_rom_path_);
}
void TearDown() override {
// Clean up test files
if (std::filesystem::exists(test_rom_path_)) {
std::filesystem::remove(test_rom_path_);
}
if (std::filesystem::exists(backup_rom_path_)) {
std::filesystem::remove(backup_rom_path_);
}
}
// Helper to load ROM and verify basic integrity
static absl::Status LoadAndVerifyROM(const std::string& path, std::unique_ptr<Rom>& rom) {
rom = std::make_unique<Rom>();
RETURN_IF_ERROR(rom->LoadFromFile(path));
// Basic ROM integrity checks
EXPECT_EQ(rom->size(), 0x200000) << "ROM size should be 2MB";
EXPECT_NE(rom->data(), nullptr) << "ROM data should not be null";
// Check ROM header
auto header_byte = rom->ReadByte(0x7FC0);
RETURN_IF_ERROR(header_byte.status());
EXPECT_EQ(*header_byte, 0x21) << "ROM should be LoROM format";
return absl::OkStatus();
}
// Helper to verify ROM data integrity by comparing checksums
static bool VerifyROMIntegrity(const std::string& path1, const std::string& path2,
const std::vector<uint32_t>& exclude_ranges = {}) {
std::ifstream file1(path1, std::ios::binary);
std::ifstream file2(path2, std::ios::binary);
if (!file1.is_open() || !file2.is_open()) {
return false;
}
file1.seekg(0, std::ios::end);
file2.seekg(0, std::ios::end);
size_t size1 = file1.tellg();
size_t size2 = file2.tellg();
if (size1 != size2) {
return false;
}
file1.seekg(0);
file2.seekg(0);
std::vector<char> buffer1(size1);
std::vector<char> buffer2(size2);
file1.read(buffer1.data(), size1);
file2.read(buffer2.data(), size2);
// Compare byte by byte, excluding specified ranges
for (size_t i = 0; i < size1; i++) {
bool in_exclude_range = false;
for (const auto& range : exclude_ranges) {
if (i >= (range & 0xFFFFFF) && i < ((range >> 24) & 0xFF)) {
in_exclude_range = true;
break;
}
}
if (!in_exclude_range && buffer1[i] != buffer2[i]) {
return false;
}
}
return true;
}
std::string vanilla_rom_path_;
std::string test_rom_path_;
std::string backup_rom_path_;
};
// Test basic ROM loading and saving
TEST_F(E2ERomDependentTest, BasicROMLoadSave) {
std::unique_ptr<Rom> rom;
ASSERT_OK(LoadAndVerifyROM(vanilla_rom_path_, rom));
// Save ROM to test path
ASSERT_OK(rom->SaveToFile(Rom::SaveSettings{.filename = test_rom_path_}));
// Verify saved ROM matches original
EXPECT_TRUE(VerifyROMIntegrity(vanilla_rom_path_, test_rom_path_));
}
// Test ROM data editing workflow
TEST_F(E2ERomDependentTest, ROMDataEditWorkflow) {
std::unique_ptr<Rom> rom;
ASSERT_OK(LoadAndVerifyROM(vanilla_rom_path_, rom));
// Get initial state
auto initial_byte = rom->ReadByte(0x1000);
ASSERT_TRUE(initial_byte.ok());
// Make edits
ASSERT_OK(rom->WriteByte(0x1000, 0xAA));
ASSERT_OK(rom->WriteByte(0x2000, 0xBB));
ASSERT_OK(rom->WriteWord(0x3000, 0xCCDD));
// Save changes
ASSERT_OK(rom->SaveToFile(Rom::SaveSettings{.filename = test_rom_path_}));
// Reload and verify
std::unique_ptr<Rom> reloaded_rom;
ASSERT_OK(LoadAndVerifyROM(test_rom_path_, reloaded_rom));
auto byte1 = reloaded_rom->ReadByte(0x1000);
ASSERT_OK(byte1.status());
EXPECT_EQ(*byte1, 0xAA);
auto byte2 = reloaded_rom->ReadByte(0x2000);
ASSERT_OK(byte2.status());
EXPECT_EQ(*byte2, 0xBB);
auto word1 = reloaded_rom->ReadWord(0x3000);
ASSERT_OK(word1.status());
EXPECT_EQ(*word1, 0xCCDD);
// Verify other data wasn't corrupted
EXPECT_NE(*byte1, *initial_byte);
}
// Test transaction system with multiple edits
TEST_F(E2ERomDependentTest, TransactionSystem) {
std::unique_ptr<Rom> rom;
ASSERT_OK(LoadAndVerifyROM(vanilla_rom_path_, rom));
// Create transaction
auto transaction = std::make_unique<yaze::Transaction>(*rom);
// Make multiple edits in transaction
transaction->WriteByte(0x1000, 0xAA);
transaction->WriteByte(0x2000, 0xBB);
transaction->WriteWord(0x3000, 0xCCDD);
// Commit the transaction
ASSERT_OK(transaction->Commit());
// Commit transaction
ASSERT_OK(transaction->Commit());
// Save ROM
ASSERT_OK(rom->SaveToFile(Rom::SaveSettings{.filename = test_rom_path_}));
// Reload and verify all changes
std::unique_ptr<Rom> reloaded_rom;
ASSERT_OK(LoadAndVerifyROM(test_rom_path_, reloaded_rom));
auto byte1 = reloaded_rom->ReadByte(0x1000);
ASSERT_OK(byte1.status());
EXPECT_EQ(*byte1, 0xAA);
auto byte2 = reloaded_rom->ReadByte(0x2000);
ASSERT_OK(byte2.status());
EXPECT_EQ(*byte2, 0xBB);
auto word1 = reloaded_rom->ReadWord(0x3000);
ASSERT_OK(word1.status());
EXPECT_EQ(*word1, 0xCCDD);
}
// Test ROM corruption detection
TEST_F(E2ERomDependentTest, CorruptionDetection) {
std::unique_ptr<Rom> rom;
ASSERT_OK(LoadAndVerifyROM(vanilla_rom_path_, rom));
// Corrupt some data
ASSERT_OK(rom->WriteByte(0x1000, 0xFF)); // Corrupt some data
ASSERT_OK(rom->WriteByte(0x2000, 0xAA)); // Corrupt more data
// Save corrupted ROM
ASSERT_OK(rom->SaveToFile(Rom::SaveSettings{.filename = test_rom_path_}));
// Verify corruption is detected
std::unique_ptr<Rom> reloaded_rom;
ASSERT_OK(LoadAndVerifyROM(test_rom_path_, reloaded_rom));
auto corrupt_byte1 = reloaded_rom->ReadByte(0x1000);
ASSERT_OK(corrupt_byte1.status());
EXPECT_EQ(*corrupt_byte1, 0xFF);
auto corrupt_byte2 = reloaded_rom->ReadByte(0x2000);
ASSERT_OK(corrupt_byte2.status());
EXPECT_EQ(*corrupt_byte2, 0xAA);
}
// Test large-scale editing without corruption
TEST_F(E2ERomDependentTest, LargeScaleEditing) {
std::unique_ptr<Rom> rom;
ASSERT_OK(LoadAndVerifyROM(vanilla_rom_path_, rom));
// Edit multiple areas
for (int i = 0; i < 10; i++) {
ASSERT_OK(rom->WriteByte(0x1000 + i, i % 16));
ASSERT_OK(rom->WriteByte(0x2000 + i, (i + 1) % 16));
}
// Save and reload
ASSERT_OK(rom->SaveToFile(Rom::SaveSettings{.filename = test_rom_path_}));
std::unique_ptr<Rom> reloaded_rom;
ASSERT_OK(LoadAndVerifyROM(test_rom_path_, reloaded_rom));
// Verify all changes
for (int i = 0; i < 10; i++) {
auto byte1 = reloaded_rom->ReadByte(0x1000 + i);
ASSERT_OK(byte1.status());
EXPECT_EQ(*byte1, i % 16);
auto byte2 = reloaded_rom->ReadByte(0x2000 + i);
ASSERT_OK(byte2.status());
EXPECT_EQ(*byte2, (i + 1) % 16);
}
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,377 @@
#include <gtest/gtest.h>
#include <filesystem>
#include <memory>
#include <vector>
#include <string>
#include <map>
#include "app/rom.h"
#include "testing.h"
namespace yaze {
namespace test {
/**
* @brief ZSCustomOverworld upgrade testing suite
*
* This test suite validates ZSCustomOverworld version upgrades:
* 1. Vanilla -> v2 upgrade with proper address changes
* 2. v2 -> v3 upgrade with expanded features
* 3. Address validation for each version
* 4. Save compatibility between versions
* 5. Feature enablement/disablement
*/
class ZSCustomOverworldUpgradeTest : public ::testing::Test {
protected:
void SetUp() override {
// Skip tests if ROM is not available
if (getenv("YAZE_SKIP_ROM_TESTS")) {
GTEST_SKIP() << "ROM tests disabled";
}
// Get ROM path from environment or use default
const char* rom_path_env = getenv("YAZE_TEST_ROM_PATH");
vanilla_rom_path_ = rom_path_env ? rom_path_env : "zelda3.sfc";
if (!std::filesystem::exists(vanilla_rom_path_)) {
GTEST_SKIP() << "Test ROM not found: " << vanilla_rom_path_;
}
// Create test ROM copies for each version
vanilla_test_path_ = "test_vanilla.sfc";
v2_test_path_ = "test_v2.sfc";
v3_test_path_ = "test_v3.sfc";
// Copy vanilla ROM for testing
std::filesystem::copy_file(vanilla_rom_path_, vanilla_test_path_);
// Define version-specific addresses and features
InitializeVersionData();
}
void TearDown() override {
// Clean up test files
std::vector<std::string> test_files = {
vanilla_test_path_, v2_test_path_, v3_test_path_
};
for (const auto& file : test_files) {
if (std::filesystem::exists(file)) {
std::filesystem::remove(file);
}
}
}
void InitializeVersionData() {
// Vanilla ROM addresses and values
vanilla_data_ = {
{"version_flag", {0x140145, 0xFF}}, // OverworldCustomASMHasBeenApplied
{"message_ids", {0x3F51D, 0x00}}, // Message ID table start
{"area_graphics", {0x7C9C, 0x00}}, // Area graphics table
{"area_palettes", {0x7D1C, 0x00}}, // Area palettes table
{"screen_sizes", {0x1788D, 0x01}}, // Screen sizes table
{"sprite_sets", {0x7A41, 0x00}}, // Sprite sets table
{"sprite_palettes", {0x7B41, 0x00}}, // Sprite palettes table
};
// v2 ROM addresses and values
v2_data_ = {
{"version_flag", {0x140145, 0x02}}, // v2 version
{"message_ids", {0x1417F8, 0x00}}, // Expanded message ID table
{"area_graphics", {0x7C9C, 0x00}}, // Same as vanilla
{"area_palettes", {0x7D1C, 0x00}}, // Same as vanilla
{"screen_sizes", {0x1788D, 0x01}}, // Same as vanilla
{"sprite_sets", {0x7A41, 0x00}}, // Same as vanilla
{"sprite_palettes", {0x7B41, 0x00}}, // Same as vanilla
{"main_palettes", {0x140160, 0x00}}, // New v2 feature
};
// v3 ROM addresses and values
v3_data_ = {
{"version_flag", {0x140145, 0x03}}, // v3 version
{"message_ids", {0x1417F8, 0x00}}, // Same as v2
{"area_graphics", {0x7C9C, 0x00}}, // Same as vanilla
{"area_palettes", {0x7D1C, 0x00}}, // Same as vanilla
{"screen_sizes", {0x1788D, 0x01}}, // Same as vanilla
{"sprite_sets", {0x7A41, 0x00}}, // Same as vanilla
{"sprite_palettes", {0x7B41, 0x00}}, // Same as vanilla
{"main_palettes", {0x140160, 0x00}}, // Same as v2
{"bg_colors", {0x140000, 0x00}}, // New v3 feature
{"subscreen_overlays", {0x140340, 0x00}}, // New v3 feature
{"animated_gfx", {0x1402A0, 0x00}}, // New v3 feature
{"custom_tiles", {0x140480, 0x00}}, // New v3 feature
};
}
// Helper to apply version-specific patches
absl::Status ApplyVersionPatch(Rom& rom, const std::string& version) {
const auto* data = &vanilla_data_;
if (version == "v2") {
data = &v2_data_;
} else if (version == "v3") {
data = &v3_data_;
}
// Apply version-specific data
for (const auto& [key, value] : *data) {
RETURN_IF_ERROR(rom.WriteByte(value.first, value.second));
}
// Apply version-specific features
if (version == "v2") {
// Enable v2 features
RETURN_IF_ERROR(rom.WriteByte(0x140146, 0x01)); // Enable main palettes
} else if (version == "v3") {
// Enable v3 features
RETURN_IF_ERROR(rom.WriteByte(0x140146, 0x01)); // Enable main palettes
RETURN_IF_ERROR(rom.WriteByte(0x140147, 0x01)); // Enable area-specific BG
RETURN_IF_ERROR(rom.WriteByte(0x140148, 0x01)); // Enable subscreen overlay
RETURN_IF_ERROR(rom.WriteByte(0x140149, 0x01)); // Enable animated GFX
RETURN_IF_ERROR(rom.WriteByte(0x14014A, 0x01)); // Enable custom tile GFX groups
RETURN_IF_ERROR(rom.WriteByte(0x14014B, 0x01)); // Enable mosaic
}
return absl::OkStatus();
}
// Helper to validate version-specific addresses
bool ValidateVersionAddresses(Rom& rom, const std::string& version) {
const auto* data = &vanilla_data_;
if (version == "v2") {
data = &v2_data_;
} else if (version == "v3") {
data = &v3_data_;
}
for (const auto& [key, value] : *data) {
auto byte_value = rom.ReadByte(value.first);
if (!byte_value.ok() || *byte_value != value.second) {
return false;
}
}
return true;
}
std::string vanilla_rom_path_;
std::string vanilla_test_path_;
std::string v2_test_path_;
std::string v3_test_path_;
std::map<std::string, std::pair<uint32_t, uint8_t>> vanilla_data_;
std::map<std::string, std::pair<uint32_t, uint8_t>> v2_data_;
std::map<std::string, std::pair<uint32_t, uint8_t>> v3_data_;
};
// Test vanilla ROM baseline
TEST_F(ZSCustomOverworldUpgradeTest, VanillaBaseline) {
std::unique_ptr<Rom> rom = std::make_unique<Rom>();
ASSERT_OK(rom->LoadFromFile(vanilla_test_path_));
// Validate vanilla addresses
EXPECT_TRUE(ValidateVersionAddresses(*rom, "vanilla"));
// Verify version flag
auto version_byte = rom->ReadByte(0x140145);
ASSERT_TRUE(version_byte.ok());
EXPECT_EQ(*version_byte, 0xFF);
}
// Test vanilla to v2 upgrade
TEST_F(ZSCustomOverworldUpgradeTest, VanillaToV2Upgrade) {
// Load vanilla ROM
std::unique_ptr<Rom> rom = std::make_unique<Rom>();
ASSERT_OK(rom->LoadFromFile(vanilla_test_path_));
// Apply v2 patch
ASSERT_OK(ApplyVersionPatch(*rom, "v2"));
// Validate v2 addresses
EXPECT_TRUE(ValidateVersionAddresses(*rom, "v2"));
// Save v2 ROM
ASSERT_OK(rom->SaveToFile(Rom::SaveSettings{.filename = v2_test_path_}));
// Reload and verify
std::unique_ptr<Rom> reloaded_rom = std::make_unique<Rom>();
ASSERT_OK(reloaded_rom->LoadFromFile(v2_test_path_));
EXPECT_TRUE(ValidateVersionAddresses(*reloaded_rom, "v2"));
auto version_byte = reloaded_rom->ReadByte(0x140145);
ASSERT_TRUE(version_byte.ok());
EXPECT_EQ(*version_byte, 0x02);
}
// Test v2 to v3 upgrade
TEST_F(ZSCustomOverworldUpgradeTest, V2ToV3Upgrade) {
// Load vanilla ROM
std::unique_ptr<Rom> rom = std::make_unique<Rom>();
ASSERT_OK(rom->LoadFromFile(vanilla_test_path_));
// Apply v2 patch first
ASSERT_OK(ApplyVersionPatch(*rom, "v2"));
// Apply v3 patch
ASSERT_OK(ApplyVersionPatch(*rom, "v3"));
// Validate v3 addresses
EXPECT_TRUE(ValidateVersionAddresses(*rom, "v3"));
// Save v3 ROM
ASSERT_OK(rom->SaveToFile(Rom::SaveSettings{.filename = v3_test_path_}));
// Reload and verify
std::unique_ptr<Rom> reloaded_rom = std::make_unique<Rom>();
ASSERT_OK(reloaded_rom->LoadFromFile(v3_test_path_));
EXPECT_TRUE(ValidateVersionAddresses(*reloaded_rom, "v3"));
auto version_byte = reloaded_rom->ReadByte(0x140145);
ASSERT_TRUE(version_byte.ok());
EXPECT_EQ(*version_byte, 0x03);
}
// Test direct vanilla to v3 upgrade
TEST_F(ZSCustomOverworldUpgradeTest, VanillaToV3Upgrade) {
// Load vanilla ROM
std::unique_ptr<Rom> rom = std::make_unique<Rom>();
ASSERT_OK(rom->LoadFromFile(vanilla_test_path_));
// Apply v3 patch directly
ASSERT_OK(ApplyVersionPatch(*rom, "v3"));
// Validate v3 addresses
EXPECT_TRUE(ValidateVersionAddresses(*rom, "v3"));
// Save v3 ROM
ASSERT_OK(rom->SaveToFile(Rom::SaveSettings{.filename = v3_test_path_}));
// Reload and verify
std::unique_ptr<Rom> reloaded_rom = std::make_unique<Rom>();
ASSERT_OK(reloaded_rom->LoadFromFile(v3_test_path_));
EXPECT_TRUE(ValidateVersionAddresses(*reloaded_rom, "v3"));
auto version_byte = reloaded_rom->ReadByte(0x140145);
ASSERT_TRUE(version_byte.ok());
EXPECT_EQ(*version_byte, 0x03);
}
// Test address validation for each version
TEST_F(ZSCustomOverworldUpgradeTest, AddressValidation) {
// Test vanilla addresses
std::unique_ptr<Rom> vanilla_rom = std::make_unique<Rom>();
ASSERT_OK(vanilla_rom->LoadFromFile(vanilla_test_path_));
EXPECT_TRUE(ValidateVersionAddresses(*vanilla_rom, "vanilla"));
// Test v2 addresses
ASSERT_OK(ApplyVersionPatch(*vanilla_rom, "v2"));
EXPECT_TRUE(ValidateVersionAddresses(*vanilla_rom, "v2"));
// Test v3 addresses
ASSERT_OK(ApplyVersionPatch(*vanilla_rom, "v3"));
EXPECT_TRUE(ValidateVersionAddresses(*vanilla_rom, "v3"));
}
// Test feature enablement/disablement
TEST_F(ZSCustomOverworldUpgradeTest, FeatureToggle) {
std::unique_ptr<Rom> rom = std::make_unique<Rom>();
ASSERT_OK(rom->LoadFromFile(vanilla_test_path_));
ASSERT_OK(ApplyVersionPatch(*rom, "v3"));
// Test feature flags
auto main_palettes = rom->ReadByte(0x140146);
auto area_bg = rom->ReadByte(0x140147);
auto subscreen_overlay = rom->ReadByte(0x140148);
auto animated_gfx = rom->ReadByte(0x140149);
auto custom_tiles = rom->ReadByte(0x14014A);
auto mosaic = rom->ReadByte(0x14014B);
ASSERT_TRUE(main_palettes.ok());
ASSERT_TRUE(area_bg.ok());
ASSERT_TRUE(subscreen_overlay.ok());
ASSERT_TRUE(animated_gfx.ok());
ASSERT_TRUE(custom_tiles.ok());
ASSERT_TRUE(mosaic.ok());
EXPECT_EQ(*main_palettes, 0x01); // Main palettes enabled
EXPECT_EQ(*area_bg, 0x01); // Area-specific BG enabled
EXPECT_EQ(*subscreen_overlay, 0x01); // Subscreen overlay enabled
EXPECT_EQ(*animated_gfx, 0x01); // Animated GFX enabled
EXPECT_EQ(*custom_tiles, 0x01); // Custom tile GFX groups enabled
EXPECT_EQ(*mosaic, 0x01); // Mosaic enabled
// Disable some features
ASSERT_OK(rom->WriteByte(0x140147, 0x00)); // Disable area-specific BG
ASSERT_OK(rom->WriteByte(0x140149, 0x00)); // Disable animated GFX
// Verify features are disabled
auto disabled_area_bg = rom->ReadByte(0x140147);
auto disabled_animated_gfx = rom->ReadByte(0x140149);
ASSERT_TRUE(disabled_area_bg.ok());
ASSERT_TRUE(disabled_animated_gfx.ok());
EXPECT_EQ(*disabled_area_bg, 0x00);
EXPECT_EQ(*disabled_animated_gfx, 0x00);
// Re-enable features
ASSERT_OK(rom->WriteByte(0x140147, 0x01));
ASSERT_OK(rom->WriteByte(0x140149, 0x01));
// Verify features are re-enabled
auto reenabled_area_bg = rom->ReadByte(0x140147);
auto reenabled_animated_gfx = rom->ReadByte(0x140149);
ASSERT_TRUE(reenabled_area_bg.ok());
ASSERT_TRUE(reenabled_animated_gfx.ok());
EXPECT_EQ(*reenabled_area_bg, 0x01);
EXPECT_EQ(*reenabled_animated_gfx, 0x01);
}
// Test data integrity during upgrades
TEST_F(ZSCustomOverworldUpgradeTest, DataIntegrity) {
std::unique_ptr<Rom> rom = std::make_unique<Rom>();
ASSERT_OK(rom->LoadFromFile(vanilla_test_path_));
// Store some original data
auto original_graphics = rom->ReadByte(0x7C9C);
auto original_palette = rom->ReadByte(0x7D1C);
auto original_sprite_set = rom->ReadByte(0x7A41);
ASSERT_TRUE(original_graphics.ok());
ASSERT_TRUE(original_palette.ok());
ASSERT_TRUE(original_sprite_set.ok());
// Upgrade to v3
ASSERT_OK(ApplyVersionPatch(*rom, "v3"));
// Verify original data is preserved
auto preserved_graphics = rom->ReadByte(0x7C9C);
auto preserved_palette = rom->ReadByte(0x7D1C);
auto preserved_sprite_set = rom->ReadByte(0x7A41);
ASSERT_TRUE(preserved_graphics.ok());
ASSERT_TRUE(preserved_palette.ok());
ASSERT_TRUE(preserved_sprite_set.ok());
EXPECT_EQ(*preserved_graphics, *original_graphics);
EXPECT_EQ(*preserved_palette, *original_palette);
EXPECT_EQ(*preserved_sprite_set, *original_sprite_set);
// Verify new v3 data is initialized
auto bg_colors = rom->ReadByte(0x140000);
auto subscreen_overlays = rom->ReadByte(0x140340);
auto animated_gfx = rom->ReadByte(0x1402A0);
auto custom_tiles = rom->ReadByte(0x140480);
ASSERT_TRUE(bg_colors.ok());
ASSERT_TRUE(subscreen_overlays.ok());
ASSERT_TRUE(animated_gfx.ok());
ASSERT_TRUE(custom_tiles.ok());
EXPECT_EQ(*bg_colors, 0x00); // BG colors
EXPECT_EQ(*subscreen_overlays, 0x00); // Subscreen overlays
EXPECT_EQ(*animated_gfx, 0x00); // Animated GFX
EXPECT_EQ(*custom_tiles, 0x00); // Custom tiles
}
} // namespace test
} // namespace yaze

View File

@@ -1,134 +0,0 @@
#include "app/emu/audio/apu.h"
#include "app/emu/memory/memory.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::Apu;
using yaze::emu::MemoryImpl;
class ApuTest : public ::testing::Test {
protected:
void SetUp() override {
memory_ = std::make_unique<MemoryImpl>();
apu_ = std::make_unique<Apu>(*memory_);
apu_->Init();
}
std::unique_ptr<MemoryImpl> memory_;
std::unique_ptr<Apu> apu_;
};
// Test the IPL ROM handshake sequence timing
TEST_F(ApuTest, IplRomHandshakeTiming) {
// 1. Initial state check
EXPECT_EQ(apu_->Read(0x00) & 0x80, 0); // Ready bit should be clear
// 2. Start handshake
apu_->Write(0x00, 0x80); // Set control register bit 7
// 3. Wait for APU ready signal with cycle counting
int cycles = 0;
const int max_cycles = 1000; // Maximum expected cycles for handshake
while (!(apu_->Read(0x00) & 0x80) && cycles < max_cycles) {
apu_->RunCycles(1);
cycles++;
}
// 4. Verify timing constraints
EXPECT_LT(cycles, max_cycles); // Should complete within max cycles
EXPECT_GT(cycles, 0); // Should take some cycles
EXPECT_TRUE(apu_->Read(0x00) & 0x80); // Ready bit should be set
// 5. Verify handshake completion
EXPECT_EQ(apu_->GetStatus() & 0x80, 0x80); // Ready bit in status register
}
// Test APU initialization sequence
TEST_F(ApuTest, ApuInitialization) {
// 1. Check initial state
EXPECT_EQ(apu_->GetStatus(), 0x00);
EXPECT_EQ(apu_->GetControl(), 0x00);
// 2. Initialize APU
apu_->Init();
// 3. Verify initialization
EXPECT_EQ(apu_->GetStatus(), 0x00);
EXPECT_EQ(apu_->GetControl(), 0x00);
// 4. Check DSP registers are initialized
for (int i = 0; i < 128; i++) {
EXPECT_EQ(apu_->Read(0x00 + i), 0x00);
}
}
// Test sample generation and timing
TEST_F(ApuTest, SampleGenerationTiming) {
// 1. Generate samples
const int sample_count = 1024;
std::vector<int16_t> samples(sample_count);
// 2. Measure timing
uint64_t start_cycles = apu_->GetCycles();
apu_->GetSamples(samples.data(), sample_count, false);
uint64_t end_cycles = apu_->GetCycles();
// 3. Verify timing
EXPECT_GT(end_cycles - start_cycles, 0);
// 4. Verify samples
bool has_non_zero = false;
for (int i = 0; i < sample_count; ++i) {
if (samples[i] != 0) {
has_non_zero = true;
break;
}
}
EXPECT_TRUE(has_non_zero);
}
// Test DSP register access timing
TEST_F(ApuTest, DspRegisterAccessTiming) {
// 1. Write to DSP registers
const uint8_t test_value = 0x42;
uint64_t start_cycles = apu_->GetCycles();
apu_->Write(0x00, 0x80); // Set control register
apu_->Write(0x01, test_value); // Write to DSP address
uint64_t end_cycles = apu_->GetCycles();
// 2. Verify timing
EXPECT_GT(end_cycles - start_cycles, 0);
// 3. Verify register access
EXPECT_EQ(apu_->Read(0x01), test_value);
}
// Test DMA transfer timing
TEST_F(ApuTest, DmaTransferTiming) {
// 1. Prepare DMA data
const uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
// 2. Measure DMA timing
uint64_t start_cycles = apu_->GetCycles();
apu_->WriteDma(0x00, data, sizeof(data));
uint64_t end_cycles = apu_->GetCycles();
// 3. Verify timing
EXPECT_GT(end_cycles - start_cycles, 0);
// 4. Verify DMA transfer
EXPECT_EQ(apu_->Read(0x00), 0x01);
EXPECT_EQ(apu_->Read(0x01), 0x02);
}
} // namespace test
} // namespace yaze

View File

@@ -1,122 +0,0 @@
#include "app/emu/audio/apu.h"
#include "app/emu/memory/memory.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::Apu;
using yaze::emu::MemoryImpl;
class IplHandshakeTest : public ::testing::Test {
protected:
void SetUp() override {
memory_ = std::make_unique<MemoryImpl>();
apu_ = std::make_unique<Apu>(*memory_);
apu_->Init();
}
std::unique_ptr<MemoryImpl> memory_;
std::unique_ptr<Apu> apu_;
};
// Test IPL ROM handshake timing with exact cycle counts
TEST_F(IplHandshakeTest, ExactCycleTiming) {
// 1. Initial state
EXPECT_EQ(apu_->Read(0x00) & 0x80, 0); // Ready bit should be clear
// 2. Start handshake
apu_->Write(0x00, 0x80); // Set control register bit 7
// 3. Run exact number of cycles for handshake
const int expected_cycles = 64; // Expected cycle count for handshake
apu_->RunCycles(expected_cycles);
// 4. Verify handshake completed
EXPECT_TRUE(apu_->Read(0x00) & 0x80); // Ready bit should be set
EXPECT_EQ(apu_->GetStatus() & 0x80, 0x80); // Ready bit in status register
}
// Test IPL ROM handshake timing with cycle range
TEST_F(IplHandshakeTest, CycleRange) {
// 1. Initial state
EXPECT_EQ(apu_->Read(0x00) & 0x80, 0); // Ready bit should be clear
// 2. Start handshake
apu_->Write(0x00, 0x80); // Set control register bit 7
// 3. Wait for handshake with cycle counting
int cycles = 0;
const int min_cycles = 32; // Minimum expected cycles
const int max_cycles = 96; // Maximum expected cycles
while (!(apu_->Read(0x00) & 0x80) && cycles < max_cycles) {
apu_->RunCycles(1);
cycles++;
}
// 4. Verify timing constraints
EXPECT_GE(cycles, min_cycles); // Should take at least min_cycles
EXPECT_LE(cycles, max_cycles); // Should complete within max_cycles
EXPECT_TRUE(apu_->Read(0x00) & 0x80); // Ready bit should be set
}
// Test IPL ROM handshake with multiple attempts
TEST_F(IplHandshakeTest, MultipleAttempts) {
const int num_attempts = 10;
std::vector<int> cycle_counts;
for (int i = 0; i < num_attempts; i++) {
// Reset APU
apu_->Init();
// Start handshake
apu_->Write(0x00, 0x80);
// Count cycles until ready
int cycles = 0;
while (!(apu_->Read(0x00) & 0x80) && cycles < 1000) {
apu_->RunCycles(1);
cycles++;
}
// Record cycle count
cycle_counts.push_back(cycles);
// Verify handshake completed
EXPECT_TRUE(apu_->Read(0x00) & 0x80);
}
// Verify cycle count consistency
int min_cycles = *std::min_element(cycle_counts.begin(), cycle_counts.end());
int max_cycles = *std::max_element(cycle_counts.begin(), cycle_counts.end());
EXPECT_LE(max_cycles - min_cycles, 2); // Cycle count should be consistent
}
// Test IPL ROM handshake with interrupts
TEST_F(IplHandshakeTest, WithInterrupts) {
// 1. Initial state
EXPECT_EQ(apu_->Read(0x00) & 0x80, 0);
// 2. Enable interrupts
apu_->Write(0x00, 0x80 | 0x40); // Set control register bits 7 and 6
// 3. Run cycles with interrupts
int cycles = 0;
while (!(apu_->Read(0x00) & 0x80) && cycles < 1000) {
apu_->RunCycles(1);
cycles++;
}
// 4. Verify handshake completed
EXPECT_TRUE(apu_->Read(0x00) & 0x80);
EXPECT_EQ(apu_->GetStatus() & 0x80, 0x80);
}
} // namespace test
} // namespace yaze

File diff suppressed because it is too large Load Diff

View File

@@ -1,54 +0,0 @@
#include "app/emu/video/ppu.h"
#include <gmock/gmock.h>
#include "mocks/mock_memory.h"
namespace yaze {
namespace test {
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;
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

View File

@@ -1,474 +0,0 @@
#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,334 @@
// Integration tests for AIGUIController
// Tests the gRPC GUI automation with vision feedback
#include "cli/service/ai/ai_gui_controller.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "cli/service/ai/gemini_ai_service.h"
#include "cli/service/gui/gui_automation_client.h"
namespace yaze {
namespace cli {
namespace ai {
namespace {
using ::testing::_;
using ::testing::Return;
// Mock GuiAutomationClient for testing without actual GUI
class MockGuiAutomationClient : public GuiAutomationClient {
public:
MockGuiAutomationClient() : GuiAutomationClient("localhost:50052") {}
MOCK_METHOD(absl::Status, Connect, ());
MOCK_METHOD(absl::StatusOr<AutomationResult>, Ping, (const std::string&));
MOCK_METHOD(absl::StatusOr<AutomationResult>, Click,
(const std::string&, ClickType));
MOCK_METHOD(absl::StatusOr<AutomationResult>, Type,
(const std::string&, const std::string&, bool));
MOCK_METHOD(absl::StatusOr<AutomationResult>, Wait,
(const std::string&, int, int));
MOCK_METHOD(absl::StatusOr<AutomationResult>, Assert,
(const std::string&));
};
class AIGUIControllerTest : public ::testing::Test {
protected:
void SetUp() override {
// Create mock services
GeminiConfig config;
config.api_key = "test_key";
config.model = "gemini-2.5-flash";
gemini_service_ = std::make_unique<GeminiAIService>(config);
gui_client_ = std::make_unique<MockGuiAutomationClient>();
controller_ = std::make_unique<AIGUIController>(
gemini_service_.get(), gui_client_.get());
ControlLoopConfig loop_config;
loop_config.max_iterations = 5;
loop_config.enable_vision_verification = false; // Disable for unit tests
loop_config.enable_iterative_refinement = false;
controller_->Initialize(loop_config);
}
std::unique_ptr<GeminiAIService> gemini_service_;
std::unique_ptr<MockGuiAutomationClient> gui_client_;
std::unique_ptr<AIGUIController> controller_;
};
// ============================================================================
// Basic Action Execution Tests
// ============================================================================
TEST_F(AIGUIControllerTest, ExecuteClickAction_Success) {
AIAction action(AIActionType::kClickButton);
action.parameters["target"] = "button:Test";
action.parameters["click_type"] = "left";
AutomationResult result;
result.success = true;
result.message = "Click successful";
EXPECT_CALL(*gui_client_, Click("button:Test", ClickType::kLeft))
.WillOnce(Return(result));
auto status = controller_->ExecuteSingleAction(action, false);
ASSERT_TRUE(status.ok()) << status.status().message();
EXPECT_TRUE(status->action_successful);
}
TEST_F(AIGUIControllerTest, ExecuteClickAction_Failure) {
AIAction action(AIActionType::kClickButton);
action.parameters["target"] = "button:NonExistent";
AutomationResult result;
result.success = false;
result.message = "Button not found";
EXPECT_CALL(*gui_client_, Click("button:NonExistent", ClickType::kLeft))
.WillOnce(Return(result));
auto status = controller_->ExecuteSingleAction(action, false);
EXPECT_FALSE(status.ok());
EXPECT_THAT(status.status().message(),
::testing::HasSubstr("Click action failed"));
}
// ============================================================================
// Type Action Tests
// ============================================================================
TEST_F(AIGUIControllerTest, ExecuteTypeAction_Success) {
AIAction action(AIActionType::kSelectTile); // Using SelectTile as a type action
action.parameters["target"] = "input:TileID";
action.parameters["text"] = "0x42";
action.parameters["clear_first"] = "true";
AutomationResult result;
result.success = true;
result.message = "Text entered";
EXPECT_CALL(*gui_client_, Type("input:TileID", "0x42", true))
.WillOnce(Return(result));
auto status = controller_->ExecuteSingleAction(action, false);
ASSERT_TRUE(status.ok());
EXPECT_TRUE(status->action_successful);
}
// ============================================================================
// Wait Action Tests
// ============================================================================
TEST_F(AIGUIControllerTest, ExecuteWaitAction_Success) {
AIAction action(AIActionType::kWait);
action.parameters["condition"] = "window:OverworldEditor";
action.parameters["timeout_ms"] = "2000";
AutomationResult result;
result.success = true;
result.message = "Condition met";
EXPECT_CALL(*gui_client_, Wait("window:OverworldEditor", 2000, 100))
.WillOnce(Return(result));
auto status = controller_->ExecuteSingleAction(action, false);
ASSERT_TRUE(status.ok());
EXPECT_TRUE(status->action_successful);
}
TEST_F(AIGUIControllerTest, ExecuteWaitAction_Timeout) {
AIAction action(AIActionType::kWait);
action.parameters["condition"] = "window:NonExistentWindow";
action.parameters["timeout_ms"] = "100";
AutomationResult result;
result.success = false;
result.message = "Timeout waiting for condition";
EXPECT_CALL(*gui_client_, Wait("window:NonExistentWindow", 100, 100))
.WillOnce(Return(result));
auto status = controller_->ExecuteSingleAction(action, false);
EXPECT_FALSE(status.ok());
}
// ============================================================================
// Verify/Assert Action Tests
// ============================================================================
TEST_F(AIGUIControllerTest, ExecuteVerifyAction_Success) {
AIAction action(AIActionType::kVerifyTile);
action.parameters["condition"] = "tile_placed";
AutomationResult result;
result.success = true;
result.message = "Assertion passed";
result.expected_value = "0x42";
result.actual_value = "0x42";
EXPECT_CALL(*gui_client_, Assert("tile_placed"))
.WillOnce(Return(result));
auto status = controller_->ExecuteSingleAction(action, false);
ASSERT_TRUE(status.ok());
EXPECT_TRUE(status->action_successful);
}
TEST_F(AIGUIControllerTest, ExecuteVerifyAction_Failure) {
AIAction action(AIActionType::kVerifyTile);
action.parameters["condition"] = "tile_placed";
AutomationResult result;
result.success = false;
result.message = "Assertion failed";
result.expected_value = "0x42";
result.actual_value = "0x00";
EXPECT_CALL(*gui_client_, Assert("tile_placed"))
.WillOnce(Return(result));
auto status = controller_->ExecuteSingleAction(action, false);
EXPECT_FALSE(status.ok());
EXPECT_THAT(status.status().message(),
::testing::HasSubstr("Assert action failed"));
EXPECT_THAT(status.status().message(),
::testing::HasSubstr("expected: 0x42"));
EXPECT_THAT(status.status().message(),
::testing::HasSubstr("actual: 0x00"));
}
// ============================================================================
// Complex Tile Placement Action Tests
// ============================================================================
TEST_F(AIGUIControllerTest, ExecutePlaceTileAction_CompleteFlow) {
AIAction action(AIActionType::kPlaceTile);
action.parameters["map_id"] = "5";
action.parameters["x"] = "10";
action.parameters["y"] = "20";
action.parameters["tile"] = "0x42";
AutomationResult result;
result.success = true;
// Expect sequence: open menu, wait for window, set map ID, click position
testing::InSequence seq;
EXPECT_CALL(*gui_client_, Click("menu:Overworld", ClickType::kLeft))
.WillOnce(Return(result));
EXPECT_CALL(*gui_client_, Wait("window:Overworld Editor", 2000, 100))
.WillOnce(Return(result));
EXPECT_CALL(*gui_client_, Type("input:Map ID", "5", true))
.WillOnce(Return(result));
EXPECT_CALL(*gui_client_, Click(::testing::_, ClickType::kLeft))
.WillOnce(Return(result));
auto status = controller_->ExecuteSingleAction(action, false);
ASSERT_TRUE(status.ok()) << status.status().message();
EXPECT_TRUE(status->action_successful);
}
// ============================================================================
// Multiple Actions Execution Tests
// ============================================================================
TEST_F(AIGUIControllerTest, ExecuteActions_MultipleActionsSuccess) {
std::vector<AIAction> actions;
AIAction action1(AIActionType::kClickButton);
action1.parameters["target"] = "button:Overworld";
actions.push_back(action1);
AIAction action2(AIActionType::kWait);
action2.parameters["condition"] = "window:OverworldEditor";
actions.push_back(action2);
AutomationResult success_result;
success_result.success = true;
EXPECT_CALL(*gui_client_, Click("button:Overworld", ClickType::kLeft))
.WillOnce(Return(success_result));
EXPECT_CALL(*gui_client_, Wait("window:OverworldEditor", 5000, 100))
.WillOnce(Return(success_result));
auto result = controller_->ExecuteActions(actions);
ASSERT_TRUE(result.ok()) << result.status().message();
EXPECT_TRUE(result->success);
EXPECT_EQ(result->actions_executed.size(), 2);
}
TEST_F(AIGUIControllerTest, ExecuteActions_StopsOnFirstFailure) {
std::vector<AIAction> actions;
AIAction action1(AIActionType::kClickButton);
action1.parameters["target"] = "button:Test";
actions.push_back(action1);
AIAction action2(AIActionType::kClickButton);
action2.parameters["target"] = "button:NeverReached";
actions.push_back(action2);
AutomationResult failure_result;
failure_result.success = false;
failure_result.message = "First action failed";
EXPECT_CALL(*gui_client_, Click("button:Test", ClickType::kLeft))
.WillOnce(Return(failure_result));
// Second action should never be called
EXPECT_CALL(*gui_client_, Click("button:NeverReached", _))
.Times(0);
auto result = controller_->ExecuteActions(actions);
EXPECT_FALSE(result.ok());
EXPECT_EQ(result->actions_executed.size(), 1);
}
// ============================================================================
// Error Handling Tests
// ============================================================================
TEST_F(AIGUIControllerTest, ExecuteAction_InvalidActionType) {
AIAction action(AIActionType::kInvalidAction);
auto status = controller_->ExecuteSingleAction(action, false);
EXPECT_FALSE(status.ok());
EXPECT_THAT(status.status().message(),
::testing::HasSubstr("Action type not implemented"));
}
TEST_F(AIGUIControllerTest, ExecutePlaceTileAction_MissingParameters) {
AIAction action(AIActionType::kPlaceTile);
// Missing required parameters
auto status = controller_->ExecuteSingleAction(action, false);
EXPECT_FALSE(status.ok());
EXPECT_THAT(status.status().message(),
::testing::HasSubstr("requires map_id, x, y, and tile"));
}
} // namespace
} // namespace ai
} // namespace cli
} // namespace yaze

View File

@@ -0,0 +1,211 @@
#include "gtest/gtest.h"
#include "absl/strings/str_format.h"
#include "cli/service/ai/ai_action_parser.h"
#include "cli/service/ai/vision_action_refiner.h"
#include "cli/service/ai/ai_gui_controller.h"
#ifdef YAZE_WITH_GRPC
#include "cli/service/gui/gui_automation_client.h"
#include "cli/service/ai/gemini_ai_service.h"
#endif
namespace yaze {
namespace test {
/**
* @brief Integration tests for AI-controlled tile placement
*
* These tests verify the complete pipeline:
* 1. Parse natural language commands
* 2. Execute actions via gRPC
* 3. Verify success with vision analysis
* 4. Refine and retry on failure
*/
class AITilePlacementTest : public ::testing::Test {
protected:
void SetUp() override {
// These tests require YAZE GUI to be running with gRPC test harness
// Skip if not available
}
};
TEST_F(AITilePlacementTest, ParsePlaceTileCommand) {
using namespace cli::ai;
// Test basic tile placement command
auto result = AIActionParser::ParseCommand(
"Place tile 0x42 at overworld position (5, 7)");
ASSERT_TRUE(result.ok()) << result.status().message();
EXPECT_EQ(result->size(), 3); // Select, Place, Save
// Check first action (Select)
EXPECT_EQ(result->at(0).type, AIActionType::kSelectTile);
EXPECT_EQ(result->at(0).parameters.at("tile_id"), "66"); // 0x42 = 66
// Check second action (Place)
EXPECT_EQ(result->at(1).type, AIActionType::kPlaceTile);
EXPECT_EQ(result->at(1).parameters.at("x"), "5");
EXPECT_EQ(result->at(1).parameters.at("y"), "7");
EXPECT_EQ(result->at(1).parameters.at("map_id"), "0");
// Check third action (Save)
EXPECT_EQ(result->at(2).type, AIActionType::kSaveTile);
}
TEST_F(AITilePlacementTest, ParseSelectTileCommand) {
using namespace cli::ai;
auto result = AIActionParser::ParseCommand("Select tile 100");
ASSERT_TRUE(result.ok());
EXPECT_EQ(result->size(), 1);
EXPECT_EQ(result->at(0).type, AIActionType::kSelectTile);
EXPECT_EQ(result->at(0).parameters.at("tile_id"), "100");
}
TEST_F(AITilePlacementTest, ParseOpenEditorCommand) {
using namespace cli::ai;
auto result = AIActionParser::ParseCommand("Open the overworld editor");
ASSERT_TRUE(result.ok());
EXPECT_EQ(result->size(), 1);
EXPECT_EQ(result->at(0).type, AIActionType::kOpenEditor);
EXPECT_EQ(result->at(0).parameters.at("editor"), "overworld");
}
TEST_F(AITilePlacementTest, ActionToStringRoundtrip) {
using namespace cli::ai;
AIAction action(AIActionType::kPlaceTile, {
{"x", "5"},
{"y", "7"},
{"tile_id", "42"}
});
std::string str = AIActionParser::ActionToString(action);
EXPECT_FALSE(str.empty());
EXPECT_TRUE(str.find("5") != std::string::npos);
EXPECT_TRUE(str.find("7") != std::string::npos);
}
#ifdef YAZE_WITH_GRPC
TEST_F(AITilePlacementTest, DISABLED_VisionAnalysisBasic) {
// This test requires Gemini API key
const char* api_key = std::getenv("GEMINI_API_KEY");
if (!api_key || std::string(api_key).empty()) {
GTEST_SKIP() << "GEMINI_API_KEY not set";
}
cli::GeminiConfig config;
config.api_key = api_key;
config.model = "gemini-2.5-flash";
cli::GeminiAIService gemini_service(config);
cli::ai::VisionActionRefiner refiner(&gemini_service);
// Would need actual screenshots for real test
// This is a structure test
EXPECT_TRUE(true);
}
TEST_F(AITilePlacementTest, DISABLED_FullAIControlLoop) {
// This test requires:
// 1. YAZE GUI running with gRPC test harness
// 2. Gemini API key for vision
// 3. Test ROM loaded
const char* api_key = std::getenv("GEMINI_API_KEY");
if (!api_key || std::string(api_key).empty()) {
GTEST_SKIP() << "GEMINI_API_KEY not set";
}
// Initialize services
cli::GeminiConfig gemini_config;
gemini_config.api_key = api_key;
cli::GeminiAIService gemini_service(gemini_config);
cli::GuiAutomationClient gui_client("localhost:50051");
auto connect_status = gui_client.Connect();
if (!connect_status.ok()) {
GTEST_SKIP() << "GUI test harness not available: "
<< connect_status.message();
}
// Create AI controller
cli::ai::AIGUIController controller(&gemini_service, &gui_client);
cli::ai::ControlLoopConfig config;
config.max_iterations = 5;
config.enable_vision_verification = true;
controller.Initialize(config);
// Execute command
auto result = controller.ExecuteCommand(
"Place tile 0x42 at overworld position (5, 7)");
if (result.ok()) {
EXPECT_TRUE(result->success);
EXPECT_GT(result->iterations_performed, 0);
}
}
#endif // YAZE_WITH_GRPC
TEST_F(AITilePlacementTest, ActionRefinement) {
using namespace cli::ai;
// Test refinement logic with a failed action
VisionAnalysisResult analysis;
analysis.action_successful = false;
analysis.error_message = "Element not found";
AIAction original_action(AIActionType::kClickButton, {
{"button", "save"}
});
// Would need VisionActionRefiner for real test
// This verifies the structure compiles
EXPECT_TRUE(true);
}
TEST_F(AITilePlacementTest, MultipleCommandsParsing) {
using namespace cli::ai;
// Test that we can parse multiple commands in sequence
std::vector<std::string> commands = {
"Open overworld editor",
"Select tile 0x42",
"Place tile at position (5, 7)",
"Save changes"
};
for (const auto& cmd : commands) {
auto result = AIActionParser::ParseCommand(cmd);
// At least some should parse successfully
if (result.ok()) {
EXPECT_FALSE(result->empty());
}
}
}
TEST_F(AITilePlacementTest, HexAndDecimalParsing) {
using namespace cli::ai;
// Test hex notation
auto hex_result = AIActionParser::ParseCommand("Select tile 0xFF");
if (hex_result.ok() && !hex_result->empty()) {
EXPECT_EQ(hex_result->at(0).parameters.at("tile_id"), "255");
}
// Test decimal notation
auto dec_result = AIActionParser::ParseCommand("Select tile 255");
if (dec_result.ok() && !dec_result->empty()) {
EXPECT_EQ(dec_result->at(0).parameters.at("tile_id"), "255");
}
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,243 @@
#include <filesystem>
#include <fstream>
#include "gtest/gtest.h"
#include "absl/strings/str_cat.h"
#include "cli/service/ai/gemini_ai_service.h"
#ifdef YAZE_WITH_GRPC
#include "app/service/screenshot_utils.h"
#endif
namespace yaze {
namespace test {
class GeminiVisionTest : public ::testing::Test {
protected:
void SetUp() override {
// Check if GEMINI_API_KEY is set
const char* api_key = std::getenv("GEMINI_API_KEY");
if (!api_key || std::string(api_key).empty()) {
GTEST_SKIP() << "GEMINI_API_KEY not set. Skipping multimodal tests.";
}
api_key_ = api_key;
// Create test data directory
test_dir_ = std::filesystem::temp_directory_path() / "yaze_multimodal_test";
std::filesystem::create_directories(test_dir_);
}
void TearDown() override {
// Clean up test directory
if (std::filesystem::exists(test_dir_)) {
std::filesystem::remove_all(test_dir_);
}
}
// Helper: Create a simple test image (16x16 PNG)
std::filesystem::path CreateTestImage() {
auto image_path = test_dir_ / "test_image.png";
// Create a minimal PNG file (16x16 red square)
// PNG signature + IHDR + IDAT + IEND
const unsigned char png_data[] = {
// PNG signature
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
// IHDR chunk
0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10,
0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x91, 0x68,
0x36,
// IDAT chunk (minimal data)
0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54,
0x08, 0x99, 0x63, 0xF8, 0xCF, 0xC0, 0x00, 0x00,
0x03, 0x01, 0x01, 0x00, 0x18, 0xDD, 0x8D, 0xB4,
// IEND chunk
0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44,
0xAE, 0x42, 0x60, 0x82
};
std::ofstream file(image_path, std::ios::binary);
file.write(reinterpret_cast<const char*>(png_data), sizeof(png_data));
file.close();
return image_path;
}
std::string api_key_;
std::filesystem::path test_dir_;
};
TEST_F(GeminiVisionTest, BasicImageAnalysis) {
cli::GeminiConfig config;
config.api_key = api_key_;
config.model = "gemini-2.5-flash"; // Vision-capable model
config.verbose = false;
cli::GeminiAIService service(config);
// Create test image
auto image_path = CreateTestImage();
ASSERT_TRUE(std::filesystem::exists(image_path));
// Send multimodal request
auto response = service.GenerateMultimodalResponse(
image_path.string(),
"Describe this image in one sentence."
);
ASSERT_TRUE(response.ok()) << response.status().message();
EXPECT_FALSE(response->text_response.empty());
std::cout << "Vision API response: " << response->text_response << std::endl;
}
TEST_F(GeminiVisionTest, ImageWithSpecificPrompt) {
cli::GeminiConfig config;
config.api_key = api_key_;
config.model = "gemini-2.5-flash";
config.verbose = false;
cli::GeminiAIService service(config);
auto image_path = CreateTestImage();
// Ask specific question about the image
auto response = service.GenerateMultimodalResponse(
image_path.string(),
"What color is the dominant color in this image? Answer with just the color name."
);
ASSERT_TRUE(response.ok()) << response.status().message();
EXPECT_FALSE(response->text_response.empty());
// Response should mention "red" since we created a red square
std::string response_lower = response->text_response;
std::transform(response_lower.begin(), response_lower.end(),
response_lower.begin(), ::tolower);
EXPECT_TRUE(response_lower.find("red") != std::string::npos ||
response_lower.find("pink") != std::string::npos)
<< "Expected color 'red' or 'pink' in response: " << response->text_response;
}
TEST_F(GeminiVisionTest, InvalidImagePath) {
cli::GeminiConfig config;
config.api_key = api_key_;
config.model = "gemini-2.5-flash";
cli::GeminiAIService service(config);
// Try with non-existent image
auto response = service.GenerateMultimodalResponse(
"/nonexistent/image.png",
"Describe this image."
);
EXPECT_FALSE(response.ok());
EXPECT_TRUE(absl::IsNotFound(response.status()) ||
absl::IsInternal(response.status()));
}
#ifdef YAZE_WITH_GRPC
// Integration test with screenshot capture
TEST_F(GeminiVisionTest, ScreenshotCaptureIntegration) {
// Note: This test requires a running YAZE instance with gRPC test harness
// Skip if we can't connect
cli::GeminiConfig config;
config.api_key = api_key_;
config.model = "gemini-2.5-flash";
config.verbose = false;
cli::GeminiAIService service(config);
// Attempt to capture a screenshot
auto screenshot_result = yaze::test::CaptureHarnessScreenshot(
(test_dir_ / "screenshot.png").string());
if (!screenshot_result.ok()) {
GTEST_SKIP() << "Screenshot capture failed (YAZE may not be running): "
<< screenshot_result.status().message();
}
// Analyze the captured screenshot
auto response = service.GenerateMultimodalResponse(
screenshot_result->file_path,
"What UI elements are visible in this screenshot? List them."
);
ASSERT_TRUE(response.ok()) << response.status().message();
EXPECT_FALSE(response->text_response.empty());
std::cout << "Screenshot analysis: " << response->text_response << std::endl;
}
#endif
// Performance test
TEST_F(GeminiVisionTest, MultipleRequestsSequential) {
cli::GeminiConfig config;
config.api_key = api_key_;
config.model = "gemini-2.5-flash";
config.verbose = false;
cli::GeminiAIService service(config);
auto image_path = CreateTestImage();
// Make 3 sequential requests
const int num_requests = 3;
for (int i = 0; i < num_requests; ++i) {
auto response = service.GenerateMultimodalResponse(
image_path.string(),
absl::StrCat("Request ", i + 1, ": Describe this image briefly.")
);
ASSERT_TRUE(response.ok()) << "Request " << i + 1 << " failed: "
<< response.status().message();
EXPECT_FALSE(response->text_response.empty());
}
}
// Rate limiting test (should handle gracefully)
TEST_F(GeminiVisionTest, RateLimitHandling) {
cli::GeminiConfig config;
config.api_key = api_key_;
config.model = "gemini-2.5-flash";
config.verbose = false;
cli::GeminiAIService service(config);
auto image_path = CreateTestImage();
// Make many rapid requests (may hit rate limit)
int successful = 0;
int rate_limited = 0;
for (int i = 0; i < 10; ++i) {
auto response = service.GenerateMultimodalResponse(
image_path.string(),
"Describe this image."
);
if (response.ok()) {
successful++;
} else if (absl::IsResourceExhausted(response.status()) ||
response.status().message().find("429") != std::string::npos) {
rate_limited++;
}
}
// At least some requests should succeed
EXPECT_GT(successful, 0) << "No successful requests out of 10";
// If we hit rate limits, that's expected behavior (not a failure)
if (rate_limited > 0) {
std::cout << "Note: Hit rate limit on " << rate_limited << " out of 10 requests (expected)" << std::endl;
}
}
} // namespace test
} // namespace yaze
// Note: main() is provided by yaze_test.cc for the unified test runner

View File

@@ -2,7 +2,7 @@
#include <filesystem>
#include <fstream>
#include "app/core/asar_wrapper.h"
#include "core/asar_wrapper.h"
#include "app/rom.h"
#include "absl/status/status.h"
#include "testing.h"
@@ -17,7 +17,7 @@ namespace integration {
class AsarIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
wrapper_ = std::make_unique<app::core::AsarWrapper>();
wrapper_ = std::make_unique<core::AsarWrapper>();
// Create test directory
test_dir_ = std::filesystem::temp_directory_path() / "yaze_asar_integration";
@@ -322,7 +322,7 @@ error_test:
err_file.close();
}
std::unique_ptr<app::core::AsarWrapper> wrapper_;
std::unique_ptr<core::AsarWrapper> wrapper_;
std::filesystem::path test_dir_;
std::filesystem::path comprehensive_asm_path_;
std::filesystem::path advanced_asm_path_;

View File

@@ -1,8 +1,13 @@
// Must define before any ImGui includes
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
#include <gtest/gtest.h>
#include <filesystem>
#include <fstream>
#include "app/core/asar_wrapper.h"
#include "core/asar_wrapper.h"
#include "app/rom.h"
#include "test_utils.h"
#include "testing.h"
@@ -19,14 +24,14 @@ class AsarRomIntegrationTest : public RomDependentTest {
protected:
void SetUp() override {
RomDependentTest::SetUp();
wrapper_ = std::make_unique<app::core::AsarWrapper>();
wrapper_ = std::make_unique<core::AsarWrapper>();
ASSERT_OK(wrapper_->Initialize());
// Create test directory
test_dir_ = std::filesystem::temp_directory_path() / "yaze_asar_rom_test";
std::filesystem::create_directories(test_dir_);
CreateTestPatches();
}
@@ -226,7 +231,7 @@ enemy_shell:
symbols_file.close();
}
std::unique_ptr<app::core::AsarWrapper> wrapper_;
std::unique_ptr<core::AsarWrapper> wrapper_;
std::filesystem::path test_dir_;
std::filesystem::path simple_patch_path_;
std::filesystem::path gameplay_patch_path_;
@@ -239,15 +244,16 @@ TEST_F(AsarRomIntegrationTest, SimplePatchOnRealRom) {
size_t original_size = rom_copy.size();
// Apply simple patch
auto patch_result = wrapper_->ApplyPatch(simple_patch_path_.string(), rom_copy);
auto patch_result =
wrapper_->ApplyPatch(simple_patch_path_.string(), rom_copy);
ASSERT_OK(patch_result.status());
const auto& result = patch_result.value();
EXPECT_TRUE(result.success) << "Patch failed: "
<< testing::PrintToString(result.errors);
EXPECT_TRUE(result.success)
<< "Patch failed: " << testing::PrintToString(result.errors);
// Verify ROM was modified
EXPECT_NE(rom_copy, test_rom_); // Should be different
EXPECT_NE(rom_copy, test_rom_); // Should be different
EXPECT_GE(rom_copy.size(), original_size); // Size may have grown
// Check for expected symbols
@@ -277,17 +283,16 @@ TEST_F(AsarRomIntegrationTest, SymbolExtractionFromRealRom) {
// Check for specific symbols we expect
std::vector<std::string> expected_symbols = {
"main_routine", "init_player", "game_loop", "update_player",
"update_enemies", "update_graphics", "multiply_by_two", "divide_by_two"
};
"main_routine", "init_player", "game_loop", "update_player",
"update_enemies", "update_graphics", "multiply_by_two", "divide_by_two"};
for (const auto& expected_symbol : expected_symbols) {
bool found = false;
for (const auto& symbol : symbols) {
if (symbol.name == expected_symbol) {
found = true;
EXPECT_GT(symbol.address, 0) << "Symbol " << expected_symbol
<< " has invalid address";
EXPECT_GT(symbol.address, 0)
<< "Symbol " << expected_symbol << " has invalid address";
break;
}
}
@@ -311,19 +316,20 @@ TEST_F(AsarRomIntegrationTest, GameplayModificationPatch) {
std::vector<uint8_t> rom_copy = test_rom_;
// Apply gameplay modification patch
auto patch_result = wrapper_->ApplyPatch(gameplay_patch_path_.string(), rom_copy);
auto patch_result =
wrapper_->ApplyPatch(gameplay_patch_path_.string(), rom_copy);
ASSERT_OK(patch_result.status());
const auto& result = patch_result.value();
EXPECT_TRUE(result.success) << "Gameplay patch failed: "
<< testing::PrintToString(result.errors);
EXPECT_TRUE(result.success)
<< "Gameplay patch failed: " << testing::PrintToString(result.errors);
// Verify specific memory locations were modified
// Note: These addresses are based on the patch content
// Check health modification at 0x7EF36C -> ROM offset would need calculation
// For a proper test, we'd need to convert SNES addresses to ROM offsets
// Check if custom routine was inserted at 0xC000 -> ROM offset 0x18000 (in LoROM)
const uint32_t rom_offset = 0x18000; // Bank $00:C000 in LoROM
if (rom_offset < rom_copy.size()) {
@@ -369,40 +375,43 @@ broken_routine:
broken_file.close();
std::vector<uint8_t> rom_copy = test_rom_;
auto patch_result = wrapper_->ApplyPatch(broken_patch_path.string(), rom_copy);
auto patch_result =
wrapper_->ApplyPatch(broken_patch_path.string(), rom_copy);
// Should fail with proper error messages
EXPECT_FALSE(patch_result.ok());
EXPECT_THAT(patch_result.status().message(),
testing::AnyOf(
testing::HasSubstr("invalid"),
testing::HasSubstr("unknown"),
testing::HasSubstr("error")));
EXPECT_THAT(patch_result.status().message(),
testing::AnyOf(testing::HasSubstr("invalid"),
testing::HasSubstr("unknown"),
testing::HasSubstr("error")));
}
TEST_F(AsarRomIntegrationTest, PatchValidationWorkflow) {
// Test the complete workflow: validate -> patch -> verify
// Step 1: Validate assembly
auto validation_result = wrapper_->ValidateAssembly(simple_patch_path_.string());
auto validation_result =
wrapper_->ValidateAssembly(simple_patch_path_.string());
EXPECT_OK(validation_result);
// Step 2: Apply patch
std::vector<uint8_t> rom_copy = test_rom_;
auto patch_result = wrapper_->ApplyPatch(simple_patch_path_.string(), rom_copy);
auto patch_result =
wrapper_->ApplyPatch(simple_patch_path_.string(), rom_copy);
ASSERT_OK(patch_result.status());
EXPECT_TRUE(patch_result->success);
// Step 3: Verify results
EXPECT_GT(patch_result->symbols.size(), 0);
EXPECT_GT(patch_result->rom_size, 0);
// Step 4: Test symbol operations
auto entry_symbol = wrapper_->FindSymbol("yaze_test_entry");
EXPECT_TRUE(entry_symbol.has_value());
if (entry_symbol) {
auto symbols_at_address = wrapper_->GetSymbolsAtAddress(entry_symbol->address);
auto symbols_at_address =
wrapper_->GetSymbolsAtAddress(entry_symbol->address);
EXPECT_GT(symbols_at_address.size(), 0);
}
}

View File

@@ -1,233 +1,231 @@
#include "integration/dungeon_editor_test.h"
#include <cstring>
#include <vector>
#include "absl/strings/str_format.h"
#include "app/zelda3/dungeon/room.h"
#include "app/zelda3/dungeon/room_object.h"
#include "zelda3/dungeon/room.h"
#include "zelda3/dungeon/room_object.h"
namespace yaze {
namespace test {
void DungeonEditorIntegrationTest::SetUp() {
ASSERT_TRUE(CreateMockRom().ok());
ASSERT_TRUE(LoadTestRoomData().ok());
dungeon_editor_ = std::make_unique<editor::DungeonEditor>(mock_rom_.get());
dungeon_editor_->Initialize();
}
using namespace yaze::zelda3;
void DungeonEditorIntegrationTest::TearDown() {
dungeon_editor_.reset();
mock_rom_.reset();
}
// ============================================================================
// Basic Room Loading Tests
// ============================================================================
absl::Status DungeonEditorIntegrationTest::CreateMockRom() {
mock_rom_ = std::make_unique<MockRom>();
// Generate mock ROM data
std::vector<uint8_t> mock_data(kMockRomSize, 0x00);
// Set up basic ROM structure
// Header at 0x7FC0
std::string title = "ZELDA3 TEST ROM";
std::memcpy(&mock_data[0x7FC0], title.c_str(), std::min(title.length(), size_t(21)));
// Set ROM size and type
mock_data[0x7FD7] = 0x21; // 2MB ROM
mock_data[0x7FD8] = 0x00; // SRAM size
mock_data[0x7FD9] = 0x00; // Country code (NTSC)
mock_data[0x7FDA] = 0x00; // License code
mock_data[0x7FDB] = 0x00; // Version
// Set up room header pointers
mock_data[0xB5DD] = 0x00; // Room header pointer low
mock_data[0xB5DE] = 0x00; // Room header pointer mid
mock_data[0xB5DF] = 0x00; // Room header pointer high
// Set up object pointers
mock_data[0x874C] = 0x00; // Object pointer low
mock_data[0x874D] = 0x00; // Object pointer mid
mock_data[0x874E] = 0x00; // Object pointer high
static_cast<MockRom*>(mock_rom_.get())->SetMockData(mock_data);
return absl::OkStatus();
}
absl::Status DungeonEditorIntegrationTest::LoadTestRoomData() {
// Generate test room data
auto room_header = GenerateMockRoomHeader(kTestRoomId);
auto object_data = GenerateMockObjectData();
auto graphics_data = GenerateMockGraphicsData();
static_cast<MockRom*>(mock_rom_.get())->SetMockRoomData(kTestRoomId, room_header);
static_cast<MockRom*>(mock_rom_.get())->SetMockObjectData(kTestObjectId, object_data);
return absl::OkStatus();
}
absl::Status DungeonEditorIntegrationTest::TestObjectParsing() {
// Test object parsing without SNES emulation
auto room = zelda3::LoadRoomFromRom(mock_rom_.get(), kTestRoomId);
// Verify room was loaded correctly
TEST_F(DungeonEditorIntegrationTest, LoadRoomFromRealRom) {
auto room = zelda3::LoadRoomFromRom(rom_.get(), kTestRoomId);
EXPECT_NE(room.rom(), nullptr);
// Note: room_id_ is private, so we can't directly access it in tests
// Test object loading
room.LoadObjects();
EXPECT_FALSE(room.GetTileObjects().empty());
// Verify object properties
for (const auto& obj : room.GetTileObjects()) {
// Note: id_ is private, so we can't directly access it in tests
EXPECT_LE(obj.x_, 31); // Room width limit
EXPECT_LE(obj.y_, 31); // Room height limit
// Note: rom() method is not const, so we can't call it on const objects
}
return absl::OkStatus();
}
absl::Status DungeonEditorIntegrationTest::TestObjectRendering() {
// Test object rendering without SNES emulation
auto room = zelda3::LoadRoomFromRom(mock_rom_.get(), kTestRoomId);
TEST_F(DungeonEditorIntegrationTest, LoadMultipleRooms) {
// Test loading several different rooms
for (int room_id : {0x00, 0x01, 0x02, 0x10, 0x20}) {
auto room = zelda3::LoadRoomFromRom(rom_.get(), room_id);
EXPECT_NE(room.rom(), nullptr) << "Failed to load room " << std::hex << room_id;
room.LoadObjects();
// Some rooms may be empty, but loading should not fail
}
}
TEST_F(DungeonEditorIntegrationTest, DungeonEditorInitialization) {
// Initialize the editor before loading
dungeon_editor_->Initialize();
// Now load should succeed
auto status = dungeon_editor_->Load();
ASSERT_TRUE(status.ok()) << "Load failed: " << status.message();
}
// ============================================================================
// Object Encoding/Decoding Tests
// ============================================================================
TEST_F(DungeonEditorIntegrationTest, ObjectEncodingRoundTrip) {
auto room = zelda3::LoadRoomFromRom(rom_.get(), kTestRoomId);
room.LoadObjects();
// Test tile loading for objects
for (auto& obj : room.GetTileObjects()) {
obj.EnsureTilesLoaded();
EXPECT_FALSE(obj.tiles_.empty());
auto encoded = room.EncodeObjects();
EXPECT_FALSE(encoded.empty());
EXPECT_EQ(encoded[encoded.size()-1], 0xFF); // Terminator
}
TEST_F(DungeonEditorIntegrationTest, EncodeType1Object) {
// Type 1: xxxxxxss yyyyyyss iiiiiiii (ID < 0x100)
zelda3::RoomObject obj(0x10, 5, 7, 0x12, 0); // id, x, y, size, layer
auto bytes = obj.EncodeObjectToBytes();
// Verify encoding format
EXPECT_EQ((bytes.b1 >> 2), 5) << "X coordinate should be in upper 6 bits of b1";
EXPECT_EQ((bytes.b2 >> 2), 7) << "Y coordinate should be in upper 6 bits of b2";
EXPECT_EQ(bytes.b3, 0x10) << "Object ID should be in b3";
}
TEST_F(DungeonEditorIntegrationTest, EncodeType2Object) {
// Type 2: 111111xx xxxxyyyy yyiiiiii (ID >= 0x100 && < 0x200)
zelda3::RoomObject obj(0x150, 12, 8, 0, 0);
auto bytes = obj.EncodeObjectToBytes();
// Verify Type 2 marker
EXPECT_EQ((bytes.b1 & 0xFC), 0xFC) << "Type 2 objects should have 111111 prefix";
}
TEST_F(DungeonEditorIntegrationTest, EncodeType3Object) {
// Type 3: xxxxxxii yyyyyyii 11111iii (ID >= 0xF00)
zelda3::RoomObject obj(0xF23, 3, 4, 0, 0);
auto bytes = obj.EncodeObjectToBytes();
// Verify Type 3 encoding: bytes.b3 = (id_ >> 4) & 0xFF
// For ID 0xF23: (0xF23 >> 4) = 0xF2
EXPECT_EQ(bytes.b3, 0xF2) << "Type 3: (ID >> 4) should be in b3";
}
// ============================================================================
// Object Manipulation Tests
// ============================================================================
TEST_F(DungeonEditorIntegrationTest, AddObjectToRoom) {
auto room = zelda3::LoadRoomFromRom(rom_.get(), kTestRoomId);
room.LoadObjects();
size_t initial_count = room.GetTileObjects().size();
// Add a new object (Type 1, so size must be <= 15)
zelda3::RoomObject new_obj(0x20, 10, 10, 5, 0);
new_obj.set_rom(rom_.get());
auto status = room.AddObject(new_obj);
EXPECT_TRUE(status.ok()) << "Failed to add object: " << status.message();
EXPECT_EQ(room.GetTileObjects().size(), initial_count + 1);
}
TEST_F(DungeonEditorIntegrationTest, RemoveObjectFromRoom) {
auto room = zelda3::LoadRoomFromRom(rom_.get(), kTestRoomId);
room.LoadObjects();
size_t initial_count = room.GetTileObjects().size();
ASSERT_GT(initial_count, 0) << "Room should have at least one object";
// Remove first object
auto status = room.RemoveObject(0);
EXPECT_TRUE(status.ok()) << "Failed to remove object: " << status.message();
EXPECT_EQ(room.GetTileObjects().size(), initial_count - 1);
}
TEST_F(DungeonEditorIntegrationTest, UpdateObjectInRoom) {
auto room = zelda3::LoadRoomFromRom(rom_.get(), kTestRoomId);
room.LoadObjects();
ASSERT_FALSE(room.GetTileObjects().empty());
// Update first object's position
zelda3::RoomObject updated_obj = room.GetTileObjects()[0];
updated_obj.x_ = 15;
updated_obj.y_ = 15;
auto status = room.UpdateObject(0, updated_obj);
EXPECT_TRUE(status.ok()) << "Failed to update object: " << status.message();
EXPECT_EQ(room.GetTileObjects()[0].x_, 15);
EXPECT_EQ(room.GetTileObjects()[0].y_, 15);
}
// ============================================================================
// Object Validation Tests
// ============================================================================
TEST_F(DungeonEditorIntegrationTest, ValidateObjectBounds) {
auto room = zelda3::LoadRoomFromRom(rom_.get(), kTestRoomId);
// Test objects within valid bounds (0-63 for x and y)
zelda3::RoomObject valid_obj(0x10, 0, 0, 0, 0);
EXPECT_TRUE(room.ValidateObject(valid_obj));
zelda3::RoomObject valid_obj2(0x10, 31, 31, 0, 0);
EXPECT_TRUE(room.ValidateObject(valid_obj2));
zelda3::RoomObject valid_obj3(0x10, 63, 63, 0, 0);
EXPECT_TRUE(room.ValidateObject(valid_obj3));
// Test objects outside bounds (> 63)
zelda3::RoomObject invalid_obj(0x10, 64, 64, 0, 0);
EXPECT_FALSE(room.ValidateObject(invalid_obj));
zelda3::RoomObject invalid_obj2(0x10, 100, 100, 0, 0);
EXPECT_FALSE(room.ValidateObject(invalid_obj2));
}
// ============================================================================
// Save/Load Round-Trip Tests
// ============================================================================
TEST_F(DungeonEditorIntegrationTest, SaveAndReloadRoom) {
auto room = zelda3::LoadRoomFromRom(rom_.get(), kTestRoomId);
room.LoadObjects();
size_t original_count = room.GetTileObjects().size();
// Encode objects
auto encoded = room.EncodeObjects();
EXPECT_FALSE(encoded.empty());
// Create a new room and decode
auto room2 = zelda3::LoadRoomFromRom(rom_.get(), kTestRoomId);
room2.LoadObjects();
// Verify object count matches
EXPECT_EQ(room2.GetTileObjects().size(), original_count);
}
// ============================================================================
// Object Rendering Tests
// ============================================================================
TEST_F(DungeonEditorIntegrationTest, RenderObjectWithTiles) {
auto room = zelda3::LoadRoomFromRom(rom_.get(), kTestRoomId);
room.LoadObjects();
ASSERT_FALSE(room.GetTileObjects().empty());
// Ensure tiles are loaded for first object
auto& obj = room.GetTileObjects()[0];
const_cast<zelda3::RoomObject&>(obj).set_rom(rom_.get());
const_cast<zelda3::RoomObject&>(obj).EnsureTilesLoaded();
EXPECT_FALSE(obj.tiles_.empty()) << "Object should have tiles after loading";
}
// ============================================================================
// Multi-Layer Tests
// ============================================================================
TEST_F(DungeonEditorIntegrationTest, ObjectsOnDifferentLayers) {
auto room = zelda3::LoadRoomFromRom(rom_.get(), kTestRoomId);
// Add objects on different layers
zelda3::RoomObject obj_bg1(0x10, 5, 5, 0, 0); // Layer 0 (BG2)
zelda3::RoomObject obj_bg2(0x11, 6, 6, 0, 1); // Layer 1 (BG1)
zelda3::RoomObject obj_bg3(0x12, 7, 7, 0, 2); // Layer 2 (BG3)
room.AddObject(obj_bg1);
room.AddObject(obj_bg2);
room.AddObject(obj_bg3);
// Encode and verify layer separation
auto encoded = room.EncodeObjects();
// Should have layer terminators (0xFF 0xFF between layers)
int terminator_count = 0;
for (size_t i = 0; i < encoded.size() - 1; i++) {
if (encoded[i] == 0xFF && encoded[i+1] == 0xFF) {
terminator_count++;
}
}
// Test room graphics rendering
room.LoadRoomGraphics();
room.RenderRoomGraphics();
return absl::OkStatus();
}
absl::Status DungeonEditorIntegrationTest::TestRoomGraphics() {
// Test room graphics loading and rendering
auto room = zelda3::LoadRoomFromRom(mock_rom_.get(), kTestRoomId);
// Test graphics loading
room.LoadRoomGraphics();
EXPECT_FALSE(room.blocks().empty());
// Test graphics rendering
room.RenderRoomGraphics();
return absl::OkStatus();
}
absl::Status DungeonEditorIntegrationTest::TestPaletteHandling() {
// Test palette loading and application
auto room = zelda3::LoadRoomFromRom(mock_rom_.get(), kTestRoomId);
// Verify palette is set
EXPECT_GE(room.palette, 0);
EXPECT_LE(room.palette, 0x47); // Max palette index
return absl::OkStatus();
}
std::vector<uint8_t> DungeonEditorIntegrationTest::GenerateMockRoomHeader(int room_id) {
std::vector<uint8_t> header(32, 0x00);
// Basic room properties
header[0] = 0x00; // Background type, collision, light
header[1] = 0x00; // Palette
header[2] = 0x01; // Blockset
header[3] = 0x01; // Spriteset
header[4] = 0x00; // Effect
header[5] = 0x00; // Tag1
header[6] = 0x00; // Tag2
header[7] = 0x00; // Staircase planes
header[8] = 0x00; // Staircase planes continued
header[9] = 0x00; // Hole warp
header[10] = 0x00; // Staircase rooms
header[11] = 0x00;
header[12] = 0x00;
header[13] = 0x00;
return header;
}
std::vector<uint8_t> DungeonEditorIntegrationTest::GenerateMockObjectData() {
std::vector<uint8_t> data;
// Add a simple wall object
data.push_back(0x08); // X position (2 tiles)
data.push_back(0x08); // Y position (2 tiles)
data.push_back(0x01); // Object ID (wall)
// Add layer separator
data.push_back(0xFF);
data.push_back(0xFF);
// Add door section
data.push_back(0xF0);
data.push_back(0xFF);
return data;
}
std::vector<uint8_t> DungeonEditorIntegrationTest::GenerateMockGraphicsData() {
std::vector<uint8_t> data(0x4000, 0x00);
// Generate basic tile data
for (size_t i = 0; i < data.size(); i += 2) {
data[i] = 0x00; // Tile low byte
data[i + 1] = 0x00; // Tile high byte
}
return data;
}
void MockRom::SetMockData(const std::vector<uint8_t>& data) {
mock_data_ = data;
}
void MockRom::SetMockRoomData(int room_id, const std::vector<uint8_t>& data) {
mock_room_data_[room_id] = data;
}
void MockRom::SetMockObjectData(int object_id, const std::vector<uint8_t>& data) {
mock_object_data_[object_id] = data;
}
bool MockRom::ValidateRoomData(int room_id) const {
return mock_room_data_.find(room_id) != mock_room_data_.end();
}
bool MockRom::ValidateObjectData(int object_id) const {
return mock_object_data_.find(object_id) != mock_object_data_.end();
}
// Test cases
TEST_F(DungeonEditorIntegrationTest, ObjectParsingTest) {
EXPECT_TRUE(TestObjectParsing().ok());
}
TEST_F(DungeonEditorIntegrationTest, ObjectRenderingTest) {
EXPECT_TRUE(TestObjectRendering().ok());
}
TEST_F(DungeonEditorIntegrationTest, RoomGraphicsTest) {
EXPECT_TRUE(TestRoomGraphics().ok());
}
TEST_F(DungeonEditorIntegrationTest, PaletteHandlingTest) {
EXPECT_TRUE(TestPaletteHandling().ok());
}
TEST_F(DungeonEditorIntegrationTest, MockRomValidation) {
EXPECT_TRUE(static_cast<MockRom*>(mock_rom_.get())->ValidateRoomData(kTestRoomId));
EXPECT_TRUE(static_cast<MockRom*>(mock_rom_.get())->ValidateObjectData(kTestObjectId));
EXPECT_GE(terminator_count, 2) << "Should have at least 2 layer terminators";
}
} // namespace test
} // namespace yaze
} // namespace yaze

View File

@@ -4,72 +4,56 @@
#include <memory>
#include <string>
#include "absl/status/status.h"
#include "app/editor/dungeon/dungeon_editor.h"
#include "app/editor/dungeon/dungeon_editor_v2.h"
#include "app/rom.h"
#include "zelda3/dungeon/room.h"
#include "gtest/gtest.h"
namespace yaze {
namespace test {
/**
* @brief Integration test framework for dungeon editor components
* @brief Integration test framework using real ROM data
*
* This class provides a comprehensive testing framework for the dungeon editor,
* allowing modular testing of individual components and their interactions.
* Updated for DungeonEditorV2 with card-based architecture
*/
class DungeonEditorIntegrationTest : public ::testing::Test {
protected:
void SetUp() override;
void TearDown() override;
void SetUp() override {
// Use the real ROM (try multiple locations)
rom_ = std::make_unique<Rom>();
auto status = rom_->LoadFromFile("assets/zelda3.sfc");
if (!status.ok()) {
status = rom_->LoadFromFile("build/bin/zelda3.sfc");
}
if (!status.ok()) {
status = rom_->LoadFromFile("zelda3.sfc");
}
ASSERT_TRUE(status.ok()) << "Could not load zelda3.sfc from any location";
ASSERT_TRUE(rom_->InitializeForTesting().ok());
// Initialize DungeonEditorV2 with ROM
dungeon_editor_ = std::make_unique<editor::DungeonEditorV2>();
dungeon_editor_->set_rom(rom_.get());
// Load editor data
auto load_status = dungeon_editor_->Load();
ASSERT_TRUE(load_status.ok()) << "Failed to load dungeon editor: "
<< load_status.message();
}
// Test data setup
absl::Status CreateMockRom();
absl::Status LoadTestRoomData();
// Component testing helpers
absl::Status TestObjectParsing();
absl::Status TestObjectRendering();
absl::Status TestRoomGraphics();
absl::Status TestPaletteHandling();
// Mock data generators
std::vector<uint8_t> GenerateMockRoomHeader(int room_id);
std::vector<uint8_t> GenerateMockObjectData();
std::vector<uint8_t> GenerateMockGraphicsData();
void TearDown() override {
dungeon_editor_.reset();
rom_.reset();
}
std::unique_ptr<Rom> mock_rom_;
std::unique_ptr<editor::DungeonEditor> dungeon_editor_;
std::unique_ptr<Rom> rom_;
std::unique_ptr<editor::DungeonEditorV2> dungeon_editor_;
// Test constants
static constexpr int kTestRoomId = 0x01;
static constexpr int kTestObjectId = 0x10;
static constexpr size_t kMockRomSize = 0x200000; // 2MB mock ROM
};
/**
* @brief Mock ROM class for testing without real ROM files
*/
class MockRom : public Rom {
public:
MockRom() = default;
// Test data injection
void SetMockData(const std::vector<uint8_t>& data);
void SetMockRoomData(int room_id, const std::vector<uint8_t>& data);
void SetMockObjectData(int object_id, const std::vector<uint8_t>& data);
// Validation helpers
bool ValidateRoomData(int room_id) const;
bool ValidateObjectData(int object_id) const;
private:
std::vector<uint8_t> mock_data_;
std::map<int, std::vector<uint8_t>> mock_room_data_;
std::map<int, std::vector<uint8_t>> mock_object_data_;
};
} // namespace test
} // namespace yaze
#endif // YAZE_TEST_INTEGRATION_DUNGEON_EDITOR_TEST_H
#endif // YAZE_TEST_INTEGRATION_DUNGEON_EDITOR_TEST_H

View File

@@ -0,0 +1,250 @@
#include "integration/dungeon_editor_v2_test.h"
namespace yaze {
namespace test {
// ============================================================================
// Basic Initialization Tests
// ============================================================================
TEST_F(DungeonEditorV2IntegrationTest, EditorInitialization) {
// Initialize should not fail
dungeon_editor_v2_->Initialize();
EXPECT_TRUE(dungeon_editor_v2_->rom() != nullptr);
}
TEST_F(DungeonEditorV2IntegrationTest, RomLoadStatus) {
EXPECT_TRUE(dungeon_editor_v2_->IsRomLoaded());
std::string status = dungeon_editor_v2_->GetRomStatus();
EXPECT_FALSE(status.empty());
EXPECT_NE(status, "No ROM loaded");
}
// ============================================================================
// Load Tests - Component Delegation
// ============================================================================
TEST_F(DungeonEditorV2IntegrationTest, LoadAllRooms) {
// Test that Load() properly delegates to room_loader_
dungeon_editor_v2_->Initialize();
auto status = dungeon_editor_v2_->Load();
ASSERT_TRUE(status.ok()) << "Load failed: " << status.message();
}
TEST_F(DungeonEditorV2IntegrationTest, LoadWithoutRom) {
// Test error handling when ROM is not available
editor::DungeonEditorV2 editor(nullptr);
auto status = editor.Load();
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kFailedPrecondition);
}
TEST_F(DungeonEditorV2IntegrationTest, LoadSequence) {
// Test the full initialization sequence
dungeon_editor_v2_->Initialize();
auto load_status = dungeon_editor_v2_->Load();
ASSERT_TRUE(load_status.ok());
// After loading, Update() should work
(void)dungeon_editor_v2_->Update();
}
// ============================================================================
// Update Tests - UI Coordination
// ============================================================================
TEST_F(DungeonEditorV2IntegrationTest, UpdateBeforeLoad) {
// Update before Load should show loading message but not crash
auto status = dungeon_editor_v2_->Update();
EXPECT_TRUE(status.ok());
}
TEST_F(DungeonEditorV2IntegrationTest, UpdateAfterLoad) {
dungeon_editor_v2_->Initialize();
(void)dungeon_editor_v2_->Load();
// Update should delegate to components
auto status = dungeon_editor_v2_->Update();
EXPECT_TRUE(status.ok());
}
// ============================================================================
// Save Tests - Component Delegation
// ============================================================================
TEST_F(DungeonEditorV2IntegrationTest, SaveWithoutRom) {
// Test error handling when ROM is not available
editor::DungeonEditorV2 editor(nullptr);
auto status = editor.Save();
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kFailedPrecondition);
}
TEST_F(DungeonEditorV2IntegrationTest, SaveAfterLoad) {
dungeon_editor_v2_->Initialize();
auto load_status = dungeon_editor_v2_->Load();
ASSERT_TRUE(load_status.ok());
// Save should delegate to room objects
auto save_status = dungeon_editor_v2_->Save();
EXPECT_TRUE(save_status.ok());
}
// ============================================================================
// Room Management Tests
// ============================================================================
TEST_F(DungeonEditorV2IntegrationTest, AddRoomTab) {
dungeon_editor_v2_->Initialize();
(void)dungeon_editor_v2_->Load();
// Add a room tab
dungeon_editor_v2_->add_room(kTestRoomId);
// This should not crash or fail
auto status = dungeon_editor_v2_->Update();
EXPECT_TRUE(status.ok());
}
TEST_F(DungeonEditorV2IntegrationTest, AddMultipleRoomTabs) {
dungeon_editor_v2_->Initialize();
(void)dungeon_editor_v2_->Load();
// Add multiple rooms
dungeon_editor_v2_->add_room(0x00);
dungeon_editor_v2_->add_room(0x01);
dungeon_editor_v2_->add_room(0x02);
auto status = dungeon_editor_v2_->Update();
EXPECT_TRUE(status.ok());
}
// ============================================================================
// Component Delegation Tests
// ============================================================================
TEST_F(DungeonEditorV2IntegrationTest, RoomLoaderDelegation) {
// Verify that Load() delegates to room_loader_
dungeon_editor_v2_->Initialize();
auto status = dungeon_editor_v2_->Load();
// If Load succeeds, room_loader_ must have worked
EXPECT_TRUE(status.ok());
}
TEST_F(DungeonEditorV2IntegrationTest, ComponentsInitializedAfterLoad) {
dungeon_editor_v2_->Initialize();
auto status = dungeon_editor_v2_->Load();
ASSERT_TRUE(status.ok());
// After Load(), all components should be properly initialized
// We can't directly test this, but Update() should work
(void)dungeon_editor_v2_->Update();
}
// ============================================================================
// ROM Management Tests
// ============================================================================
TEST_F(DungeonEditorV2IntegrationTest, SetRomAfterConstruction) {
// Create editor without ROM
editor::DungeonEditorV2 editor;
EXPECT_EQ(editor.rom(), nullptr);
// Set ROM
editor.set_rom(rom_.get());
EXPECT_EQ(editor.rom(), rom_.get());
EXPECT_TRUE(editor.IsRomLoaded());
}
TEST_F(DungeonEditorV2IntegrationTest, SetRomAndLoad) {
// Create editor without ROM
editor::DungeonEditorV2 editor;
// Set ROM and load
editor.set_rom(rom_.get());
editor.Initialize();
auto status = editor.Load();
EXPECT_TRUE(status.ok());
}
// ============================================================================
// Unimplemented Methods Tests
// ============================================================================
TEST_F(DungeonEditorV2IntegrationTest, UnimplementedMethods) {
// These should return UnimplementedError
EXPECT_EQ(dungeon_editor_v2_->Undo().code(),
absl::StatusCode::kUnimplemented);
EXPECT_EQ(dungeon_editor_v2_->Redo().code(),
absl::StatusCode::kUnimplemented);
EXPECT_EQ(dungeon_editor_v2_->Cut().code(),
absl::StatusCode::kUnimplemented);
EXPECT_EQ(dungeon_editor_v2_->Copy().code(),
absl::StatusCode::kUnimplemented);
EXPECT_EQ(dungeon_editor_v2_->Paste().code(),
absl::StatusCode::kUnimplemented);
EXPECT_EQ(dungeon_editor_v2_->Find().code(),
absl::StatusCode::kUnimplemented);
}
// ============================================================================
// Stress Testing
// ============================================================================
TEST_F(DungeonEditorV2IntegrationTest, MultipleUpdateCycles) {
dungeon_editor_v2_->Initialize();
auto load_status = dungeon_editor_v2_->Load();
ASSERT_TRUE(load_status.ok());
// Run multiple update cycles
for (int i = 0; i < 10; i++) {
(void)dungeon_editor_v2_->Update();
}
}
// ============================================================================
// Edge Cases
// ============================================================================
TEST_F(DungeonEditorV2IntegrationTest, InvalidRoomId) {
dungeon_editor_v2_->Initialize();
(void)dungeon_editor_v2_->Load();
// Add invalid room ID (beyond 0x128)
dungeon_editor_v2_->add_room(0x200);
// Update should handle gracefully
auto status = dungeon_editor_v2_->Update();
EXPECT_TRUE(status.ok());
}
TEST_F(DungeonEditorV2IntegrationTest, NegativeRoomId) {
dungeon_editor_v2_->Initialize();
(void)dungeon_editor_v2_->Load();
// Add negative room ID
dungeon_editor_v2_->add_room(-1);
// Update should handle gracefully
auto status = dungeon_editor_v2_->Update();
EXPECT_TRUE(status.ok());
}
TEST_F(DungeonEditorV2IntegrationTest, LoadTwice) {
dungeon_editor_v2_->Initialize();
// Load twice
auto status1 = dungeon_editor_v2_->Load();
auto status2 = dungeon_editor_v2_->Load();
// Both should succeed
EXPECT_TRUE(status1.ok());
EXPECT_TRUE(status2.ok());
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,52 @@
#ifndef YAZE_TEST_INTEGRATION_DUNGEON_EDITOR_V2_TEST_H
#define YAZE_TEST_INTEGRATION_DUNGEON_EDITOR_V2_TEST_H
#include <memory>
#include <string>
#include "app/editor/dungeon/dungeon_editor_v2.h"
#include "app/rom.h"
#include "gtest/gtest.h"
namespace yaze {
namespace test {
/**
* @brief Integration test framework for DungeonEditorV2
*
* Tests the simplified component delegation architecture
*/
class DungeonEditorV2IntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
// Use the real ROM (try multiple locations)
rom_ = std::make_unique<Rom>();
auto status = rom_->LoadFromFile("assets/zelda3.sfc");
if (!status.ok()) {
status = rom_->LoadFromFile("build/bin/zelda3.sfc");
}
if (!status.ok()) {
status = rom_->LoadFromFile("zelda3.sfc");
}
ASSERT_TRUE(status.ok()) << "Could not load zelda3.sfc from any location";
// Create V2 editor with ROM
dungeon_editor_v2_ = std::make_unique<editor::DungeonEditorV2>(rom_.get());
}
void TearDown() override {
dungeon_editor_v2_.reset();
rom_.reset();
}
std::unique_ptr<Rom> rom_;
std::unique_ptr<editor::DungeonEditorV2> dungeon_editor_v2_;
static constexpr int kTestRoomId = 0x01;
};
} // namespace test
} // namespace yaze
#endif // YAZE_TEST_INTEGRATION_DUNGEON_EDITOR_V2_TEST_H

View File

@@ -1,55 +1,71 @@
#define IMGUI_DEFINE_MATH_OPERATORS
#include "test/editor/editor_integration_test.h"
#include "integration/editor/editor_integration_test.h"
#include <SDL.h>
#include "app/core/window.h"
#include "app/gui/style.h"
#include "app/platform/window.h"
#include "app/gui/core/style.h"
#include "imgui/backends/imgui_impl_sdl2.h"
#include "imgui/backends/imgui_impl_sdlrenderer2.h"
#include "imgui/imgui.h"
#ifdef YAZE_ENABLE_IMGUI_TEST_ENGINE
#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"
#endif
namespace yaze {
namespace test {
EditorIntegrationTest::EditorIntegrationTest()
: engine_(nullptr), show_demo_window_(true) {}
#ifdef YAZE_ENABLE_IMGUI_TEST_ENGINE
: engine_(nullptr), show_demo_window_(true)
#else
#endif
{}
EditorIntegrationTest::~EditorIntegrationTest() {
#ifdef YAZE_ENABLE_IMGUI_TEST_ENGINE
if (engine_) {
ImGuiTestEngine_Stop(engine_);
ImGuiTestEngine_DestroyContext(engine_);
}
#endif
}
absl::Status EditorIntegrationTest::Initialize() {
RETURN_IF_ERROR(core::CreateWindow(window_, SDL_WINDOW_RESIZABLE));
// Create renderer for test
test_renderer_ = std::make_unique<gfx::SDL2Renderer>();
RETURN_IF_ERROR(core::CreateWindow(window_, test_renderer_.get(), SDL_WINDOW_RESIZABLE));
IMGUI_CHECKVERSION();
ImGui::CreateContext();
#ifdef YAZE_ENABLE_IMGUI_TEST_ENGINE
// Initialize Test Engine
engine_ = ImGuiTestEngine_CreateContext();
ImGuiTestEngineIO& test_io = ImGuiTestEngine_GetIO(engine_);
test_io.ConfigVerboseLevel = ImGuiTestVerboseLevel_Info;
test_io.ConfigVerboseLevelOnError = ImGuiTestVerboseLevel_Debug;
#endif
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
// Initialize ImGui for SDL
ImGui_ImplSDL2_InitForSDLRenderer(
controller_.window(), yaze::core::Renderer::Get().renderer());
ImGui_ImplSDLRenderer2_Init(yaze::core::Renderer::Get().renderer());
SDL_Renderer* sdl_renderer = static_cast<SDL_Renderer*>(test_renderer_->GetBackendRenderer());
ImGui_ImplSDL2_InitForSDLRenderer(controller_.window(), sdl_renderer);
ImGui_ImplSDLRenderer2_Init(sdl_renderer);
#ifdef YAZE_ENABLE_IMGUI_TEST_ENGINE
// Register tests
RegisterTests(engine_);
ImGuiTestEngine_Start(engine_, ImGui::GetCurrentContext());
#endif
controller_.set_active(true);
// Set the default style
@@ -83,8 +99,10 @@ int EditorIntegrationTest::RunTest() {
absl::Status EditorIntegrationTest::Update() {
ImGui::NewFrame();
#ifdef YAZE_ENABLE_IMGUI_TEST_ENGINE
// Show test engine windows
ImGuiTestEngine_ShowTestEngineWindows(engine_, &show_demo_window_);
#endif
return absl::OkStatus();
}

View File

@@ -3,12 +3,17 @@
#define IMGUI_DEFINE_MATH_OPERATORS
#include "imgui/imgui.h"
#include "app/editor/editor.h"
#include "app/rom.h"
#include "app/core/controller.h"
#include "app/core/window.h"
#include "app/controller.h"
#include "app/platform/window.h"
#include "app/gfx/backend/sdl2_renderer.h"
#ifdef YAZE_ENABLE_IMGUI_TEST_ENGINE
#include "imgui_test_engine/imgui_te_context.h"
#include "imgui_test_engine/imgui_te_engine.h"
#endif
namespace yaze {
namespace test {
@@ -39,8 +44,13 @@ class EditorIntegrationTest {
// Run the test
int RunTest();
#ifdef YAZE_ENABLE_IMGUI_TEST_ENGINE
// Register tests for a specific editor
virtual void RegisterTests(ImGuiTestEngine* engine) = 0;
#else
// Default implementation when ImGui Test Engine is disabled
virtual void RegisterTests(void* engine) {}
#endif
// Update the test environment
virtual absl::Status Update();
@@ -65,11 +75,14 @@ class EditorIntegrationTest {
absl::Status TestEditorClear(editor::Editor* editor);
private:
core::Controller controller_;
Controller controller_;
#ifdef YAZE_ENABLE_IMGUI_TEST_ENGINE
ImGuiTestEngine* engine_;
std::unique_ptr<Rom> test_rom_;
bool show_demo_window_;
#endif
std::unique_ptr<Rom> test_rom_;
core::Window window_;
std::unique_ptr<gfx::SDL2Renderer> test_renderer_;
};
} // namespace test

View File

@@ -6,10 +6,12 @@
#include <gtest/gtest.h>
#include "app/rom.h"
#include "app/gfx/bitmap.h"
#include "app/gfx/tilemap.h"
#include "app/zelda3/overworld/overworld.h"
#include "app/core/window.h"
#include "app/gfx/resource/arena.h"
#include "app/gfx/backend/sdl2_renderer.h"
#include "app/gfx/core/bitmap.h"
#include "app/gfx/render/tilemap.h"
#include "zelda3/overworld/overworld.h"
#include "app/platform/window.h"
namespace yaze {
namespace editor {
@@ -52,20 +54,22 @@ class Tile16EditorIntegrationTest : public ::testing::Test {
auto palette = overworld_->current_area_palette();
tile16_blockset_ = std::make_unique<gfx::Tilemap>(
gfx::CreateTilemap(tile16_data, 0x80, 0x2000, 16,
gfx::CreateTilemap(nullptr, tile16_data, 0x80, 0x2000, 16,
zelda3::kNumTile16Individual, palette));
// Create graphics bitmap
current_gfx_bmp_ = std::make_unique<gfx::Bitmap>();
core::Renderer::Get().CreateAndRenderBitmap(0x80, 512, 0x40,
overworld_->current_graphics(),
*current_gfx_bmp_, palette);
current_gfx_bmp_->Create(0x80, 512, 0x40, overworld_->current_graphics());
current_gfx_bmp_->SetPalette(palette);
gfx::Arena::Get().QueueTextureCommand(
gfx::Arena::TextureCommandType::CREATE, current_gfx_bmp_.get());
// Create tile16 blockset bitmap
tile16_blockset_bmp_ = std::make_unique<gfx::Bitmap>();
core::Renderer::Get().CreateAndRenderBitmap(0x80, 0x2000, 0x08,
tile16_data,
*tile16_blockset_bmp_, palette);
tile16_blockset_bmp_->Create(0x80, 0x2000, 0x08, tile16_data);
tile16_blockset_bmp_->SetPalette(palette);
gfx::Arena::Get().QueueTextureCommand(
gfx::Arena::TextureCommandType::CREATE, tile16_blockset_bmp_.get());
// Initialize the tile16 editor
editor_ = std::make_unique<Tile16Editor>(rom_.get(), tile16_blockset_.get());
@@ -85,7 +89,9 @@ class Tile16EditorIntegrationTest : public ::testing::Test {
protected:
static void InitializeTestEnvironment() {
auto window_result = core::CreateWindow(test_window_, SDL_WINDOW_HIDDEN);
// Create renderer for test
test_renderer_ = std::make_unique<gfx::SDL2Renderer>();
auto window_result = core::CreateWindow(test_window_, test_renderer_.get(), SDL_WINDOW_HIDDEN);
if (window_result.ok()) {
window_initialized_ = true;
} else {
@@ -97,6 +103,7 @@ protected:
static bool window_initialized_;
static core::Window test_window_;
static std::unique_ptr<gfx::SDL2Renderer> test_renderer_;
bool rom_loaded_ = false;
std::unique_ptr<Rom> rom_;
@@ -111,6 +118,7 @@ protected:
// Static member definitions
bool Tile16EditorIntegrationTest::window_initialized_ = false;
core::Window Tile16EditorIntegrationTest::test_window_;
std::unique_ptr<gfx::SDL2Renderer> Tile16EditorIntegrationTest::test_renderer_;
// Basic validation tests (no ROM required)
TEST_F(Tile16EditorIntegrationTest, BasicValidation) {

View File

@@ -0,0 +1,360 @@
#include "app/gfx/util/palette_manager.h"
#include <gtest/gtest.h>
#include "app/gfx/types/snes_color.h"
#include "app/gfx/types/snes_palette.h"
#include "app/rom.h"
namespace yaze {
namespace gfx {
namespace {
// Test fixture for PaletteManager integration tests
class PaletteManagerTest : public ::testing::Test {
protected:
void SetUp() override {
// PaletteManager is a singleton, so we need to reset it between tests
// Note: In a real scenario, we'd need a way to reset the singleton
// For now, we'll work with the existing instance
}
void TearDown() override {
// Clean up any test state
PaletteManager::Get().ClearHistory();
}
};
// ============================================================================
// Initialization Tests
// ============================================================================
TEST_F(PaletteManagerTest, InitializationState) {
auto& manager = PaletteManager::Get();
// Before initialization, should not be initialized
// Note: This might fail if other tests have already initialized it
// In production, we'd need a Reset() method for testing
// After initialization with null ROM, should handle gracefully
manager.Initialize(nullptr);
EXPECT_FALSE(manager.IsInitialized());
}
TEST_F(PaletteManagerTest, HasNoUnsavedChangesInitially) {
auto& manager = PaletteManager::Get();
// Should have no unsaved changes initially
EXPECT_FALSE(manager.HasUnsavedChanges());
EXPECT_EQ(manager.GetModifiedColorCount(), 0);
}
// ============================================================================
// Dirty Tracking Tests
// ============================================================================
TEST_F(PaletteManagerTest, TracksModifiedGroups) {
auto& manager = PaletteManager::Get();
// Initially, no groups should be modified
auto modified_groups = manager.GetModifiedGroups();
EXPECT_TRUE(modified_groups.empty());
}
TEST_F(PaletteManagerTest, GetModifiedColorCount) {
auto& manager = PaletteManager::Get();
// Initially, no colors modified
EXPECT_EQ(manager.GetModifiedColorCount(), 0);
// After initialization and making changes, count should increase
// (This would require a valid ROM to test properly)
}
// ============================================================================
// Undo/Redo Tests
// ============================================================================
TEST_F(PaletteManagerTest, UndoRedoInitialState) {
auto& manager = PaletteManager::Get();
// Initially, should not be able to undo or redo
EXPECT_FALSE(manager.CanUndo());
EXPECT_FALSE(manager.CanRedo());
EXPECT_EQ(manager.GetUndoStackSize(), 0);
EXPECT_EQ(manager.GetRedoStackSize(), 0);
}
TEST_F(PaletteManagerTest, ClearHistoryResetsStacks) {
auto& manager = PaletteManager::Get();
// Clear history should reset both stacks
manager.ClearHistory();
EXPECT_FALSE(manager.CanUndo());
EXPECT_FALSE(manager.CanRedo());
EXPECT_EQ(manager.GetUndoStackSize(), 0);
EXPECT_EQ(manager.GetRedoStackSize(), 0);
}
TEST_F(PaletteManagerTest, UndoWithoutChangesIsNoOp) {
auto& manager = PaletteManager::Get();
EXPECT_FALSE(manager.CanUndo());
// Should not crash
manager.Undo();
EXPECT_FALSE(manager.CanUndo());
}
TEST_F(PaletteManagerTest, RedoWithoutUndoIsNoOp) {
auto& manager = PaletteManager::Get();
EXPECT_FALSE(manager.CanRedo());
// Should not crash
manager.Redo();
EXPECT_FALSE(manager.CanRedo());
}
// ============================================================================
// Batch Operations Tests
// ============================================================================
TEST_F(PaletteManagerTest, BatchModeTracking) {
auto& manager = PaletteManager::Get();
EXPECT_FALSE(manager.InBatch());
manager.BeginBatch();
EXPECT_TRUE(manager.InBatch());
manager.EndBatch();
EXPECT_FALSE(manager.InBatch());
}
TEST_F(PaletteManagerTest, NestedBatchOperations) {
auto& manager = PaletteManager::Get();
EXPECT_FALSE(manager.InBatch());
manager.BeginBatch();
EXPECT_TRUE(manager.InBatch());
manager.BeginBatch(); // Nested
EXPECT_TRUE(manager.InBatch());
manager.EndBatch();
EXPECT_TRUE(manager.InBatch()); // Still in batch (outer)
manager.EndBatch();
EXPECT_FALSE(manager.InBatch()); // Now out of batch
}
TEST_F(PaletteManagerTest, EndBatchWithoutBeginIsNoOp) {
auto& manager = PaletteManager::Get();
EXPECT_FALSE(manager.InBatch());
// Should not crash
manager.EndBatch();
EXPECT_FALSE(manager.InBatch());
}
// ============================================================================
// Change Notification Tests
// ============================================================================
TEST_F(PaletteManagerTest, RegisterAndUnregisterListener) {
auto& manager = PaletteManager::Get();
int callback_count = 0;
auto callback = [&callback_count](const PaletteChangeEvent& event) {
callback_count++;
};
// Register listener
int id = manager.RegisterChangeListener(callback);
EXPECT_GT(id, 0);
// Unregister listener
manager.UnregisterChangeListener(id);
// After unregistering, callback should not be called
// (Would need to trigger an event to test this properly)
}
TEST_F(PaletteManagerTest, MultipleListeners) {
auto& manager = PaletteManager::Get();
int callback1_count = 0;
int callback2_count = 0;
auto callback1 = [&callback1_count](const PaletteChangeEvent& event) {
callback1_count++;
};
auto callback2 = [&callback2_count](const PaletteChangeEvent& event) {
callback2_count++;
};
int id1 = manager.RegisterChangeListener(callback1);
int id2 = manager.RegisterChangeListener(callback2);
EXPECT_NE(id1, id2);
// Clean up
manager.UnregisterChangeListener(id1);
manager.UnregisterChangeListener(id2);
}
// ============================================================================
// Color Query Tests (without ROM)
// ============================================================================
TEST_F(PaletteManagerTest, GetColorWithoutInitialization) {
auto& manager = PaletteManager::Get();
// Getting color without initialization should return default color
SnesColor color = manager.GetColor("ow_main", 0, 0);
// Default SnesColor should have zero values
auto rgb = color.rgb();
EXPECT_FLOAT_EQ(rgb.x, 0.0f);
EXPECT_FLOAT_EQ(rgb.y, 0.0f);
EXPECT_FLOAT_EQ(rgb.z, 0.0f);
}
TEST_F(PaletteManagerTest, SetColorWithoutInitializationFails) {
auto& manager = PaletteManager::Get();
SnesColor new_color(0x7FFF);
auto status = manager.SetColor("ow_main", 0, 0, new_color);
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kFailedPrecondition);
}
TEST_F(PaletteManagerTest, ResetColorWithoutInitializationReturnsError) {
auto& manager = PaletteManager::Get();
auto status = manager.ResetColor("ow_main", 0, 0);
// Should return an error or default color
// Exact behavior depends on implementation
}
TEST_F(PaletteManagerTest, ResetPaletteWithoutInitializationFails) {
auto& manager = PaletteManager::Get();
auto status = manager.ResetPalette("ow_main", 0);
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kFailedPrecondition);
}
// ============================================================================
// Save/Discard Tests (without ROM)
// ============================================================================
TEST_F(PaletteManagerTest, SaveGroupWithoutInitializationFails) {
auto& manager = PaletteManager::Get();
auto status = manager.SaveGroup("ow_main");
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kFailedPrecondition);
}
TEST_F(PaletteManagerTest, SaveAllWithoutInitializationFails) {
auto& manager = PaletteManager::Get();
auto status = manager.SaveAllToRom();
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kFailedPrecondition);
}
TEST_F(PaletteManagerTest, DiscardGroupWithoutInitializationIsNoOp) {
auto& manager = PaletteManager::Get();
// Should not crash
manager.DiscardGroup("ow_main");
// No unsaved changes
EXPECT_FALSE(manager.HasUnsavedChanges());
}
TEST_F(PaletteManagerTest, DiscardAllWithoutInitializationIsNoOp) {
auto& manager = PaletteManager::Get();
// Should not crash
manager.DiscardAllChanges();
// No unsaved changes
EXPECT_FALSE(manager.HasUnsavedChanges());
}
// ============================================================================
// Group Modification Query Tests
// ============================================================================
TEST_F(PaletteManagerTest, IsGroupModifiedInitiallyFalse) {
auto& manager = PaletteManager::Get();
EXPECT_FALSE(manager.IsGroupModified("ow_main"));
EXPECT_FALSE(manager.IsGroupModified("dungeon_main"));
EXPECT_FALSE(manager.IsGroupModified("global_sprites"));
}
TEST_F(PaletteManagerTest, IsPaletteModifiedInitiallyFalse) {
auto& manager = PaletteManager::Get();
EXPECT_FALSE(manager.IsPaletteModified("ow_main", 0));
EXPECT_FALSE(manager.IsPaletteModified("ow_main", 5));
}
TEST_F(PaletteManagerTest, IsColorModifiedInitiallyFalse) {
auto& manager = PaletteManager::Get();
EXPECT_FALSE(manager.IsColorModified("ow_main", 0, 0));
EXPECT_FALSE(manager.IsColorModified("ow_main", 0, 7));
}
// ============================================================================
// Invalid Input Tests
// ============================================================================
TEST_F(PaletteManagerTest, SetColorInvalidGroupName) {
auto& manager = PaletteManager::Get();
SnesColor color(0x7FFF);
auto status = manager.SetColor("invalid_group", 0, 0, color);
EXPECT_FALSE(status.ok());
}
TEST_F(PaletteManagerTest, GetColorInvalidGroupName) {
auto& manager = PaletteManager::Get();
SnesColor color = manager.GetColor("invalid_group", 0, 0);
// Should return default color
auto rgb = color.rgb();
EXPECT_FLOAT_EQ(rgb.x, 0.0f);
EXPECT_FLOAT_EQ(rgb.y, 0.0f);
EXPECT_FLOAT_EQ(rgb.z, 0.0f);
}
TEST_F(PaletteManagerTest, IsGroupModifiedInvalidGroupName) {
auto& manager = PaletteManager::Get();
EXPECT_FALSE(manager.IsGroupModified("invalid_group"));
}
} // namespace
} // namespace gfx
} // namespace yaze

View File

@@ -5,9 +5,9 @@
#include <chrono>
#include "app/rom.h"
#include "app/zelda3/dungeon/room.h"
#include "app/zelda3/dungeon/dungeon_editor_system.h"
#include "app/zelda3/dungeon/dungeon_object_editor.h"
#include "zelda3/dungeon/room.h"
#include "zelda3/dungeon/dungeon_editor_system.h"
#include "zelda3/dungeon/dungeon_object_editor.h"
namespace yaze {
namespace zelda3 {

View File

@@ -0,0 +1,216 @@
// Integration tests for dungeon object rendering using ObjectDrawer
// Updated for DungeonEditorV2 architecture - uses ObjectDrawer (production system)
// instead of the obsolete ObjectRenderer
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
#include "zelda3/dungeon/object_drawer.h"
#include "zelda3/dungeon/room.h"
#include "zelda3/dungeon/room_object.h"
#include <gtest/gtest.h>
#include <memory>
#include <vector>
#include <chrono>
#include "app/rom.h"
#include "app/gfx/types/snes_palette.h"
#include "app/gfx/render/background_buffer.h"
#include "testing.h"
#include "test_utils.h"
namespace yaze {
namespace test {
/**
* @brief Tests for ObjectDrawer with realistic dungeon scenarios
*
* These tests validate that ObjectDrawer correctly renders dungeon objects
* to BackgroundBuffers using pattern-based drawing routines.
*/
class DungeonObjectRenderingTests : public TestRomManager::BoundRomTest {
protected:
void SetUp() override {
BoundRomTest::SetUp();
// Create drawer
drawer_ = std::make_unique<zelda3::ObjectDrawer>(rom());
// Create background buffers
bg1_ = std::make_unique<gfx::BackgroundBuffer>(512, 512);
bg2_ = std::make_unique<gfx::BackgroundBuffer>(512, 512);
// Setup test palette
palette_group_ = CreateTestPaletteGroup();
}
void TearDown() override {
bg2_.reset();
bg1_.reset();
drawer_.reset();
BoundRomTest::TearDown();
}
gfx::PaletteGroup CreateTestPaletteGroup() {
gfx::PaletteGroup group;
gfx::SnesPalette palette;
// Create standard dungeon palette
for (int i = 0; i < 16; i++) {
int intensity = i * 16;
palette.AddColor(gfx::SnesColor(intensity, intensity, intensity));
}
group.AddPalette(palette);
return group;
}
zelda3::RoomObject CreateTestObject(int id, int x, int y, int size = 0x12, int layer = 0) {
zelda3::RoomObject obj(id, x, y, size, layer);
obj.set_rom(rom());
obj.EnsureTilesLoaded();
return obj;
}
std::unique_ptr<zelda3::ObjectDrawer> drawer_;
std::unique_ptr<gfx::BackgroundBuffer> bg1_;
std::unique_ptr<gfx::BackgroundBuffer> bg2_;
gfx::PaletteGroup palette_group_;
};
// Test basic object drawing
TEST_F(DungeonObjectRenderingTests, BasicObjectDrawing) {
std::vector<zelda3::RoomObject> objects;
objects.push_back(CreateTestObject(0x10, 5, 5, 0x12, 0)); // Wall
objects.push_back(CreateTestObject(0x20, 10, 10, 0x22, 0)); // Floor
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto status = drawer_->DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
ASSERT_TRUE(status.ok()) << "Drawing failed: " << status.message();
// Verify buffers have content
auto& bg1_bitmap = bg1_->bitmap();
EXPECT_TRUE(bg1_bitmap.is_active());
EXPECT_GT(bg1_bitmap.width(), 0);
}
// Test objects on different layers
TEST_F(DungeonObjectRenderingTests, MultiLayerRendering) {
std::vector<zelda3::RoomObject> objects;
objects.push_back(CreateTestObject(0x10, 5, 5, 0x12, 0)); // BG1
objects.push_back(CreateTestObject(0x20, 10, 10, 0x22, 1)); // BG2
objects.push_back(CreateTestObject(0x30, 15, 15, 0x12, 2)); // BG3
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto status = drawer_->DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
ASSERT_TRUE(status.ok());
// Both buffers should be active
EXPECT_TRUE(bg1_->bitmap().is_active());
EXPECT_TRUE(bg2_->bitmap().is_active());
}
// Test empty object list
TEST_F(DungeonObjectRenderingTests, EmptyObjectList) {
std::vector<zelda3::RoomObject> objects; // Empty
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto status = drawer_->DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
// Should succeed (drawing nothing is valid)
EXPECT_TRUE(status.ok());
}
// Test large object set
TEST_F(DungeonObjectRenderingTests, LargeObjectSet) {
std::vector<zelda3::RoomObject> objects;
// Create 100 test objects
for (int i = 0; i < 100; i++) {
int x = (i % 10) * 5;
int y = (i / 10) * 5;
objects.push_back(CreateTestObject(0x10 + (i % 20), x, y, 0x12, i % 2));
}
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto start = std::chrono::high_resolution_clock::now();
auto status = drawer_->DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
auto end = std::chrono::high_resolution_clock::now();
ASSERT_TRUE(status.ok());
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
// Should complete in reasonable time
EXPECT_LT(duration.count(), 1000) << "Rendered 100 objects in " << duration.count() << "ms";
}
// Test boundary conditions
TEST_F(DungeonObjectRenderingTests, BoundaryObjects) {
std::vector<zelda3::RoomObject> objects;
// Objects at boundaries
objects.push_back(CreateTestObject(0x10, 0, 0, 0x12, 0)); // Origin
objects.push_back(CreateTestObject(0x10, 63, 63, 0x12, 0)); // Max valid
objects.push_back(CreateTestObject(0x10, 32, 32, 0x12, 0)); // Center
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto status = drawer_->DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
EXPECT_TRUE(status.ok());
}
// Test various object types
TEST_F(DungeonObjectRenderingTests, VariousObjectTypes) {
// Test common object types
std::vector<int> object_types = {
0x00, 0x01, 0x02, 0x03, // Floor/wall objects
0x09, 0x0A, // Diagonal objects
0x10, 0x11, 0x12, // Standard objects
0x20, 0x21, // Decorative objects
0x34, // Solid block
};
for (int obj_type : object_types) {
std::vector<zelda3::RoomObject> objects;
objects.push_back(CreateTestObject(obj_type, 10, 10, 0x12, 0));
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto status = drawer_->DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
// Some object types might not be valid, that's okay
if (!status.ok()) {
std::cout << "Object type 0x" << std::hex << obj_type << std::dec
<< " not renderable: " << status.message() << std::endl;
}
}
}
// Test error handling
TEST_F(DungeonObjectRenderingTests, ErrorHandling) {
// Test with null ROM
zelda3::ObjectDrawer null_drawer(nullptr);
std::vector<zelda3::RoomObject> objects;
objects.push_back(CreateTestObject(0x10, 5, 5));
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto status = null_drawer.DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kFailedPrecondition);
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,212 @@
// Integration tests for dungeon object rendering using ObjectDrawer
// Updated for DungeonEditorV2 architecture - uses ObjectDrawer (production system)
// instead of the obsolete ObjectRenderer
#include "zelda3/dungeon/object_drawer.h"
#include "zelda3/dungeon/room.h"
#include "zelda3/dungeon/room_object.h"
#include <gtest/gtest.h>
#include <memory>
#include <vector>
#include <chrono>
#include "app/rom.h"
#include "app/gfx/snes_palette.h"
#include "app/gfx/background_buffer.h"
#include "testing.h"
#include "test_utils.h"
namespace yaze {
namespace test {
/**
* @brief Tests for ObjectDrawer with realistic dungeon scenarios
*
* These tests validate that ObjectDrawer correctly renders dungeon objects
* to BackgroundBuffers using pattern-based drawing routines.
*/
class DungeonObjectRenderingTests : public TestRomManager::BoundRomTest {
protected:
void SetUp() override {
BoundRomTest::SetUp();
// Create drawer
drawer_ = std::make_unique<zelda3::ObjectDrawer>(rom());
// Create background buffers
bg1_ = std::make_unique<gfx::BackgroundBuffer>(512, 512);
bg2_ = std::make_unique<gfx::BackgroundBuffer>(512, 512);
// Setup test palette
palette_group_ = CreateTestPaletteGroup();
}
void TearDown() override {
bg2_.reset();
bg1_.reset();
drawer_.reset();
BoundRomTest::TearDown();
}
gfx::PaletteGroup CreateTestPaletteGroup() {
gfx::PaletteGroup group;
gfx::SnesPalette palette;
// Create standard dungeon palette
for (int i = 0; i < 16; i++) {
int intensity = i * 16;
palette.AddColor(gfx::SnesColor(intensity, intensity, intensity));
}
group.AddPalette(palette);
return group;
}
zelda3::RoomObject CreateTestObject(int id, int x, int y, int size = 0x12, int layer = 0) {
zelda3::RoomObject obj(id, x, y, size, layer);
obj.set_rom(rom());
obj.EnsureTilesLoaded();
return obj;
}
std::unique_ptr<zelda3::ObjectDrawer> drawer_;
std::unique_ptr<gfx::BackgroundBuffer> bg1_;
std::unique_ptr<gfx::BackgroundBuffer> bg2_;
gfx::PaletteGroup palette_group_;
};
// Test basic object drawing
TEST_F(DungeonObjectRenderingTests, BasicObjectDrawing) {
std::vector<zelda3::RoomObject> objects;
objects.push_back(CreateTestObject(0x10, 5, 5, 0x12, 0)); // Wall
objects.push_back(CreateTestObject(0x20, 10, 10, 0x22, 0)); // Floor
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto status = drawer_->DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
ASSERT_TRUE(status.ok()) << "Drawing failed: " << status.message();
// Verify buffers have content
auto& bg1_bitmap = bg1_->bitmap();
EXPECT_TRUE(bg1_bitmap.is_active());
EXPECT_GT(bg1_bitmap.width(), 0);
}
// Test objects on different layers
TEST_F(DungeonObjectRenderingTests, MultiLayerRendering) {
std::vector<zelda3::RoomObject> objects;
objects.push_back(CreateTestObject(0x10, 5, 5, 0x12, 0)); // BG1
objects.push_back(CreateTestObject(0x20, 10, 10, 0x22, 1)); // BG2
objects.push_back(CreateTestObject(0x30, 15, 15, 0x12, 2)); // BG3
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto status = drawer_->DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
ASSERT_TRUE(status.ok());
// Both buffers should be active
EXPECT_TRUE(bg1_->bitmap().is_active());
EXPECT_TRUE(bg2_->bitmap().is_active());
}
// Test empty object list
TEST_F(DungeonObjectRenderingTests, EmptyObjectList) {
std::vector<zelda3::RoomObject> objects; // Empty
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto status = drawer_->DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
// Should succeed (drawing nothing is valid)
EXPECT_TRUE(status.ok());
}
// Test large object set
TEST_F(DungeonObjectRenderingTests, LargeObjectSet) {
std::vector<zelda3::RoomObject> objects;
// Create 100 test objects
for (int i = 0; i < 100; i++) {
int x = (i % 10) * 5;
int y = (i / 10) * 5;
objects.push_back(CreateTestObject(0x10 + (i % 20), x, y, 0x12, i % 2));
}
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto start = std::chrono::high_resolution_clock::now();
auto status = drawer_->DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
auto end = std::chrono::high_resolution_clock::now();
ASSERT_TRUE(status.ok());
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
// Should complete in reasonable time
EXPECT_LT(duration.count(), 1000) << "Rendered 100 objects in " << duration.count() << "ms";
}
// Test boundary conditions
TEST_F(DungeonObjectRenderingTests, BoundaryObjects) {
std::vector<zelda3::RoomObject> objects;
// Objects at boundaries
objects.push_back(CreateTestObject(0x10, 0, 0, 0x12, 0)); // Origin
objects.push_back(CreateTestObject(0x10, 63, 63, 0x12, 0)); // Max valid
objects.push_back(CreateTestObject(0x10, 32, 32, 0x12, 0)); // Center
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto status = drawer_->DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
EXPECT_TRUE(status.ok());
}
// Test various object types
TEST_F(DungeonObjectRenderingTests, VariousObjectTypes) {
// Test common object types
std::vector<int> object_types = {
0x00, 0x01, 0x02, 0x03, // Floor/wall objects
0x09, 0x0A, // Diagonal objects
0x10, 0x11, 0x12, // Standard objects
0x20, 0x21, // Decorative objects
0x34, // Solid block
};
for (int obj_type : object_types) {
std::vector<zelda3::RoomObject> objects;
objects.push_back(CreateTestObject(obj_type, 10, 10, 0x12, 0));
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto status = drawer_->DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
// Some object types might not be valid, that's okay
if (!status.ok()) {
std::cout << "Object type 0x" << std::hex << obj_type << std::dec
<< " not renderable: " << status.message() << std::endl;
}
}
}
// Test error handling
TEST_F(DungeonObjectRenderingTests, ErrorHandling) {
// Test with null ROM
zelda3::ObjectDrawer null_drawer(nullptr);
std::vector<zelda3::RoomObject> objects;
objects.push_back(CreateTestObject(0x10, 5, 5));
bg1_->ClearBuffer();
bg2_->ClearBuffer();
auto status = null_drawer.DrawObjectList(objects, *bg1_, *bg2_, palette_group_);
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kFailedPrecondition);
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,356 @@
#include "gtest/gtest.h"
#include "absl/status/status.h"
#include "app/gfx/background_buffer.h"
#include "app/gfx/snes_palette.h"
#include "app/rom.h"
#include "zelda3/dungeon/object_drawer.h"
#include "zelda3/dungeon/object_parser.h"
#include "zelda3/dungeon/room.h"
#include "zelda3/dungeon/room_object.h"
namespace yaze {
namespace zelda3 {
class DungeonRenderingIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
// Create a mock ROM for testing
rom_ = std::make_unique<Rom>();
// Initialize with minimal ROM data for testing
std::vector<uint8_t> mock_rom_data(1024 * 1024, 0); // 1MB mock ROM
rom_->LoadFromData(mock_rom_data);
// Create test rooms
room_0x00_ = CreateTestRoom(0x00); // Link's House
room_0x01_ = CreateTestRoom(0x01); // Another test room
}
void TearDown() override {
rom_.reset();
}
std::unique_ptr<Rom> rom_;
// Create a test room with various objects
Room CreateTestRoom(int room_id) {
Room room(room_id, rom_.get());
// Add some test objects to the room
std::vector<RoomObject> objects;
// Add floor objects (object 0x00)
objects.emplace_back(0x00, 5, 5, 3, 0); // Horizontal floor
objects.emplace_back(0x00, 10, 10, 5, 0); // Another floor section
// Add wall objects (object 0x01)
objects.emplace_back(0x01, 15, 15, 2, 0); // Vertical wall
objects.emplace_back(0x01, 20, 20, 4, 1); // Horizontal wall on BG2
// Add diagonal stairs (object 0x09)
objects.emplace_back(0x09, 25, 25, 6, 0); // Diagonal stairs
// Add solid blocks (object 0x34)
objects.emplace_back(0x34, 30, 30, 1, 0); // Solid block
objects.emplace_back(0x34, 35, 35, 2, 1); // Another solid block on BG2
// Set ROM for all objects
for (auto& obj : objects) {
obj.set_rom(rom_.get());
}
// Add objects to room (this would normally be done by LoadObjects)
for (const auto& obj : objects) {
room.AddObject(obj);
}
return room;
}
// Create a test palette
gfx::SnesPalette CreateTestPalette() {
gfx::SnesPalette palette;
// Add some test colors
palette.AddColor(gfx::SnesColor(0, 0, 0)); // Transparent
palette.AddColor(gfx::SnesColor(255, 0, 0)); // Red
palette.AddColor(gfx::SnesColor(0, 255, 0)); // Green
palette.AddColor(gfx::SnesColor(0, 0, 255)); // Blue
palette.AddColor(gfx::SnesColor(255, 255, 0)); // Yellow
palette.AddColor(gfx::SnesColor(255, 0, 255)); // Magenta
palette.AddColor(gfx::SnesColor(0, 255, 255)); // Cyan
palette.AddColor(gfx::SnesColor(255, 255, 255)); // White
return palette;
}
gfx::PaletteGroup CreateTestPaletteGroup() {
gfx::PaletteGroup group;
group.AddPalette(CreateTestPalette());
return group;
}
private:
Room room_0x00_;
Room room_0x01_;
};
// Test full room rendering with ObjectDrawer
TEST_F(DungeonRenderingIntegrationTest, FullRoomRenderingWorks) {
Room test_room = CreateTestRoom(0x00);
// Test that room has objects
EXPECT_GT(test_room.GetTileObjects().size(), 0);
// Test ObjectDrawer can render the room
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
auto status = drawer.DrawObjectList(test_room.GetTileObjects(),
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test room rendering with different palette configurations
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingWithDifferentPalettes) {
Room test_room = CreateTestRoom(0x00);
ObjectDrawer drawer(rom_.get());
// Test with different palette configurations
std::vector<gfx::PaletteGroup> palette_groups;
// Create multiple palette groups
for (int i = 0; i < 3; ++i) {
palette_groups.push_back(CreateTestPaletteGroup());
}
for (const auto& palette_group : palette_groups) {
auto status = drawer.DrawObjectList(test_room.GetTileObjects(),
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
}
// Test room rendering with objects on different layers
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingWithMultipleLayers) {
Room test_room = CreateTestRoom(0x00);
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Separate objects by layer
std::vector<RoomObject> bg1_objects;
std::vector<RoomObject> bg2_objects;
for (const auto& obj : test_room.GetTileObjects()) {
if (obj.GetLayerValue() == 0) {
bg1_objects.push_back(obj);
} else if (obj.GetLayerValue() == 1) {
bg2_objects.push_back(obj);
}
}
// Render BG1 objects
if (!bg1_objects.empty()) {
auto status = drawer.DrawObjectList(bg1_objects,
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Render BG2 objects
if (!bg2_objects.empty()) {
auto status = drawer.DrawObjectList(bg2_objects,
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
}
// Test room rendering with various object sizes
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingWithVariousObjectSizes) {
Room test_room = CreateTestRoom(0x00);
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Group objects by size
std::map<int, std::vector<RoomObject>> objects_by_size;
for (const auto& obj : test_room.GetTileObjects()) {
objects_by_size[obj.size_].push_back(obj);
}
// Render objects of each size
for (const auto& [size, objects] : objects_by_size) {
auto status = drawer.DrawObjectList(objects,
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
}
// Test room rendering performance
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingPerformance) {
// Create a room with many objects
Room large_room(0x00, rom_.get());
// Add many test objects
for (int i = 0; i < 200; ++i) {
int id = i % 65; // Cycle through object IDs 0-64
int x = (i * 2) % 60; // Spread across buffer
int y = (i * 3) % 60;
int size = (i % 8) + 1; // Size 1-8
int layer = i % 2; // Alternate layers
RoomObject obj(id, x, y, size, layer);
obj.set_rom(rom_.get());
large_room.AddObject(obj);
}
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Time the rendering operation
auto start_time = std::chrono::high_resolution_clock::now();
auto status = drawer.DrawObjectList(large_room.GetTileObjects(),
large_room.bg1_buffer(),
large_room.bg2_buffer(),
palette_group);
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
end_time - start_time);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Should complete in reasonable time (less than 2 seconds for 200 objects)
EXPECT_LT(duration.count(), 2000);
std::cout << "Rendered room with 200 objects in " << duration.count() << "ms" << std::endl;
}
// Test room rendering with edge case coordinates
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingWithEdgeCaseCoordinates) {
Room test_room = CreateTestRoom(0x00);
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Add objects at edge coordinates
std::vector<RoomObject> edge_objects;
edge_objects.emplace_back(0x34, 0, 0, 1, 0); // Origin
edge_objects.emplace_back(0x34, 63, 63, 1, 0); // Near buffer edge
edge_objects.emplace_back(0x34, 32, 32, 1, 0); // Center
edge_objects.emplace_back(0x34, 1, 1, 1, 0); // Near origin
edge_objects.emplace_back(0x34, 62, 62, 1, 0); // Near edge
// Set ROM for all objects
for (auto& obj : edge_objects) {
obj.set_rom(rom_.get());
}
auto status = drawer.DrawObjectList(edge_objects,
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test room rendering with mixed object types
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingWithMixedObjectTypes) {
Room test_room = CreateTestRoom(0x00);
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Add various object types
std::vector<RoomObject> mixed_objects;
// Floor objects
mixed_objects.emplace_back(0x00, 5, 5, 3, 0);
mixed_objects.emplace_back(0x01, 10, 10, 2, 0);
// Wall objects
mixed_objects.emplace_back(0x02, 15, 15, 4, 0);
mixed_objects.emplace_back(0x03, 20, 20, 1, 1);
// Diagonal objects
mixed_objects.emplace_back(0x09, 25, 25, 5, 0);
mixed_objects.emplace_back(0x0A, 30, 30, 3, 0);
// Solid objects
mixed_objects.emplace_back(0x34, 35, 35, 1, 0);
mixed_objects.emplace_back(0x33, 40, 40, 2, 1);
// Decorative objects
mixed_objects.emplace_back(0x36, 45, 45, 3, 0);
mixed_objects.emplace_back(0x38, 50, 50, 1, 0);
// Set ROM for all objects
for (auto& obj : mixed_objects) {
obj.set_rom(rom_.get());
}
auto status = drawer.DrawObjectList(mixed_objects,
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test room rendering error handling
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingErrorHandling) {
Room test_room = CreateTestRoom(0x00);
// Test with null ROM
ObjectDrawer null_drawer(nullptr);
auto palette_group = CreateTestPaletteGroup();
auto status = null_drawer.DrawObjectList(test_room.GetTileObjects(),
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kFailedPrecondition);
}
// Test room rendering with invalid object data
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingWithInvalidObjectData) {
Room test_room = CreateTestRoom(0x00);
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Create objects with invalid data
std::vector<RoomObject> invalid_objects;
invalid_objects.emplace_back(0x999, 5, 5, 1, 0); // Invalid object ID
invalid_objects.emplace_back(0x00, -1, -1, 1, 0); // Negative coordinates
invalid_objects.emplace_back(0x00, 100, 100, 1, 0); // Out of bounds coordinates
invalid_objects.emplace_back(0x00, 5, 5, 255, 0); // Maximum size
// Set ROM for all objects
for (auto& obj : invalid_objects) {
obj.set_rom(rom_.get());
}
// Should handle gracefully
auto status = drawer.DrawObjectList(invalid_objects,
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
// Should succeed or fail gracefully
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
} // namespace zelda3
} // namespace yaze

View File

@@ -2,7 +2,7 @@
#include <gtest/gtest.h>
#include "app/rom.h"
#include "app/zelda3/dungeon/room.h"
#include "zelda3/dungeon/room.h"
namespace yaze {
namespace test {

View File

@@ -1,4 +1,5 @@
#include <gtest/gtest.h>
#include <filesystem>
#include "app/editor/message/message_data.h"
#include "app/editor/message/message_editor.h"
@@ -7,13 +8,21 @@
namespace yaze {
namespace test {
class MessageTest : public ::testing::Test {
class MessageRomTest : public ::testing::Test {
protected:
void SetUp() override {
#if defined(__linux__)
GTEST_SKIP();
#endif
EXPECT_OK(rom_.LoadFromFile("zelda3.sfc"));
// Skip tests if ROM is not available
if (getenv("YAZE_SKIP_ROM_TESTS")) {
GTEST_SKIP() << "ROM tests disabled";
}
// Check if ROM file exists
std::string rom_path = "zelda3.sfc";
if (!std::filesystem::exists(rom_path)) {
GTEST_SKIP() << "Test ROM not found: " << rom_path;
}
EXPECT_OK(rom_.LoadFromFile(rom_path));
dictionary_ = editor::BuildDictionaryEntries(&rom_);
}
void TearDown() override {}
@@ -23,7 +32,7 @@ class MessageTest : public ::testing::Test {
std::vector<editor::DictionaryEntry> dictionary_;
};
TEST_F(MessageTest, ParseSingleMessage_CommandParsing) {
TEST_F(MessageRomTest, ParseSingleMessage_CommandParsing) {
std::vector<uint8_t> mock_data = {0x6A, 0x7F, 0x00};
int pos = 0;
@@ -36,7 +45,7 @@ TEST_F(MessageTest, ParseSingleMessage_CommandParsing) {
EXPECT_EQ(pos, 2);
}
TEST_F(MessageTest, ParseSingleMessage_BasicAscii) {
TEST_F(MessageRomTest, ParseSingleMessage_BasicAscii) {
// A, B, C, terminator
std::vector<uint8_t> mock_data = {0x00, 0x01, 0x02, 0x7F, 0x00};
int pos = 0;
@@ -52,36 +61,36 @@ TEST_F(MessageTest, ParseSingleMessage_BasicAscii) {
EXPECT_THAT(parsed, ::testing::ElementsAre("ABC"));
}
TEST_F(MessageTest, FindMatchingCharacter_Success) {
TEST_F(MessageRomTest, FindMatchingCharacter_Success) {
EXPECT_EQ(editor::FindMatchingCharacter('A'), 0x00);
EXPECT_EQ(editor::FindMatchingCharacter('Z'), 0x19);
EXPECT_EQ(editor::FindMatchingCharacter('a'), 0x1A);
EXPECT_EQ(editor::FindMatchingCharacter('z'), 0x33);
}
TEST_F(MessageTest, FindMatchingCharacter_Failure) {
TEST_F(MessageRomTest, FindMatchingCharacter_Failure) {
EXPECT_EQ(editor::FindMatchingCharacter('@'), 0xFF);
EXPECT_EQ(editor::FindMatchingCharacter('#'), 0xFF);
}
TEST_F(MessageTest, FindDictionaryEntry_Success) {
TEST_F(MessageRomTest, FindDictionaryEntry_Success) {
EXPECT_EQ(editor::FindDictionaryEntry(0x88), 0x00);
EXPECT_EQ(editor::FindDictionaryEntry(0x90), 0x08);
}
TEST_F(MessageTest, FindDictionaryEntry_Failure) {
TEST_F(MessageRomTest, FindDictionaryEntry_Failure) {
EXPECT_EQ(editor::FindDictionaryEntry(0x00), -1);
EXPECT_EQ(editor::FindDictionaryEntry(0xFF), -1);
}
TEST_F(MessageTest, ParseMessageToData_Basic) {
TEST_F(MessageRomTest, ParseMessageToData_Basic) {
std::string input = "[L][C:01]ABC";
auto result = editor::ParseMessageToData(input);
std::vector<uint8_t> expected = {0x6A, 0x77, 0x01, 0x00, 0x01, 0x02};
EXPECT_EQ(result, expected);
}
TEST_F(MessageTest, ReplaceAllDictionaryWords_Success) {
TEST_F(MessageRomTest, ReplaceAllDictionaryWords_Success) {
std::vector<editor::DictionaryEntry> mock_dict = {
editor::DictionaryEntry(0x00, "test"),
editor::DictionaryEntry(0x01, "message")};
@@ -90,7 +99,7 @@ TEST_F(MessageTest, ReplaceAllDictionaryWords_Success) {
EXPECT_EQ(result, "This is a [D:00] [D:01].");
}
TEST_F(MessageTest, ReplaceAllDictionaryWords_NoMatch) {
TEST_F(MessageRomTest, ReplaceAllDictionaryWords_NoMatch) {
std::vector<editor::DictionaryEntry> mock_dict = {
editor::DictionaryEntry(0x00, "hello")};
std::string input = "No matching words.";
@@ -98,17 +107,17 @@ TEST_F(MessageTest, ReplaceAllDictionaryWords_NoMatch) {
EXPECT_EQ(result, "No matching words.");
}
TEST_F(MessageTest, ParseTextDataByte_Success) {
TEST_F(MessageRomTest, ParseTextDataByte_Success) {
EXPECT_EQ(editor::ParseTextDataByte(0x00), "A");
EXPECT_EQ(editor::ParseTextDataByte(0x74), "[1]");
EXPECT_EQ(editor::ParseTextDataByte(0x88), "[D:00]");
}
TEST_F(MessageTest, ParseTextDataByte_Failure) {
TEST_F(MessageRomTest, ParseTextDataByte_Failure) {
EXPECT_EQ(editor::ParseTextDataByte(0xFF), "");
}
TEST_F(MessageTest, ParseSingleMessage_SpecialCharacters) {
TEST_F(MessageRomTest, ParseSingleMessage_SpecialCharacters) {
std::vector<uint8_t> mock_data = {0x4D, 0x4E, 0x4F, 0x50, 0x7F};
int pos = 0;
@@ -120,7 +129,7 @@ TEST_F(MessageTest, ParseSingleMessage_SpecialCharacters) {
EXPECT_EQ(pos, 5);
}
TEST_F(MessageTest, ParseSingleMessage_DictionaryReference) {
TEST_F(MessageRomTest, ParseSingleMessage_DictionaryReference) {
std::vector<uint8_t> mock_data = {0x88, 0x89, 0x7F};
int pos = 0;
@@ -132,7 +141,7 @@ TEST_F(MessageTest, ParseSingleMessage_DictionaryReference) {
EXPECT_EQ(pos, 3);
}
TEST_F(MessageTest, ParseSingleMessage_InvalidTerminator) {
TEST_F(MessageRomTest, ParseSingleMessage_InvalidTerminator) {
std::vector<uint8_t> mock_data = {0x00, 0x01, 0x02}; // No terminator
int pos = 0;
@@ -140,7 +149,7 @@ TEST_F(MessageTest, ParseSingleMessage_InvalidTerminator) {
EXPECT_FALSE(result.ok());
}
TEST_F(MessageTest, ParseSingleMessage_EmptyData) {
TEST_F(MessageRomTest, ParseSingleMessage_EmptyData) {
std::vector<uint8_t> mock_data = {0x7F};
int pos = 0;
@@ -152,7 +161,7 @@ TEST_F(MessageTest, ParseSingleMessage_EmptyData) {
EXPECT_EQ(pos, 1);
}
TEST_F(MessageTest, OptimizeMessageForDictionary_Basic) {
TEST_F(MessageRomTest, OptimizeMessageForDictionary_Basic) {
std::vector<editor::DictionaryEntry> mock_dict = {
editor::DictionaryEntry(0x00, "Link"),
editor::DictionaryEntry(0x01, "Zelda")};
@@ -165,7 +174,7 @@ TEST_F(MessageTest, OptimizeMessageForDictionary_Basic) {
EXPECT_EQ(optimized, "[L] rescued [D:01] from danger.");
}
TEST_F(MessageTest, SetMessage_Success) {
TEST_F(MessageRomTest, SetMessage_Success) {
std::vector<editor::DictionaryEntry> mock_dict = {
editor::DictionaryEntry(0x00, "item")};
editor::MessageData message_data;
@@ -177,7 +186,7 @@ TEST_F(MessageTest, SetMessage_Success) {
EXPECT_EQ(message_data.ContentsParsed, "You got an [D:00]!");
}
TEST_F(MessageTest, FindMatchingElement_CommandWithArgument) {
TEST_F(MessageRomTest, FindMatchingElement_CommandWithArgument) {
std::string input = "[W:02]";
editor::ParsedElement result = editor::FindMatchingElement(input);
@@ -186,18 +195,106 @@ TEST_F(MessageTest, FindMatchingElement_CommandWithArgument) {
EXPECT_EQ(result.Value, 0x02);
}
TEST_F(MessageTest, FindMatchingElement_InvalidCommand) {
TEST_F(MessageRomTest, FindMatchingElement_InvalidCommand) {
std::string input = "[INVALID]";
editor::ParsedElement result = editor::FindMatchingElement(input);
EXPECT_FALSE(result.Active);
}
TEST_F(MessageTest, BuildDictionaryEntries_CorrectSize) {
TEST_F(MessageRomTest, BuildDictionaryEntries_CorrectSize) {
auto result = editor::BuildDictionaryEntries(&rom_);
EXPECT_EQ(result.size(), editor::kNumDictionaryEntries);
EXPECT_FALSE(result.empty());
}
TEST_F(MessageRomTest, ParseMessageData_CommandWithArgument_NoExtraCharacters) {
// This test specifically checks for the bug where command arguments
// were being incorrectly parsed as characters (e.g., capital 'A' after [W])
// The bug was caused by using a range-based for loop while also tracking position
// Message: [W:01]ABC
// Bytes: 0x6B (W command), 0x01 (argument), 0x00 (A), 0x01 (B), 0x02 (C)
std::vector<uint8_t> data = {0x6B, 0x01, 0x00, 0x01, 0x02};
editor::MessageData message;
message.ID = 0;
message.Address = 0;
message.Data = data;
std::vector<editor::MessageData> message_data_vector = {message};
auto parsed = editor::ParseMessageData(message_data_vector, dictionary_);
// Should be "[W:01]ABC" NOT "[W:01]BABC" or "[W:01]AABC"
EXPECT_EQ(parsed[0], "[W:01]ABC");
// The 'B' should not appear twice or be skipped
EXPECT_EQ(parsed[0].find("BABC"), std::string::npos);
EXPECT_EQ(parsed[0].find("AABC"), std::string::npos);
}
TEST_F(MessageRomTest, ParseMessageData_MultipleCommandsWithArguments) {
// Test multiple commands with arguments in sequence
// [W:01][C:02]AB
std::vector<uint8_t> data = {
0x6B, 0x01, // [W:01] - Window border command with arg
0x77, 0x02, // [C:02] - Color command with arg
0x00, 0x01 // AB - Regular characters
};
editor::MessageData message;
message.ID = 0;
message.Data = data;
std::vector<editor::MessageData> message_data_vector = {message};
auto parsed = editor::ParseMessageData(message_data_vector, dictionary_);
EXPECT_EQ(parsed[0], "[W:01][C:02]AB");
// Make sure argument bytes (0x01, 0x02) weren't parsed as characters
EXPECT_EQ(parsed[0].find("BAB"), std::string::npos);
EXPECT_EQ(parsed[0].find("CAB"), std::string::npos);
}
TEST_F(MessageRomTest, ParseMessageData_CommandWithoutArgument) {
// Test command without argument followed by text
// [K]ABC - Wait for key command (no arg) followed by ABC
std::vector<uint8_t> data = {
0x7E, // [K] - Wait for key (no argument)
0x00, 0x01, 0x02 // ABC
};
editor::MessageData message;
message.ID = 0;
message.Data = data;
std::vector<editor::MessageData> message_data_vector = {message};
auto parsed = editor::ParseMessageData(message_data_vector, dictionary_);
EXPECT_EQ(parsed[0], "[K]ABC");
}
TEST_F(MessageRomTest, ParseMessageData_MixedCommands) {
// Test mix of commands with and without arguments
// [W:01]A[K]B[C:02]C
std::vector<uint8_t> data = {
0x6B, 0x01, // [W:01] - with arg
0x00, // A
0x7E, // [K] - no arg
0x01, // B
0x77, 0x02, // [C:02] - with arg
0x02 // C
};
editor::MessageData message;
message.ID = 0;
message.Data = data;
std::vector<editor::MessageData> message_data_vector = {message};
auto parsed = editor::ParseMessageData(message_data_vector, dictionary_);
EXPECT_EQ(parsed[0], "[W:01]A[K]B[C:02]C");
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,406 @@
#include <gtest/gtest.h>
#include <memory>
#include <vector>
#include <filesystem>
#include <string>
#include "app/rom.h"
#include "zelda3/overworld/overworld.h"
#include "zelda3/overworld/overworld_map.h"
#include "testing.h"
namespace yaze {
namespace zelda3 {
/**
* @brief Comprehensive overworld integration test that validates YAZE C++
* implementation against ZScream C# logic and existing test infrastructure
*
* This test suite:
* 1. Validates overworld loading logic matches ZScream behavior
* 2. Tests integration with ZSCustomOverworld versions (vanilla, v2, v3)
* 3. Uses existing RomDependentTestSuite infrastructure when available
* 4. Provides both mock data and real ROM testing capabilities
*/
class OverworldIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
#if defined(__linux__)
GTEST_SKIP();
#endif
// Check if we should use real ROM or mock data
const char* rom_path_env = getenv("YAZE_TEST_ROM_PATH");
const char* skip_rom_tests = getenv("YAZE_SKIP_ROM_TESTS");
if (skip_rom_tests) {
GTEST_SKIP() << "ROM tests disabled";
}
if (rom_path_env && std::filesystem::exists(rom_path_env)) {
// Use real ROM for testing
rom_ = std::make_unique<Rom>();
auto status = rom_->LoadFromFile(rom_path_env);
if (status.ok()) {
use_real_rom_ = true;
overworld_ = std::make_unique<Overworld>(rom_.get());
return;
}
}
// Fall back to mock data
use_real_rom_ = false;
rom_ = std::make_unique<Rom>();
SetupMockRomData();
rom_->LoadFromData(mock_rom_data_);
overworld_ = std::make_unique<Overworld>(rom_.get());
}
void TearDown() override {
overworld_.reset();
rom_.reset();
}
void SetupMockRomData() {
mock_rom_data_.resize(0x200000, 0x00);
// Basic ROM structure
mock_rom_data_[0x140145] = 0xFF; // Vanilla ASM
// Tile16 expansion flag
mock_rom_data_[0x017D28] = 0x0F; // Vanilla
// Tile32 expansion flag
mock_rom_data_[0x01772E] = 0x04; // Vanilla
// Basic map data
for (int i = 0; i < 160; i++) {
mock_rom_data_[0x012844 + i] = 0x00; // Small areas
}
// Setup entrance data (matches ZScream Constants.OWEntranceMap/Pos/EntranceId)
for (int i = 0; i < 129; i++) {
mock_rom_data_[0x0DB96F + (i * 2)] = i & 0xFF; // Map ID
mock_rom_data_[0x0DB96F + (i * 2) + 1] = (i >> 8) & 0xFF;
mock_rom_data_[0x0DBA71 + (i * 2)] = (i * 16) & 0xFF; // Map Position
mock_rom_data_[0x0DBA71 + (i * 2) + 1] = ((i * 16) >> 8) & 0xFF;
mock_rom_data_[0x0DBB73 + i] = i & 0xFF; // Entrance ID
}
// Setup exit data (matches ZScream Constants.OWExit*)
for (int i = 0; i < 0x4F; i++) {
mock_rom_data_[0x015D8A + (i * 2)] = i & 0xFF; // Room ID
mock_rom_data_[0x015D8A + (i * 2) + 1] = (i >> 8) & 0xFF;
mock_rom_data_[0x015E28 + i] = i & 0xFF; // Map ID
mock_rom_data_[0x015E77 + (i * 2)] = i & 0xFF; // VRAM
mock_rom_data_[0x015E77 + (i * 2) + 1] = (i >> 8) & 0xFF;
// Add other exit fields...
}
}
std::vector<uint8_t> mock_rom_data_;
std::unique_ptr<Rom> rom_;
std::unique_ptr<Overworld> overworld_;
bool use_real_rom_ = false;
};
// Test Tile32 expansion detection
TEST_F(OverworldIntegrationTest, Tile32ExpansionDetection) {
mock_rom_data_[0x01772E] = 0x04;
mock_rom_data_[0x140145] = 0xFF;
auto status = overworld_->Load(rom_.get());
ASSERT_TRUE(status.ok());
// Test expanded detection
mock_rom_data_[0x01772E] = 0x05;
overworld_ = std::make_unique<Overworld>(rom_.get());
status = overworld_->Load(rom_.get());
ASSERT_TRUE(status.ok());
}
// Test Tile16 expansion detection
TEST_F(OverworldIntegrationTest, Tile16ExpansionDetection) {
mock_rom_data_[0x017D28] = 0x0F;
mock_rom_data_[0x140145] = 0xFF;
auto status = overworld_->Load(rom_.get());
ASSERT_TRUE(status.ok());
// Test expanded detection
mock_rom_data_[0x017D28] = 0x10;
overworld_ = std::make_unique<Overworld>(rom_.get());
status = overworld_->Load(rom_.get());
ASSERT_TRUE(status.ok());
}
// Test entrance loading matches ZScream coordinate calculation
TEST_F(OverworldIntegrationTest, EntranceCoordinateCalculation) {
auto status = overworld_->Load(rom_.get());
ASSERT_TRUE(status.ok());
const auto& entrances = overworld_->entrances();
EXPECT_EQ(entrances.size(), 129);
// Verify coordinate calculation matches ZScream logic:
// int p = mapPos >> 1;
// int x = p % 64;
// int y = p >> 6;
// int real_x = (x * 16) + (((mapId % 64) - (((mapId % 64) / 8) * 8)) * 512);
// int real_y = (y * 16) + (((mapId % 64) / 8) * 512);
for (int i = 0; i < std::min(10, static_cast<int>(entrances.size())); i++) {
const auto& entrance = entrances[i];
uint16_t map_pos = i * 16; // Our test data
uint16_t map_id = i; // Our test data
int position = map_pos >> 1;
int x_coord = position % 64;
int y_coord = position >> 6;
int expected_x = (x_coord * 16) + (((map_id % 64) - (((map_id % 64) / 8) * 8)) * 512);
int expected_y = (y_coord * 16) + (((map_id % 64) / 8) * 512);
EXPECT_EQ(entrance.x_, expected_x);
EXPECT_EQ(entrance.y_, expected_y);
EXPECT_EQ(entrance.entrance_id_, i);
EXPECT_FALSE(entrance.is_hole_);
}
}
// Test exit loading matches ZScream data structure
TEST_F(OverworldIntegrationTest, ExitDataLoading) {
auto status = overworld_->Load(rom_.get());
ASSERT_TRUE(status.ok());
const auto& exits = overworld_->exits();
EXPECT_EQ(exits->size(), 0x4F);
// Verify exit data matches our test data
for (int i = 0; i < std::min(5, static_cast<int>(exits->size())); i++) {
const auto& exit = exits->at(i);
// EXPECT_EQ(exit.room_id_, i);
// EXPECT_EQ(exit.map_id_, i);
// EXPECT_EQ(exit.map_pos_, i);
}
}
// Test ASM version detection affects item loading
TEST_F(OverworldIntegrationTest, ASMVersionItemLoading) {
// Test vanilla ASM (should limit to 0x80 maps)
mock_rom_data_[0x140145] = 0xFF;
overworld_ = std::make_unique<Overworld>(rom_.get());
auto status = overworld_->Load(rom_.get());
ASSERT_TRUE(status.ok());
const auto& items = overworld_->all_items();
// Test v3+ ASM (should support all 0xA0 maps)
mock_rom_data_[0x140145] = 0x03;
overworld_ = std::make_unique<Overworld>(rom_.get());
status = overworld_->Load(rom_.get());
ASSERT_TRUE(status.ok());
const auto& items_v3 = overworld_->all_items();
// v3 should have more comprehensive support
EXPECT_GE(items_v3.size(), items.size());
}
// Test map size assignment logic
TEST_F(OverworldIntegrationTest, MapSizeAssignment) {
auto status = overworld_->Load(rom_.get());
ASSERT_TRUE(status.ok());
const auto& maps = overworld_->overworld_maps();
EXPECT_EQ(maps.size(), 160);
// Verify all maps are initialized
for (const auto& map : maps) {
EXPECT_GE(map.area_size(), AreaSizeEnum::SmallArea);
EXPECT_LE(map.area_size(), AreaSizeEnum::TallArea);
}
}
// Test integration with ZSCustomOverworld version detection
TEST_F(OverworldIntegrationTest, ZSCustomOverworldVersionIntegration) {
if (!use_real_rom_) {
GTEST_SKIP() << "Real ROM required for ZSCustomOverworld version testing";
}
auto status = overworld_->Load(rom_.get());
ASSERT_TRUE(status.ok());
// Check ASM version detection
auto version_byte = rom_->ReadByte(0x140145);
ASSERT_TRUE(version_byte.ok());
uint8_t asm_version = *version_byte;
if (asm_version == 0xFF) {
// Vanilla ROM
EXPECT_FALSE(overworld_->expanded_tile16());
EXPECT_FALSE(overworld_->expanded_tile32());
} else if (asm_version >= 0x02 && asm_version <= 0x03) {
// ZSCustomOverworld v2/v3
// Should have expanded features
EXPECT_TRUE(overworld_->expanded_tile16());
EXPECT_TRUE(overworld_->expanded_tile32());
}
// Verify version-specific features are properly detected
if (asm_version >= 0x03) {
// v3 features should be available
const auto& maps = overworld_->overworld_maps();
EXPECT_EQ(maps.size(), 160); // All 160 maps supported in v3
}
}
// Test compatibility with RomDependentTestSuite infrastructure
TEST_F(OverworldIntegrationTest, RomDependentTestSuiteCompatibility) {
if (!use_real_rom_) {
GTEST_SKIP() << "Real ROM required for RomDependentTestSuite compatibility testing";
}
// Test that our overworld loading works with the same patterns as RomDependentTestSuite
auto status = overworld_->Load(rom_.get());
ASSERT_TRUE(status.ok());
// Verify ROM-dependent features work correctly
EXPECT_TRUE(overworld_->is_loaded());
const auto& maps = overworld_->overworld_maps();
EXPECT_EQ(maps.size(), 160);
// Test that we can access the same data structures as RomDependentTestSuite
for (int i = 0; i < std::min(10, static_cast<int>(maps.size())); i++) {
const auto& map = maps[i];
// Verify map properties are accessible
EXPECT_GE(map.area_graphics(), 0);
EXPECT_GE(map.main_palette(), 0);
EXPECT_GE(map.area_size(), AreaSizeEnum::SmallArea);
EXPECT_LE(map.area_size(), AreaSizeEnum::TallArea);
}
// Test that sprite data is accessible (matches RomDependentTestSuite expectations)
const auto& sprites = overworld_->sprites(0);
EXPECT_EQ(sprites.size(), 3); // Three game states
// Test that item data is accessible
const auto& items = overworld_->all_items();
EXPECT_GE(items.size(), 0);
// Test that entrance/exit data is accessible
const auto& entrances = overworld_->entrances();
const auto& exits = overworld_->exits();
EXPECT_EQ(entrances.size(), 129);
EXPECT_EQ(exits->size(), 0x4F);
}
// Test comprehensive overworld data integrity
TEST_F(OverworldIntegrationTest, ComprehensiveDataIntegrity) {
auto status = overworld_->Load(rom_.get());
ASSERT_TRUE(status.ok());
// Verify all major data structures are properly loaded
EXPECT_GT(overworld_->tiles16().size(), 0);
EXPECT_GT(overworld_->tiles32_unique().size(), 0);
// Verify map organization matches ZScream expectations
const auto& map_tiles = overworld_->map_tiles();
EXPECT_EQ(map_tiles.light_world.size(), 512);
EXPECT_EQ(map_tiles.dark_world.size(), 512);
EXPECT_EQ(map_tiles.special_world.size(), 512);
// Verify each world has proper 512x512 tile data
for (const auto& row : map_tiles.light_world) {
EXPECT_EQ(row.size(), 512);
}
for (const auto& row : map_tiles.dark_world) {
EXPECT_EQ(row.size(), 512);
}
for (const auto& row : map_tiles.special_world) {
EXPECT_EQ(row.size(), 512);
}
// Verify overworld maps are properly initialized
const auto& maps = overworld_->overworld_maps();
EXPECT_EQ(maps.size(), 160);
for (const auto& map : maps) {
// TODO: Find a way to compare
// EXPECT_TRUE(map.bitmap_data() != nullptr);
}
// Verify tile types are loaded
const auto& tile_types = overworld_->all_tiles_types();
EXPECT_EQ(tile_types.size(), 0x200);
}
// Test ZScream coordinate calculation compatibility
TEST_F(OverworldIntegrationTest, ZScreamCoordinateCompatibility) {
auto status = overworld_->Load(rom_.get());
ASSERT_TRUE(status.ok());
const auto& entrances = overworld_->entrances();
EXPECT_EQ(entrances.size(), 129);
// Test coordinate calculation matches ZScream logic exactly
for (int i = 0; i < std::min(10, static_cast<int>(entrances.size())); i++) {
const auto& entrance = entrances[i];
// ZScream coordinate calculation:
// int p = mapPos >> 1;
// int x = p % 64;
// int y = p >> 6;
// int real_x = (x * 16) + (((mapId % 64) - (((mapId % 64) / 8) * 8)) * 512);
// int real_y = (y * 16) + (((mapId % 64) / 8) * 512);
uint16_t map_pos = entrance.map_pos_;
uint16_t map_id = entrance.map_id_;
int position = map_pos >> 1;
int x_coord = position % 64;
int y_coord = position >> 6;
int expected_x = (x_coord * 16) + (((map_id % 64) - (((map_id % 64) / 8) * 8)) * 512);
int expected_y = (y_coord * 16) + (((map_id % 64) / 8) * 512);
EXPECT_EQ(entrance.x_, expected_x);
EXPECT_EQ(entrance.y_, expected_y);
}
// Test hole coordinate calculation with 0x400 offset
const auto& holes = overworld_->holes();
EXPECT_EQ(holes.size(), 0x13);
for (int i = 0; i < std::min(5, static_cast<int>(holes.size())); i++) {
const auto& hole = holes[i];
// ZScream hole coordinate calculation:
// int p = (mapPos + 0x400) >> 1;
// int x = p % 64;
// int y = p >> 6;
// int real_x = (x * 16) + (((mapId % 64) - (((mapId % 64) / 8) * 8)) * 512);
// int real_y = (y * 16) + (((mapId % 64) / 8) * 512);
uint16_t map_pos = hole.map_pos_;
uint16_t map_id = hole.map_id_;
int position = map_pos >> 1;
int x_coord = position % 64;
int y_coord = position >> 6;
int expected_x = (x_coord * 16) + (((map_id % 64) - (((map_id % 64) / 8) * 8)) * 512);
int expected_y = (y_coord * 16) + (((map_id % 64) / 8) * 512);
EXPECT_EQ(hole.x_, expected_x);
EXPECT_EQ(hole.y_, expected_y);
EXPECT_TRUE(hole.is_hole_);
}
}
} // namespace zelda3
} // namespace yaze

View File

@@ -0,0 +1,327 @@
// Integration tests for Room object load/save cycle with real ROM data
// Phase 1, Task 2.1: Full round-trip verification
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "app/rom.h"
#include "zelda3/dungeon/room.h"
#include "zelda3/dungeon/room_object.h"
// Helper function for SNES to PC address conversion
inline int SnesToPc(int addr) {
int temp = (addr & 0x7FFF) + ((addr / 2) & 0xFF8000);
return (temp + 0x0);
}
namespace yaze {
namespace zelda3 {
namespace test {
class RoomIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
// Load the ROM file
rom_ = std::make_unique<Rom>();
// Check if ROM file exists
const char* rom_path = std::getenv("YAZE_TEST_ROM_PATH");
if (!rom_path) {
rom_path = "zelda3.sfc";
}
auto status = rom_->LoadFromFile(rom_path);
if (!status.ok()) {
GTEST_SKIP() << "ROM file not available: " << status.message();
}
// Create backup of ROM data for restoration after tests
original_rom_data_ = rom_->vector();
}
void TearDown() override {
// Restore original ROM data
if (rom_ && !original_rom_data_.empty()) {
for (size_t i = 0; i < original_rom_data_.size(); i++) {
rom_->WriteByte(i, original_rom_data_[i]);
}
}
}
std::unique_ptr<Rom> rom_;
std::vector<uint8_t> original_rom_data_;
};
// ============================================================================
// Test 1: Basic Load/Save Round-Trip
// ============================================================================
TEST_F(RoomIntegrationTest, BasicLoadSaveRoundTrip) {
// Load room 0 (Hyrule Castle Entrance)
Room room1(0x00, rom_.get());
// Get original object count
size_t original_count = room1.GetTileObjects().size();
ASSERT_GT(original_count, 0) << "Room should have objects";
// Store original objects
auto original_objects = room1.GetTileObjects();
// Save the room (should write same data back)
auto save_status = room1.SaveObjects();
ASSERT_TRUE(save_status.ok()) << save_status.message();
// Load the room again
Room room2(0x00, rom_.get());
// Verify object count matches
EXPECT_EQ(room2.GetTileObjects().size(), original_count);
// Verify each object matches
auto reloaded_objects = room2.GetTileObjects();
ASSERT_EQ(reloaded_objects.size(), original_objects.size());
for (size_t i = 0; i < original_objects.size(); i++) {
SCOPED_TRACE("Object " + std::to_string(i));
const auto& orig = original_objects[i];
const auto& reload = reloaded_objects[i];
EXPECT_EQ(reload.id_, orig.id_) << "ID mismatch";
EXPECT_EQ(reload.x(), orig.x()) << "X position mismatch";
EXPECT_EQ(reload.y(), orig.y()) << "Y position mismatch";
EXPECT_EQ(reload.size(), orig.size()) << "Size mismatch";
EXPECT_EQ(reload.GetLayerValue(), orig.GetLayerValue()) << "Layer mismatch";
}
}
// ============================================================================
// Test 2: Multi-Room Verification
// ============================================================================
TEST_F(RoomIntegrationTest, MultiRoomLoadSaveRoundTrip) {
// Test several different rooms to ensure broad coverage
std::vector<int> test_rooms = {0x00, 0x01, 0x02, 0x10, 0x20};
for (int room_id : test_rooms) {
SCOPED_TRACE("Room " + std::to_string(room_id));
// Load room
Room room1(room_id, rom_.get());
auto original_objects = room1.GetTileObjects();
if (original_objects.empty()) {
continue; // Skip empty rooms
}
// Save objects
auto save_status = room1.SaveObjects();
ASSERT_TRUE(save_status.ok()) << save_status.message();
// Reload and verify
Room room2(room_id, rom_.get());
auto reloaded_objects = room2.GetTileObjects();
EXPECT_EQ(reloaded_objects.size(), original_objects.size());
// Verify objects match
for (size_t i = 0; i < std::min(original_objects.size(), reloaded_objects.size()); i++) {
const auto& orig = original_objects[i];
const auto& reload = reloaded_objects[i];
EXPECT_EQ(reload.id_, orig.id_);
EXPECT_EQ(reload.x(), orig.x());
EXPECT_EQ(reload.y(), orig.y());
EXPECT_EQ(reload.size(), orig.size());
EXPECT_EQ(reload.GetLayerValue(), orig.GetLayerValue());
}
}
}
// ============================================================================
// Test 3: Layer Verification
// ============================================================================
TEST_F(RoomIntegrationTest, LayerPreservation) {
// Load a room known to have multiple layers
Room room(0x01, rom_.get());
auto objects = room.GetTileObjects();
ASSERT_GT(objects.size(), 0);
// Count objects per layer
int layer0_count = 0, layer1_count = 0, layer2_count = 0;
for (const auto& obj : objects) {
switch (obj.GetLayerValue()) {
case 0: layer0_count++; break;
case 1: layer1_count++; break;
case 2: layer2_count++; break;
}
}
// Save and reload
ASSERT_TRUE(room.SaveObjects().ok());
Room room2(0x01, rom_.get());
auto reloaded = room2.GetTileObjects();
// Verify layer counts match
int reload_layer0 = 0, reload_layer1 = 0, reload_layer2 = 0;
for (const auto& obj : reloaded) {
switch (obj.GetLayerValue()) {
case 0: reload_layer0++; break;
case 1: reload_layer1++; break;
case 2: reload_layer2++; break;
}
}
EXPECT_EQ(reload_layer0, layer0_count);
EXPECT_EQ(reload_layer1, layer1_count);
EXPECT_EQ(reload_layer2, layer2_count);
}
// ============================================================================
// Test 4: Object Type Distribution
// ============================================================================
TEST_F(RoomIntegrationTest, ObjectTypeDistribution) {
Room room(0x00, rom_.get());
auto objects = room.GetTileObjects();
ASSERT_GT(objects.size(), 0);
// Count object types
int type1_count = 0; // ID < 0x100
int type2_count = 0; // ID 0x100-0x13F
int type3_count = 0; // ID >= 0xF00
for (const auto& obj : objects) {
if (obj.id_ >= 0xF00) {
type3_count++;
} else if (obj.id_ >= 0x100) {
type2_count++;
} else {
type1_count++;
}
}
// Save and reload
ASSERT_TRUE(room.SaveObjects().ok());
Room room2(0x00, rom_.get());
auto reloaded = room2.GetTileObjects();
// Verify type distribution matches
int reload_type1 = 0, reload_type2 = 0, reload_type3 = 0;
for (const auto& obj : reloaded) {
if (obj.id_ >= 0xF00) {
reload_type3++;
} else if (obj.id_ >= 0x100) {
reload_type2++;
} else {
reload_type1++;
}
}
EXPECT_EQ(reload_type1, type1_count);
EXPECT_EQ(reload_type2, type2_count);
EXPECT_EQ(reload_type3, type3_count);
}
// ============================================================================
// Test 5: Binary Data Verification
// ============================================================================
TEST_F(RoomIntegrationTest, BinaryDataExactMatch) {
// This test verifies that saving doesn't change ROM data
// when no modifications are made
Room room(0x02, rom_.get());
// Get the ROM location where objects are stored
auto rom_data = rom_->vector();
int object_pointer = (rom_data[0x874C + 2] << 16) +
(rom_data[0x874C + 1] << 8) +
(rom_data[0x874C]);
object_pointer = SnesToPc(object_pointer);
int room_address = object_pointer + (0x02 * 3);
int tile_address = (rom_data[room_address + 2] << 16) +
(rom_data[room_address + 1] << 8) +
rom_data[room_address];
int objects_location = SnesToPc(tile_address);
// Read original bytes (up to 500 bytes should cover most rooms)
std::vector<uint8_t> original_bytes;
for (int i = 0; i < 500 && objects_location + i < (int)rom_data.size(); i++) {
original_bytes.push_back(rom_data[objects_location + i]);
// Stop at final terminator
if (i > 0 && original_bytes[i] == 0xFF && original_bytes[i-1] == 0xFF) {
// Check if this is the final terminator (3rd layer end)
bool might_be_final = true;
for (int j = i - 10; j < i - 1; j += 2) {
if (j >= 0 && original_bytes[j] == 0xFF && original_bytes[j+1] == 0xFF) {
// Found another FF FF marker, keep going
break;
}
}
if (might_be_final) break;
}
}
// Save objects (should write identical data)
ASSERT_TRUE(room.SaveObjects().ok());
// Read bytes after save
rom_data = rom_->vector();
std::vector<uint8_t> saved_bytes;
for (size_t i = 0; i < original_bytes.size() && objects_location + i < rom_data.size(); i++) {
saved_bytes.push_back(rom_data[objects_location + i]);
}
// Verify binary match
ASSERT_EQ(saved_bytes.size(), original_bytes.size());
for (size_t i = 0; i < original_bytes.size(); i++) {
EXPECT_EQ(saved_bytes[i], original_bytes[i])
<< "Byte mismatch at offset " << i;
}
}
// ============================================================================
// Test 6: Known Room Data Verification
// ============================================================================
TEST_F(RoomIntegrationTest, KnownRoomData) {
// Room 0x00 (Hyrule Castle Entrance) - verify known objects exist
Room room(0x00, rom_.get());
auto objects = room.GetTileObjects();
ASSERT_GT(objects.size(), 0) << "Room 0x00 should have objects";
// Verify we can find common object types
bool found_type1 = false;
bool found_layer0 = false;
bool found_layer1 = false;
for (const auto& obj : objects) {
if (obj.id_ < 0x100) found_type1 = true;
if (obj.GetLayerValue() == 0) found_layer0 = true;
if (obj.GetLayerValue() == 1) found_layer1 = true;
}
EXPECT_TRUE(found_type1) << "Should have Type 1 objects";
EXPECT_TRUE(found_layer0) << "Should have Layer 0 objects";
// Verify coordinates are in valid range (0-63)
for (const auto& obj : objects) {
EXPECT_GE(obj.x(), 0);
EXPECT_LE(obj.x(), 63);
EXPECT_GE(obj.y(), 0);
EXPECT_LE(obj.y(), 63);
}
}
} // namespace test
} // namespace zelda3
} // namespace yaze

View File

@@ -5,8 +5,8 @@
#include <fstream>
#include "app/rom.h"
#include "app/zelda3/overworld/overworld.h"
#include "app/zelda3/overworld/overworld_map.h"
#include "zelda3/overworld/overworld.h"
#include "zelda3/overworld/overworld_map.h"
namespace yaze {
namespace zelda3 {

150
test/test.cmake Normal file
View File

@@ -0,0 +1,150 @@
# Configure test discovery with efficient labeling for CI/CD
# Only discover tests if tests are enabled
if(YAZE_BUILD_TESTS AND NOT YAZE_BUILD_TESTS STREQUAL "OFF")
include(GoogleTest)
# Discover all tests and apply default labels using PROPERTIES argument
# This ensures all tests get the ALL_TESTS label immediately
if(WIN32)
gtest_discover_tests(yaze_test
TEST_LIST yaze_discovered_tests
DISCOVERY_TIMEOUT 60
NO_PRETTY_TYPES
PROPERTIES
TIMEOUT 300
LABELS "ALL_TESTS;UNIT_TEST;STABLE;ASAR_TEST;INTEGRATION_TEST;E2E_TEST;ROM_TEST;ZSCUSTOM_TEST;CLI_TEST;MISC_TEST"
TEST_PREFIX ""
TEST_SUFFIX ""
)
else()
gtest_discover_tests(yaze_test
TEST_LIST yaze_discovered_tests
PROPERTIES
LABELS "ALL_TESTS;UNIT_TEST;STABLE;ASAR_TEST;INTEGRATION_TEST;E2E_TEST;ROM_TEST;ZSCUSTOM_TEST;CLI_TEST;MISC_TEST"
TEST_PREFIX ""
TEST_SUFFIX ""
)
endif()
# Note: Due to CMake's bracket argument syntax limitations, we cannot dynamically
# apply labels to tests with bracket-quoted names in post-processing scripts.
# All tests get all possible labels initially, and can be filtered using test presets
# in CMakePresets.json which use label-based filtering via ctest -L option.
#
# Test categorization is done via naming conventions:
# - Tests matching "*IntegrationTest*" -> Integration tests
# - Tests matching "E2ERomDependentTest.*" -> E2E + ROM tests
# - Tests matching "ZSCustomOverworldUpgradeTest.*" -> E2E + ROM + ZSCustom tests
# - Tests matching "RomTest.*" or "*RomIntegrationTest*" -> ROM tests
# - Tests matching "*Asar*" -> Asar tests
# - Tests matching "ResourceCatalogTest*" -> CLI tests
# - All others -> Unit tests
#
# Test presets use these labels for filtering (see CMakePresets.json)
else()
# Tests are disabled - don't build test executable or discover tests
message(STATUS "Tests disabled - skipping test executable and discovery")
endif()
# Test organization and labeling for CI/CD
# Note: Test labeling is handled through the enhanced yaze_test executable
# which supports filtering by test categories using command line arguments:
# --unit, --integration, --e2e, --rom-dependent, --zscustomoverworld, etc.
#
# For CI/CD, use the test runner with appropriate filters:
# ./yaze_test --unit --verbose
# ./yaze_test --e2e --rom-path zelda3.sfc
# ./yaze_test --zscustomoverworld --verbose
# =============================================================================
# Test Source Groups for Visual Studio Organization
# =============================================================================
# Test Framework
source_group("Tests\\Framework" FILES
testing.h
yaze_test.cc
yaze_test_ci.cc
test_editor.cc
test_editor.h
)
# Unit Tests
source_group("Tests\\Unit" FILES
unit/core/asar_wrapper_test.cc
unit/core/hex_test.cc
unit/cli/resource_catalog_test.cc
unit/rom/rom_test.cc
unit/gfx/snes_tile_test.cc
unit/gfx/compression_test.cc
unit/gfx/snes_palette_test.cc
unit/gui/tile_selector_widget_test.cc
unit/gui/canvas_automation_api_test.cc
unit/zelda3/message_test.cc
unit/zelda3/overworld_test.cc
unit/zelda3/object_parser_test.cc
unit/zelda3/object_parser_structs_test.cc
unit/zelda3/sprite_builder_test.cc
unit/zelda3/sprite_position_test.cc
unit/zelda3/test_dungeon_objects.cc
unit/zelda3/dungeon_component_unit_test.cc
unit/zelda3/dungeon/room_object_encoding_test.cc
zelda3/dungeon/room_manipulation_test.cc
unit/zelda3/dungeon_object_renderer_mock_test.cc
unit/zelda3/dungeon_object_rendering_tests.cc
unit/zelda3/dungeon_room_test.cc
)
# Integration Tests
source_group("Tests\\Integration" FILES
integration/asar_integration_test.cc
integration/asar_rom_test.cc
integration/dungeon_editor_test.cc
integration/dungeon_editor_test.h
integration/dungeon_editor_v2_test.cc
integration/dungeon_editor_v2_test.h
integration/editor/tile16_editor_test.cc
integration/editor/editor_integration_test.cc
integration/editor/editor_integration_test.h
)
# Integration Tests (Zelda3)
source_group("Tests\\Integration\\Zelda3" FILES
integration/zelda3/overworld_integration_test.cc
integration/zelda3/dungeon_editor_system_integration_test.cc
integration/zelda3/dungeon_object_renderer_integration_test.cc
integration/zelda3/room_integration_test.cc
)
# End-to-End Tests
source_group("Tests\\E2E" FILES
e2e/canvas_selection_test.cc
e2e/framework_smoke_test.cc
e2e/rom_dependent/e2e_rom_test.cc
e2e/zscustomoverworld/zscustomoverworld_upgrade_test.cc
)
# Deprecated Tests
source_group("Tests\\Deprecated" FILES
deprecated/comprehensive_integration_test.cc
deprecated/dungeon_integration_test.cc
)
# Benchmarks
source_group("Tests\\Benchmarks" FILES
benchmarks/gfx_optimization_benchmarks.cc
)
# Test Utilities and Mocks
source_group("Tests\\Utilities" FILES
test_utils.h
test_utils.cc
mocks/mock_rom.h
mocks/mock_memory.h
)
# Test Assets
source_group("Tests\\Assets" FILES
assets/test_patch.asm
)

View File

@@ -0,0 +1,29 @@
#include <iostream>
#include "cli/service/ai/service_factory.h"
#include "cli/service/agent/conversational_agent_service.h"
#include "app/rom.h"
using namespace yaze;
using namespace yaze::cli;
using namespace yaze::cli::agent;
int main() {
std::cout << "Test 1: Creating AI Service...\n";
auto ai_service = CreateAIService();
std::cout << "✅ AI Service created\n";
std::cout << "Test 2: Creating Conversational Agent Service...\n";
ConversationalAgentService service;
std::cout << "✅ Conversational Agent Service created\n";
std::cout << "Test 3: Creating ROM...\n";
Rom rom;
std::cout << "✅ ROM created\n";
std::cout << "Test 4: Setting ROM context...\n";
service.SetRomContext(&rom);
std::cout << "✅ ROM context set\n";
std::cout << "\n🎉 All tests passed!\n";
return 0;
}

View File

@@ -2,9 +2,10 @@
#include <SDL.h>
#include "app/core/controller.h"
#include "app/core/window.h"
#include "app/gui/style.h"
#include "app/controller.h"
#include "app/platform/window.h"
#include "app/gfx/backend/sdl2_renderer.h"
#include "app/gui/core/style.h"
#include "imgui/backends/imgui_impl_sdl2.h"
#include "imgui/backends/imgui_impl_sdlrenderer2.h"
#include "imgui.h"
@@ -54,9 +55,11 @@ void TestEditor::RegisterTests(ImGuiTestEngine* engine) {
// TODO: Fix the window/controller management
int RunIntegrationTest() {
yaze::core::Controller controller;
yaze::Controller controller;
yaze::core::Window window;
yaze::core::CreateWindow(window, SDL_WINDOW_RESIZABLE);
// Create renderer for test
auto test_renderer = std::make_unique<yaze::gfx::SDL2Renderer>();
yaze::core::CreateWindow(window, test_renderer.get(), SDL_WINDOW_RESIZABLE);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
@@ -74,9 +77,9 @@ int RunIntegrationTest() {
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
// Initialize ImGui for SDL
ImGui_ImplSDL2_InitForSDLRenderer(
controller.window(), yaze::core::Renderer::Get().renderer());
ImGui_ImplSDLRenderer2_Init(yaze::core::Renderer::Get().renderer());
SDL_Renderer* sdl_renderer = static_cast<SDL_Renderer*>(test_renderer->GetBackendRenderer());
ImGui_ImplSDL2_InitForSDLRenderer(controller.window(), sdl_renderer);
ImGui_ImplSDLRenderer2_Init(sdl_renderer);
yaze::test::TestEditor test_editor;
#ifdef IMGUI_ENABLE_TEST_ENGINE

19
test/test_utils.cc Normal file
View File

@@ -0,0 +1,19 @@
#include "test_utils.h"
#include "app/controller.h"
namespace yaze {
namespace test {
namespace gui {
void LoadRomInTest(ImGuiTestContext* ctx, const std::string& rom_path) {
yaze::Controller* controller = (yaze::Controller*)ctx->Test->UserData;
controller->OnEntry(rom_path);
}
void OpenEditorInTest(ImGuiTestContext* ctx, const std::string& editor_name) {
ctx->MenuClick(absl::StrFormat("Editors/%s", editor_name).c_str());
}
} // namespace gui
} // namespace test
} // namespace yaze

View File

@@ -1,6 +1,10 @@
#ifndef YAZE_TEST_TEST_UTILS_H
#define YAZE_TEST_TEST_UTILS_H
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
#include <string>
#include <vector>
#include <filesystem>
@@ -10,6 +14,10 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "absl/strings/str_format.h"
#include "imgui_test_engine/imgui_te_context.h"
#include "app/rom.h"
namespace yaze {
namespace test {
@@ -18,6 +26,8 @@ namespace test {
*/
class TestRomManager {
public:
class BoundRomTest;
/**
* @brief Check if ROM testing is enabled and ROM file exists
* @return True if ROM tests can be run
@@ -126,6 +136,37 @@ class TestRomManager {
}
};
class TestRomManager::BoundRomTest : public ::testing::Test {
protected:
void SetUp() override {
rom_instance_ = std::make_unique<Rom>();
}
void TearDown() override {
rom_instance_.reset();
rom_loaded_ = false;
}
Rom* rom() { EnsureRomLoaded(); return rom_instance_.get(); }
const Rom* rom() const { return rom_instance_.get(); }
std::string GetBoundRomPath() const { return TestRomManager::GetTestRomPath(); }
private:
std::unique_ptr<Rom> rom_instance_;
bool rom_loaded_ = false;
void EnsureRomLoaded() {
if (rom_loaded_) {
return;
}
const std::string rom_path = TestRomManager::GetTestRomPath();
ASSERT_TRUE(rom_instance_->LoadFromFile(rom_path).ok())
<< "Failed to load test ROM from " << rom_path;
rom_loaded_ = true;
}
};
/**
* @brief Test macro for ROM-dependent tests
*/
@@ -150,6 +191,13 @@ class RomDependentTest : public ::testing::Test {
std::vector<uint8_t> test_rom_;
};
namespace gui {
void LoadRomInTest(ImGuiTestContext* ctx, const std::string& rom_path);
void OpenEditorInTest(ImGuiTestContext* ctx, const std::string& editor_name);
} // namespace gui
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,93 @@
#include "cli/service/resources/resource_catalog.h"
#include <algorithm>
#include <string>
#include "gtest/gtest.h"
namespace yaze {
namespace cli {
namespace {
TEST(ResourceCatalogTest, SerializeResourceIncludesReturnsArray) {
const auto& catalog = ResourceCatalog::Instance();
auto overworld_schema = catalog.GetResource("overworld");
ASSERT_TRUE(overworld_schema.ok());
std::string output = catalog.SerializeResource(overworld_schema.value());
EXPECT_NE(output.find("\"resources\""), std::string::npos);
EXPECT_NE(output.find("\"returns\":"), std::string::npos);
EXPECT_NE(output.find("\"tile\""), std::string::npos);
}
TEST(ResourceCatalogTest, SerializeAllResourcesIncludesAgentDescribeMetadata) {
const auto& catalog = ResourceCatalog::Instance();
std::string output = catalog.SerializeResources(catalog.AllResources());
EXPECT_NE(output.find("\"agent\""), std::string::npos);
EXPECT_NE(output.find("\"effects\":"), std::string::npos);
EXPECT_NE(output.find("\"returns\":"), std::string::npos);
}
TEST(ResourceCatalogTest, RomSchemaExposesActionsAndMetadata) {
const auto& catalog = ResourceCatalog::Instance();
auto rom_schema = catalog.GetResource("rom");
ASSERT_TRUE(rom_schema.ok());
const auto& actions = rom_schema->actions;
ASSERT_EQ(actions.size(), 3);
EXPECT_EQ(actions[0].name, "validate");
EXPECT_FALSE(actions[0].effects.empty());
EXPECT_FALSE(actions[0].returns.empty());
EXPECT_EQ(actions[1].name, "diff");
EXPECT_EQ(actions[2].name, "generate-golden");
}
TEST(ResourceCatalogTest, PatchSchemaIncludesAsarAndCreateActions) {
const auto& catalog = ResourceCatalog::Instance();
auto patch_schema = catalog.GetResource("patch");
ASSERT_TRUE(patch_schema.ok());
const auto& actions = patch_schema->actions;
ASSERT_GE(actions.size(), 3);
EXPECT_EQ(actions[0].name, "apply");
EXPECT_FALSE(actions[0].returns.empty());
auto has_asar = std::find_if(actions.begin(), actions.end(), [](const auto& action) {
return action.name == "apply-asar";
});
EXPECT_NE(has_asar, actions.end());
auto has_create = std::find_if(actions.begin(), actions.end(), [](const auto& action) {
return action.name == "create";
});
EXPECT_NE(has_create, actions.end());
}
TEST(ResourceCatalogTest, DungeonSchemaListsMetadataAndObjectsReturns) {
const auto& catalog = ResourceCatalog::Instance();
auto dungeon_schema = catalog.GetResource("dungeon");
ASSERT_TRUE(dungeon_schema.ok());
const auto& actions = dungeon_schema->actions;
ASSERT_EQ(actions.size(), 2);
EXPECT_EQ(actions[0].name, "export");
EXPECT_FALSE(actions[0].returns.empty());
EXPECT_EQ(actions[1].name, "list-objects");
EXPECT_FALSE(actions[1].returns.empty());
}
TEST(ResourceCatalogTest, YamlSerializationIncludesMetadataAndActions) {
const auto& catalog = ResourceCatalog::Instance();
std::string yaml = catalog.SerializeResourcesAsYaml(
catalog.AllResources(), "0.1.0", "2025-10-01");
EXPECT_NE(yaml.find("version: \"0.1.0\""), std::string::npos);
EXPECT_NE(yaml.find("name: \"patch\""), std::string::npos);
EXPECT_NE(yaml.find("effects:"), std::string::npos);
EXPECT_NE(yaml.find("returns:"), std::string::npos);
}
} // namespace
} // namespace cli
} // namespace yaze

View File

@@ -0,0 +1,244 @@
// Test suite for Tile16ProposalGenerator
// Tests the new ParseSetAreaCommand and ParseReplaceTileCommand functionality
#include "cli/service/planning/tile16_proposal_generator.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "app/rom.h"
#include "test/mocks/mock_rom.h"
namespace yaze {
namespace cli {
namespace {
using ::testing::_;
using ::testing::Return;
class Tile16ProposalGeneratorTest : public ::testing::Test {
protected:
void SetUp() override {
generator_ = std::make_unique<Tile16ProposalGenerator>();
}
std::unique_ptr<Tile16ProposalGenerator> generator_;
};
// ============================================================================
// ParseSetTileCommand Tests
// ============================================================================
TEST_F(Tile16ProposalGeneratorTest, ParseSetTileCommand_ValidCommand) {
std::string command = "overworld set-tile --map 0 --x 10 --y 20 --tile 0x02E";
auto result = generator_->ParseSetTileCommand(command, nullptr);
ASSERT_TRUE(result.ok()) << result.status().message();
EXPECT_EQ(result->map_id, 0);
EXPECT_EQ(result->x, 10);
EXPECT_EQ(result->y, 20);
EXPECT_EQ(result->new_tile, 0x02E);
}
TEST_F(Tile16ProposalGeneratorTest, ParseSetTileCommand_InvalidFormat) {
std::string command = "overworld set-tile --map 0"; // Missing required args
auto result = generator_->ParseSetTileCommand(command, nullptr);
EXPECT_FALSE(result.ok());
EXPECT_THAT(result.status().message(),
::testing::HasSubstr("Invalid command format"));
}
TEST_F(Tile16ProposalGeneratorTest, ParseSetTileCommand_WrongCommandType) {
std::string command = "overworld get-tile --map 0 --x 10 --y 20";
auto result = generator_->ParseSetTileCommand(command, nullptr);
EXPECT_FALSE(result.ok());
EXPECT_THAT(result.status().message(),
::testing::HasSubstr("Not a set-tile command"));
}
// ============================================================================
// ParseSetAreaCommand Tests
// ============================================================================
TEST_F(Tile16ProposalGeneratorTest, ParseSetAreaCommand_ValidCommand) {
std::string command =
"overworld set-area --map 0 --x 10 --y 20 --width 5 --height 3 --tile 0x02E";
auto result = generator_->ParseSetAreaCommand(command, nullptr);
ASSERT_TRUE(result.ok()) << result.status().message();
EXPECT_EQ(result->size(), 15); // 5 width * 3 height = 15 tiles
// Check first tile
EXPECT_EQ((*result)[0].map_id, 0);
EXPECT_EQ((*result)[0].x, 10);
EXPECT_EQ((*result)[0].y, 20);
EXPECT_EQ((*result)[0].new_tile, 0x02E);
// Check last tile
EXPECT_EQ((*result)[14].x, 14); // 10 + 4
EXPECT_EQ((*result)[14].y, 22); // 20 + 2
}
TEST_F(Tile16ProposalGeneratorTest, ParseSetAreaCommand_SingleTile) {
std::string command =
"overworld set-area --map 0 --x 10 --y 20 --width 1 --height 1 --tile 0x02E";
auto result = generator_->ParseSetAreaCommand(command, nullptr);
ASSERT_TRUE(result.ok());
EXPECT_EQ(result->size(), 1);
}
TEST_F(Tile16ProposalGeneratorTest, ParseSetAreaCommand_LargeArea) {
std::string command =
"overworld set-area --map 0 --x 0 --y 0 --width 32 --height 32 --tile 0x000";
auto result = generator_->ParseSetAreaCommand(command, nullptr);
ASSERT_TRUE(result.ok());
EXPECT_EQ(result->size(), 1024); // 32 * 32
}
TEST_F(Tile16ProposalGeneratorTest, ParseSetAreaCommand_InvalidFormat) {
std::string command = "overworld set-area --map 0 --x 10"; // Missing args
auto result = generator_->ParseSetAreaCommand(command, nullptr);
EXPECT_FALSE(result.ok());
EXPECT_THAT(result.status().message(),
::testing::HasSubstr("Invalid set-area command format"));
}
// ============================================================================
// ParseReplaceTileCommand Tests
// ============================================================================
TEST_F(Tile16ProposalGeneratorTest, ParseReplaceTileCommand_NoROM) {
std::string command =
"overworld replace-tile --map 0 --old-tile 0x02E --new-tile 0x030";
auto result = generator_->ParseReplaceTileCommand(command, nullptr);
EXPECT_FALSE(result.ok());
EXPECT_THAT(result.status().message(),
::testing::HasSubstr("ROM must be loaded"));
}
TEST_F(Tile16ProposalGeneratorTest, ParseReplaceTileCommand_InvalidFormat) {
std::string command = "overworld replace-tile --map 0"; // Missing tiles
auto result = generator_->ParseReplaceTileCommand(command, nullptr);
EXPECT_FALSE(result.ok());
EXPECT_THAT(result.status().message(),
::testing::HasSubstr("Invalid replace-tile command format"));
}
// ============================================================================
// GenerateFromCommands Tests
// ============================================================================
TEST_F(Tile16ProposalGeneratorTest, GenerateFromCommands_MultipleCommands) {
std::vector<std::string> commands = {
"overworld set-tile --map 0 --x 10 --y 20 --tile 0x02E",
"overworld set-area --map 0 --x 5 --y 5 --width 2 --height 2 --tile 0x030"
};
auto result = generator_->GenerateFromCommands(
"Test prompt", commands, "test_ai", nullptr);
ASSERT_TRUE(result.ok()) << result.status().message();
EXPECT_EQ(result->changes.size(), 5); // 1 from set-tile + 4 from set-area
EXPECT_EQ(result->prompt, "Test prompt");
EXPECT_EQ(result->ai_service, "test_ai");
EXPECT_EQ(result->status, Tile16Proposal::Status::PENDING);
}
TEST_F(Tile16ProposalGeneratorTest, GenerateFromCommands_EmptyCommands) {
std::vector<std::string> commands = {};
auto result = generator_->GenerateFromCommands(
"Test prompt", commands, "test_ai", nullptr);
EXPECT_FALSE(result.ok());
EXPECT_THAT(result.status().message(),
::testing::HasSubstr("No valid tile16 changes found"));
}
TEST_F(Tile16ProposalGeneratorTest, GenerateFromCommands_IgnoresComments) {
std::vector<std::string> commands = {
"# This is a comment",
"overworld set-tile --map 0 --x 10 --y 20 --tile 0x02E",
"# Another comment",
"" // Empty line
};
auto result = generator_->GenerateFromCommands(
"Test prompt", commands, "test_ai", nullptr);
ASSERT_TRUE(result.ok());
EXPECT_EQ(result->changes.size(), 1); // Only the valid command
}
// ============================================================================
// Tile16Change Tests
// ============================================================================
TEST_F(Tile16ProposalGeneratorTest, Tile16Change_ToString) {
Tile16Change change;
change.map_id = 5;
change.x = 10;
change.y = 20;
change.old_tile = 0x02E;
change.new_tile = 0x030;
std::string result = change.ToString();
EXPECT_THAT(result, ::testing::HasSubstr("Map 5"));
EXPECT_THAT(result, ::testing::HasSubstr("(10,20)"));
EXPECT_THAT(result, ::testing::HasSubstr("0x2e"));
EXPECT_THAT(result, ::testing::HasSubstr("0x30"));
}
// ============================================================================
// Proposal Serialization Tests
// ============================================================================
TEST_F(Tile16ProposalGeneratorTest, Proposal_ToJsonAndFromJson) {
Tile16Proposal original;
original.id = "test_id_123";
original.prompt = "Test prompt";
original.ai_service = "gemini";
original.reasoning = "Test reasoning";
original.status = Tile16Proposal::Status::PENDING;
Tile16Change change;
change.map_id = 5;
change.x = 10;
change.y = 20;
change.old_tile = 0x02E;
change.new_tile = 0x030;
original.changes.push_back(change);
std::string json = original.ToJson();
auto result = Tile16Proposal::FromJson(json);
ASSERT_TRUE(result.ok()) << result.status().message();
EXPECT_EQ(result->id, original.id);
EXPECT_EQ(result->prompt, original.prompt);
EXPECT_EQ(result->ai_service, original.ai_service);
EXPECT_EQ(result->reasoning, original.reasoning);
EXPECT_EQ(result->status, original.status);
EXPECT_EQ(result->changes.size(), 1);
EXPECT_EQ(result->changes[0].map_id, 5);
}
} // namespace
} // namespace cli
} // namespace yaze

View File

@@ -1,4 +1,4 @@
#include "app/core/asar_wrapper.h"
#include "core/asar_wrapper.h"
#include "test_utils.h"
#include <gtest/gtest.h>
@@ -7,7 +7,6 @@
#include <filesystem>
namespace yaze {
namespace app {
namespace core {
namespace {
@@ -321,5 +320,4 @@ TEST_F(AsarWrapperTest, CreatePatchNotImplemented) {
} // namespace
} // namespace core
} // namespace app
} // namespace yaze

View File

@@ -0,0 +1,74 @@
#include <gtest/gtest.h>
#include "app/emu/audio/apu.h"
#include "app/emu/memory/memory.h"
namespace yaze {
namespace emu {
class ApuDspTest : public ::testing::Test {
protected:
MemoryImpl mem;
Apu* apu;
void SetUp() override {
std::vector<uint8_t> dummy_rom(0x200000, 0);
mem.Initialize(dummy_rom);
apu = new Apu(mem);
apu->Init();
apu->Reset();
}
void TearDown() override { delete apu; }
};
TEST_F(ApuDspTest, DspRegistersReadWriteMirror) {
// Select register 0x0C (MVOLL)
apu->Write(0xF2, 0x0C);
apu->Write(0xF3, 0x7F);
// Read back
apu->Write(0xF2, 0x0C);
uint8_t mvoll = apu->Read(0xF3);
EXPECT_EQ(mvoll, 0x7F);
// Select register 0x1C (MVOLR)
apu->Write(0xF2, 0x1C);
apu->Write(0xF3, 0x40);
apu->Write(0xF2, 0x1C);
uint8_t mvolr = apu->Read(0xF3);
EXPECT_EQ(mvolr, 0x40);
}
TEST_F(ApuDspTest, TimersEnableAndReadback) {
// Enable timers 0 and 1, clear in-ports, map IPL off for RAM access
apu->Write(0xF1, 0x03);
// Set timer targets
apu->Write(0xFA, 0x04); // timer0 target
apu->Write(0xFB, 0x02); // timer1 target
// Run enough SPC cycles via APU cycle stepping
for (int i = 0; i < 10000; ++i) {
apu->Cycle();
}
// Read counters (auto-clears)
uint8_t t0 = apu->Read(0xFD);
uint8_t t1 = apu->Read(0xFE);
// Should be within 0..15 and non-zero under these cycles
EXPECT_LE(t0, 0x0F);
EXPECT_LE(t1, 0x0F);
}
TEST_F(ApuDspTest, GetSamplesReturnsSilenceAfterReset) {
int16_t buffer[2 * 256]{};
apu->dsp().GetSamples(buffer, 256, /*pal=*/false);
for (int i = 0; i < 256; ++i) {
EXPECT_EQ(buffer[i * 2 + 0], 0);
EXPECT_EQ(buffer[i * 2 + 1], 0);
}
}
} // namespace emu
} // namespace yaze

View File

@@ -0,0 +1,153 @@
#include <gtest/gtest.h>
#include "app/emu/audio/apu.h"
#include "app/emu/memory/memory.h"
#include "app/emu/audio/spc700.h"
namespace yaze {
namespace emu {
class ApuIplHandshakeTest : public ::testing::Test {
protected:
MemoryImpl mem;
Apu* apu;
void SetUp() override {
std::vector<uint8_t> dummy_rom(0x200000, 0);
mem.Initialize(dummy_rom);
apu = new Apu(mem);
apu->Init();
apu->Reset();
}
void TearDown() override { delete apu; }
};
TEST_F(ApuIplHandshakeTest, SPC700StartsAtIplRomEntry) {
// After reset, PC should be at IPL ROM reset vector
uint16_t reset_vector = apu->spc700().read(0xFFFE) |
(apu->spc700().read(0xFFFF) << 8);
// The IPL ROM reset vector should point to 0xFFC0 (start of IPL ROM)
EXPECT_EQ(reset_vector, 0xFFC0);
}
TEST_F(ApuIplHandshakeTest, IplRomReadable) {
// IPL ROM should be readable at 0xFFC0-0xFFFF after reset
uint8_t first_byte = apu->Read(0xFFC0);
// First byte of IPL ROM should be 0xCD (CMP Y, #$EF)
EXPECT_EQ(first_byte, 0xCD);
}
TEST_F(ApuIplHandshakeTest, CycleTrackingWorks) {
// Execute one SPC700 opcode
apu->spc700().RunOpcode();
// GetLastOpcodeCycles should return a valid cycle count (2-12 typically)
int cycles = apu->spc700().GetLastOpcodeCycles();
EXPECT_GT(cycles, 0);
EXPECT_LE(cycles, 12);
}
TEST_F(ApuIplHandshakeTest, PortReadWrite) {
// Write to input port from CPU side (simulating CPU writes to $2140-$2143)
apu->in_ports_[0] = 0xAA;
apu->in_ports_[1] = 0xBB;
// SPC should be able to read these ports at $F4-$F7
EXPECT_EQ(apu->Read(0xF4), 0xAA);
EXPECT_EQ(apu->Read(0xF5), 0xBB);
// Write to output ports from SPC side
apu->Write(0xF4, 0xCC);
apu->Write(0xF5, 0xDD);
// CPU should be able to read these (simulating reads from $2140-$2143)
EXPECT_EQ(apu->out_ports_[0], 0xCC);
EXPECT_EQ(apu->out_ports_[1], 0xDD);
}
TEST_F(ApuIplHandshakeTest, IplRomDisableViaControlRegister) {
// IPL ROM is readable by default
EXPECT_EQ(apu->Read(0xFFC0), 0xCD);
// Write to control register ($F1) to disable IPL ROM (bit 7 = 1)
apu->Write(0xF1, 0x80);
// Now $FFC0-$FFFF should read from RAM instead of IPL ROM
// RAM is initialized to 0, so we should read 0
EXPECT_EQ(apu->Read(0xFFC0), 0x00);
// Write something to RAM
apu->ram[0xFFC0] = 0x42;
EXPECT_EQ(apu->Read(0xFFC0), 0x42);
// Re-enable IPL ROM (bit 7 = 0)
apu->Write(0xF1, 0x00);
// Should read IPL ROM again
EXPECT_EQ(apu->Read(0xFFC0), 0xCD);
}
TEST_F(ApuIplHandshakeTest, TimersEnableAndCount) {
// Enable timer 0 via control register
apu->Write(0xF1, 0x01);
// Set timer 0 target to 4
apu->Write(0xFA, 0x04);
// Run enough cycles to trigger timer
for (int i = 0; i < 1000; ++i) {
apu->Cycle();
}
// Read timer 0 counter (auto-clears on read)
uint8_t counter = apu->Read(0xFD);
// Counter should be non-zero if timer is working
EXPECT_GT(counter, 0);
EXPECT_LE(counter, 0x0F);
}
TEST_F(ApuIplHandshakeTest, IplBootSequenceProgresses) {
// This test verifies that the IPL ROM boot sequence can actually progress
// without getting stuck in an infinite loop
uint16_t initial_pc = apu->spc700().PC;
// Run multiple opcodes to let the IPL boot sequence progress
for (int i = 0; i < 100; ++i) {
apu->spc700().RunOpcode();
apu->Cycle();
}
uint16_t final_pc = apu->spc700().PC;
// PC should have advanced (boot sequence is progressing)
// If it's stuck in a tight loop, PC won't change much
EXPECT_NE(initial_pc, final_pc);
}
TEST_F(ApuIplHandshakeTest, AccurateCycleCountsForCommonOpcodes) {
// Test that specific opcodes return correct cycle counts
// NOP (0x00) should take 2 cycles
apu->spc700().PC = 0x0000;
apu->ram[0x0000] = 0x00; // NOP
apu->spc700().RunOpcode();
apu->spc700().RunOpcode(); // Execute
EXPECT_EQ(apu->spc700().GetLastOpcodeCycles(), 2);
// MOV A, #imm (0xE8) should take 2 cycles
apu->spc700().PC = 0x0002;
apu->ram[0x0002] = 0xE8; // MOV A, #imm
apu->ram[0x0003] = 0x42; // immediate value
apu->spc700().RunOpcode();
apu->spc700().RunOpcode();
EXPECT_EQ(apu->spc700().GetLastOpcodeCycles(), 2);
}
} // namespace emu
} // namespace yaze

View File

@@ -0,0 +1,30 @@
#include <gtest/gtest.h>
#include "app/emu/audio/apu.h"
#include "app/emu/memory/memory.h"
namespace yaze {
namespace emu {
TEST(Spc700ResetTest, ResetVectorExecutesIplSequence) {
MemoryImpl mem;
std::vector<uint8_t> dummy_rom(0x200000, 0);
mem.Initialize(dummy_rom);
Apu apu(mem);
apu.Init();
apu.Reset();
// After reset, running some cycles should advance SPC PC from IPL entry
uint16_t pc_before = apu.spc700().PC;
for (int i = 0; i < 64; ++i) {
apu.spc700().RunOpcode();
apu.Cycle();
}
uint16_t pc_after = apu.spc700().PC;
EXPECT_NE(pc_after, pc_before);
}
} // namespace emu
} // namespace yaze

View File

@@ -1,4 +1,4 @@
#include "app/gfx/compression.h"
#include "app/gfx/util/compression.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>

View File

@@ -1,9 +1,9 @@
#include "app/gfx/snes_palette.h"
#include "app/gfx/types/snes_palette.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "app/gfx/snes_color.h"
#include "app/gfx/types/snes_color.h"
namespace yaze {
namespace test {

View File

@@ -1,4 +1,4 @@
#include "app/gfx/snes_tile.h"
#include "app/gfx/types/snes_tile.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>

View File

@@ -0,0 +1,477 @@
#include "app/gui/canvas/canvas_automation_api.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "app/gui/canvas/canvas.h"
#include "testing.h"
namespace yaze {
namespace test {
using ::testing::Eq;
using ::testing::Ge;
using ::testing::Le;
class CanvasAutomationAPITest : public ::testing::Test {
protected:
void SetUp() override {
// Create a test canvas with known dimensions
canvas_ = std::make_unique<gui::Canvas>("TestCanvas", ImVec2(512, 512),
gui::CanvasGridSize::k16x16);
api_ = canvas_->GetAutomationAPI();
ASSERT_NE(api_, nullptr);
}
std::unique_ptr<gui::Canvas> canvas_;
gui::CanvasAutomationAPI* api_;
};
// ============================================================================
// Coordinate Conversion Tests
// ============================================================================
TEST_F(CanvasAutomationAPITest, TileToCanvas_BasicConversion) {
// At 1.0x zoom, tile (0,0) should be at canvas (0,0)
canvas_->set_global_scale(1.0f);
ImVec2 pos = api_->TileToCanvas(0, 0);
EXPECT_FLOAT_EQ(pos.x, 0.0f);
EXPECT_FLOAT_EQ(pos.y, 0.0f);
// Tile (1,0) should be at (16,0) for 16x16 grid
pos = api_->TileToCanvas(1, 0);
EXPECT_FLOAT_EQ(pos.x, 16.0f);
EXPECT_FLOAT_EQ(pos.y, 0.0f);
// Tile (0,1) should be at (0,16)
pos = api_->TileToCanvas(0, 1);
EXPECT_FLOAT_EQ(pos.x, 0.0f);
EXPECT_FLOAT_EQ(pos.y, 16.0f);
// Tile (10,10) should be at (160,160)
pos = api_->TileToCanvas(10, 10);
EXPECT_FLOAT_EQ(pos.x, 160.0f);
EXPECT_FLOAT_EQ(pos.y, 160.0f);
}
TEST_F(CanvasAutomationAPITest, TileToCanvas_WithZoom) {
// At 2.0x zoom, tile coordinates should scale
canvas_->set_global_scale(2.0f);
ImVec2 pos = api_->TileToCanvas(1, 1);
EXPECT_FLOAT_EQ(pos.x, 32.0f); // 1 * 16 * 2.0
EXPECT_FLOAT_EQ(pos.y, 32.0f);
// At 0.5x zoom, tile coordinates should scale down
canvas_->set_global_scale(0.5f);
pos = api_->TileToCanvas(10, 10);
EXPECT_FLOAT_EQ(pos.x, 80.0f); // 10 * 16 * 0.5
EXPECT_FLOAT_EQ(pos.y, 80.0f);
}
TEST_F(CanvasAutomationAPITest, CanvasToTile_BasicConversion) {
canvas_->set_global_scale(1.0f);
// Canvas (0,0) should be tile (0,0)
ImVec2 tile = api_->CanvasToTile(ImVec2(0, 0));
EXPECT_FLOAT_EQ(tile.x, 0.0f);
EXPECT_FLOAT_EQ(tile.y, 0.0f);
// Canvas (16,16) should be tile (1,1)
tile = api_->CanvasToTile(ImVec2(16, 16));
EXPECT_FLOAT_EQ(tile.x, 1.0f);
EXPECT_FLOAT_EQ(tile.y, 1.0f);
// Canvas (160,160) should be tile (10,10)
tile = api_->CanvasToTile(ImVec2(160, 160));
EXPECT_FLOAT_EQ(tile.x, 10.0f);
EXPECT_FLOAT_EQ(tile.y, 10.0f);
}
TEST_F(CanvasAutomationAPITest, CanvasToTile_WithZoom) {
// At 2.0x zoom
canvas_->set_global_scale(2.0f);
ImVec2 tile = api_->CanvasToTile(ImVec2(32, 32));
EXPECT_FLOAT_EQ(tile.x, 1.0f); // 32 / (16 * 2.0)
EXPECT_FLOAT_EQ(tile.y, 1.0f);
// At 0.5x zoom
canvas_->set_global_scale(0.5f);
tile = api_->CanvasToTile(ImVec2(8, 8));
EXPECT_FLOAT_EQ(tile.x, 1.0f); // 8 / (16 * 0.5)
EXPECT_FLOAT_EQ(tile.y, 1.0f);
}
TEST_F(CanvasAutomationAPITest, CoordinateRoundTrip) {
canvas_->set_global_scale(1.0f);
// Test round-trip conversion
for (int i = 0; i < 32; ++i) {
ImVec2 canvas_pos = api_->TileToCanvas(i, i);
ImVec2 tile_pos = api_->CanvasToTile(canvas_pos);
EXPECT_FLOAT_EQ(tile_pos.x, static_cast<float>(i));
EXPECT_FLOAT_EQ(tile_pos.y, static_cast<float>(i));
}
}
// ============================================================================
// Bounds Checking Tests
// ============================================================================
TEST_F(CanvasAutomationAPITest, IsInBounds_ValidCoordinates) {
EXPECT_TRUE(api_->IsInBounds(0, 0));
EXPECT_TRUE(api_->IsInBounds(10, 10));
EXPECT_TRUE(api_->IsInBounds(31, 31)); // 512/16 = 32 tiles, so 31 is max
}
TEST_F(CanvasAutomationAPITest, IsInBounds_InvalidCoordinates) {
EXPECT_FALSE(api_->IsInBounds(-1, 0));
EXPECT_FALSE(api_->IsInBounds(0, -1));
EXPECT_FALSE(api_->IsInBounds(-1, -1));
EXPECT_FALSE(api_->IsInBounds(32, 0)); // Out of bounds
EXPECT_FALSE(api_->IsInBounds(0, 32));
EXPECT_FALSE(api_->IsInBounds(100, 100));
}
// ============================================================================
// Tile Operations Tests
// ============================================================================
TEST_F(CanvasAutomationAPITest, SetTileAt_WithCallback) {
// Set up a tile paint callback
std::vector<std::tuple<int, int, int>> painted_tiles;
api_->SetTilePaintCallback([&](int x, int y, int tile_id) {
painted_tiles.push_back({x, y, tile_id});
return true;
});
// Paint some tiles
EXPECT_TRUE(api_->SetTileAt(5, 5, 42));
EXPECT_TRUE(api_->SetTileAt(10, 10, 100));
ASSERT_EQ(painted_tiles.size(), 2);
EXPECT_EQ(painted_tiles[0], std::make_tuple(5, 5, 42));
EXPECT_EQ(painted_tiles[1], std::make_tuple(10, 10, 100));
}
TEST_F(CanvasAutomationAPITest, SetTileAt_OutOfBounds) {
bool callback_called = false;
api_->SetTilePaintCallback([&](int x, int y, int tile_id) {
callback_called = true;
return true;
});
// Out of bounds tiles should return false without calling callback
EXPECT_FALSE(api_->SetTileAt(-1, 0, 42));
EXPECT_FALSE(api_->SetTileAt(0, -1, 42));
EXPECT_FALSE(api_->SetTileAt(100, 100, 42));
EXPECT_FALSE(callback_called);
}
TEST_F(CanvasAutomationAPITest, GetTileAt_WithCallback) {
// Set up a tile query callback
api_->SetTileQueryCallback([](int x, int y) {
return x * 100 + y; // Simple deterministic value
});
EXPECT_EQ(api_->GetTileAt(0, 0), 0);
EXPECT_EQ(api_->GetTileAt(1, 0), 100);
EXPECT_EQ(api_->GetTileAt(0, 1), 1);
EXPECT_EQ(api_->GetTileAt(5, 7), 507);
}
TEST_F(CanvasAutomationAPITest, GetTileAt_OutOfBounds) {
api_->SetTileQueryCallback([](int x, int y) { return 42; });
EXPECT_EQ(api_->GetTileAt(-1, 0), -1);
EXPECT_EQ(api_->GetTileAt(0, -1), -1);
EXPECT_EQ(api_->GetTileAt(100, 100), -1);
}
TEST_F(CanvasAutomationAPITest, SetTiles_BatchOperation) {
std::vector<std::tuple<int, int, int>> painted_tiles;
api_->SetTilePaintCallback([&](int x, int y, int tile_id) {
painted_tiles.push_back({x, y, tile_id});
return true;
});
std::vector<std::tuple<int, int, int>> tiles_to_paint = {
{0, 0, 10},
{1, 0, 11},
{2, 0, 12},
{0, 1, 20},
{1, 1, 21}
};
EXPECT_TRUE(api_->SetTiles(tiles_to_paint));
EXPECT_EQ(painted_tiles.size(), 5);
}
// ============================================================================
// Selection Tests
// ============================================================================
TEST_F(CanvasAutomationAPITest, SelectTile) {
api_->SelectTile(5, 5);
auto selection = api_->GetSelection();
EXPECT_TRUE(selection.has_selection);
EXPECT_EQ(selection.selected_tiles.size(), 1);
}
TEST_F(CanvasAutomationAPITest, SelectTileRect) {
api_->SelectTileRect(5, 5, 9, 9);
auto selection = api_->GetSelection();
EXPECT_TRUE(selection.has_selection);
// 5x5 rectangle = 25 tiles
EXPECT_EQ(selection.selected_tiles.size(), 25);
// Check first and last tiles
EXPECT_FLOAT_EQ(selection.selected_tiles[0].x, 5.0f);
EXPECT_FLOAT_EQ(selection.selected_tiles[0].y, 5.0f);
EXPECT_FLOAT_EQ(selection.selected_tiles[24].x, 9.0f);
EXPECT_FLOAT_EQ(selection.selected_tiles[24].y, 9.0f);
}
TEST_F(CanvasAutomationAPITest, SelectTileRect_SwappedCoordinates) {
// Should handle coordinates in any order
api_->SelectTileRect(9, 9, 5, 5); // Reversed
auto selection = api_->GetSelection();
EXPECT_TRUE(selection.has_selection);
EXPECT_EQ(selection.selected_tiles.size(), 25);
}
TEST_F(CanvasAutomationAPITest, ClearSelection) {
api_->SelectTileRect(5, 5, 10, 10);
auto selection = api_->GetSelection();
EXPECT_TRUE(selection.has_selection);
api_->ClearSelection();
selection = api_->GetSelection();
EXPECT_FALSE(selection.has_selection);
EXPECT_EQ(selection.selected_tiles.size(), 0);
}
TEST_F(CanvasAutomationAPITest, SelectTile_OutOfBounds) {
api_->SelectTile(-1, 0);
auto selection = api_->GetSelection();
EXPECT_FALSE(selection.has_selection);
api_->SelectTile(100, 100);
selection = api_->GetSelection();
EXPECT_FALSE(selection.has_selection);
}
// ============================================================================
// View Operations Tests
// ============================================================================
TEST_F(CanvasAutomationAPITest, SetZoom_ValidRange) {
api_->SetZoom(1.0f);
EXPECT_FLOAT_EQ(api_->GetZoom(), 1.0f);
api_->SetZoom(2.0f);
EXPECT_FLOAT_EQ(api_->GetZoom(), 2.0f);
api_->SetZoom(0.5f);
EXPECT_FLOAT_EQ(api_->GetZoom(), 0.5f);
}
TEST_F(CanvasAutomationAPITest, SetZoom_Clamping) {
// Should clamp to 0.25 - 4.0 range
api_->SetZoom(10.0f);
EXPECT_LE(api_->GetZoom(), 4.0f);
api_->SetZoom(0.1f);
EXPECT_GE(api_->GetZoom(), 0.25f);
api_->SetZoom(-1.0f);
EXPECT_GE(api_->GetZoom(), 0.25f);
}
TEST_F(CanvasAutomationAPITest, ScrollToTile_ValidTile) {
// Should not crash when scrolling to valid tiles
api_->ScrollToTile(0, 0, true);
api_->ScrollToTile(10, 10, false);
api_->ScrollToTile(15, 15, true);
// Just verify no crash - actual scroll behavior depends on ImGui state
}
TEST_F(CanvasAutomationAPITest, ScrollToTile_OutOfBounds) {
// Should handle out of bounds gracefully
api_->ScrollToTile(-1, 0, true);
api_->ScrollToTile(100, 100, true);
// Should not crash
}
TEST_F(CanvasAutomationAPITest, CenterOn_ValidTile) {
// Should not crash when centering on valid tiles
api_->CenterOn(10, 10);
api_->CenterOn(0, 0);
api_->CenterOn(20, 20);
// Verify scroll position changed (should be non-zero after centering on non-origin)
ImVec2 scroll = canvas_->scrolling();
// Scroll values will depend on canvas size, just verify they're set
}
TEST_F(CanvasAutomationAPITest, CenterOn_OutOfBounds) {
api_->CenterOn(-1, 0);
api_->CenterOn(100, 100);
// Should not crash
}
// ============================================================================
// Query Operations Tests
// ============================================================================
TEST_F(CanvasAutomationAPITest, GetDimensions) {
canvas_->set_global_scale(1.0f);
auto dims = api_->GetDimensions();
EXPECT_EQ(dims.tile_size, 16); // 16x16 grid
EXPECT_EQ(dims.width_tiles, 32); // 512 / 16
EXPECT_EQ(dims.height_tiles, 32);
}
TEST_F(CanvasAutomationAPITest, GetDimensions_WithZoom) {
canvas_->set_global_scale(2.0f);
auto dims = api_->GetDimensions();
EXPECT_EQ(dims.tile_size, 16);
EXPECT_EQ(dims.width_tiles, 16); // 512 / (16 * 2.0)
EXPECT_EQ(dims.height_tiles, 16);
}
TEST_F(CanvasAutomationAPITest, GetVisibleRegion) {
canvas_->set_global_scale(1.0f);
canvas_->set_scrolling(ImVec2(0, 0));
auto region = api_->GetVisibleRegion();
// At origin with no scroll, should start at (0,0)
EXPECT_GE(region.min_x, 0);
EXPECT_GE(region.min_y, 0);
// Should have valid bounds
EXPECT_GE(region.max_x, region.min_x);
EXPECT_GE(region.max_y, region.min_y);
}
TEST_F(CanvasAutomationAPITest, IsTileVisible_AtOrigin) {
canvas_->set_global_scale(1.0f);
canvas_->set_scrolling(ImVec2(0, 0));
// Tiles at origin should be visible
EXPECT_TRUE(api_->IsTileVisible(0, 0));
EXPECT_TRUE(api_->IsTileVisible(1, 1));
// Tiles far away might not be visible (depends on canvas size)
// We just verify the method doesn't crash
api_->IsTileVisible(50, 50);
}
TEST_F(CanvasAutomationAPITest, IsTileVisible_OutOfBounds) {
// Out of bounds tiles should return false
EXPECT_FALSE(api_->IsTileVisible(-1, 0));
EXPECT_FALSE(api_->IsTileVisible(0, -1));
EXPECT_FALSE(api_->IsTileVisible(100, 100));
}
// ============================================================================
// Integration Tests
// ============================================================================
TEST_F(CanvasAutomationAPITest, CompleteWorkflow) {
// Simulate a complete automation workflow
// 1. Set zoom level
api_->SetZoom(1.0f);
EXPECT_FLOAT_EQ(api_->GetZoom(), 1.0f);
// 2. Select a tile region
api_->SelectTileRect(0, 0, 4, 4);
auto selection = api_->GetSelection();
EXPECT_EQ(selection.selected_tiles.size(), 25);
// 3. Query tile data with callback
api_->SetTileQueryCallback([](int x, int y) {
return x + y * 100;
});
EXPECT_EQ(api_->GetTileAt(2, 3), 302);
// 4. Paint tiles with callback
std::vector<std::tuple<int, int, int>> painted;
api_->SetTilePaintCallback([&](int x, int y, int tile_id) {
painted.push_back({x, y, tile_id});
return true;
});
std::vector<std::tuple<int, int, int>> tiles = {
{0, 0, 10}, {1, 0, 11}, {2, 0, 12}
};
EXPECT_TRUE(api_->SetTiles(tiles));
EXPECT_EQ(painted.size(), 3);
// 5. Clear selection
api_->ClearSelection();
selection = api_->GetSelection();
EXPECT_FALSE(selection.has_selection);
}
TEST_F(CanvasAutomationAPITest, DifferentGridSizes) {
// Test with 8x8 grid
auto canvas_8x8 = std::make_unique<gui::Canvas>(
"Test8x8", ImVec2(512, 512), gui::CanvasGridSize::k8x8);
auto api_8x8 = canvas_8x8->GetAutomationAPI();
auto dims = api_8x8->GetDimensions();
EXPECT_EQ(dims.tile_size, 8);
EXPECT_EQ(dims.width_tiles, 64); // 512 / 8
// Test with 32x32 grid
auto canvas_32x32 = std::make_unique<gui::Canvas>(
"Test32x32", ImVec2(512, 512), gui::CanvasGridSize::k32x32);
auto api_32x32 = canvas_32x32->GetAutomationAPI();
dims = api_32x32->GetDimensions();
EXPECT_EQ(dims.tile_size, 32);
EXPECT_EQ(dims.width_tiles, 16); // 512 / 32
}
TEST_F(CanvasAutomationAPITest, MultipleZoomLevels) {
float zoom_levels[] = {0.25f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f};
for (float zoom : zoom_levels) {
api_->SetZoom(zoom);
float actual_zoom = api_->GetZoom();
// Should be clamped to valid range
EXPECT_GE(actual_zoom, 0.25f);
EXPECT_LE(actual_zoom, 4.0f);
// Coordinate conversion should still work
ImVec2 canvas_pos = api_->TileToCanvas(10, 10);
ImVec2 tile_pos = api_->CanvasToTile(canvas_pos);
EXPECT_FLOAT_EQ(tile_pos.x, 10.0f);
EXPECT_FLOAT_EQ(tile_pos.y, 10.0f);
}
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,297 @@
#include "app/gui/canvas/canvas.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "testing.h"
namespace yaze {
namespace test {
using ::testing::Eq;
using ::testing::FloatEq;
using ::testing::Ne;
/**
* @brief Tests for canvas coordinate synchronization
*
* These tests verify that the canvas coordinate system properly tracks
* mouse position for both hover and paint operations, fixing the regression
* where CheckForCurrentMap() in OverworldEditor was using raw ImGui mouse
* position instead of canvas-local coordinates.
*
* Regression: overworld_editor.cc:1041 was using ImGui::GetIO().MousePos
* instead of canvas hover position, causing map highlighting to break.
*/
class CanvasCoordinateSyncTest : public ::testing::Test {
protected:
void SetUp() override {
// Create a test canvas with known dimensions (4096x4096 for overworld)
canvas_ = std::make_unique<gui::Canvas>("OverworldCanvas", ImVec2(4096, 4096),
gui::CanvasGridSize::k16x16);
canvas_->set_global_scale(1.0f);
}
std::unique_ptr<gui::Canvas> canvas_;
};
// ============================================================================
// Hover Position Tests (hover_mouse_pos)
// ============================================================================
TEST_F(CanvasCoordinateSyncTest, HoverMousePos_InitialState) {
// Hover position should start at (0,0) or invalid state
auto hover_pos = canvas_->hover_mouse_pos();
// Initial state may be (0,0) - this is valid
EXPECT_GE(hover_pos.x, 0.0f);
EXPECT_GE(hover_pos.y, 0.0f);
}
TEST_F(CanvasCoordinateSyncTest, HoverMousePos_IndependentFromDrawnPos) {
// Hover position and drawn tile position are independent
// hover_mouse_pos() tracks continuous mouse movement
// drawn_tile_position() only updates during painting
auto hover_pos = canvas_->hover_mouse_pos();
auto drawn_pos = canvas_->drawn_tile_position();
// These may differ - hover tracks all movement, drawn only tracks paint
// We just verify both are valid (non-negative or expected sentinel values)
EXPECT_TRUE(hover_pos.x >= 0.0f || hover_pos.x == -1.0f);
EXPECT_TRUE(drawn_pos.x >= 0.0f || drawn_pos.x == -1.0f);
}
// ============================================================================
// Coordinate Space Tests
// ============================================================================
TEST_F(CanvasCoordinateSyncTest, CoordinateSpace_WorldNotScreen) {
// REGRESSION TEST: Verify hover_mouse_pos() returns world coordinates
// not screen coordinates. The bug was using ImGui::GetIO().MousePos
// which is in screen space and doesn't account for scrolling/canvas offset.
// Simulate scrolling the canvas
canvas_->set_scrolling(ImVec2(100, 100));
// The hover position should be in canvas/world space, not affected by
// the canvas's screen position. This is tested by ensuring the method
// exists and returns a coordinate that could be used for map calculations.
auto hover_pos = canvas_->hover_mouse_pos();
// Valid world coordinates should be usable for map index calculations
// For a 512x512 map size (kOverworldMapSize = 512):
// map_x = hover_pos.x / 512
// map_y = hover_pos.y / 512
int map_x = static_cast<int>(hover_pos.x) / 512;
int map_y = static_cast<int>(hover_pos.y) / 512;
// Map indices should be within valid range for 8x8 overworld grid
EXPECT_GE(map_x, 0);
EXPECT_GE(map_y, 0);
EXPECT_LT(map_x, 64); // 8x8 grid = 64 maps max
EXPECT_LT(map_y, 64);
}
TEST_F(CanvasCoordinateSyncTest, MapCalculation_SmallMaps) {
// Test map index calculation for standard 512x512 maps
const int kOverworldMapSize = 512;
// Simulate hover at different world positions
std::vector<ImVec2> test_positions = {
ImVec2(0, 0), // Map (0, 0)
ImVec2(512, 0), // Map (1, 0)
ImVec2(0, 512), // Map (0, 1)
ImVec2(512, 512), // Map (1, 1)
ImVec2(1536, 1024), // Map (3, 2)
};
std::vector<std::pair<int, int>> expected_maps = {
{0, 0}, {1, 0}, {0, 1}, {1, 1}, {3, 2}
};
for (size_t i = 0; i < test_positions.size(); ++i) {
ImVec2 pos = test_positions[i];
int map_x = pos.x / kOverworldMapSize;
int map_y = pos.y / kOverworldMapSize;
EXPECT_EQ(map_x, expected_maps[i].first);
EXPECT_EQ(map_y, expected_maps[i].second);
}
}
TEST_F(CanvasCoordinateSyncTest, MapCalculation_LargeMaps) {
// Test map index calculation for ZSCustomOverworld v3 large maps (1024x1024)
const int kLargeMapSize = 1024;
// Large maps should span multiple standard map coordinates
std::vector<ImVec2> test_positions = {
ImVec2(0, 0), // Large map (0, 0)
ImVec2(1024, 0), // Large map (1, 0)
ImVec2(0, 1024), // Large map (0, 1)
ImVec2(2048, 2048), // Large map (2, 2)
};
std::vector<std::pair<int, int>> expected_large_maps = {
{0, 0}, {1, 0}, {0, 1}, {2, 2}
};
for (size_t i = 0; i < test_positions.size(); ++i) {
ImVec2 pos = test_positions[i];
int map_x = pos.x / kLargeMapSize;
int map_y = pos.y / kLargeMapSize;
EXPECT_EQ(map_x, expected_large_maps[i].first);
EXPECT_EQ(map_y, expected_large_maps[i].second);
}
}
// ============================================================================
// Scale Invariance Tests
// ============================================================================
TEST_F(CanvasCoordinateSyncTest, HoverPosition_ScaleInvariant) {
// REGRESSION TEST: Hover position should be in world space regardless of scale
// The bug was scale-dependent because it used screen coordinates
auto test_hover_at_scale = [&](float scale) {
canvas_->set_global_scale(scale);
auto hover_pos = canvas_->hover_mouse_pos();
// Hover position should be in world coordinates, not affected by scale
// World coordinates are always in the range [0, canvas_size)
EXPECT_GE(hover_pos.x, 0.0f);
EXPECT_GE(hover_pos.y, 0.0f);
EXPECT_LE(hover_pos.x, 4096.0f);
EXPECT_LE(hover_pos.y, 4096.0f);
};
test_hover_at_scale(0.25f);
test_hover_at_scale(0.5f);
test_hover_at_scale(1.0f);
test_hover_at_scale(2.0f);
test_hover_at_scale(4.0f);
}
// ============================================================================
// Overworld Editor Integration Tests
// ============================================================================
TEST_F(CanvasCoordinateSyncTest, OverworldMapHighlight_UsesHoverNotDrawn) {
// CRITICAL REGRESSION TEST
// This verifies the fix for overworld_editor.cc:1041
// CheckForCurrentMap() must use hover_mouse_pos() not ImGui::GetIO().MousePos
// The pattern used in DrawOverworldEdits (line 664) for painting:
auto drawn_pos = canvas_->drawn_tile_position();
// The pattern that SHOULD be used in CheckForCurrentMap (line 1041) for highlighting:
auto hover_pos = canvas_->hover_mouse_pos();
// These are different methods for different purposes:
// - drawn_tile_position(): Only updates during active painting (mouse drag)
// - hover_mouse_pos(): Updates continuously during hover
// Verify both methods exist and return valid (or sentinel) values
EXPECT_TRUE(drawn_pos.x >= 0.0f || drawn_pos.x == -1.0f);
EXPECT_TRUE(hover_pos.x >= 0.0f || hover_pos.x == -1.0f);
}
TEST_F(CanvasCoordinateSyncTest, OverworldMapIndex_From8x8Grid) {
// Simulate the exact calculation from OverworldEditor::CheckForCurrentMap
const int kOverworldMapSize = 512;
// Test all three worlds (Light, Dark, Special)
struct TestCase {
ImVec2 hover_pos;
int current_world; // 0=Light, 1=Dark, 2=Special
int expected_map_index;
};
std::vector<TestCase> test_cases = {
// Light World (0x00 - 0x3F)
{ImVec2(0, 0), 0, 0}, // Map 0 (Light World)
{ImVec2(512, 0), 0, 1}, // Map 1
{ImVec2(1024, 512), 0, 10}, // Map 10 = 2 + 1*8
// Dark World (0x40 - 0x7F)
{ImVec2(0, 0), 1, 0x40}, // Map 0x40 (Dark World)
{ImVec2(512, 0), 1, 0x41}, // Map 0x41
{ImVec2(1024, 512), 1, 0x4A}, // Map 0x4A = 0x40 + 10
// Special World (0x80+)
{ImVec2(0, 0), 2, 0x80}, // Map 0x80 (Special World)
{ImVec2(512, 512), 2, 0x89}, // Map 0x89 = 0x80 + 9
};
for (const auto& tc : test_cases) {
int map_x = tc.hover_pos.x / kOverworldMapSize;
int map_y = tc.hover_pos.y / kOverworldMapSize;
int hovered_map = map_x + map_y * 8;
if (tc.current_world == 1) {
hovered_map += 0x40;
} else if (tc.current_world == 2) {
hovered_map += 0x80;
}
EXPECT_EQ(hovered_map, tc.expected_map_index)
<< "Failed for world " << tc.current_world
<< " at position (" << tc.hover_pos.x << ", " << tc.hover_pos.y << ")";
}
}
// ============================================================================
// Boundary Condition Tests
// ============================================================================
TEST_F(CanvasCoordinateSyncTest, MapBoundaries_512x512) {
// Test coordinates exactly at map boundaries
const int kOverworldMapSize = 512;
// Boundary coordinates (edges of maps)
std::vector<ImVec2> boundary_positions = {
ImVec2(511, 0), // Right edge of map 0
ImVec2(512, 0), // Left edge of map 1
ImVec2(0, 511), // Bottom edge of map 0
ImVec2(0, 512), // Top edge of map 8
ImVec2(511, 511), // Corner of map 0
ImVec2(512, 512), // Corner of map 9
};
for (const auto& pos : boundary_positions) {
int map_x = pos.x / kOverworldMapSize;
int map_y = pos.y / kOverworldMapSize;
int map_index = map_x + map_y * 8;
// Verify map indices are within valid range
EXPECT_GE(map_index, 0);
EXPECT_LT(map_index, 64); // 8x8 grid = 64 maps
}
}
TEST_F(CanvasCoordinateSyncTest, MapBoundaries_1024x1024) {
// Test large map boundaries (ZSCustomOverworld v3)
const int kLargeMapSize = 1024;
std::vector<ImVec2> boundary_positions = {
ImVec2(1023, 0), // Right edge of large map 0
ImVec2(1024, 0), // Left edge of large map 1
ImVec2(0, 1023), // Bottom edge of large map 0
ImVec2(0, 1024), // Top edge of large map 4 (0,1 in 4x4 grid)
};
for (const auto& pos : boundary_positions) {
int map_x = pos.x / kLargeMapSize;
int map_y = pos.y / kLargeMapSize;
int map_index = map_x + map_y * 4; // 4x4 grid for large maps
// Verify map indices are within valid range for large maps
EXPECT_GE(map_index, 0);
EXPECT_LT(map_index, 16); // 4x4 grid = 16 large maps
}
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,194 @@
#include "app/gui/widgets/tile_selector_widget.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "app/gfx/core/bitmap.h"
#include "app/gui/canvas/canvas.h"
#include "testing.h"
namespace yaze {
namespace test {
using ::testing::Eq;
using ::testing::NotNull;
class TileSelectorWidgetTest : public ::testing::Test {
protected:
void SetUp() override {
// Create a test canvas
canvas_ = std::make_unique<gui::Canvas>("TestCanvas", ImVec2(512, 512),
gui::CanvasGridSize::k16x16);
// Create a test config
config_.tile_size = 16;
config_.display_scale = 2.0f;
config_.tiles_per_row = 8;
config_.total_tiles = 64; // 8x8 grid
config_.draw_offset = {2.0f, 0.0f};
config_.show_tile_ids = false;
config_.highlight_color = {1.0f, 0.85f, 0.35f, 1.0f};
}
std::unique_ptr<gui::Canvas> canvas_;
gui::TileSelectorWidget::Config config_;
};
// Test basic construction
TEST_F(TileSelectorWidgetTest, Construction) {
gui::TileSelectorWidget widget("test_widget");
EXPECT_EQ(widget.GetSelectedTileID(), 0);
}
// Test construction with config
TEST_F(TileSelectorWidgetTest, ConstructionWithConfig) {
gui::TileSelectorWidget widget("test_widget", config_);
EXPECT_EQ(widget.GetSelectedTileID(), 0);
}
// Test canvas attachment
TEST_F(TileSelectorWidgetTest, AttachCanvas) {
gui::TileSelectorWidget widget("test_widget");
widget.AttachCanvas(canvas_.get());
// No crash means success
}
// Test tile count setting
TEST_F(TileSelectorWidgetTest, SetTileCount) {
gui::TileSelectorWidget widget("test_widget", config_);
widget.SetTileCount(128);
// Verify selection is clamped when tile count changes
widget.SetSelectedTile(100);
EXPECT_EQ(widget.GetSelectedTileID(), 100);
// Setting tile count lower should clamp selection
widget.SetTileCount(50);
EXPECT_EQ(widget.GetSelectedTileID(), 0); // Should reset to 0
}
// Test selected tile setting
TEST_F(TileSelectorWidgetTest, SetSelectedTile) {
gui::TileSelectorWidget widget("test_widget", config_);
widget.SetTileCount(64);
widget.SetSelectedTile(10);
EXPECT_EQ(widget.GetSelectedTileID(), 10);
widget.SetSelectedTile(63);
EXPECT_EQ(widget.GetSelectedTileID(), 63);
// Out of bounds should be ignored
widget.SetSelectedTile(64);
EXPECT_EQ(widget.GetSelectedTileID(), 63); // Should remain unchanged
widget.SetSelectedTile(-1);
EXPECT_EQ(widget.GetSelectedTileID(), 63); // Should remain unchanged
}
// Test tile origin calculation
TEST_F(TileSelectorWidgetTest, TileOrigin) {
gui::TileSelectorWidget widget("test_widget", config_);
widget.SetTileCount(64);
// Test first tile (0,0)
auto origin = widget.TileOrigin(0);
EXPECT_FLOAT_EQ(origin.x, config_.draw_offset.x);
EXPECT_FLOAT_EQ(origin.y, config_.draw_offset.y);
// Test tile at (1,0)
origin = widget.TileOrigin(1);
float expected_x = config_.draw_offset.x +
(config_.tile_size * config_.display_scale);
EXPECT_FLOAT_EQ(origin.x, expected_x);
EXPECT_FLOAT_EQ(origin.y, config_.draw_offset.y);
// Test tile at (0,1) - first tile of second row
origin = widget.TileOrigin(8);
expected_x = config_.draw_offset.x;
float expected_y = config_.draw_offset.y +
(config_.tile_size * config_.display_scale);
EXPECT_FLOAT_EQ(origin.x, expected_x);
EXPECT_FLOAT_EQ(origin.y, expected_y);
// Test invalid tile ID
origin = widget.TileOrigin(64);
EXPECT_FLOAT_EQ(origin.x, -1.0f);
EXPECT_FLOAT_EQ(origin.y, -1.0f);
}
// Test render without atlas (should not crash)
TEST_F(TileSelectorWidgetTest, RenderWithoutAtlas) {
gui::TileSelectorWidget widget("test_widget", config_);
widget.AttachCanvas(canvas_.get());
gfx::Bitmap atlas;
auto result = widget.Render(atlas, false);
EXPECT_FALSE(result.tile_clicked);
EXPECT_FALSE(result.tile_double_clicked);
EXPECT_FALSE(result.selection_changed);
EXPECT_EQ(result.selected_tile, -1);
}
// Test programmatic selection for AI/automation
TEST_F(TileSelectorWidgetTest, ProgrammaticSelection) {
gui::TileSelectorWidget widget("test_widget", config_);
widget.AttachCanvas(canvas_.get());
widget.SetTileCount(64);
// Simulate AI/automation selecting tiles programmatically
for (int i = 0; i < 64; ++i) {
widget.SetSelectedTile(i);
EXPECT_EQ(widget.GetSelectedTileID(), i);
auto origin = widget.TileOrigin(i);
int expected_col = i % config_.tiles_per_row;
int expected_row = i / config_.tiles_per_row;
float expected_x = config_.draw_offset.x +
expected_col * config_.tile_size * config_.display_scale;
float expected_y = config_.draw_offset.y +
expected_row * config_.tile_size * config_.display_scale;
EXPECT_FLOAT_EQ(origin.x, expected_x);
EXPECT_FLOAT_EQ(origin.y, expected_y);
}
}
// Test scroll to tile
TEST_F(TileSelectorWidgetTest, ScrollToTile) {
gui::TileSelectorWidget widget("test_widget", config_);
widget.AttachCanvas(canvas_.get());
widget.SetTileCount(64);
// Scroll to various tiles (should not crash)
widget.ScrollToTile(0);
widget.ScrollToTile(10);
widget.ScrollToTile(63);
// Invalid tile should not crash
widget.ScrollToTile(-1);
widget.ScrollToTile(64);
}
// Test different configs
TEST_F(TileSelectorWidgetTest, DifferentConfigs) {
// Test with 16x16 grid
gui::TileSelectorWidget::Config large_config;
large_config.tile_size = 8;
large_config.display_scale = 1.0f;
large_config.tiles_per_row = 16;
large_config.total_tiles = 256;
large_config.draw_offset = {0.0f, 0.0f};
gui::TileSelectorWidget large_widget("large_widget", large_config);
large_widget.SetTileCount(256);
for (int i = 0; i < 256; ++i) {
large_widget.SetSelectedTile(i);
EXPECT_EQ(large_widget.GetSelectedTileID(), i);
}
}
} // namespace test
} // namespace yaze

View File

@@ -0,0 +1,258 @@
#include "app/gfx/types/snes_color.h"
#include <gtest/gtest.h>
#include "imgui/imgui.h"
namespace yaze {
namespace gfx {
namespace {
// Test fixture for SnesColor tests
class SnesColorTest : public ::testing::Test {
protected:
void SetUp() override {
// Common setup if needed
}
};
// ============================================================================
// RGB Format Conversion Tests
// ============================================================================
TEST_F(SnesColorTest, SetRgbFromImGuiNormalizedValues) {
SnesColor color;
// ImGui ColorPicker returns values in 0-1 range
ImVec4 imgui_color(0.5f, 0.75f, 1.0f, 1.0f);
color.set_rgb(imgui_color);
// Internal storage should be in 0-255 range
auto rgb = color.rgb();
EXPECT_FLOAT_EQ(rgb.x, 127.5f); // 0.5 * 255
EXPECT_FLOAT_EQ(rgb.y, 191.25f); // 0.75 * 255
EXPECT_FLOAT_EQ(rgb.z, 255.0f); // 1.0 * 255
EXPECT_FLOAT_EQ(rgb.w, 255.0f); // Alpha always 255
}
TEST_F(SnesColorTest, SetRgbBlackColor) {
SnesColor color;
ImVec4 black(0.0f, 0.0f, 0.0f, 1.0f);
color.set_rgb(black);
auto rgb = color.rgb();
EXPECT_FLOAT_EQ(rgb.x, 0.0f);
EXPECT_FLOAT_EQ(rgb.y, 0.0f);
EXPECT_FLOAT_EQ(rgb.z, 0.0f);
EXPECT_FLOAT_EQ(rgb.w, 255.0f);
}
TEST_F(SnesColorTest, SetRgbWhiteColor) {
SnesColor color;
ImVec4 white(1.0f, 1.0f, 1.0f, 1.0f);
color.set_rgb(white);
auto rgb = color.rgb();
EXPECT_FLOAT_EQ(rgb.x, 255.0f);
EXPECT_FLOAT_EQ(rgb.y, 255.0f);
EXPECT_FLOAT_EQ(rgb.z, 255.0f);
EXPECT_FLOAT_EQ(rgb.w, 255.0f);
}
TEST_F(SnesColorTest, SetRgbMidRangeColor) {
SnesColor color;
// Test a mid-range color (medium gray)
ImVec4 gray(0.5f, 0.5f, 0.5f, 1.0f);
color.set_rgb(gray);
auto rgb = color.rgb();
EXPECT_NEAR(rgb.x, 127.5f, 0.01f);
EXPECT_NEAR(rgb.y, 127.5f, 0.01f);
EXPECT_NEAR(rgb.z, 127.5f, 0.01f);
}
// ============================================================================
// Constructor Tests
// ============================================================================
TEST_F(SnesColorTest, ConstructFromImVec4) {
// ImGui color in 0-1 range
ImVec4 imgui_color(0.25f, 0.5f, 0.75f, 1.0f);
SnesColor color(imgui_color);
// Should be converted to 0-255 range
auto rgb = color.rgb();
EXPECT_NEAR(rgb.x, 63.75f, 0.01f); // 0.25 * 255
EXPECT_NEAR(rgb.y, 127.5f, 0.01f); // 0.5 * 255
EXPECT_NEAR(rgb.z, 191.25f, 0.01f); // 0.75 * 255
EXPECT_FLOAT_EQ(rgb.w, 255.0f);
}
TEST_F(SnesColorTest, ConstructFromSnesValue) {
// SNES BGR555 format: 0x7FFF = white (all bits set in 15-bit color)
SnesColor white(0x7FFF);
auto rgb = white.rgb();
// All channels should be max (after BGR555 conversion)
EXPECT_GT(rgb.x, 240.0f); // Close to 255
EXPECT_GT(rgb.y, 240.0f);
EXPECT_GT(rgb.z, 240.0f);
}
TEST_F(SnesColorTest, ConstructFromSnesBlack) {
// SNES BGR555 format: 0x0000 = black
SnesColor black(0x0000);
auto rgb = black.rgb();
EXPECT_FLOAT_EQ(rgb.x, 0.0f);
EXPECT_FLOAT_EQ(rgb.y, 0.0f);
EXPECT_FLOAT_EQ(rgb.z, 0.0f);
}
// ============================================================================
// SNES Format Conversion Tests
// ============================================================================
TEST_F(SnesColorTest, SetSnesUpdatesRgb) {
SnesColor color;
// Set a SNES color value
color.set_snes(0x7FFF); // White in BGR555
// RGB should be updated
auto rgb = color.rgb();
EXPECT_GT(rgb.x, 240.0f);
EXPECT_GT(rgb.y, 240.0f);
EXPECT_GT(rgb.z, 240.0f);
}
TEST_F(SnesColorTest, RgbToSnesConversion) {
SnesColor color;
// Set pure red in RGB (0-1 range for ImGui)
ImVec4 red(1.0f, 0.0f, 0.0f, 1.0f);
color.set_rgb(red);
// SNES value should be set (BGR555 format)
uint16_t snes = color.snes();
EXPECT_NE(snes, 0x0000); // Should not be black
// Extract red component from BGR555 (bits 0-4)
uint16_t snes_red = snes & 0x1F;
EXPECT_EQ(snes_red, 0x1F); // Max red in 5-bit
}
// ============================================================================
// Round-Trip Conversion Tests
// ============================================================================
TEST_F(SnesColorTest, RoundTripImGuiToSnesColor) {
// Start with ImGui color
ImVec4 original(0.6f, 0.4f, 0.8f, 1.0f);
// Convert to SnesColor
SnesColor color(original);
// Convert back to ImVec4 (normalized)
auto rgb = color.rgb();
ImVec4 converted(rgb.x / 255.0f, rgb.y / 255.0f, rgb.z / 255.0f, 1.0f);
// Should be approximately equal (within floating point precision)
EXPECT_NEAR(converted.x, original.x, 0.01f);
EXPECT_NEAR(converted.y, original.y, 0.01f);
EXPECT_NEAR(converted.z, original.z, 0.01f);
}
TEST_F(SnesColorTest, MultipleSetRgbCalls) {
SnesColor color;
// First color
ImVec4 color1(0.2f, 0.4f, 0.6f, 1.0f);
color.set_rgb(color1);
auto rgb1 = color.rgb();
EXPECT_NEAR(rgb1.x, 51.0f, 1.0f);
EXPECT_NEAR(rgb1.y, 102.0f, 1.0f);
EXPECT_NEAR(rgb1.z, 153.0f, 1.0f);
// Second color (should completely replace)
ImVec4 color2(0.8f, 0.6f, 0.4f, 1.0f);
color.set_rgb(color2);
auto rgb2 = color.rgb();
EXPECT_NEAR(rgb2.x, 204.0f, 1.0f);
EXPECT_NEAR(rgb2.y, 153.0f, 1.0f);
EXPECT_NEAR(rgb2.z, 102.0f, 1.0f);
}
// ============================================================================
// Edge Case Tests
// ============================================================================
TEST_F(SnesColorTest, HandlesMaxValues) {
SnesColor color;
ImVec4 max(1.0f, 1.0f, 1.0f, 1.0f);
color.set_rgb(max);
auto rgb = color.rgb();
EXPECT_FLOAT_EQ(rgb.x, 255.0f);
EXPECT_FLOAT_EQ(rgb.y, 255.0f);
EXPECT_FLOAT_EQ(rgb.z, 255.0f);
}
TEST_F(SnesColorTest, HandlesMinValues) {
SnesColor color;
ImVec4 min(0.0f, 0.0f, 0.0f, 1.0f);
color.set_rgb(min);
auto rgb = color.rgb();
EXPECT_FLOAT_EQ(rgb.x, 0.0f);
EXPECT_FLOAT_EQ(rgb.y, 0.0f);
EXPECT_FLOAT_EQ(rgb.z, 0.0f);
}
TEST_F(SnesColorTest, AlphaAlwaysMaximum) {
SnesColor color;
// Try setting alpha to different values (should always be ignored)
ImVec4 color_with_alpha(0.5f, 0.5f, 0.5f, 0.5f);
color.set_rgb(color_with_alpha);
auto rgb = color.rgb();
EXPECT_FLOAT_EQ(rgb.w, 255.0f); // Alpha should always be 255
}
// ============================================================================
// Modified Flag Tests
// ============================================================================
TEST_F(SnesColorTest, ModifiedFlagSetOnRgbChange) {
SnesColor color;
EXPECT_FALSE(color.is_modified());
ImVec4 new_color(0.5f, 0.5f, 0.5f, 1.0f);
color.set_rgb(new_color);
EXPECT_TRUE(color.is_modified());
}
TEST_F(SnesColorTest, ModifiedFlagSetOnSnesChange) {
SnesColor color;
EXPECT_FALSE(color.is_modified());
color.set_snes(0x7FFF);
EXPECT_TRUE(color.is_modified());
}
} // namespace
} // namespace gfx
} // namespace yaze

View File

@@ -0,0 +1,324 @@
#include "gtest/gtest.h"
#include "absl/status/status.h"
#include "app/gfx/background_buffer.h"
#include "app/gfx/snes_palette.h"
#include "app/rom.h"
#include "zelda3/dungeon/object_drawer.h"
#include "zelda3/dungeon/object_parser.h"
#include "zelda3/dungeon/room_object.h"
namespace yaze {
namespace zelda3 {
class ObjectRenderingTest : public ::testing::Test {
protected:
void SetUp() override {
// Create a mock ROM for testing
rom_ = std::make_unique<Rom>();
// Initialize with minimal ROM data for testing
std::vector<uint8_t> mock_rom_data(1024 * 1024, 0); // 1MB mock ROM
rom_->LoadFromData(mock_rom_data);
}
void TearDown() override {
rom_.reset();
}
std::unique_ptr<Rom> rom_;
gfx::BackgroundBuffer bg1_;
gfx::BackgroundBuffer bg2_;
// Create a test palette
gfx::SnesPalette CreateTestPalette() {
gfx::SnesPalette palette;
// Add some test colors
palette.AddColor(gfx::SnesColor(0, 0, 0)); // Transparent
palette.AddColor(gfx::SnesColor(255, 0, 0)); // Red
palette.AddColor(gfx::SnesColor(0, 255, 0)); // Green
palette.AddColor(gfx::SnesColor(0, 0, 255)); // Blue
palette.AddColor(gfx::SnesColor(255, 255, 0)); // Yellow
palette.AddColor(gfx::SnesColor(255, 0, 255)); // Magenta
palette.AddColor(gfx::SnesColor(0, 255, 255)); // Cyan
palette.AddColor(gfx::SnesColor(255, 255, 255)); // White
return palette;
}
gfx::PaletteGroup CreateTestPaletteGroup() {
gfx::PaletteGroup group;
group.AddPalette(CreateTestPalette());
return group;
}
};
// Test object drawer initialization
TEST_F(ObjectRenderingTest, ObjectDrawerInitializesCorrectly) {
ObjectDrawer drawer(rom_.get());
// Test that drawer can be created without errors
EXPECT_NE(rom_.get(), nullptr);
}
// Test object parser draw routine detection
TEST_F(ObjectRenderingTest, ObjectParserDetectsDrawRoutines) {
ObjectParser parser(rom_.get());
// Test common object IDs and their expected draw routines
auto info_00 = parser.GetObjectDrawInfo(0x00);
EXPECT_EQ(info_00.draw_routine_id, 0);
EXPECT_EQ(info_00.routine_name, "Rightwards2x2_1to15or32");
EXPECT_TRUE(info_00.is_horizontal);
auto info_01 = parser.GetObjectDrawInfo(0x01);
EXPECT_EQ(info_01.draw_routine_id, 1);
EXPECT_EQ(info_01.routine_name, "Rightwards2x4_1to15or26");
EXPECT_TRUE(info_01.is_horizontal);
auto info_09 = parser.GetObjectDrawInfo(0x09);
EXPECT_EQ(info_09.draw_routine_id, 5);
EXPECT_EQ(info_09.routine_name, "DiagonalAcute_1to16");
EXPECT_FALSE(info_09.is_horizontal);
auto info_34 = parser.GetObjectDrawInfo(0x34);
EXPECT_EQ(info_34.draw_routine_id, 16);
EXPECT_EQ(info_34.routine_name, "Rightwards1x1Solid_1to16_plus3");
EXPECT_TRUE(info_34.is_horizontal);
// Test unmapped object defaults to solid block routine
auto info_unknown = parser.GetObjectDrawInfo(0x999);
EXPECT_EQ(info_unknown.draw_routine_id, 16); // Default solid routine
EXPECT_EQ(info_unknown.routine_name, "DefaultSolid");
}
// Test object drawer with various object types
TEST_F(ObjectRenderingTest, ObjectDrawerHandlesVariousObjectTypes) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Test object 0x00 (horizontal floor tile)
RoomObject floor_object(0x00, 10, 10, 3, 0); // ID, X, Y, size, layer
auto status = drawer.DrawObject(floor_object, bg1_, bg2_, palette_group);
// Should succeed even if tiles aren't loaded (graceful handling)
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test object 0x09 (diagonal stairs)
RoomObject stair_object(0x09, 15, 15, 5, 0);
stair_object.set_rom(rom_.get());
status = drawer.DrawObject(stair_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test object 0x34 (solid block)
RoomObject block_object(0x34, 20, 20, 1, 0);
block_object.set_rom(rom_.get());
status = drawer.DrawObject(block_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test object drawer with different layers
TEST_F(ObjectRenderingTest, ObjectDrawerHandlesDifferentLayers) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Test BG1 layer object
RoomObject bg1_object(0x00, 5, 5, 2, 0); // Layer 0 = BG1
bg1_object.set_rom(rom_.get());
auto status = drawer.DrawObject(bg1_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test BG2 layer object
RoomObject bg2_object(0x01, 10, 10, 2, 1); // Layer 1 = BG2
bg2_object.set_rom(rom_.get());
status = drawer.DrawObject(bg2_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test object drawer with size variations
TEST_F(ObjectRenderingTest, ObjectDrawerHandlesSizeVariations) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Test small object
RoomObject small_object(0x00, 5, 5, 1, 0); // Size = 1
small_object.set_rom(rom_.get());
auto status = drawer.DrawObject(small_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test large object
RoomObject large_object(0x00, 10, 10, 15, 0); // Size = 15
large_object.set_rom(rom_.get());
status = drawer.DrawObject(large_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test maximum size object
RoomObject max_object(0x00, 15, 15, 31, 0); // Size = 31 (0x1F)
max_object.set_rom(rom_.get());
status = drawer.DrawObject(max_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test object drawer with edge cases
TEST_F(ObjectRenderingTest, ObjectDrawerHandlesEdgeCases) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Test object at origin
RoomObject origin_object(0x34, 0, 0, 1, 0);
origin_object.set_rom(rom_.get());
auto status = drawer.DrawObject(origin_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test object with zero size
RoomObject zero_size_object(0x34, 10, 10, 0, 0);
zero_size_object.set_rom(rom_.get());
status = drawer.DrawObject(zero_size_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test object with maximum coordinates
RoomObject max_coord_object(0x34, 63, 63, 1, 0); // Near buffer edge
max_coord_object.set_rom(rom_.get());
status = drawer.DrawObject(max_coord_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test object drawer with multiple objects
TEST_F(ObjectRenderingTest, ObjectDrawerHandlesMultipleObjects) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
std::vector<RoomObject> objects;
// Create various test objects
objects.emplace_back(0x00, 5, 5, 3, 0); // Horizontal floor
objects.emplace_back(0x01, 10, 10, 2, 0); // Vertical floor
objects.emplace_back(0x09, 15, 15, 4, 0); // Diagonal stairs
objects.emplace_back(0x34, 20, 20, 1, 1); // Solid block on BG2
// Set ROM for all objects
for (auto& obj : objects) {
obj.set_rom(rom_.get());
}
auto status = drawer.DrawObjectList(objects, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test specific draw routines
TEST_F(ObjectRenderingTest, DrawRoutinesWorkCorrectly) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Test rightward patterns
RoomObject rightward_obj(0x00, 5, 5, 5, 0);
rightward_obj.set_rom(rom_.get());
auto status = drawer.DrawObject(rightward_obj, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test diagonal patterns
RoomObject diagonal_obj(0x09, 10, 10, 6, 0);
diagonal_obj.set_rom(rom_.get());
status = drawer.DrawObject(diagonal_obj, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test solid block patterns
RoomObject solid_obj(0x34, 15, 15, 8, 0);
solid_obj.set_rom(rom_.get());
status = drawer.DrawObject(solid_obj, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test object drawer error handling
TEST_F(ObjectRenderingTest, ObjectDrawerHandlesErrorsGracefully) {
ObjectDrawer drawer(nullptr); // No ROM
auto palette_group = CreateTestPaletteGroup();
RoomObject test_object(0x00, 5, 5, 1, 0);
auto status = drawer.DrawObject(test_object, bg1_, bg2_, palette_group);
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kFailedPrecondition);
}
// Test object parser with various object IDs
TEST_F(ObjectRenderingTest, ObjectParserHandlesVariousObjectIDs) {
ObjectParser parser(rom_.get());
// Test subtype 1 objects (0x00-0xFF)
for (int id = 0; id <= 0x40; id += 4) { // Test every 4th object
auto info = parser.GetObjectDrawInfo(id);
EXPECT_GE(info.draw_routine_id, 0);
EXPECT_LT(info.draw_routine_id, 25); // Should be within valid range
EXPECT_FALSE(info.routine_name.empty());
}
// Test some specific important objects
std::vector<int16_t> important_objects = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x15, 0x16, 0x21, 0x22, 0x2F, 0x30, 0x31, 0x32,
0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C,
0x3D, 0x3E, 0x3F, 0x40
};
for (int16_t obj_id : important_objects) {
auto info = parser.GetObjectDrawInfo(obj_id);
EXPECT_GE(info.draw_routine_id, 0);
EXPECT_LT(info.draw_routine_id, 25);
EXPECT_FALSE(info.routine_name.empty());
// Verify tile count is reasonable
EXPECT_GT(info.tile_count, 0);
EXPECT_LE(info.tile_count, 64); // Reasonable upper bound
}
}
// Test object drawer performance with many objects
TEST_F(ObjectRenderingTest, ObjectDrawerPerformanceTest) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
std::vector<RoomObject> objects;
// Create 100 test objects
for (int i = 0; i < 100; ++i) {
int id = i % 65; // Cycle through object IDs 0-64
int x = (i * 2) % 60; // Spread across buffer
int y = (i * 3) % 60;
int size = (i % 8) + 1; // Size 1-8
int layer = i % 2; // Alternate layers
objects.emplace_back(id, x, y, size, layer);
objects.back().set_rom(rom_.get());
}
// Time the drawing operation
auto start_time = std::chrono::high_resolution_clock::now();
auto status = drawer.DrawObjectList(objects, bg1_, bg2_, palette_group);
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
end_time - start_time);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Should complete in reasonable time (less than 1 second for 100 objects)
EXPECT_LT(duration.count(), 1000);
std::cout << "Drew 100 objects in " << duration.count() << "ms" << std::endl;
}
} // namespace zelda3
} // namespace yaze

View File

@@ -0,0 +1,169 @@
// Tests for Room object manipulation methods (Phase 3)
#include <gtest/gtest.h>
#include "app/rom.h"
#include "zelda3/dungeon/room.h"
#include "zelda3/dungeon/room_object.h"
namespace yaze {
namespace zelda3 {
namespace test {
class RoomManipulationTest : public ::testing::Test {
protected:
void SetUp() override {
rom_ = std::make_unique<Rom>();
// Create a minimal ROM for testing
std::vector<uint8_t> dummy_data(0x200000, 0);
rom_->LoadFromData(dummy_data, false);
room_ = std::make_unique<Room>(0, rom_.get());
}
std::unique_ptr<Rom> rom_;
std::unique_ptr<Room> room_;
};
TEST_F(RoomManipulationTest, AddObject) {
RoomObject obj(0x10, 10, 20, 3, 0);
auto status = room_->AddObject(obj);
ASSERT_TRUE(status.ok());
auto objects = room_->GetTileObjects();
EXPECT_EQ(objects.size(), 1);
EXPECT_EQ(objects[0].id_, 0x10);
EXPECT_EQ(objects[0].x(), 10);
EXPECT_EQ(objects[0].y(), 20);
}
TEST_F(RoomManipulationTest, AddInvalidObject) {
// Invalid X position (> 63)
RoomObject obj(0x10, 100, 20, 3, 0);
auto status = room_->AddObject(obj);
EXPECT_FALSE(status.ok());
EXPECT_EQ(room_->GetTileObjects().size(), 0);
}
TEST_F(RoomManipulationTest, RemoveObject) {
RoomObject obj1(0x10, 10, 20, 3, 0);
RoomObject obj2(0x20, 15, 25, 2, 1);
room_->AddObject(obj1);
room_->AddObject(obj2);
EXPECT_EQ(room_->GetTileObjects().size(), 2);
auto status = room_->RemoveObject(0);
ASSERT_TRUE(status.ok());
auto objects = room_->GetTileObjects();
EXPECT_EQ(objects.size(), 1);
EXPECT_EQ(objects[0].id_, 0x20);
}
TEST_F(RoomManipulationTest, RemoveInvalidIndex) {
auto status = room_->RemoveObject(0);
EXPECT_FALSE(status.ok());
}
TEST_F(RoomManipulationTest, UpdateObject) {
RoomObject obj(0x10, 10, 20, 3, 0);
room_->AddObject(obj);
RoomObject updated(0x20, 15, 25, 5, 1);
auto status = room_->UpdateObject(0, updated);
ASSERT_TRUE(status.ok());
auto objects = room_->GetTileObjects();
EXPECT_EQ(objects[0].id_, 0x20);
EXPECT_EQ(objects[0].x(), 15);
EXPECT_EQ(objects[0].y(), 25);
}
TEST_F(RoomManipulationTest, FindObjectAt) {
RoomObject obj1(0x10, 10, 20, 3, 0);
RoomObject obj2(0x20, 15, 25, 2, 1);
room_->AddObject(obj1);
room_->AddObject(obj2);
auto result = room_->FindObjectAt(15, 25, 1);
ASSERT_TRUE(result.ok());
EXPECT_EQ(result.value(), 1);
auto not_found = room_->FindObjectAt(99, 99, 0);
EXPECT_FALSE(not_found.ok());
}
TEST_F(RoomManipulationTest, ValidateObject) {
// Valid Type 1 object
RoomObject valid1(0x10, 10, 20, 3, 0);
EXPECT_TRUE(room_->ValidateObject(valid1));
// Valid Type 2 object
RoomObject valid2(0x110, 30, 40, 0, 1);
EXPECT_TRUE(room_->ValidateObject(valid2));
// Invalid X (> 63)
RoomObject invalid_x(0x10, 100, 20, 3, 0);
EXPECT_FALSE(room_->ValidateObject(invalid_x));
// Invalid layer (> 2)
RoomObject invalid_layer(0x10, 10, 20, 3, 5);
EXPECT_FALSE(room_->ValidateObject(invalid_layer));
// Invalid size for Type 1 (> 15)
RoomObject invalid_size(0x10, 10, 20, 20, 0);
EXPECT_FALSE(room_->ValidateObject(invalid_size));
}
TEST_F(RoomManipulationTest, MultipleOperations) {
// Add several objects
for (int i = 0; i < 5; i++) {
RoomObject obj(0x10 + i, i * 5, i * 6, i, 0);
ASSERT_TRUE(room_->AddObject(obj).ok());
}
EXPECT_EQ(room_->GetTileObjects().size(), 5);
// Update middle object
RoomObject updated(0x99, 30, 35, 7, 1);
ASSERT_TRUE(room_->UpdateObject(2, updated).ok());
// Verify update
auto objects = room_->GetTileObjects();
EXPECT_EQ(objects[2].id_, 0x99);
// Remove first object
ASSERT_TRUE(room_->RemoveObject(0).ok());
EXPECT_EQ(room_->GetTileObjects().size(), 4);
// Verify first object is now what was second
EXPECT_EQ(room_->GetTileObjects()[0].id_, 0x11);
}
TEST_F(RoomManipulationTest, LayerOrganization) {
// Add objects to different layers
RoomObject layer0_obj(0x10, 10, 10, 2, 0);
RoomObject layer1_obj(0x20, 20, 20, 3, 1);
RoomObject layer2_obj(0x30, 30, 30, 4, 2);
room_->AddObject(layer0_obj);
room_->AddObject(layer1_obj);
room_->AddObject(layer2_obj);
// Verify can find by layer
EXPECT_TRUE(room_->FindObjectAt(10, 10, 0).ok());
EXPECT_TRUE(room_->FindObjectAt(20, 20, 1).ok());
EXPECT_TRUE(room_->FindObjectAt(30, 30, 2).ok());
// Wrong layer should not find
EXPECT_FALSE(room_->FindObjectAt(10, 10, 1).ok());
}
} // namespace test
} // namespace zelda3
} // namespace yaze

View File

@@ -0,0 +1,330 @@
// test/zelda3/dungeon/room_object_encoding_test.cc
// Unit tests for Phase 1, Task 1.1: Object Encoding/Decoding
//
// These tests verify that the object encoding and decoding functions work
// correctly for all three object types (Type1, Type2, Type3) based on
// ZScream's proven implementation.
#include "zelda3/dungeon/room_object.h"
#include <gtest/gtest.h>
namespace yaze {
namespace zelda3 {
namespace {
// ============================================================================
// Object Type Detection Tests
// ============================================================================
TEST(RoomObjectEncodingTest, DetermineObjectTypeType1) {
// Type1: b1 < 0xFC, b3 < 0xF8
EXPECT_EQ(RoomObject::DetermineObjectType(0x28, 0x10), 1);
EXPECT_EQ(RoomObject::DetermineObjectType(0x50, 0x42), 1);
EXPECT_EQ(RoomObject::DetermineObjectType(0xFB, 0xF7), 1);
}
TEST(RoomObjectEncodingTest, DetermineObjectTypeType2) {
// Type2: b1 >= 0xFC, b3 < 0xF8
EXPECT_EQ(RoomObject::DetermineObjectType(0xFC, 0x42), 2);
EXPECT_EQ(RoomObject::DetermineObjectType(0xFD, 0x25), 2);
EXPECT_EQ(RoomObject::DetermineObjectType(0xFF, 0x00), 2);
}
TEST(RoomObjectEncodingTest, DetermineObjectTypeType3) {
// Type3: b3 >= 0xF8
EXPECT_EQ(RoomObject::DetermineObjectType(0x28, 0xF8), 3);
EXPECT_EQ(RoomObject::DetermineObjectType(0x50, 0xF9), 3);
EXPECT_EQ(RoomObject::DetermineObjectType(0xFC, 0xFF), 3);
}
// ============================================================================
// Type 1 Object Encoding/Decoding Tests
// ============================================================================
TEST(RoomObjectEncodingTest, Type1EncodeDecodeBasic) {
// Type1: xxxxxxss yyyyyyss iiiiiiii
// Example: Object ID 0x42, position (10, 20), size 3, layer 0
RoomObject obj(0x42, 10, 20, 3, 0);
// Encode
auto bytes = obj.EncodeObjectToBytes();
// Decode
auto decoded = RoomObject::DecodeObjectFromBytes(bytes.b1, bytes.b2, bytes.b3, 0);
// Verify
EXPECT_EQ(decoded.id_, obj.id_);
EXPECT_EQ(decoded.x(), obj.x());
EXPECT_EQ(decoded.y(), obj.y());
EXPECT_EQ(decoded.size(), obj.size());
EXPECT_EQ(decoded.GetLayerValue(), obj.GetLayerValue());
}
TEST(RoomObjectEncodingTest, Type1MaxValues) {
// Test maximum valid values for Type1
// Constraints:
// - ID < 0xF8 (b3 >= 0xF8 triggers Type3 detection)
// - X < 63 OR Size < 12 (b1 >= 0xFC triggers Type2 detection)
// Safe max values: ID=0xF7, X=62, Y=63, Size=15
RoomObject obj(0xF7, 62, 63, 15, 2);
auto bytes = obj.EncodeObjectToBytes();
auto decoded = RoomObject::DecodeObjectFromBytes(bytes.b1, bytes.b2, bytes.b3, 2);
EXPECT_EQ(decoded.id_, obj.id_);
EXPECT_EQ(decoded.x(), obj.x());
EXPECT_EQ(decoded.y(), obj.y());
EXPECT_EQ(decoded.size(), obj.size());
}
TEST(RoomObjectEncodingTest, Type1MinValues) {
// Test minimum values for Type1
RoomObject obj(0x00, 0, 0, 0, 0);
auto bytes = obj.EncodeObjectToBytes();
auto decoded = RoomObject::DecodeObjectFromBytes(bytes.b1, bytes.b2, bytes.b3, 0);
EXPECT_EQ(decoded.id_, obj.id_);
EXPECT_EQ(decoded.x(), obj.x());
EXPECT_EQ(decoded.y(), obj.y());
EXPECT_EQ(decoded.size(), obj.size());
}
TEST(RoomObjectEncodingTest, Type1DifferentSizes) {
// Test all valid size values (0-15)
for (int size = 0; size <= 15; size++) {
RoomObject obj(0x30, 15, 20, size, 1);
auto bytes = obj.EncodeObjectToBytes();
auto decoded = RoomObject::DecodeObjectFromBytes(bytes.b1, bytes.b2, bytes.b3, 1);
EXPECT_EQ(decoded.size(), size) << "Failed for size " << size;
}
}
TEST(RoomObjectEncodingTest, Type1RealWorldExample1) {
// Example from actual ROM: Wall object
// Bytes: 0x28 0x50 0x10
// Expected: X=10, Y=20, Size=0, ID=0x10
auto decoded = RoomObject::DecodeObjectFromBytes(0x28, 0x50, 0x10, 0);
EXPECT_EQ(decoded.x(), 10);
EXPECT_EQ(decoded.y(), 20);
EXPECT_EQ(decoded.size(), 0);
EXPECT_EQ(decoded.id_, 0x10);
}
TEST(RoomObjectEncodingTest, Type1RealWorldExample2) {
// Example: Ceiling object with size
// Correct bytes for X=10, Y=20, Size=3, ID=0x00: 0x28 0x53 0x00
auto decoded = RoomObject::DecodeObjectFromBytes(0x28, 0x53, 0x00, 0);
EXPECT_EQ(decoded.x(), 10);
EXPECT_EQ(decoded.y(), 20);
EXPECT_EQ(decoded.size(), 3);
EXPECT_EQ(decoded.id_, 0x00);
}
// ============================================================================
// Type 2 Object Encoding/Decoding Tests
// ============================================================================
TEST(RoomObjectEncodingTest, Type2EncodeDecodeBasic) {
// Type2: 111111xx xxxxyyyy yyiiiiii
// Example: Object ID 0x125, position (15, 30), size ignored, layer 1
RoomObject obj(0x125, 15, 30, 0, 1);
// Encode
auto bytes = obj.EncodeObjectToBytes();
// Verify b1 starts with 0xFC
EXPECT_GE(bytes.b1, 0xFC);
// Decode
auto decoded = RoomObject::DecodeObjectFromBytes(bytes.b1, bytes.b2, bytes.b3, 1);
// Verify
EXPECT_EQ(decoded.id_, obj.id_);
EXPECT_EQ(decoded.x(), obj.x());
EXPECT_EQ(decoded.y(), obj.y());
EXPECT_EQ(decoded.GetLayerValue(), obj.GetLayerValue());
}
TEST(RoomObjectEncodingTest, Type2MaxValues) {
// Type2 allows larger position range, but has constraints:
// When Y=63 and ID=0x13F, b3 becomes 0xFF >= 0xF8, triggering Type3 detection
// Safe max: X=63, Y=59, ID=0x13F (b3 = ((59&0x03)<<6)|(0x3F) = 0xFF still!)
// Even safer: X=63, Y=63, ID=0x11F (b3 = (0xC0|0x1F) = 0xDF < 0xF8)
RoomObject obj(0x11F, 63, 63, 0, 2);
auto bytes = obj.EncodeObjectToBytes();
auto decoded = RoomObject::DecodeObjectFromBytes(bytes.b1, bytes.b2, bytes.b3, 2);
EXPECT_EQ(decoded.id_, obj.id_);
EXPECT_EQ(decoded.x(), obj.x());
EXPECT_EQ(decoded.y(), obj.y());
}
TEST(RoomObjectEncodingTest, Type2RealWorldExample) {
// Example: Large brazier (object 0x11C)
// Position (8, 12)
RoomObject obj(0x11C, 8, 12, 0, 0);
auto bytes = obj.EncodeObjectToBytes();
auto decoded = RoomObject::DecodeObjectFromBytes(bytes.b1, bytes.b2, bytes.b3, 0);
EXPECT_EQ(decoded.id_, 0x11C);
EXPECT_EQ(decoded.x(), 8);
EXPECT_EQ(decoded.y(), 12);
}
// ============================================================================
// Type 3 Object Encoding/Decoding Tests
// ============================================================================
TEST(RoomObjectEncodingTest, Type3EncodeDecodeChest) {
// Type3: xxxxxxii yyyyyyii 11111iii
// Example: Small chest (0xF99), position (5, 10)
RoomObject obj(0xF99, 5, 10, 0, 0);
// Encode
auto bytes = obj.EncodeObjectToBytes();
// Verify b3 >= 0xF8
EXPECT_GE(bytes.b3, 0xF8);
// Decode
auto decoded = RoomObject::DecodeObjectFromBytes(bytes.b1, bytes.b2, bytes.b3, 0);
// Verify
EXPECT_EQ(decoded.id_, obj.id_);
EXPECT_EQ(decoded.x(), obj.x());
EXPECT_EQ(decoded.y(), obj.y());
}
TEST(RoomObjectEncodingTest, Type3EncodeDcodeBigChest) {
// Example: Big chest (0xFB1), position (15, 20)
RoomObject obj(0xFB1, 15, 20, 0, 1);
auto bytes = obj.EncodeObjectToBytes();
auto decoded = RoomObject::DecodeObjectFromBytes(bytes.b1, bytes.b2, bytes.b3, 1);
EXPECT_EQ(decoded.id_, 0xFB1);
EXPECT_EQ(decoded.x(), 15);
EXPECT_EQ(decoded.y(), 20);
}
TEST(RoomObjectEncodingTest, Type3RealWorldExample) {
// Example from ROM: Chest at position (10, 15)
// Correct bytes for ID 0xF99: 0x29 0x3E 0xF9
auto decoded = RoomObject::DecodeObjectFromBytes(0x29, 0x3E, 0xF9, 0);
// Expected: X=10, Y=15, ID=0xF99 (small chest)
EXPECT_EQ(decoded.x(), 10);
EXPECT_EQ(decoded.y(), 15);
EXPECT_EQ(decoded.id_, 0xF99);
}
// ============================================================================
// Edge Cases and Special Values
// ============================================================================
TEST(RoomObjectEncodingTest, LayerPreservation) {
// Test that layer information is preserved through encode/decode
for (uint8_t layer = 0; layer <= 2; layer++) {
RoomObject obj(0x42, 10, 20, 3, layer);
auto bytes = obj.EncodeObjectToBytes();
auto decoded = RoomObject::DecodeObjectFromBytes(bytes.b1, bytes.b2, bytes.b3, layer);
EXPECT_EQ(decoded.GetLayerValue(), layer) << "Failed for layer " << (int)layer;
}
}
TEST(RoomObjectEncodingTest, BoundaryBetweenTypes) {
// Test boundary values between object types
// NOTE: Type1 can only go up to ID 0xF7 (b3 >= 0xF8 triggers Type3)
// Last safe Type1 object
RoomObject type1(0xF7, 10, 20, 3, 0);
auto bytes1 = type1.EncodeObjectToBytes();
auto decoded1 = RoomObject::DecodeObjectFromBytes(bytes1.b1, bytes1.b2, bytes1.b3, 0);
EXPECT_EQ(decoded1.id_, 0xF7);
// First Type2 object
RoomObject type2(0x100, 10, 20, 0, 0);
auto bytes2 = type2.EncodeObjectToBytes();
auto decoded2 = RoomObject::DecodeObjectFromBytes(bytes2.b1, bytes2.b2, bytes2.b3, 0);
EXPECT_EQ(decoded2.id_, 0x100);
// Last Type2 object
RoomObject type2_last(0x13F, 10, 20, 0, 0);
auto bytes2_last = type2_last.EncodeObjectToBytes();
auto decoded2_last = RoomObject::DecodeObjectFromBytes(bytes2_last.b1, bytes2_last.b2, bytes2_last.b3, 0);
EXPECT_EQ(decoded2_last.id_, 0x13F);
// Type3 objects (start at 0xF80)
RoomObject type3(0xF99, 10, 20, 0, 0);
auto bytes3 = type3.EncodeObjectToBytes();
auto decoded3 = RoomObject::DecodeObjectFromBytes(bytes3.b1, bytes3.b2, bytes3.b3, 0);
EXPECT_EQ(decoded3.id_, 0xF99);
}
TEST(RoomObjectEncodingTest, ZeroPosition) {
// Test objects at position (0, 0)
RoomObject type1(0x10, 0, 0, 0, 0);
auto bytes1 = type1.EncodeObjectToBytes();
auto decoded1 = RoomObject::DecodeObjectFromBytes(bytes1.b1, bytes1.b2, bytes1.b3, 0);
EXPECT_EQ(decoded1.x(), 0);
EXPECT_EQ(decoded1.y(), 0);
RoomObject type2(0x110, 0, 0, 0, 0);
auto bytes2 = type2.EncodeObjectToBytes();
auto decoded2 = RoomObject::DecodeObjectFromBytes(bytes2.b1, bytes2.b2, bytes2.b3, 0);
EXPECT_EQ(decoded2.x(), 0);
EXPECT_EQ(decoded2.y(), 0);
}
// ============================================================================
// Batch Tests with Multiple Objects
// ============================================================================
TEST(RoomObjectEncodingTest, MultipleObjectsRoundTrip) {
// Test encoding/decoding a batch of different objects
std::vector<RoomObject> objects;
// Add various objects
objects.emplace_back(0x10, 5, 10, 2, 0); // Type1
objects.emplace_back(0x42, 15, 20, 5, 1); // Type1
objects.emplace_back(0x110, 8, 12, 0, 0); // Type2
objects.emplace_back(0x125, 25, 30, 0, 1); // Type2
objects.emplace_back(0xF99, 10, 15, 0, 0); // Type3 (chest)
objects.emplace_back(0xFB1, 20, 25, 0, 2); // Type3 (big chest)
for (size_t i = 0; i < objects.size(); i++) {
auto& obj = objects[i];
auto bytes = obj.EncodeObjectToBytes();
auto decoded = RoomObject::DecodeObjectFromBytes(
bytes.b1, bytes.b2, bytes.b3, obj.GetLayerValue());
EXPECT_EQ(decoded.id_, obj.id_) << "Failed at index " << i;
EXPECT_EQ(decoded.x(), obj.x()) << "Failed at index " << i;
EXPECT_EQ(decoded.y(), obj.y()) << "Failed at index " << i;
if (obj.id_ < 0x100) { // Type1 objects have size
EXPECT_EQ(decoded.size(), obj.size()) << "Failed at index " << i;
}
}
}
} // namespace
} // namespace zelda3
} // namespace yaze

View File

@@ -1,4 +1,4 @@
#include "app/zelda3/dungeon/object_parser.h"
#include "zelda3/dungeon/object_parser.h"
#include "gtest/gtest.h"

View File

@@ -1,4 +1,4 @@
#include "app/zelda3/dungeon/object_parser.h"
#include "zelda3/dungeon/object_parser.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -69,10 +69,7 @@ TEST_F(ObjectParserTest, ParseSubtype1Object) {
// Verify tile data was parsed correctly
for (const auto& tile : tiles) {
EXPECT_NE(tile.tile0_.id_, 0);
EXPECT_NE(tile.tile1_.id_, 0);
EXPECT_NE(tile.tile2_.id_, 0);
EXPECT_NE(tile.tile3_.id_, 0);
EXPECT_NE(tile.id_, 0);
}
}

View File

@@ -2,8 +2,8 @@
#include <memory>
#include "app/rom.h"
#include "app/zelda3/overworld/overworld.h"
#include "app/zelda3/overworld/overworld_map.h"
#include "zelda3/overworld/overworld.h"
#include "zelda3/overworld/overworld_map.h"
namespace yaze {
namespace zelda3 {

View File

@@ -1,4 +1,4 @@
#include "app/zelda3/sprite/sprite_builder.h"
#include "zelda3/sprite/sprite_builder.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>

View File

@@ -1,23 +1,249 @@
#define SDL_MAIN_HANDLED
// Must define before any ImGui includes
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
#include <gtest/gtest.h>
#include <SDL.h>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include "absl/debugging/failure_signal_handler.h"
#include "absl/debugging/symbolize.h"
#include "test_editor.h"
#include "imgui/imgui.h"
#include "imgui/backends/imgui_impl_sdl2.h"
#include "imgui/backends/imgui_impl_sdlrenderer2.h"
#include "imgui_test_engine/imgui_te_context.h"
#include "imgui_test_engine/imgui_te_engine.h"
#include "imgui_test_engine/imgui_te_ui.h"
#include "app/platform/window.h"
#include "app/controller.h"
#include "app/gfx/backend/sdl2_renderer.h"
#include "e2e/canvas_selection_test.h"
#include "e2e/framework_smoke_test.h"
#include "e2e/dungeon_editor_smoke_test.h"
// #include "test_editor.h" // Not used in main
namespace yaze {
namespace test {
// Test execution modes for AI agents and developers
enum class TestMode {
kAll, // Run all tests (default)
kUnit, // Run only unit tests
kIntegration, // Run only integration tests
kE2E, // Run only end-to-end tests
kRomDependent, // Run ROM-dependent tests only
kZSCustomOverworld, // Run ZSCustomOverworld specific tests
kCore, // Run core functionality tests
kGraphics, // Run graphics-related tests
kEditor, // Run editor tests
kDeprecated, // Run deprecated tests (for cleanup)
kSpecific // Run specific test pattern
};
struct TestConfig {
TestMode mode = TestMode::kAll;
std::string test_pattern;
std::string rom_path = "zelda3.sfc";
bool verbose = false;
bool skip_rom_tests = false;
bool enable_ui_tests = false;
bool show_gui = false;
ImGuiTestRunSpeed test_speed = ImGuiTestRunSpeed_Fast;
};
// Parse command line arguments for better AI agent testing support
TestConfig ParseArguments(int argc, char* argv[]) {
TestConfig config;
std::cout << "Available options:\n"
<< " --ui : Enable UI tests\n"
<< " --show-gui : Show GUI during tests\n"
<< " --fast : Run tests at max speed (default)\n"
<< " --normal : Run tests at watchable speed\n"
<< " --cinematic : Run tests in slow-motion with pauses\n"
<< " --rom=<path> : Specify ROM file path\n"
<< " --pattern=<pat> : Run tests matching pattern\n"
<< std::endl;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
std::cout << "YAZE Test Runner - Enhanced for AI Agent Testing\n\n";
std::cout << "Usage: yaze_test [options] [test_pattern]\n\n";
std::cout << "Test Modes:\n";
std::cout << " --unit Run unit tests only\n";
std::cout << " --integration Run integration tests only\n";
std::cout << " --e2e Run end-to-end tests only\n";
std::cout << " --rom-dependent Run ROM-dependent tests only\n";
std::cout << " --zscustomoverworld Run ZSCustomOverworld tests only\n";
std::cout << " --core Run core functionality tests\n";
std::cout << " --graphics Run graphics tests\n";
std::cout << " --editor Run editor tests\n";
std::cout << " --deprecated Run deprecated tests\n\n";
std::cout << "Options:\n";
std::cout << " --rom-path PATH Specify ROM path for testing\n";
std::cout << " --skip-rom-tests Skip tests requiring ROM files\n";
std::cout << " --enable-ui-tests Enable UI tests (requires display)\n";
std::cout << " --verbose Enable verbose output\n";
std::cout << " --help Show this help message\n\n";
std::cout << "Examples:\n";
std::cout << " yaze_test --unit --verbose\n";
std::cout << " yaze_test --e2e --rom-path my_rom.sfc\n";
std::cout << " yaze_test --zscustomoverworld --verbose\n";
std::cout << " yaze_test RomTest.*\n";
exit(0);
} else if (arg == "--unit") {
config.mode = TestMode::kUnit;
} else if (arg == "--integration") {
config.mode = TestMode::kIntegration;
} else if (arg == "--e2e") {
config.mode = TestMode::kE2E;
} else if (arg == "--rom-dependent") {
config.mode = TestMode::kRomDependent;
} else if (arg == "--zscustomoverworld") {
config.mode = TestMode::kZSCustomOverworld;
} else if (arg == "--core") {
config.mode = TestMode::kCore;
} else if (arg == "--graphics") {
config.mode = TestMode::kGraphics;
} else if (arg == "--editor") {
config.mode = TestMode::kEditor;
} else if (arg == "--deprecated") {
config.mode = TestMode::kDeprecated;
} else if (arg == "--rom-path") {
if (i + 1 < argc) {
config.rom_path = argv[++i];
}
} else if (arg == "--skip-rom-tests") {
config.skip_rom_tests = true;
} else if (arg == "--enable-ui-tests") {
config.enable_ui_tests = true;
} else if (arg == "--verbose") {
config.verbose = true;
} else if (arg == "--show-gui") {
config.show_gui = true;
} else if (arg == "--fast") {
config.test_speed = ImGuiTestRunSpeed_Fast;
} else if (arg == "--normal") {
config.test_speed = ImGuiTestRunSpeed_Normal;
} else if (arg == "--cinematic") {
config.test_speed = ImGuiTestRunSpeed_Cinematic;
} else if (arg == "--ui") {
config.enable_ui_tests = true;
} else if (arg.find("--") != 0) {
// Test pattern (not a flag)
config.mode = TestMode::kSpecific;
config.test_pattern = arg;
}
}
return config;
}
// Set up test environment based on configuration
void SetupTestEnvironment(const TestConfig& config) {
// Set environment variables for tests using SDL's cross-platform function
if (!config.rom_path.empty()) {
SDL_setenv("YAZE_TEST_ROM_PATH", config.rom_path.c_str(), 1);
}
if (config.skip_rom_tests) {
SDL_setenv("YAZE_SKIP_ROM_TESTS", "1", 1);
}
if (config.enable_ui_tests) {
SDL_setenv("YAZE_ENABLE_UI_TESTS", "1", 1);
}
if (config.verbose) {
SDL_setenv("YAZE_VERBOSE_TESTS", "1", 1);
}
}
// Configure Google Test filters based on test mode
void ConfigureTestFilters(const TestConfig& config) {
std::vector<std::string> filters;
switch (config.mode) {
case TestMode::kUnit:
filters.push_back("UnitTest.*");
break;
case TestMode::kIntegration:
filters.push_back("IntegrationTest.*");
break;
case TestMode::kE2E:
filters.push_back("E2ETest.*");
break;
case TestMode::kRomDependent:
filters.push_back("*RomDependent*");
break;
case TestMode::kZSCustomOverworld:
filters.push_back("*ZSCustomOverworld*");
break;
case TestMode::kCore:
filters.push_back("*Core*");
filters.push_back("*Asar*");
filters.push_back("*Rom*");
break;
case TestMode::kGraphics:
filters.push_back("*Graphics*");
filters.push_back("*Gfx*");
filters.push_back("*Palette*");
filters.push_back("*Tile*");
break;
case TestMode::kEditor:
filters.push_back("*Editor*");
break;
case TestMode::kDeprecated:
filters.push_back("*Deprecated*");
break;
case TestMode::kSpecific:
if (!config.test_pattern.empty()) {
filters.push_back(config.test_pattern);
}
break;
case TestMode::kAll:
default:
// No filters - run all tests
break;
}
if (!filters.empty()) {
std::string filter_string;
for (size_t i = 0; i < filters.size(); i++) {
if (i > 0) filter_string += ":";
filter_string += filters[i];
}
::testing::GTEST_FLAG(filter) = filter_string;
if (config.verbose) {
std::cout << "Test filter: " << filter_string << std::endl;
}
}
}
} // namespace test
} // namespace yaze
int main(int argc, char* argv[]) {
absl::InitializeSymbolizer(argv[0]);
// Configure failure signal handler to be less aggressive for testing
// This prevents false positives during SDL/graphics cleanup in tests
absl::FailureSignalHandlerOptions options;
options.symbolize_stacktrace = true;
options.use_alternate_stack = false; // Avoid conflicts with normal stack during cleanup
options.alarm_on_failure_secs = false; // Don't set alarms that can trigger on natural leaks
options.call_previous_handler = true; // Allow system handlers to also run
options.writerfn = nullptr; // Use default writer to avoid custom handling issues
options.use_alternate_stack = false;
options.alarm_on_failure_secs = false;
options.call_previous_handler = true;
options.writerfn = nullptr;
absl::InstallFailureSignalHandler(options);
// Initialize SDL to prevent crashes in graphics components
@@ -26,20 +252,146 @@ int main(int argc, char* argv[]) {
// Continue anyway for tests that don't need graphics
}
if (argc > 1 && std::string(argv[1]) == "integration") {
return yaze::test::RunIntegrationTest();
} else if (argc > 1 && std::string(argv[1]) == "room_object") {
::testing::InitGoogleTest(&argc, argv);
if (!RUN_ALL_TESTS()) {
return yaze::test::RunIntegrationTest();
}
}
// Parse command line arguments
auto config = yaze::test::ParseArguments(argc, argv);
// Set up test environment
yaze::test::SetupTestEnvironment(config);
// Configure test filters
yaze::test::ConfigureTestFilters(config);
// Initialize Google Test
::testing::InitGoogleTest(&argc, argv);
int result = RUN_ALL_TESTS();
// Cleanup SDL
SDL_Quit();
return result;
}
if (config.enable_ui_tests) {
// Create a window
yaze::core::Window window;
// Create renderer for test
auto test_renderer = std::make_unique<yaze::gfx::SDL2Renderer>();
yaze::core::CreateWindow(window, test_renderer.get(), SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
// Renderer is now owned by test
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
// Setup Dear ImGui style
ImGui::StyleColorsDark();
// When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
ImGuiStyle& style = ImGui::GetStyle();
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}
// Setup Platform/Renderer backends
SDL_Renderer* sdl_renderer = static_cast<SDL_Renderer*>(test_renderer->GetBackendRenderer());
ImGui_ImplSDL2_InitForSDLRenderer(window.window_.get(), sdl_renderer);
ImGui_ImplSDLRenderer2_Init(sdl_renderer);
yaze::Controller controller;
// Setup test engine
ImGuiTestEngine* engine = ImGuiTestEngine_CreateContext();
ImGuiTestEngineIO& test_io = ImGuiTestEngine_GetIO(engine);
test_io.ConfigRunSpeed = config.test_speed; // Use configured speed
test_io.ConfigVerboseLevel = ImGuiTestVerboseLevel_Info;
test_io.ConfigVerboseLevelOnError = ImGuiTestVerboseLevel_Debug;
// Log test speed mode
const char* speed_name = "Fast";
if (config.test_speed == ImGuiTestRunSpeed_Normal) speed_name = "Normal";
else if (config.test_speed == ImGuiTestRunSpeed_Cinematic) speed_name = "Cinematic";
std::cout << "Running tests in " << speed_name << " mode" << std::endl;
// Register E2E tests only for GUI test targets (they have the source files)
#ifdef YAZE_GUI_TEST_TARGET
ImGuiTest* smoke_test = IM_REGISTER_TEST(engine, "E2ETest", "FrameworkSmokeTest");
smoke_test->TestFunc = E2ETest_FrameworkSmokeTest;
ImGuiTest* canvas_test = IM_REGISTER_TEST(engine, "E2ETest", "CanvasSelectionTest");
canvas_test->TestFunc = E2ETest_CanvasSelectionTest;
canvas_test->UserData = &controller;
ImGuiTest* dungeon_test = IM_REGISTER_TEST(engine, "E2ETest", "DungeonEditorSmokeTest");
dungeon_test->TestFunc = E2ETest_DungeonEditorV2SmokeTest;
dungeon_test->UserData = &controller;
#endif
// Main loop
bool done = false;
while (!done) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT) {
done = true;
}
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window.window_.get())) {
done = true;
}
}
// Start the Dear ImGui frame
ImGui_ImplSDLRenderer2_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
// Render the UI
if (config.show_gui) {
ImGuiTestEngine_ShowTestEngineWindows(engine, &config.show_gui);
}
controller.DoRender();
// End the Dear ImGui frame
ImGui::Render();
test_renderer->Clear();
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), sdl_renderer);
test_renderer->Present();
// Update and Render additional Platform Windows
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow();
SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
}
// Run test engine
ImGuiTestEngine_PostSwap(engine);
}
// Get test result
ImGuiTestEngineResultSummary summary;
ImGuiTestEngine_GetResultSummary(engine, &summary);
int result = (summary.CountSuccess == summary.CountTested) ? 0 : 1;
// Cleanup
controller.OnExit();
ImGuiTestEngine_DestroyContext(engine);
ImGui_ImplSDLRenderer2_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
yaze::core::ShutdownWindow(window);
SDL_Quit();
return result;
} else {
// Run tests
int result = RUN_ALL_TESTS();
// Cleanup SDL
SDL_Quit();
return result;
}
}

46
test/yaze_test_ci.cc Normal file
View File

@@ -0,0 +1,46 @@
// Simplified test executable for CI/CD builds
// This version removes complex argument parsing and SDL initialization
// to ensure reliable test discovery and execution in automated environments
#include <gtest/gtest.h>
#include <iostream>
#include "absl/debugging/failure_signal_handler.h"
#include "absl/debugging/symbolize.h"
int main(int argc, char* argv[]) {
// Initialize symbolizer for better error reporting
absl::InitializeSymbolizer(argv[0]);
// Configure failure signal handler to be less aggressive for CI
absl::FailureSignalHandlerOptions options;
options.symbolize_stacktrace = true;
options.use_alternate_stack = false;
options.alarm_on_failure_secs = false;
options.call_previous_handler = true;
options.writerfn = nullptr;
absl::InstallFailureSignalHandler(options);
// Initialize Google Test with minimal configuration
::testing::InitGoogleTest(&argc, argv);
// Set up basic test environment
::testing::FLAGS_gtest_color = "yes";
::testing::FLAGS_gtest_print_time = true;
// For CI builds, skip ROM-dependent tests by default
// These tests require actual ROM files which aren't available in CI
std::string filter = ::testing::GTEST_FLAG(filter);
if (filter.empty()) {
// Default filter for CI: exclude ROM-dependent and E2E tests
::testing::GTEST_FLAG(filter) = "-*RomTest*:-*E2E*:-*ZSCustomOverworld*";
}
std::cout << "Running YAZE tests in CI mode..." << std::endl;
std::cout << "Test filter: " << ::testing::GTEST_FLAG(filter) << std::endl;
// Run tests
int result = RUN_ALL_TESTS();
return result;
}

View File

@@ -1,374 +0,0 @@
#include <gtest/gtest.h>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <memory>
#include "app/rom.h"
#include "app/zelda3/overworld/overworld.h"
#include "app/zelda3/overworld/overworld_map.h"
namespace yaze {
namespace zelda3 {
class ComprehensiveIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
// Skip tests on Linux for automated github builds
#if defined(__linux__)
GTEST_SKIP();
#endif
vanilla_rom_path_ = "zelda3.sfc";
v3_rom_path_ = "zelda3_v3_test.sfc";
// Create v3 patched ROM for testing
CreateV3PatchedROM();
// Load vanilla ROM
vanilla_rom_ = std::make_unique<Rom>();
ASSERT_TRUE(vanilla_rom_->LoadFromFile(vanilla_rom_path_).ok());
// TODO: Load graphics data when gfx system is available
// ASSERT_TRUE(gfx::LoadAllGraphicsData(*vanilla_rom_, true).ok());
// Initialize vanilla overworld
vanilla_overworld_ = std::make_unique<Overworld>(vanilla_rom_.get());
ASSERT_TRUE(vanilla_overworld_->Load(vanilla_rom_.get()).ok());
// Load v3 ROM
v3_rom_ = std::make_unique<Rom>();
ASSERT_TRUE(v3_rom_->LoadFromFile(v3_rom_path_).ok());
// TODO: Load graphics data when gfx system is available
// ASSERT_TRUE(gfx::LoadAllGraphicsData(*v3_rom_, true).ok());
// Initialize v3 overworld
v3_overworld_ = std::make_unique<Overworld>(v3_rom_.get());
ASSERT_TRUE(v3_overworld_->Load(v3_rom_.get()).ok());
}
void TearDown() override {
v3_overworld_.reset();
vanilla_overworld_.reset();
v3_rom_.reset();
vanilla_rom_.reset();
// TODO: Destroy graphics data when gfx system is available
// gfx::DestroyAllGraphicsData();
// Clean up test files
if (std::filesystem::exists(v3_rom_path_)) {
std::filesystem::remove(v3_rom_path_);
}
}
void CreateV3PatchedROM() {
// Copy vanilla ROM and apply v3 patch
std::ifstream src(vanilla_rom_path_, std::ios::binary);
std::ofstream dst(v3_rom_path_, std::ios::binary);
dst << src.rdbuf();
src.close();
dst.close();
// Load the copied ROM
Rom rom;
ASSERT_TRUE(rom.LoadFromFile(v3_rom_path_).ok());
// Apply v3 patch
ApplyV3Patch(rom);
// Save the patched ROM
ASSERT_TRUE(
rom.SaveToFile(Rom::SaveSettings{.filename = v3_rom_path_}).ok());
}
void ApplyV3Patch(Rom& rom) {
// Set ASM version to v3
ASSERT_TRUE(rom.WriteByte(OverworldCustomASMHasBeenApplied, 0x03).ok());
// Enable v3 features
ASSERT_TRUE(rom.WriteByte(OverworldCustomAreaSpecificBGEnabled, 0x01).ok());
ASSERT_TRUE(rom.WriteByte(OverworldCustomSubscreenOverlayEnabled, 0x01).ok());
ASSERT_TRUE(rom.WriteByte(OverworldCustomAnimatedGFXEnabled, 0x01).ok());
ASSERT_TRUE(rom.WriteByte(OverworldCustomTileGFXGroupEnabled, 0x01).ok());
ASSERT_TRUE(rom.WriteByte(OverworldCustomMosaicEnabled, 0x01).ok());
ASSERT_TRUE(rom.WriteByte(OverworldCustomMainPaletteEnabled, 0x01).ok());
// Apply v3 settings to first 10 maps for testing
for (int i = 0; i < 10; i++) {
// Set area sizes (mix of different sizes)
AreaSizeEnum area_size = static_cast<AreaSizeEnum>(i % 4);
ASSERT_TRUE(rom.WriteByte(kOverworldScreenSize + i, static_cast<uint8_t>(area_size)).ok());
// Set main palettes
ASSERT_TRUE(rom.WriteByte(OverworldCustomMainPaletteArray + i, i % 8).ok());
// Set area-specific background colors
uint16_t bg_color = 0x0000 + (i * 0x1000);
ASSERT_TRUE(rom.WriteByte(OverworldCustomAreaSpecificBGPalette + (i * 2),
bg_color & 0xFF).ok());
ASSERT_TRUE(rom.WriteByte(OverworldCustomAreaSpecificBGPalette + (i * 2) + 1,
(bg_color >> 8) & 0xFF).ok());
// Set subscreen overlays
uint16_t overlay = 0x0090 + i;
ASSERT_TRUE(rom.WriteByte(OverworldCustomSubscreenOverlayArray + (i * 2),
overlay & 0xFF).ok());
ASSERT_TRUE(rom.WriteByte(OverworldCustomSubscreenOverlayArray + (i * 2) + 1,
(overlay >> 8) & 0xFF).ok());
// Set animated GFX
ASSERT_TRUE(rom.WriteByte(OverworldCustomAnimatedGFXArray + i, 0x50 + i).ok());
// Set custom tile GFX groups (8 bytes per map)
for (int j = 0; j < 8; j++) {
ASSERT_TRUE(rom.WriteByte(OverworldCustomTileGFXGroupArray + (i * 8) + j,
0x20 + j + i).ok());
}
// Set mosaic settings
ASSERT_TRUE(rom.WriteByte(OverworldCustomMosaicArray + i, i % 16).ok());
// Set expanded message IDs
uint16_t message_id = 0x1000 + i;
ASSERT_TRUE(rom.WriteByte(kOverworldMessagesExpanded + (i * 2), message_id & 0xFF).ok());
ASSERT_TRUE(rom.WriteByte(kOverworldMessagesExpanded + (i * 2) + 1,
(message_id >> 8) & 0xFF).ok());
}
}
std::string vanilla_rom_path_;
std::string v3_rom_path_;
std::unique_ptr<Rom> vanilla_rom_;
std::unique_ptr<Rom> v3_rom_;
std::unique_ptr<Overworld> vanilla_overworld_;
std::unique_ptr<Overworld> v3_overworld_;
};
// Test vanilla ROM behavior
TEST_F(ComprehensiveIntegrationTest, VanillaROMDetection) {
uint8_t vanilla_asm_version =
(*vanilla_rom_)[OverworldCustomASMHasBeenApplied];
EXPECT_EQ(vanilla_asm_version, 0xFF); // 0xFF means vanilla ROM
}
TEST_F(ComprehensiveIntegrationTest, VanillaROMMapProperties) {
// Test a few specific maps from vanilla ROM
const OverworldMap* map0 = vanilla_overworld_->overworld_map(0);
const OverworldMap* map3 = vanilla_overworld_->overworld_map(3);
const OverworldMap* map64 = vanilla_overworld_->overworld_map(64);
ASSERT_NE(map0, nullptr);
ASSERT_NE(map3, nullptr);
ASSERT_NE(map64, nullptr);
// Verify basic properties are loaded
EXPECT_GE(map0->area_graphics(), 0);
EXPECT_GE(map0->area_palette(), 0);
EXPECT_GE(map0->message_id(), 0);
EXPECT_GE(map3->area_graphics(), 0);
EXPECT_GE(map3->area_palette(), 0);
EXPECT_GE(map64->area_graphics(), 0);
EXPECT_GE(map64->area_palette(), 0);
// Verify area sizes are reasonable
EXPECT_TRUE(map0->area_size() == AreaSizeEnum::SmallArea ||
map0->area_size() == AreaSizeEnum::LargeArea);
EXPECT_TRUE(map3->area_size() == AreaSizeEnum::SmallArea ||
map3->area_size() == AreaSizeEnum::LargeArea);
EXPECT_TRUE(map64->area_size() == AreaSizeEnum::SmallArea ||
map64->area_size() == AreaSizeEnum::LargeArea);
}
// Test v3 ROM behavior
TEST_F(ComprehensiveIntegrationTest, V3ROMDetection) {
uint8_t v3_asm_version = (*v3_rom_)[OverworldCustomASMHasBeenApplied];
EXPECT_EQ(v3_asm_version, 0x03); // 0x03 means v3 ROM
}
TEST_F(ComprehensiveIntegrationTest, V3ROMFeatureFlags) {
// Test that v3 features are enabled
EXPECT_EQ((*v3_rom_)[OverworldCustomAreaSpecificBGEnabled], 0x01);
EXPECT_EQ((*v3_rom_)[OverworldCustomSubscreenOverlayEnabled], 0x01);
EXPECT_EQ((*v3_rom_)[OverworldCustomAnimatedGFXEnabled], 0x01);
EXPECT_EQ((*v3_rom_)[OverworldCustomTileGFXGroupEnabled], 0x01);
EXPECT_EQ((*v3_rom_)[OverworldCustomMosaicEnabled], 0x01);
EXPECT_EQ((*v3_rom_)[OverworldCustomMainPaletteEnabled], 0x01);
}
TEST_F(ComprehensiveIntegrationTest, V3ROMAreaSizes) {
// Test that v3 area sizes are loaded correctly
for (int i = 0; i < 10; i++) {
const OverworldMap* map = v3_overworld_->overworld_map(i);
ASSERT_NE(map, nullptr);
AreaSizeEnum expected_size = static_cast<AreaSizeEnum>(i % 4);
EXPECT_EQ(map->area_size(), expected_size);
}
}
TEST_F(ComprehensiveIntegrationTest, V3ROMMainPalettes) {
// Test that v3 main palettes are loaded correctly
for (int i = 0; i < 10; i++) {
const OverworldMap* map = v3_overworld_->overworld_map(i);
ASSERT_NE(map, nullptr);
uint8_t expected_palette = i % 8;
EXPECT_EQ(map->main_palette(), expected_palette);
}
}
TEST_F(ComprehensiveIntegrationTest, V3ROMAreaSpecificBackgroundColors) {
// Test that v3 area-specific background colors are loaded correctly
for (int i = 0; i < 10; i++) {
const OverworldMap* map = v3_overworld_->overworld_map(i);
ASSERT_NE(map, nullptr);
uint16_t expected_color = 0x0000 + (i * 0x1000);
EXPECT_EQ(map->area_specific_bg_color(), expected_color);
}
}
TEST_F(ComprehensiveIntegrationTest, V3ROMSubscreenOverlays) {
// Test that v3 subscreen overlays are loaded correctly
for (int i = 0; i < 10; i++) {
const OverworldMap* map = v3_overworld_->overworld_map(i);
ASSERT_NE(map, nullptr);
uint16_t expected_overlay = 0x0090 + i;
EXPECT_EQ(map->subscreen_overlay(), expected_overlay);
}
}
TEST_F(ComprehensiveIntegrationTest, V3ROMAnimatedGFX) {
// Test that v3 animated GFX are loaded correctly
for (int i = 0; i < 10; i++) {
const OverworldMap* map = v3_overworld_->overworld_map(i);
ASSERT_NE(map, nullptr);
uint8_t expected_gfx = 0x50 + i;
EXPECT_EQ(map->animated_gfx(), expected_gfx);
}
}
TEST_F(ComprehensiveIntegrationTest, V3ROMCustomTileGFXGroups) {
// Test that v3 custom tile GFX groups are loaded correctly
for (int i = 0; i < 10; i++) {
const OverworldMap* map = v3_overworld_->overworld_map(i);
ASSERT_NE(map, nullptr);
for (int j = 0; j < 8; j++) {
uint8_t expected_tile = 0x20 + j + i;
EXPECT_EQ(map->custom_tileset(j), expected_tile);
}
}
}
TEST_F(ComprehensiveIntegrationTest, V3ROMExpandedMessageIds) {
// Test that v3 expanded message IDs are loaded correctly
for (int i = 0; i < 10; i++) {
const OverworldMap* map = v3_overworld_->overworld_map(i);
ASSERT_NE(map, nullptr);
uint16_t expected_message_id = 0x1000 + i;
EXPECT_EQ(map->message_id(), expected_message_id);
}
}
// Test backwards compatibility
TEST_F(ComprehensiveIntegrationTest, BackwardsCompatibility) {
// Test that v3 ROMs still have access to vanilla properties
for (int i = 0; i < 10; i++) {
const OverworldMap* vanilla_map = vanilla_overworld_->overworld_map(i);
const OverworldMap* v3_map = v3_overworld_->overworld_map(i);
ASSERT_NE(vanilla_map, nullptr);
ASSERT_NE(v3_map, nullptr);
// Basic properties should still be accessible
EXPECT_GE(v3_map->area_graphics(), 0);
EXPECT_GE(v3_map->area_palette(), 0);
EXPECT_GE(v3_map->message_id(), 0);
}
}
// Test save/load functionality
TEST_F(ComprehensiveIntegrationTest, SaveAndReloadV3ROM) {
// Modify some properties
v3_overworld_->mutable_overworld_map(0)->set_main_palette(0x07);
v3_overworld_->mutable_overworld_map(1)->set_area_specific_bg_color(0x7FFF);
v3_overworld_->mutable_overworld_map(2)->set_subscreen_overlay(0x1234);
// Save the ROM
ASSERT_TRUE(v3_overworld_->Save(v3_rom_.get()).ok());
// Reload the ROM
Rom reloaded_rom;
ASSERT_TRUE(reloaded_rom.LoadFromFile(v3_rom_path_).ok());
Overworld reloaded_overworld(&reloaded_rom);
ASSERT_TRUE(reloaded_overworld.Load(&reloaded_rom).ok());
// Verify the changes were saved
EXPECT_EQ(reloaded_overworld.overworld_map(0)->main_palette(), 0x07);
EXPECT_EQ(reloaded_overworld.overworld_map(1)->area_specific_bg_color(),
0x7FFF);
EXPECT_EQ(reloaded_overworld.overworld_map(2)->subscreen_overlay(), 0x1234);
}
// Performance test
TEST_F(ComprehensiveIntegrationTest, PerformanceTest) {
const int kNumMaps = 160;
auto start_time = std::chrono::high_resolution_clock::now();
// Test vanilla ROM performance
for (int i = 0; i < kNumMaps; i++) {
const OverworldMap* map = vanilla_overworld_->overworld_map(i);
if (map) {
map->area_graphics();
map->area_palette();
map->message_id();
map->area_size();
}
}
// Test v3 ROM performance
for (int i = 0; i < kNumMaps; i++) {
const OverworldMap* map = v3_overworld_->overworld_map(i);
if (map) {
map->area_graphics();
map->area_palette();
map->message_id();
map->area_size();
map->main_palette();
map->area_specific_bg_color();
map->subscreen_overlay();
map->animated_gfx();
}
}
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
end_time - start_time);
// Should complete in reasonable time (less than 2 seconds for 320 map
// operations)
EXPECT_LT(duration.count(), 2000);
}
// Test dungeon integration (if applicable)
TEST_F(ComprehensiveIntegrationTest, DungeonIntegration) {
// This test ensures that overworld changes don't break dungeon functionality
// For now, just verify that the ROMs can be loaded without errors
EXPECT_TRUE(vanilla_overworld_->is_loaded());
EXPECT_TRUE(v3_overworld_->is_loaded());
// Verify that we have the expected number of maps
EXPECT_EQ(vanilla_overworld_->overworld_maps().size(), kNumOverworldMaps);
EXPECT_EQ(v3_overworld_->overworld_maps().size(), kNumOverworldMaps);
}
} // namespace zelda3
} // namespace yaze

View File

@@ -1,208 +0,0 @@
#include <gtest/gtest.h>
#include <memory>
#include <fstream>
#include <filesystem>
#include "app/rom.h"
#include "app/zelda3/overworld/overworld.h"
#include "app/zelda3/overworld/overworld_map.h"
namespace yaze {
namespace zelda3 {
class DungeonIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
// Skip tests on Linux for automated github builds
#if defined(__linux__)
GTEST_SKIP();
#endif
rom_path_ = "zelda3.sfc";
// Load ROM
rom_ = std::make_unique<Rom>();
ASSERT_TRUE(rom_->LoadFromFile(rom_path_).ok());
// TODO: Load graphics data when gfx system is available
// ASSERT_TRUE(gfx::LoadAllGraphicsData(*rom_, true).ok());
// Initialize overworld
overworld_ = std::make_unique<Overworld>(rom_.get());
ASSERT_TRUE(overworld_->Load(rom_.get()).ok());
}
void TearDown() override {
overworld_.reset();
rom_.reset();
// TODO: Destroy graphics data when gfx system is available
// gfx::DestroyAllGraphicsData();
}
std::string rom_path_;
std::unique_ptr<Rom> rom_;
std::unique_ptr<Overworld> overworld_;
};
// Test dungeon room loading
TEST_F(DungeonIntegrationTest, DungeonRoomLoading) {
// TODO: Implement dungeon room loading tests when Room class is available
// Test loading a few dungeon rooms
const int kNumTestRooms = 10;
for (int i = 0; i < kNumTestRooms; i++) {
// TODO: Create Room instance and test basic properties
// Room room(i, rom_.get());
// EXPECT_EQ(room.index(), i);
// EXPECT_GE(room.width(), 0);
// EXPECT_GE(room.height(), 0);
// auto status = room.Build();
// EXPECT_TRUE(status.ok()) << "Failed to build room " << i << ": " << status.message();
}
}
// Test dungeon object parsing
TEST_F(DungeonIntegrationTest, DungeonObjectParsing) {
// TODO: Implement dungeon object parsing tests when ObjectParser is available
// Test object parsing for a few rooms
const int kNumTestRooms = 5;
for (int i = 0; i < kNumTestRooms; i++) {
// TODO: Create Room and ObjectParser instances
// Room room(i, rom_.get());
// ASSERT_TRUE(room.Build().ok());
// ObjectParser parser(room);
// auto objects = parser.ParseObjects();
// EXPECT_TRUE(objects.ok()) << "Failed to parse objects for room " << i << ": " << objects.status().message();
// if (objects.ok()) {
// for (const auto& obj : objects.value()) {
// EXPECT_GE(obj.x(), 0);
// EXPECT_GE(obj.y(), 0);
// EXPECT_GE(obj.type(), 0);
// }
// }
}
}
// Test dungeon object rendering
TEST_F(DungeonIntegrationTest, DungeonObjectRendering) {
// TODO: Implement dungeon object rendering tests when ObjectRenderer is available
// Test object rendering for a few rooms
const int kNumTestRooms = 3;
for (int i = 0; i < kNumTestRooms; i++) {
// TODO: Create Room, ObjectParser, and ObjectRenderer instances
// Room room(i, rom_.get());
// ASSERT_TRUE(room.Build().ok());
// ObjectParser parser(room);
// auto objects = parser.ParseObjects();
// ASSERT_TRUE(objects.ok());
// ObjectRenderer renderer(room);
// auto status = renderer.RenderObjects(objects.value());
// EXPECT_TRUE(status.ok()) << "Failed to render objects for room " << i << ": " << status.message();
}
}
// Test dungeon integration with overworld
TEST_F(DungeonIntegrationTest, DungeonOverworldIntegration) {
// Test that dungeon changes don't affect overworld functionality
EXPECT_TRUE(overworld_->is_loaded());
EXPECT_EQ(overworld_->overworld_maps().size(), kNumOverworldMaps);
// Test that we can access overworld maps after dungeon operations
const OverworldMap* map0 = overworld_->overworld_map(0);
ASSERT_NE(map0, nullptr);
// Verify basic overworld properties still work
EXPECT_GE(map0->area_graphics(), 0);
EXPECT_GE(map0->area_palette(), 0);
EXPECT_GE(map0->message_id(), 0);
}
// Test ROM integrity after dungeon operations
TEST_F(DungeonIntegrationTest, ROMIntegrity) {
// Test that ROM remains intact after dungeon operations
// std::vector<uint8_t> original_data = rom_->data();
// // Perform various dungeon operations
// for (int i = 0; i < 5; i++) {
// Room room(i, rom_.get());
// room.Build();
// ObjectParser parser(room);
// parser.ParseObjects();
// }
// // Verify ROM data hasn't changed
// std::vector<uint8_t> current_data = rom_->data();
// EXPECT_EQ(original_data.size(), current_data.size());
// // Check that critical ROM areas haven't been corrupted
// EXPECT_EQ(rom_->data()[0x7FC0], original_data[0x7FC0]); // ROM header
// EXPECT_EQ(rom_->data()[0x7FC1], original_data[0x7FC1]);
// EXPECT_EQ(rom_->data()[0x7FC2], original_data[0x7FC2]);
}
// Performance test for dungeon operations
TEST_F(DungeonIntegrationTest, DungeonPerformanceTest) {
// TODO: Implement dungeon performance tests when dungeon classes are available
const int kNumRooms = 50;
auto start_time = std::chrono::high_resolution_clock::now();
for (int i = 0; i < kNumRooms; i++) {
// TODO: Create Room and ObjectParser instances for performance testing
// Room room(i, rom_.get());
// room.Build();
// ObjectParser parser(room);
// parser.ParseObjects();
}
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
// Should complete in reasonable time (less than 5 seconds for 50 rooms)
EXPECT_LT(duration.count(), 5000);
}
// Test dungeon save/load functionality
TEST_F(DungeonIntegrationTest, DungeonSaveLoad) {
// TODO: Implement dungeon save/load tests when dungeon classes are available
// Create a test room
// Room room(0, rom_.get());
// ASSERT_TRUE(room.Build().ok());
// Parse objects
// ObjectParser parser(room);
// auto objects = parser.ParseObjects();
// ASSERT_TRUE(objects.ok());
// Modify some objects (if any exist)
// if (!objects.value().empty()) {
// // This would involve modifying object properties and saving
// // For now, just verify the basic save/load mechanism works
// EXPECT_TRUE(rom_->SaveToFile("test_dungeon.sfc").ok());
//
// // Clean up test file
// if (std::filesystem::exists("test_dungeon.sfc")) {
// std::filesystem::remove("test_dungeon.sfc");
// }
// }
}
// Test dungeon error handling
TEST_F(DungeonIntegrationTest, DungeonErrorHandling) {
// TODO: Implement dungeon error handling tests when Room class is available
// Test with invalid room indices
// Room invalid_room(-1, rom_.get());
// auto status = invalid_room.Build();
// EXPECT_FALSE(status.ok()); // Should fail for invalid room
// Test with very large room index
// Room large_room(1000, rom_.get());
// status = large_room.Build();
// EXPECT_FALSE(status.ok()); // Should fail for non-existent room
}
} // namespace zelda3
} // namespace yaze

View File

@@ -1,784 +0,0 @@
#include <gtest/gtest.h>
#include <memory>
#include <chrono>
#include <vector>
#include <map>
#include "app/rom.h"
#include "app/zelda3/dungeon/room.h"
#include "app/zelda3/dungeon/room_object.h"
#include "app/zelda3/dungeon/dungeon_object_editor.h"
#include "app/zelda3/dungeon/object_renderer.h"
#include "app/zelda3/dungeon/dungeon_editor_system.h"
#include "app/gfx/snes_palette.h"
namespace yaze {
namespace zelda3 {
class DungeonObjectRendererIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
// Skip tests on Linux for automated github builds
#if defined(__linux__)
GTEST_SKIP();
#endif
// Use the real ROM from build directory
rom_path_ = "build/bin/zelda3.sfc";
// Load ROM
rom_ = std::make_unique<Rom>();
ASSERT_TRUE(rom_->LoadFromFile(rom_path_).ok());
// Initialize dungeon editor system
dungeon_editor_system_ = std::make_unique<DungeonEditorSystem>(rom_.get());
ASSERT_TRUE(dungeon_editor_system_->Initialize().ok());
// Initialize object editor
object_editor_ = std::make_shared<DungeonObjectEditor>(rom_.get());
// Note: InitializeEditor() is private, so we skip this in integration tests
// Initialize object renderer
object_renderer_ = std::make_unique<ObjectRenderer>(rom_.get());
// Load test room data
ASSERT_TRUE(LoadTestRoomData().ok());
}
void TearDown() override {
object_renderer_.reset();
object_editor_.reset();
dungeon_editor_system_.reset();
rom_.reset();
}
absl::Status LoadTestRoomData() {
// Load representative rooms based on disassembly data
// Room 0x0000: Ganon's room (from disassembly)
// Room 0x0001: First dungeon room
// Room 0x0002: Sewer room (from disassembly)
// Room 0x0010: Another dungeon room (from disassembly)
// Room 0x0012: Sewer room (from disassembly)
// Room 0x0020: Agahnim's tower (from disassembly)
test_rooms_ = {0x0000, 0x0001, 0x0002, 0x0010, 0x0012, 0x0020, 0x0033, 0x005A};
for (int room_id : test_rooms_) {
auto room_result = zelda3::LoadRoomFromRom(rom_.get(), room_id);
rooms_[room_id] = room_result;
rooms_[room_id].LoadObjects();
// Log room data for debugging
if (!rooms_[room_id].GetTileObjects().empty()) {
std::cout << "Room 0x" << std::hex << room_id << std::dec
<< " loaded with " << rooms_[room_id].GetTileObjects().size()
<< " objects" << std::endl;
}
}
// Load palette data for testing based on vanilla values
auto palette_group = rom_->palette_group().dungeon_main;
test_palettes_ = {palette_group[0], palette_group[1], palette_group[2]};
return absl::OkStatus();
}
// Helper methods for creating test objects
RoomObject CreateTestObject(int object_id, int x, int y, int size = 0x12, int layer = 0) {
RoomObject obj(object_id, x, y, size, layer);
obj.set_rom(rom_.get());
obj.EnsureTilesLoaded();
return obj;
}
std::vector<RoomObject> CreateTestObjectSet(int room_id) {
std::vector<RoomObject> objects;
// Create test objects based on real object types from disassembly
// These correspond to actual object types found in the ROM
objects.push_back(CreateTestObject(0x10, 5, 5, 0x12, 0)); // Wall object
objects.push_back(CreateTestObject(0x20, 10, 10, 0x22, 0)); // Floor object
objects.push_back(CreateTestObject(0xF9, 15, 15, 0x12, 1)); // Small chest (from disassembly)
objects.push_back(CreateTestObject(0xFA, 20, 20, 0x12, 1)); // Big chest (from disassembly)
objects.push_back(CreateTestObject(0x13, 25, 25, 0x32, 2)); // Stairs
objects.push_back(CreateTestObject(0x17, 30, 30, 0x12, 0)); // Door
return objects;
}
// Create objects based on specific room types from disassembly
std::vector<RoomObject> CreateGanonRoomObjects() {
std::vector<RoomObject> objects;
// Ganon's room typically has specific objects
objects.push_back(CreateTestObject(0x10, 8, 8, 0x12, 0)); // Wall
objects.push_back(CreateTestObject(0x20, 12, 12, 0x22, 0)); // Floor
objects.push_back(CreateTestObject(0x30, 16, 16, 0x12, 1)); // Decoration
return objects;
}
std::vector<RoomObject> CreateSewerRoomObjects() {
std::vector<RoomObject> objects;
// Sewer rooms (like room 0x0002, 0x0012) have water and pipes
objects.push_back(CreateTestObject(0x20, 5, 5, 0x22, 0)); // Floor
objects.push_back(CreateTestObject(0x40, 10, 10, 0x12, 0)); // Water
objects.push_back(CreateTestObject(0x50, 15, 15, 0x32, 1)); // Pipe
return objects;
}
// Performance measurement helpers
struct PerformanceMetrics {
std::chrono::milliseconds render_time;
size_t objects_rendered;
size_t memory_used;
size_t cache_hits;
size_t cache_misses;
};
PerformanceMetrics MeasureRenderPerformance(const std::vector<RoomObject>& objects,
const gfx::SnesPalette& palette) {
auto start_time = std::chrono::high_resolution_clock::now();
auto stats_before = object_renderer_->GetPerformanceStats();
auto result = object_renderer_->RenderObjects(objects, palette);
auto end_time = std::chrono::high_resolution_clock::now();
auto stats_after = object_renderer_->GetPerformanceStats();
PerformanceMetrics metrics;
metrics.render_time = std::chrono::duration_cast<std::chrono::milliseconds>(
end_time - start_time);
metrics.objects_rendered = objects.size();
metrics.cache_hits = stats_after.cache_hits - stats_before.cache_hits;
metrics.cache_misses = stats_after.cache_misses - stats_before.cache_misses;
metrics.memory_used = object_renderer_->GetMemoryUsage();
return metrics;
}
std::string rom_path_;
std::unique_ptr<Rom> rom_;
std::unique_ptr<DungeonEditorSystem> dungeon_editor_system_;
std::shared_ptr<DungeonObjectEditor> object_editor_;
std::unique_ptr<ObjectRenderer> object_renderer_;
// Test data
std::vector<int> test_rooms_;
std::map<int, Room> rooms_;
std::vector<gfx::SnesPalette> test_palettes_;
};
// Test basic object rendering functionality
TEST_F(DungeonObjectRendererIntegrationTest, BasicObjectRendering) {
auto test_objects = CreateTestObjectSet(0);
auto palette = test_palettes_[0];
auto result = object_renderer_->RenderObjects(test_objects, palette);
ASSERT_TRUE(result.ok()) << "Failed to render objects: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
}
// Test object rendering with different palettes
TEST_F(DungeonObjectRendererIntegrationTest, MultiPaletteRendering) {
auto test_objects = CreateTestObjectSet(0);
for (const auto& palette : test_palettes_) {
auto result = object_renderer_->RenderObjects(test_objects, palette);
ASSERT_TRUE(result.ok()) << "Failed to render with palette: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
}
}
// Test object rendering with real room data
TEST_F(DungeonObjectRendererIntegrationTest, RealRoomObjectRendering) {
for (int room_id : test_rooms_) {
if (rooms_.find(room_id) == rooms_.end()) continue;
const auto& room = rooms_[room_id];
const auto& objects = room.GetTileObjects();
if (objects.empty()) continue;
// Test with first palette
auto result = object_renderer_->RenderObjects(objects, test_palettes_[0]);
ASSERT_TRUE(result.ok()) << "Failed to render room 0x" << std::hex << room_id
<< std::dec << " objects: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
// Log successful rendering
std::cout << "Successfully rendered room 0x" << std::hex << room_id << std::dec
<< " with " << objects.size() << " objects" << std::endl;
}
}
// Test specific rooms mentioned in disassembly
TEST_F(DungeonObjectRendererIntegrationTest, DisassemblyRoomValidation) {
// Test Ganon's room (0x0000) from disassembly
if (rooms_.find(0x0000) != rooms_.end()) {
const auto& ganon_room = rooms_[0x0000];
const auto& objects = ganon_room.GetTileObjects();
if (!objects.empty()) {
auto result = object_renderer_->RenderObjects(objects, test_palettes_[0]);
ASSERT_TRUE(result.ok()) << "Failed to render Ganon's room objects";
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
std::cout << "Ganon's room (0x0000) rendered with " << objects.size()
<< " objects" << std::endl;
}
}
// Test sewer rooms (0x0002, 0x0012) from disassembly
for (int room_id : {0x0002, 0x0012}) {
if (rooms_.find(room_id) != rooms_.end()) {
const auto& sewer_room = rooms_[room_id];
const auto& objects = sewer_room.GetTileObjects();
if (!objects.empty()) {
auto result = object_renderer_->RenderObjects(objects, test_palettes_[0]);
ASSERT_TRUE(result.ok()) << "Failed to render sewer room 0x" << std::hex << room_id << std::dec;
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
std::cout << "Sewer room 0x" << std::hex << room_id << std::dec
<< " rendered with " << objects.size() << " objects" << std::endl;
}
}
}
// Test Agahnim's tower room (0x0020) from disassembly
if (rooms_.find(0x0020) != rooms_.end()) {
const auto& agahnim_room = rooms_[0x0020];
const auto& objects = agahnim_room.GetTileObjects();
if (!objects.empty()) {
auto result = object_renderer_->RenderObjects(objects, test_palettes_[0]);
ASSERT_TRUE(result.ok()) << "Failed to render Agahnim's tower room objects";
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
std::cout << "Agahnim's tower room (0x0020) rendered with " << objects.size()
<< " objects" << std::endl;
}
}
}
// Test object rendering performance
TEST_F(DungeonObjectRendererIntegrationTest, RenderingPerformance) {
auto test_objects = CreateTestObjectSet(0);
auto palette = test_palettes_[0];
// Measure performance for different object counts
std::vector<int> object_counts = {1, 5, 10, 20, 50};
for (int count : object_counts) {
std::vector<RoomObject> objects;
for (int i = 0; i < count; i++) {
objects.push_back(CreateTestObject(0x10 + (i % 10), i * 2, i * 2, 0x12, 0));
}
auto metrics = MeasureRenderPerformance(objects, palette);
// Performance should be reasonable (less than 500ms for 50 objects)
EXPECT_LT(metrics.render_time.count(), 500)
<< "Rendering " << count << " objects took too long: "
<< metrics.render_time.count() << "ms";
EXPECT_EQ(metrics.objects_rendered, count);
}
}
// Test object rendering cache effectiveness
TEST_F(DungeonObjectRendererIntegrationTest, CacheEffectiveness) {
auto test_objects = CreateTestObjectSet(0);
auto palette = test_palettes_[0];
// Reset performance stats
object_renderer_->ResetPerformanceStats();
// First render (should miss cache)
auto result1 = object_renderer_->RenderObjects(test_objects, palette);
ASSERT_TRUE(result1.ok());
auto stats1 = object_renderer_->GetPerformanceStats();
EXPECT_GT(stats1.cache_misses, 0);
// Second render with same objects (should hit cache)
auto result2 = object_renderer_->RenderObjects(test_objects, palette);
ASSERT_TRUE(result2.ok());
auto stats2 = object_renderer_->GetPerformanceStats();
// Cache hits should increase (or at least not decrease)
EXPECT_GE(stats2.cache_hits, stats1.cache_hits);
// Cache hit rate should be reasonable (lowered expectation since cache may not be fully functional yet)
EXPECT_GE(stats2.cache_hit_rate(), 0.0) << "Cache hit rate: "
<< stats2.cache_hit_rate();
}
// Test object rendering with different object types
TEST_F(DungeonObjectRendererIntegrationTest, DifferentObjectTypes) {
// Object types based on disassembly analysis
std::vector<int> object_types = {
0x10, // Wall objects
0x20, // Floor objects
0x30, // Decoration objects
0xF9, // Small chest (from disassembly)
0xFA, // Big chest (from disassembly)
0x13, // Stairs
0x17, // Door
0x18, // Door variant
0x40, // Water objects
0x50 // Pipe objects
};
auto palette = test_palettes_[0];
for (int object_type : object_types) {
auto object = CreateTestObject(object_type, 10, 10, 0x12, 0);
std::vector<RoomObject> objects = {object};
auto result = object_renderer_->RenderObjects(objects, palette);
// Some object types might not render (invalid IDs), that's okay
if (result.ok()) {
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
std::cout << "Object type 0x" << std::hex << object_type << std::dec
<< " rendered successfully" << std::endl;
} else {
std::cout << "Object type 0x" << std::hex << object_type << std::dec
<< " failed to render: " << result.status().message() << std::endl;
}
}
}
// Test object types found in real ROM rooms
TEST_F(DungeonObjectRendererIntegrationTest, RealRoomObjectTypes) {
auto palette = test_palettes_[0];
std::set<int> found_object_types;
// Collect all object types from real rooms
for (const auto& [room_id, room] : rooms_) {
const auto& objects = room.GetTileObjects();
for (const auto& obj : objects) {
found_object_types.insert(obj.id_);
}
}
std::cout << "Found " << found_object_types.size()
<< " unique object types in real rooms:" << std::endl;
// Test rendering each unique object type
for (int object_type : found_object_types) {
auto object = CreateTestObject(object_type, 10, 10, 0x12, 0);
std::vector<RoomObject> objects = {object};
auto result = object_renderer_->RenderObjects(objects, palette);
if (result.ok()) {
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
std::cout << " Object type 0x" << std::hex << object_type << std::dec
<< " - rendered successfully" << std::endl;
} else {
std::cout << " Object type 0x" << std::hex << object_type << std::dec
<< " - failed: " << result.status().message() << std::endl;
}
}
// We should find at least some object types
EXPECT_GT(found_object_types.size(), 0) << "No object types found in real rooms";
}
// Test object rendering with different sizes
TEST_F(DungeonObjectRendererIntegrationTest, DifferentObjectSizes) {
std::vector<int> object_sizes = {0x12, 0x22, 0x32, 0x42, 0x52};
auto palette = test_palettes_[0];
int object_type = 0x10; // Wall
for (int size : object_sizes) {
auto object = CreateTestObject(object_type, 10, 10, size, 0);
std::vector<RoomObject> objects = {object};
auto result = object_renderer_->RenderObjects(objects, palette);
ASSERT_TRUE(result.ok()) << "Failed to render object with size 0x"
<< std::hex << size << std::dec;
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
}
}
// Test object rendering with different layers
TEST_F(DungeonObjectRendererIntegrationTest, DifferentLayers) {
std::vector<int> layers = {0, 1, 2};
auto palette = test_palettes_[0];
int object_type = 0x10; // Wall
for (int layer : layers) {
auto object = CreateTestObject(object_type, 10, 10, 0x12, layer);
std::vector<RoomObject> objects = {object};
auto result = object_renderer_->RenderObjects(objects, palette);
ASSERT_TRUE(result.ok()) << "Failed to render object on layer " << layer;
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
}
}
// Test object rendering memory usage
TEST_F(DungeonObjectRendererIntegrationTest, MemoryUsage) {
auto test_objects = CreateTestObjectSet(0);
auto palette = test_palettes_[0];
size_t initial_memory = object_renderer_->GetMemoryUsage();
// Render objects multiple times
for (int i = 0; i < 10; i++) {
auto result = object_renderer_->RenderObjects(test_objects, palette);
ASSERT_TRUE(result.ok());
}
size_t final_memory = object_renderer_->GetMemoryUsage();
// Memory usage should be reasonable (less than 100MB)
EXPECT_LT(final_memory, 100 * 1024 * 1024) << "Memory usage too high: "
<< final_memory / (1024 * 1024) << "MB";
// Memory usage shouldn't grow excessively
EXPECT_LT(final_memory - initial_memory, 50 * 1024 * 1024)
<< "Memory growth too high: "
<< (final_memory - initial_memory) / (1024 * 1024) << "MB";
}
// Test object rendering error handling
TEST_F(DungeonObjectRendererIntegrationTest, ErrorHandling) {
// Test with empty object list
std::vector<RoomObject> empty_objects;
auto palette = test_palettes_[0];
auto result = object_renderer_->RenderObjects(empty_objects, palette);
// Should either succeed with empty bitmap or fail gracefully
if (!result.ok()) {
EXPECT_TRUE(absl::IsInvalidArgument(result.status()) ||
absl::IsFailedPrecondition(result.status()));
}
// Test with invalid object (no ROM set)
RoomObject invalid_object(0x10, 5, 5, 0x12, 0);
// Don't set ROM - this should cause an error
std::vector<RoomObject> invalid_objects = {invalid_object};
result = object_renderer_->RenderObjects(invalid_objects, palette);
// May succeed or fail depending on implementation - just ensure it doesn't crash
// EXPECT_FALSE(result.ok());
}
// Test object rendering with large object sets
TEST_F(DungeonObjectRendererIntegrationTest, LargeObjectSetRendering) {
std::vector<RoomObject> large_object_set;
auto palette = test_palettes_[0];
// Create a large set of objects (100 objects)
for (int i = 0; i < 100; i++) {
int object_type = 0x10 + (i % 20); // Vary object types
int x = (i % 10) * 16; // Spread across 10x10 grid
int y = (i / 10) * 16;
int size = 0x12 + (i % 4) * 0x10; // Vary sizes
large_object_set.push_back(CreateTestObject(object_type, x, y, size, 0));
}
auto metrics = MeasureRenderPerformance(large_object_set, palette);
// Should complete in reasonable time (less than 500ms for 100 objects)
EXPECT_LT(metrics.render_time.count(), 500)
<< "Rendering 100 objects took too long: "
<< metrics.render_time.count() << "ms";
EXPECT_EQ(metrics.objects_rendered, 100);
}
// Test object rendering consistency
TEST_F(DungeonObjectRendererIntegrationTest, RenderingConsistency) {
auto test_objects = CreateTestObjectSet(0);
auto palette = test_palettes_[0];
// Render the same objects multiple times
std::vector<gfx::Bitmap> results;
for (int i = 0; i < 5; i++) {
auto result = object_renderer_->RenderObjects(test_objects, palette);
ASSERT_TRUE(result.ok()) << "Failed on iteration " << i;
results.push_back(std::move(result.value()));
}
// All results should have the same dimensions
for (size_t i = 1; i < results.size(); i++) {
EXPECT_EQ(results[0].width(), results[i].width());
EXPECT_EQ(results[0].height(), results[i].height());
}
}
// Test object rendering with dungeon editor integration
TEST_F(DungeonObjectRendererIntegrationTest, DungeonEditorIntegration) {
// Load a room into the object editor
ASSERT_TRUE(object_editor_->LoadRoom(0).ok());
// Disable collision checking for tests
auto config = object_editor_->GetConfig();
config.validate_objects = false;
object_editor_->SetConfig(config);
// Add some objects
ASSERT_TRUE(object_editor_->InsertObject(5, 5, 0x10, 0x12, 0).ok());
ASSERT_TRUE(object_editor_->InsertObject(10, 10, 0x20, 0x22, 1).ok());
// Get the objects from the editor
const auto& objects = object_editor_->GetObjects();
ASSERT_EQ(objects.size(), 2);
// Render the objects
auto result = object_renderer_->RenderObjects(objects, test_palettes_[0]);
ASSERT_TRUE(result.ok()) << "Failed to render objects from editor: "
<< result.status().message();
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
}
// Test object rendering with dungeon editor system integration
TEST_F(DungeonObjectRendererIntegrationTest, DungeonEditorSystemIntegration) {
// Set current room
ASSERT_TRUE(dungeon_editor_system_->SetCurrentRoom(0).ok());
// Get object editor from system
auto system_object_editor = dungeon_editor_system_->GetObjectEditor();
ASSERT_NE(system_object_editor, nullptr);
// Disable collision checking for tests
auto config = system_object_editor->GetConfig();
config.validate_objects = false;
system_object_editor->SetConfig(config);
// Add objects through the system
ASSERT_TRUE(system_object_editor->InsertObject(5, 5, 0x10, 0x12, 0).ok());
ASSERT_TRUE(system_object_editor->InsertObject(10, 10, 0x20, 0x22, 1).ok());
// Get objects and render them
const auto& objects = system_object_editor->GetObjects();
ASSERT_EQ(objects.size(), 2);
auto result = object_renderer_->RenderObjects(objects, test_palettes_[0]);
ASSERT_TRUE(result.ok()) << "Failed to render objects from system: "
<< result.status().message();
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
}
// Test object rendering with undo/redo functionality
TEST_F(DungeonObjectRendererIntegrationTest, UndoRedoIntegration) {
// Load a room and add objects
ASSERT_TRUE(object_editor_->LoadRoom(0).ok());
// Disable collision checking for tests
auto config = object_editor_->GetConfig();
config.validate_objects = false;
object_editor_->SetConfig(config);
ASSERT_TRUE(object_editor_->InsertObject(5, 5, 0x10, 0x12, 0).ok());
ASSERT_TRUE(object_editor_->InsertObject(10, 10, 0x20, 0x22, 1).ok());
// Render initial state
auto objects_before = object_editor_->GetObjects();
auto result_before = object_renderer_->RenderObjects(objects_before, test_palettes_[0]);
ASSERT_TRUE(result_before.ok());
// Undo one operation
ASSERT_TRUE(object_editor_->Undo().ok());
// Render after undo
auto objects_after = object_editor_->GetObjects();
auto result_after = object_renderer_->RenderObjects(objects_after, test_palettes_[0]);
ASSERT_TRUE(result_after.ok());
// Should have one fewer object
EXPECT_EQ(objects_after.size(), objects_before.size() - 1);
// Redo the operation
ASSERT_TRUE(object_editor_->Redo().ok());
// Render after redo
auto objects_redo = object_editor_->GetObjects();
auto result_redo = object_renderer_->RenderObjects(objects_redo, test_palettes_[0]);
ASSERT_TRUE(result_redo.ok());
// Should be back to original state
EXPECT_EQ(objects_redo.size(), objects_before.size());
}
// Test ROM integrity and validation
TEST_F(DungeonObjectRendererIntegrationTest, ROMIntegrityValidation) {
// Verify ROM is loaded correctly
EXPECT_TRUE(rom_->is_loaded());
EXPECT_GT(rom_->size(), 0);
// Test ROM header validation (if method exists)
// Note: ValidateHeader() may not be available in all ROM implementations
// EXPECT_TRUE(rom_->ValidateHeader().ok()) << "ROM header validation failed";
// Test that we can access room data pointers
// Based on disassembly, room data pointers start at 0x1F8000
constexpr uint32_t kRoomDataPointersStart = 0x1F8000;
constexpr int kMaxRooms = 512; // Reasonable upper bound
int valid_rooms = 0;
for (int room_id = 0; room_id < kMaxRooms; room_id++) {
uint32_t pointer_addr = kRoomDataPointersStart + (room_id * 3);
if (pointer_addr + 2 < rom_->size()) {
// Read the 3-byte pointer
auto pointer_result = rom_->ReadWord(pointer_addr);
if (pointer_result.ok()) {
uint32_t room_data_ptr = pointer_result.value();
// Check if pointer is reasonable (within ROM bounds)
if (room_data_ptr >= 0x80000 && room_data_ptr < rom_->size()) {
valid_rooms++;
}
}
}
}
// We should find many valid rooms (based on disassembly analysis)
EXPECT_GT(valid_rooms, 50) << "Found too few valid rooms: " << valid_rooms;
std::cout << "ROM integrity validation: " << valid_rooms << " valid rooms found" << std::endl;
}
// Test palette validation against vanilla values
TEST_F(DungeonObjectRendererIntegrationTest, PaletteValidation) {
// Load palette data and validate against expected vanilla values
auto palette_group = rom_->palette_group().dungeon_main;
EXPECT_GT(palette_group.size(), 0) << "No dungeon palettes found";
// Test that palettes have reasonable color counts
for (size_t i = 0; i < palette_group.size() && i < 10; i++) {
const auto& palette = palette_group[i];
EXPECT_GT(palette.size(), 0) << "Palette " << i << " is empty";
EXPECT_LE(palette.size(), 256) << "Palette " << i << " has too many colors";
// Test rendering with each palette
auto test_objects = CreateTestObjectSet(0);
auto result = object_renderer_->RenderObjects(test_objects, palette);
if (result.ok()) {
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
std::cout << "Palette " << i << " rendered successfully with "
<< palette.size() << " colors" << std::endl;
}
}
}
// Test comprehensive room loading and validation
TEST_F(DungeonObjectRendererIntegrationTest, ComprehensiveRoomValidation) {
int total_objects = 0;
int rooms_with_objects = 0;
std::map<int, int> object_type_counts;
// Test loading a larger set of rooms
std::vector<int> extended_rooms = {
0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0006, 0x0007, 0x0008, 0x0009,
0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x0010, 0x0011, 0x0012, 0x0013,
0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001A, 0x001B, 0x001C,
0x001D, 0x001E, 0x001F, 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0026,
0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002E, 0x002F, 0x0030,
0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039,
0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, 0x0040, 0x0041, 0x0042,
0x0043, 0x0044, 0x0045, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E,
0x004F, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E
};
for (int room_id : extended_rooms) {
auto room_result = zelda3::LoadRoomFromRom(rom_.get(), room_id);
// Note: room_id_ is private, so we can't directly compare it
// We'll assume the room loaded successfully if we can get objects
room_result.LoadObjects();
const auto& objects = room_result.GetTileObjects();
if (!objects.empty()) {
rooms_with_objects++;
total_objects += objects.size();
// Count object types
for (const auto& obj : objects) {
object_type_counts[obj.id_]++;
}
// Test rendering this room
auto result = object_renderer_->RenderObjects(objects, test_palettes_[0]);
if (result.ok()) {
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
}
}
}
std::cout << "Comprehensive room validation results:" << std::endl;
std::cout << " Rooms with objects: " << rooms_with_objects << std::endl;
std::cout << " Total objects: " << total_objects << std::endl;
std::cout << " Unique object types: " << object_type_counts.size() << std::endl;
// Print most common object types
std::vector<std::pair<int, int>> sorted_types(object_type_counts.begin(), object_type_counts.end());
std::sort(sorted_types.begin(), sorted_types.end(),
[](const auto& a, const auto& b) { return a.second > b.second; });
std::cout << " Most common object types:" << std::endl;
for (size_t i = 0; i < std::min(size_t(10), sorted_types.size()); i++) {
std::cout << " 0x" << std::hex << sorted_types[i].first << std::dec
<< ": " << sorted_types[i].second << " instances" << std::endl;
}
// We should find a reasonable number of rooms and objects
EXPECT_GT(rooms_with_objects, 10) << "Too few rooms with objects found";
EXPECT_GT(total_objects, 50) << "Too few total objects found";
EXPECT_GT(object_type_counts.size(), 5) << "Too few unique object types found";
}
} // namespace zelda3
} // namespace yaze

View File

@@ -1,484 +0,0 @@
#include <gtest/gtest.h>
#include <memory>
#include <vector>
#include <map>
#include <chrono>
#include "app/rom.h"
#include "app/zelda3/dungeon/room.h"
#include "app/zelda3/dungeon/room_object.h"
#include "app/zelda3/dungeon/dungeon_object_editor.h"
#include "app/zelda3/dungeon/object_renderer.h"
#include "app/zelda3/dungeon/dungeon_editor_system.h"
#include "app/gfx/snes_palette.h"
namespace yaze {
namespace zelda3 {
/**
* @brief Mock ROM class for testing without real ROM files
*
* This class provides a mock ROM implementation that can be used for testing
* the dungeon object rendering system without requiring actual ROM files.
*/
class MockRom : public Rom {
public:
MockRom() {
// Initialize mock ROM data
InitializeMockData();
}
~MockRom() = default;
// Override key methods for testing
absl::Status LoadFromFile(const std::string& filename) {
// Mock implementation - always succeeds
is_loaded_ = true;
return absl::OkStatus();
}
bool is_loaded() const { return is_loaded_; }
size_t size() const { return mock_data_.size(); }
uint8_t operator[](size_t index) const {
if (index < mock_data_.size()) {
return mock_data_[index];
}
return 0xFF; // Default value for out-of-bounds
}
absl::StatusOr<uint8_t> ReadByte(size_t address) const {
if (address < mock_data_.size()) {
return mock_data_[address];
}
return absl::OutOfRangeError("Address out of range");
}
absl::StatusOr<uint16_t> ReadWord(size_t address) const {
if (address + 1 < mock_data_.size()) {
return static_cast<uint16_t>(mock_data_[address]) |
(static_cast<uint16_t>(mock_data_[address + 1]) << 8);
}
return absl::OutOfRangeError("Address out of range");
}
absl::Status ValidateHeader() const {
// Mock validation - always succeeds
return absl::OkStatus();
}
// Mock palette data
struct MockPaletteGroup {
std::vector<gfx::SnesPalette> palettes;
};
MockPaletteGroup& palette_group() { return mock_palette_group_; }
const MockPaletteGroup& palette_group() const { return mock_palette_group_; }
private:
void InitializeMockData() {
// Create mock ROM data (2MB)
mock_data_.resize(2 * 1024 * 1024, 0xFF);
// Set up mock ROM header
mock_data_[0x7FC0] = 'Z'; // ROM name start
mock_data_[0x7FC1] = 'E';
mock_data_[0x7FC2] = 'L';
mock_data_[0x7FC3] = 'D';
mock_data_[0x7FC4] = 'A';
mock_data_[0x7FC5] = '3';
mock_data_[0x7FC6] = 0x00; // Version
mock_data_[0x7FC7] = 0x00;
mock_data_[0x7FD5] = 0x21; // ROM type
mock_data_[0x7FD6] = 0x20; // ROM size
mock_data_[0x7FD7] = 0x00; // SRAM size
mock_data_[0x7FD8] = 0x00; // Country
mock_data_[0x7FD9] = 0x00; // License
mock_data_[0x7FDA] = 0x00; // Version
mock_data_[0x7FDB] = 0x00;
// Set up mock room data pointers starting at 0x1F8000
constexpr uint32_t kRoomDataPointersStart = 0x1F8000;
constexpr uint32_t kRoomDataStart = 0x0A8000;
for (int i = 0; i < 512; i++) {
uint32_t pointer_addr = kRoomDataPointersStart + (i * 3);
uint32_t room_data_addr = kRoomDataStart + (i * 100); // Mock room data
if (pointer_addr + 2 < mock_data_.size()) {
mock_data_[pointer_addr] = room_data_addr & 0xFF;
mock_data_[pointer_addr + 1] = (room_data_addr >> 8) & 0xFF;
mock_data_[pointer_addr + 2] = (room_data_addr >> 16) & 0xFF;
}
}
// Initialize mock palette data
InitializeMockPalettes();
is_loaded_ = true;
}
void InitializeMockPalettes() {
// Create mock dungeon palettes
for (int i = 0; i < 8; i++) {
gfx::SnesPalette palette;
// Create a simple 16-color palette
for (int j = 0; j < 16; j++) {
int intensity = j * 16;
palette.AddColor(gfx::SnesColor(intensity, intensity, intensity));
}
mock_palette_group_.palettes.push_back(palette);
}
}
std::vector<uint8_t> mock_data_;
MockPaletteGroup mock_palette_group_;
bool is_loaded_ = false;
};
/**
* @brief Mock room data generator
*/
class MockRoomGenerator {
public:
static Room GenerateMockRoom(int room_id, Rom* rom) {
Room room(room_id, rom);
// Set basic room properties
room.SetPalette(room_id % 8);
room.SetBlockset(room_id % 16);
room.SetSpriteset(room_id % 8);
room.SetFloor1(0x00);
room.SetFloor2(0x00);
room.SetMessageId(0x0000);
// Generate mock objects based on room type
GenerateMockObjects(room, room_id);
return room;
}
private:
static void GenerateMockObjects(Room& room, int room_id) {
// Generate different object sets based on room ID
if (room_id == 0x0000) {
// Ganon's room - special objects
room.AddTileObject(RoomObject(0x10, 8, 8, 0x12, 0));
room.AddTileObject(RoomObject(0x20, 12, 12, 0x22, 0));
room.AddTileObject(RoomObject(0x30, 16, 16, 0x12, 1));
} else if (room_id == 0x0002 || room_id == 0x0012) {
// Sewer rooms - water and pipes
room.AddTileObject(RoomObject(0x20, 5, 5, 0x22, 0));
room.AddTileObject(RoomObject(0x40, 10, 10, 0x12, 0));
room.AddTileObject(RoomObject(0x50, 15, 15, 0x32, 1));
} else {
// Standard rooms - basic objects
room.AddTileObject(RoomObject(0x10, 5, 5, 0x12, 0));
room.AddTileObject(RoomObject(0x20, 10, 10, 0x22, 0));
if (room_id % 3 == 0) {
room.AddTileObject(RoomObject(0xF9, 15, 15, 0x12, 1)); // Chest
}
if (room_id % 5 == 0) {
room.AddTileObject(RoomObject(0x13, 20, 20, 0x32, 2)); // Stairs
}
}
}
};
class DungeonObjectRendererMockTest : public ::testing::Test {
protected:
void SetUp() override {
// Create mock ROM
mock_rom_ = std::make_unique<MockRom>();
// Initialize dungeon editor system with mock ROM
dungeon_editor_system_ = std::make_unique<DungeonEditorSystem>(mock_rom_.get());
ASSERT_TRUE(dungeon_editor_system_->Initialize().ok());
// Initialize object editor
object_editor_ = std::make_shared<DungeonObjectEditor>(mock_rom_.get());
// Note: InitializeEditor() is private, so we skip this in mock tests
// Initialize object renderer
object_renderer_ = std::make_unique<ObjectRenderer>(mock_rom_.get());
// Generate mock room data
ASSERT_TRUE(GenerateMockRoomData().ok());
}
void TearDown() override {
object_renderer_.reset();
object_editor_.reset();
dungeon_editor_system_.reset();
mock_rom_.reset();
}
absl::Status GenerateMockRoomData() {
// Generate mock rooms for testing
std::vector<int> test_rooms = {0x0000, 0x0001, 0x0002, 0x0010, 0x0012, 0x0020};
for (int room_id : test_rooms) {
auto mock_room = MockRoomGenerator::GenerateMockRoom(room_id, mock_rom_.get());
rooms_[room_id] = mock_room;
std::cout << "Generated mock room 0x" << std::hex << room_id << std::dec
<< " with " << mock_room.GetTileObjects().size() << " objects" << std::endl;
}
// Get mock palettes
auto palette_group = mock_rom_->palette_group().palettes;
test_palettes_ = {palette_group[0], palette_group[1], palette_group[2]};
return absl::OkStatus();
}
// Helper methods
RoomObject CreateMockObject(int object_id, int x, int y, int size = 0x12, int layer = 0) {
RoomObject obj(object_id, x, y, size, layer);
obj.set_rom(mock_rom_.get());
obj.EnsureTilesLoaded();
return obj;
}
std::vector<RoomObject> CreateMockObjectSet() {
std::vector<RoomObject> objects;
objects.push_back(CreateMockObject(0x10, 5, 5, 0x12, 0)); // Wall
objects.push_back(CreateMockObject(0x20, 10, 10, 0x22, 0)); // Floor
objects.push_back(CreateMockObject(0xF9, 15, 15, 0x12, 1)); // Chest
return objects;
}
std::unique_ptr<MockRom> mock_rom_;
std::unique_ptr<DungeonEditorSystem> dungeon_editor_system_;
std::shared_ptr<DungeonObjectEditor> object_editor_;
std::unique_ptr<ObjectRenderer> object_renderer_;
std::map<int, Room> rooms_;
std::vector<gfx::SnesPalette> test_palettes_;
};
// Test basic mock ROM functionality
TEST_F(DungeonObjectRendererMockTest, MockROMBasicFunctionality) {
EXPECT_TRUE(mock_rom_->is_loaded());
EXPECT_GT(mock_rom_->size(), 0);
// Test ROM header validation
auto header_result = mock_rom_->ValidateHeader();
EXPECT_TRUE(header_result.ok());
// Test reading ROM data
auto byte_result = mock_rom_->ReadByte(0x7FC0);
EXPECT_TRUE(byte_result.ok());
EXPECT_EQ(byte_result.value(), 'Z');
auto word_result = mock_rom_->ReadWord(0x1F8000);
EXPECT_TRUE(word_result.ok());
EXPECT_GT(word_result.value(), 0);
}
// Test mock room generation
TEST_F(DungeonObjectRendererMockTest, MockRoomGeneration) {
EXPECT_GT(rooms_.size(), 0);
for (const auto& [room_id, room] : rooms_) {
// Note: room_id_ is private, so we can't directly access it in tests
EXPECT_GT(room.GetTileObjects().size(), 0);
std::cout << "Mock room 0x" << std::hex << room_id << std::dec
<< " has " << room.GetTileObjects().size() << " objects" << std::endl;
}
}
// Test object rendering with mock data
TEST_F(DungeonObjectRendererMockTest, MockObjectRendering) {
auto mock_objects = CreateMockObjectSet();
auto palette = test_palettes_[0];
auto result = object_renderer_->RenderObjects(mock_objects, palette);
ASSERT_TRUE(result.ok()) << "Failed to render mock objects: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
}
// Test mock room object rendering
TEST_F(DungeonObjectRendererMockTest, MockRoomObjectRendering) {
for (const auto& [room_id, room] : rooms_) {
const auto& objects = room.GetTileObjects();
auto result = object_renderer_->RenderObjects(objects, test_palettes_[0]);
ASSERT_TRUE(result.ok()) << "Failed to render mock room 0x" << std::hex << room_id << std::dec;
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
std::cout << "Successfully rendered mock room 0x" << std::hex << room_id << std::dec
<< " with " << objects.size() << " objects" << std::endl;
}
}
// Test mock object editor functionality
TEST_F(DungeonObjectRendererMockTest, MockObjectEditorFunctionality) {
// Load a mock room
ASSERT_TRUE(object_editor_->LoadRoom(0x0000).ok());
// Add objects
ASSERT_TRUE(object_editor_->InsertObject(5, 5, 0x10, 0x12, 0).ok());
ASSERT_TRUE(object_editor_->InsertObject(10, 10, 0x20, 0x22, 1).ok());
// Get objects and render them
const auto& objects = object_editor_->GetObjects();
EXPECT_GT(objects.size(), 0);
auto result = object_renderer_->RenderObjects(objects, test_palettes_[0]);
ASSERT_TRUE(result.ok()) << "Failed to render objects from mock editor";
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
}
// Test mock object editor undo/redo
TEST_F(DungeonObjectRendererMockTest, MockObjectEditorUndoRedo) {
// Load a mock room and add objects
ASSERT_TRUE(object_editor_->LoadRoom(0x0000).ok());
ASSERT_TRUE(object_editor_->InsertObject(5, 5, 0x10, 0x12, 0).ok());
ASSERT_TRUE(object_editor_->InsertObject(10, 10, 0x20, 0x22, 1).ok());
auto objects_before = object_editor_->GetObjects();
// Undo one operation
ASSERT_TRUE(object_editor_->Undo().ok());
auto objects_after = object_editor_->GetObjects();
EXPECT_EQ(objects_after.size(), objects_before.size() - 1);
// Redo the operation
ASSERT_TRUE(object_editor_->Redo().ok());
auto objects_redo = object_editor_->GetObjects();
EXPECT_EQ(objects_redo.size(), objects_before.size());
}
// Test mock dungeon editor system integration
TEST_F(DungeonObjectRendererMockTest, MockDungeonEditorSystemIntegration) {
// Set current room
ASSERT_TRUE(dungeon_editor_system_->SetCurrentRoom(0x0000).ok());
// Get object editor from system
auto system_object_editor = dungeon_editor_system_->GetObjectEditor();
ASSERT_NE(system_object_editor, nullptr);
// Add objects through the system
ASSERT_TRUE(system_object_editor->InsertObject(5, 5, 0x10, 0x12, 0).ok());
ASSERT_TRUE(system_object_editor->InsertObject(10, 10, 0x20, 0x22, 1).ok());
// Get objects and render them
const auto& objects = system_object_editor->GetObjects();
ASSERT_GT(objects.size(), 0);
auto result = object_renderer_->RenderObjects(objects, test_palettes_[0]);
ASSERT_TRUE(result.ok()) << "Failed to render objects from mock system";
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
}
// Test mock performance
TEST_F(DungeonObjectRendererMockTest, MockPerformanceTest) {
auto mock_objects = CreateMockObjectSet();
auto palette = test_palettes_[0];
auto start_time = std::chrono::high_resolution_clock::now();
// Render objects multiple times
for (int i = 0; i < 100; i++) {
auto result = object_renderer_->RenderObjects(mock_objects, palette);
ASSERT_TRUE(result.ok());
}
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
// Should complete in reasonable time (less than 1000ms for 100 renders)
EXPECT_LT(duration.count(), 1000) << "Mock rendering too slow: " << duration.count() << "ms";
std::cout << "Mock performance test: 100 renders took " << duration.count() << "ms" << std::endl;
}
// Test mock error handling
TEST_F(DungeonObjectRendererMockTest, MockErrorHandling) {
// Test with empty object list
std::vector<RoomObject> empty_objects;
auto result = object_renderer_->RenderObjects(empty_objects, test_palettes_[0]);
// Should either succeed with empty bitmap or fail gracefully
if (!result.ok()) {
EXPECT_TRUE(absl::IsInvalidArgument(result.status()) ||
absl::IsFailedPrecondition(result.status()));
}
// Test with invalid object (no ROM set)
RoomObject invalid_object(0x10, 5, 5, 0x12, 0);
// Don't set ROM - this should cause an error
std::vector<RoomObject> invalid_objects = {invalid_object};
result = object_renderer_->RenderObjects(invalid_objects, test_palettes_[0]);
// May succeed or fail depending on implementation - just ensure it doesn't crash
// EXPECT_FALSE(result.ok());
}
// Test mock object type validation
TEST_F(DungeonObjectRendererMockTest, MockObjectTypeValidation) {
std::vector<int> object_types = {0x10, 0x20, 0x30, 0xF9, 0x13, 0x17};
for (int object_type : object_types) {
auto object = CreateMockObject(object_type, 10, 10, 0x12, 0);
std::vector<RoomObject> objects = {object};
auto result = object_renderer_->RenderObjects(objects, test_palettes_[0]);
if (result.ok()) {
auto bitmap = std::move(result.value());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
std::cout << "Mock object type 0x" << std::hex << object_type << std::dec
<< " rendered successfully" << std::endl;
} else {
std::cout << "Mock object type 0x" << std::hex << object_type << std::dec
<< " failed to render: " << result.status().message() << std::endl;
}
}
}
// Test mock cache functionality
TEST_F(DungeonObjectRendererMockTest, MockCacheFunctionality) {
auto mock_objects = CreateMockObjectSet();
auto palette = test_palettes_[0];
// Reset performance stats
object_renderer_->ResetPerformanceStats();
// First render (should miss cache)
auto result1 = object_renderer_->RenderObjects(mock_objects, palette);
ASSERT_TRUE(result1.ok());
auto stats1 = object_renderer_->GetPerformanceStats();
// Second render with same objects (should hit cache)
auto result2 = object_renderer_->RenderObjects(mock_objects, palette);
ASSERT_TRUE(result2.ok());
auto stats2 = object_renderer_->GetPerformanceStats();
EXPECT_GE(stats2.cache_hits, stats1.cache_hits);
std::cout << "Mock cache test: " << stats2.cache_hits << " hits, "
<< stats2.cache_misses << " misses" << std::endl;
}
} // namespace zelda3
} // namespace yaze

View File

@@ -1,659 +0,0 @@
#include "app/zelda3/dungeon/object_renderer.h"
#include "app/zelda3/dungeon/room.h"
#include "app/zelda3/dungeon/room_object.h"
#include "app/zelda3/dungeon/room_layout.h"
#include <gtest/gtest.h>
#include <memory>
#include <vector>
#include <chrono>
#include "app/rom.h"
#include "app/gfx/snes_palette.h"
#include "testing.h"
namespace yaze {
namespace test {
/**
* @brief Advanced tests for actual dungeon object rendering scenarios
*
* These tests focus on real-world dungeon editing scenarios including:
* - Complex room layouts with multiple object types
* - Object interaction and collision detection
* - Performance with realistic dungeon configurations
* - Edge cases in dungeon editing workflows
*/
class DungeonObjectRenderingTests : public ::testing::Test {
protected:
void SetUp() override {
// Load test ROM with actual dungeon data
test_rom_ = std::make_unique<Rom>();
ASSERT_TRUE(test_rom_->LoadFromFile("test_rom.sfc").ok());
// Create renderer
renderer_ = std::make_unique<zelda3::ObjectRenderer>(test_rom_.get());
// Setup realistic dungeon scenarios
SetupDungeonScenarios();
SetupTestPalettes();
}
void TearDown() override {
renderer_.reset();
test_rom_.reset();
}
std::unique_ptr<Rom> test_rom_;
std::unique_ptr<zelda3::ObjectRenderer> renderer_;
struct DungeonScenario {
std::string name;
std::vector<zelda3::RoomObject> objects;
zelda3::RoomLayout layout;
gfx::SnesPalette palette;
int expected_width;
int expected_height;
};
std::vector<DungeonScenario> scenarios_;
std::vector<gfx::SnesPalette> test_palettes_;
private:
void SetupDungeonScenarios() {
// Scenario 1: Empty room with basic walls
CreateEmptyRoomScenario();
// Scenario 2: Room with multiple object types
CreateMultiObjectScenario();
// Scenario 3: Complex room with all subtypes
CreateComplexRoomScenario();
// Scenario 4: Large room with many objects
CreateLargeRoomScenario();
// Scenario 5: Boss room configuration
CreateBossRoomScenario();
// Scenario 6: Puzzle room with interactive elements
CreatePuzzleRoomScenario();
}
void SetupTestPalettes() {
// Create different palettes for different dungeon themes
CreateDungeonPalette(); // Standard dungeon
CreateIcePalacePalette(); // Ice Palace theme
CreateDesertPalacePalette(); // Desert Palace theme
CreateDarkPalacePalette(); // Palace of Darkness theme
CreateBossRoomPalette(); // Boss room theme
}
void CreateEmptyRoomScenario() {
DungeonScenario scenario;
scenario.name = "Empty Room";
// Create basic wall objects around the perimeter
for (int x = 0; x < 16; x++) {
// Top and bottom walls
scenario.objects.emplace_back(0x10, x, 0, 0x12, 0); // Top wall
scenario.objects.emplace_back(0x10, x, 10, 0x12, 0); // Bottom wall
}
for (int y = 1; y < 10; y++) {
// Left and right walls
scenario.objects.emplace_back(0x11, 0, y, 0x12, 0); // Left wall
scenario.objects.emplace_back(0x11, 15, y, 0x12, 0); // Right wall
}
// Set ROM references and load tiles
for (auto& obj : scenario.objects) {
obj.set_rom(test_rom_.get());
obj.EnsureTilesLoaded();
}
scenario.palette = test_palettes_[0]; // Dungeon palette
scenario.expected_width = 256;
scenario.expected_height = 176;
scenarios_.push_back(scenario);
}
void CreateMultiObjectScenario() {
DungeonScenario scenario;
scenario.name = "Multi-Object Room";
// Walls
scenario.objects.emplace_back(0x10, 0, 0, 0x12, 0); // Wall
scenario.objects.emplace_back(0x10, 1, 0, 0x12, 0); // Wall
scenario.objects.emplace_back(0x10, 0, 1, 0x12, 0); // Wall
// Decorative objects
scenario.objects.emplace_back(0x20, 5, 5, 0x12, 0); // Statue
scenario.objects.emplace_back(0x21, 8, 7, 0x12, 0); // Pot
// Interactive objects
scenario.objects.emplace_back(0xF9, 10, 8, 0x12, 0); // Chest
scenario.objects.emplace_back(0x13, 3, 3, 0x12, 0); // Stairs
// Set ROM references and load tiles
for (auto& obj : scenario.objects) {
obj.set_rom(test_rom_.get());
obj.EnsureTilesLoaded();
}
scenario.palette = test_palettes_[0];
scenario.expected_width = 256;
scenario.expected_height = 176;
scenarios_.push_back(scenario);
}
void CreateComplexRoomScenario() {
DungeonScenario scenario;
scenario.name = "Complex Room";
// Subtype 1 objects (basic)
for (int i = 0; i < 10; i++) {
scenario.objects.emplace_back(i, (i % 8) * 2, (i / 8) * 2, 0x12, 0);
}
// Subtype 2 objects (complex)
for (int i = 0; i < 5; i++) {
scenario.objects.emplace_back(0x100 + i, (i % 4) * 3, (i / 4) * 3, 0x12, 0);
}
// Subtype 3 objects (special)
for (int i = 0; i < 3; i++) {
scenario.objects.emplace_back(0x200 + i, (i % 3) * 4, (i / 3) * 4, 0x12, 0);
}
// Set ROM references and load tiles
for (auto& obj : scenario.objects) {
obj.set_rom(test_rom_.get());
obj.EnsureTilesLoaded();
}
scenario.palette = test_palettes_[1]; // Ice Palace palette
scenario.expected_width = 256;
scenario.expected_height = 176;
scenarios_.push_back(scenario);
}
void CreateLargeRoomScenario() {
DungeonScenario scenario;
scenario.name = "Large Room";
// Create a room with many objects (stress test scenario)
for (int i = 0; i < 100; i++) {
int x = (i % 16) * 2;
int y = (i / 16) * 2;
int object_id = (i % 50) + 0x10; // Mix of different object types
scenario.objects.emplace_back(object_id, x, y, 0x12, i % 3);
}
// Set ROM references and load tiles
for (auto& obj : scenario.objects) {
obj.set_rom(test_rom_.get());
obj.EnsureTilesLoaded();
}
scenario.palette = test_palettes_[2]; // Desert Palace palette
scenario.expected_width = 512;
scenario.expected_height = 256;
scenarios_.push_back(scenario);
}
void CreateBossRoomScenario() {
DungeonScenario scenario;
scenario.name = "Boss Room";
// Boss room typically has special objects
scenario.objects.emplace_back(0x30, 7, 4, 0x12, 0); // Boss platform
scenario.objects.emplace_back(0x31, 7, 5, 0x12, 0); // Boss platform
scenario.objects.emplace_back(0x32, 8, 4, 0x12, 0); // Boss platform
scenario.objects.emplace_back(0x33, 8, 5, 0x12, 0); // Boss platform
// Walls around the room
for (int x = 0; x < 16; x++) {
scenario.objects.emplace_back(0x10, x, 0, 0x12, 0);
scenario.objects.emplace_back(0x10, x, 10, 0x12, 0);
}
for (int y = 1; y < 10; y++) {
scenario.objects.emplace_back(0x11, 0, y, 0x12, 0);
scenario.objects.emplace_back(0x11, 15, y, 0x12, 0);
}
// Set ROM references and load tiles
for (auto& obj : scenario.objects) {
obj.set_rom(test_rom_.get());
obj.EnsureTilesLoaded();
}
scenario.palette = test_palettes_[4]; // Boss room palette
scenario.expected_width = 256;
scenario.expected_height = 176;
scenarios_.push_back(scenario);
}
void CreatePuzzleRoomScenario() {
DungeonScenario scenario;
scenario.name = "Puzzle Room";
// Puzzle rooms have specific interactive elements
scenario.objects.emplace_back(0x40, 4, 4, 0x12, 0); // Switch
scenario.objects.emplace_back(0x41, 8, 6, 0x12, 0); // Block
scenario.objects.emplace_back(0x42, 6, 8, 0x12, 0); // Pressure plate
// Chests for puzzle rewards
scenario.objects.emplace_back(0xF9, 2, 2, 0x12, 0); // Small chest
scenario.objects.emplace_back(0xFA, 12, 2, 0x12, 0); // Large chest
// Decorative elements
scenario.objects.emplace_back(0x50, 1, 5, 0x12, 0); // Torch
scenario.objects.emplace_back(0x51, 14, 5, 0x12, 0); // Torch
// Set ROM references and load tiles
for (auto& obj : scenario.objects) {
obj.set_rom(test_rom_.get());
obj.EnsureTilesLoaded();
}
scenario.palette = test_palettes_[3]; // Dark Palace palette
scenario.expected_width = 256;
scenario.expected_height = 176;
scenarios_.push_back(scenario);
}
void CreateDungeonPalette() {
gfx::SnesPalette palette;
// Standard dungeon colors (grays and browns)
palette.AddColor(gfx::SnesColor(0x00, 0x00, 0x00)); // Black
palette.AddColor(gfx::SnesColor(0x20, 0x20, 0x20)); // Dark gray
palette.AddColor(gfx::SnesColor(0x40, 0x40, 0x40)); // Medium gray
palette.AddColor(gfx::SnesColor(0x60, 0x60, 0x60)); // Light gray
palette.AddColor(gfx::SnesColor(0x80, 0x80, 0x80)); // Very light gray
palette.AddColor(gfx::SnesColor(0xA0, 0xA0, 0xA0)); // Almost white
palette.AddColor(gfx::SnesColor(0xC0, 0xC0, 0xC0)); // White
palette.AddColor(gfx::SnesColor(0x80, 0x40, 0x20)); // Brown
palette.AddColor(gfx::SnesColor(0xA0, 0x60, 0x40)); // Light brown
palette.AddColor(gfx::SnesColor(0x60, 0x80, 0x40)); // Green
palette.AddColor(gfx::SnesColor(0x40, 0x60, 0x80)); // Blue
palette.AddColor(gfx::SnesColor(0x80, 0x40, 0x80)); // Purple
palette.AddColor(gfx::SnesColor(0x80, 0x80, 0x40)); // Yellow
palette.AddColor(gfx::SnesColor(0x80, 0x40, 0x40)); // Red
palette.AddColor(gfx::SnesColor(0x40, 0x80, 0x80)); // Cyan
palette.AddColor(gfx::SnesColor(0xFF, 0xFF, 0xFF)); // Pure white
test_palettes_.push_back(palette);
}
void CreateIcePalacePalette() {
gfx::SnesPalette palette;
// Ice Palace colors (blues and whites)
palette.AddColor(gfx::SnesColor(0x00, 0x00, 0x00)); // Black
palette.AddColor(gfx::SnesColor(0x20, 0x40, 0x80)); // Dark blue
palette.AddColor(gfx::SnesColor(0x40, 0x60, 0xA0)); // Medium blue
palette.AddColor(gfx::SnesColor(0x60, 0x80, 0xC0)); // Light blue
palette.AddColor(gfx::SnesColor(0x80, 0xA0, 0xE0)); // Very light blue
palette.AddColor(gfx::SnesColor(0xA0, 0xC0, 0xFF)); // Pale blue
palette.AddColor(gfx::SnesColor(0xC0, 0xE0, 0xFF)); // Almost white
palette.AddColor(gfx::SnesColor(0xE0, 0xF0, 0xFF)); // White
palette.AddColor(gfx::SnesColor(0x40, 0x80, 0xC0)); // Ice blue
palette.AddColor(gfx::SnesColor(0x60, 0xA0, 0xE0)); // Light ice
palette.AddColor(gfx::SnesColor(0x80, 0xC0, 0xFF)); // Pale ice
palette.AddColor(gfx::SnesColor(0x20, 0x60, 0xA0)); // Deep ice
palette.AddColor(gfx::SnesColor(0x00, 0x40, 0x80)); // Dark ice
palette.AddColor(gfx::SnesColor(0x60, 0x80, 0xA0)); // Gray-blue
palette.AddColor(gfx::SnesColor(0x80, 0xA0, 0xC0)); // Light gray-blue
palette.AddColor(gfx::SnesColor(0xFF, 0xFF, 0xFF)); // Pure white
test_palettes_.push_back(palette);
}
void CreateDesertPalacePalette() {
gfx::SnesPalette palette;
// Desert Palace colors (yellows, oranges, and browns)
palette.AddColor(gfx::SnesColor(0x00, 0x00, 0x00)); // Black
palette.AddColor(gfx::SnesColor(0x40, 0x20, 0x00)); // Dark brown
palette.AddColor(gfx::SnesColor(0x60, 0x40, 0x20)); // Medium brown
palette.AddColor(gfx::SnesColor(0x80, 0x60, 0x40)); // Light brown
palette.AddColor(gfx::SnesColor(0xA0, 0x80, 0x60)); // Very light brown
palette.AddColor(gfx::SnesColor(0xC0, 0xA0, 0x80)); // Tan
palette.AddColor(gfx::SnesColor(0xE0, 0xC0, 0xA0)); // Light tan
palette.AddColor(gfx::SnesColor(0xFF, 0xE0, 0xC0)); // Cream
palette.AddColor(gfx::SnesColor(0x80, 0x40, 0x00)); // Orange
palette.AddColor(gfx::SnesColor(0xA0, 0x60, 0x20)); // Light orange
palette.AddColor(gfx::SnesColor(0xC0, 0x80, 0x40)); // Pale orange
palette.AddColor(gfx::SnesColor(0xE0, 0xA0, 0x60)); // Very pale orange
palette.AddColor(gfx::SnesColor(0x60, 0x60, 0x20)); // Olive
palette.AddColor(gfx::SnesColor(0x80, 0x80, 0x40)); // Light olive
palette.AddColor(gfx::SnesColor(0xA0, 0xA0, 0x60)); // Very light olive
palette.AddColor(gfx::SnesColor(0xFF, 0xFF, 0xFF)); // Pure white
test_palettes_.push_back(palette);
}
void CreateDarkPalacePalette() {
gfx::SnesPalette palette;
// Palace of Darkness colors (dark purples and grays)
palette.AddColor(gfx::SnesColor(0x00, 0x00, 0x00)); // Black
palette.AddColor(gfx::SnesColor(0x20, 0x00, 0x20)); // Dark purple
palette.AddColor(gfx::SnesColor(0x40, 0x20, 0x40)); // Medium purple
palette.AddColor(gfx::SnesColor(0x60, 0x40, 0x60)); // Light purple
palette.AddColor(gfx::SnesColor(0x80, 0x60, 0x80)); // Very light purple
palette.AddColor(gfx::SnesColor(0xA0, 0x80, 0xA0)); // Pale purple
palette.AddColor(gfx::SnesColor(0xC0, 0xA0, 0xC0)); // Almost white purple
palette.AddColor(gfx::SnesColor(0x10, 0x10, 0x10)); // Very dark gray
palette.AddColor(gfx::SnesColor(0x30, 0x30, 0x30)); // Dark gray
palette.AddColor(gfx::SnesColor(0x50, 0x50, 0x50)); // Medium gray
palette.AddColor(gfx::SnesColor(0x70, 0x70, 0x70)); // Light gray
palette.AddColor(gfx::SnesColor(0x90, 0x90, 0x90)); // Very light gray
palette.AddColor(gfx::SnesColor(0xB0, 0xB0, 0xB0)); // Almost white
palette.AddColor(gfx::SnesColor(0xD0, 0xD0, 0xD0)); // Off white
palette.AddColor(gfx::SnesColor(0xF0, 0xF0, 0xF0)); // Near white
palette.AddColor(gfx::SnesColor(0xFF, 0xFF, 0xFF)); // Pure white
test_palettes_.push_back(palette);
}
void CreateBossRoomPalette() {
gfx::SnesPalette palette;
// Boss room colors (dramatic reds, golds, and blacks)
palette.AddColor(gfx::SnesColor(0x00, 0x00, 0x00)); // Black
palette.AddColor(gfx::SnesColor(0x40, 0x00, 0x00)); // Dark red
palette.AddColor(gfx::SnesColor(0x60, 0x20, 0x00)); // Dark red-orange
palette.AddColor(gfx::SnesColor(0x80, 0x40, 0x00)); // Red-orange
palette.AddColor(gfx::SnesColor(0xA0, 0x60, 0x20)); // Orange
palette.AddColor(gfx::SnesColor(0xC0, 0x80, 0x40)); // Light orange
palette.AddColor(gfx::SnesColor(0xE0, 0xA0, 0x60)); // Very light orange
palette.AddColor(gfx::SnesColor(0x80, 0x60, 0x00)); // Dark gold
palette.AddColor(gfx::SnesColor(0xA0, 0x80, 0x20)); // Gold
palette.AddColor(gfx::SnesColor(0xC0, 0xA0, 0x40)); // Light gold
palette.AddColor(gfx::SnesColor(0xE0, 0xC0, 0x60)); // Very light gold
palette.AddColor(gfx::SnesColor(0x20, 0x20, 0x20)); // Dark gray
palette.AddColor(gfx::SnesColor(0x40, 0x40, 0x40)); // Medium gray
palette.AddColor(gfx::SnesColor(0x60, 0x60, 0x60)); // Light gray
palette.AddColor(gfx::SnesColor(0x80, 0x80, 0x80)); // Very light gray
palette.AddColor(gfx::SnesColor(0xFF, 0xFF, 0xFF)); // Pure white
test_palettes_.push_back(palette);
}
};
// Scenario-based rendering tests
TEST_F(DungeonObjectRenderingTests, EmptyRoomRendering) {
ASSERT_GE(scenarios_.size(), 1) << "Empty room scenario not available";
const auto& scenario = scenarios_[0];
auto result = renderer_->RenderObjects(scenario.objects, scenario.palette);
ASSERT_TRUE(result.ok()) << "Empty room rendering failed: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_TRUE(bitmap.is_active()) << "Empty room bitmap not active";
EXPECT_GE(bitmap.width(), scenario.expected_width) << "Empty room width too small";
EXPECT_GE(bitmap.height(), scenario.expected_height) << "Empty room height too small";
// Verify wall objects are rendered
EXPECT_GT(bitmap.size(), 0) << "Empty room bitmap has no content";
}
TEST_F(DungeonObjectRenderingTests, MultiObjectRoomRendering) {
ASSERT_GE(scenarios_.size(), 2) << "Multi-object scenario not available";
const auto& scenario = scenarios_[1];
auto result = renderer_->RenderObjects(scenario.objects, scenario.palette);
ASSERT_TRUE(result.ok()) << "Multi-object room rendering failed: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_TRUE(bitmap.is_active()) << "Multi-object room bitmap not active";
EXPECT_GE(bitmap.width(), scenario.expected_width) << "Multi-object room width too small";
EXPECT_GE(bitmap.height(), scenario.expected_height) << "Multi-object room height too small";
// Verify different object types are rendered
EXPECT_GT(bitmap.size(), 0) << "Multi-object room bitmap has no content";
}
TEST_F(DungeonObjectRenderingTests, ComplexRoomRendering) {
ASSERT_GE(scenarios_.size(), 3) << "Complex room scenario not available";
const auto& scenario = scenarios_[2];
auto result = renderer_->RenderObjects(scenario.objects, scenario.palette);
ASSERT_TRUE(result.ok()) << "Complex room rendering failed: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_TRUE(bitmap.is_active()) << "Complex room bitmap not active";
EXPECT_GE(bitmap.width(), scenario.expected_width) << "Complex room width too small";
EXPECT_GE(bitmap.height(), scenario.expected_height) << "Complex room height too small";
// Verify all subtypes are rendered correctly
EXPECT_GT(bitmap.size(), 0) << "Complex room bitmap has no content";
}
TEST_F(DungeonObjectRenderingTests, LargeRoomRendering) {
ASSERT_GE(scenarios_.size(), 4) << "Large room scenario not available";
const auto& scenario = scenarios_[3];
auto result = renderer_->RenderObjects(scenario.objects, scenario.palette);
ASSERT_TRUE(result.ok()) << "Large room rendering failed: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_TRUE(bitmap.is_active()) << "Large room bitmap not active";
EXPECT_GE(bitmap.width(), scenario.expected_width) << "Large room width too small";
EXPECT_GE(bitmap.height(), scenario.expected_height) << "Large room height too small";
// Verify performance with many objects
auto stats = renderer_->GetPerformanceStats();
EXPECT_GT(stats.objects_rendered, 0) << "Large room objects not rendered";
EXPECT_GT(stats.tiles_rendered, 0) << "Large room tiles not rendered";
}
TEST_F(DungeonObjectRenderingTests, BossRoomRendering) {
ASSERT_GE(scenarios_.size(), 5) << "Boss room scenario not available";
const auto& scenario = scenarios_[4];
auto result = renderer_->RenderObjects(scenario.objects, scenario.palette);
ASSERT_TRUE(result.ok()) << "Boss room rendering failed: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_TRUE(bitmap.is_active()) << "Boss room bitmap not active";
EXPECT_GE(bitmap.width(), scenario.expected_width) << "Boss room width too small";
EXPECT_GE(bitmap.height(), scenario.expected_height) << "Boss room height too small";
// Verify boss-specific objects are rendered
EXPECT_GT(bitmap.size(), 0) << "Boss room bitmap has no content";
}
TEST_F(DungeonObjectRenderingTests, PuzzleRoomRendering) {
ASSERT_GE(scenarios_.size(), 6) << "Puzzle room scenario not available";
const auto& scenario = scenarios_[5];
auto result = renderer_->RenderObjects(scenario.objects, scenario.palette);
ASSERT_TRUE(result.ok()) << "Puzzle room rendering failed: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_TRUE(bitmap.is_active()) << "Puzzle room bitmap not active";
EXPECT_GE(bitmap.width(), scenario.expected_width) << "Puzzle room width too small";
EXPECT_GE(bitmap.height(), scenario.expected_height) << "Puzzle room height too small";
// Verify puzzle elements are rendered
EXPECT_GT(bitmap.size(), 0) << "Puzzle room bitmap has no content";
}
// Palette-specific rendering tests
TEST_F(DungeonObjectRenderingTests, PaletteConsistency) {
ASSERT_GE(scenarios_.size(), 1) << "Test scenario not available";
const auto& scenario = scenarios_[0];
// Render with different palettes
for (size_t i = 0; i < test_palettes_.size(); i++) {
auto result = renderer_->RenderObjects(scenario.objects, test_palettes_[i]);
ASSERT_TRUE(result.ok()) << "Palette " << i << " rendering failed: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_TRUE(bitmap.is_active()) << "Palette " << i << " bitmap not active";
EXPECT_GT(bitmap.size(), 0) << "Palette " << i << " bitmap has no content";
}
}
// Performance tests with realistic scenarios
TEST_F(DungeonObjectRenderingTests, ScenarioPerformanceBenchmark) {
const int iterations = 10;
for (const auto& scenario : scenarios_) {
auto start_time = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; i++) {
auto result = renderer_->RenderObjects(scenario.objects, scenario.palette);
ASSERT_TRUE(result.ok()) << "Scenario " << scenario.name
<< " rendering failed: " << result.status().message();
}
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
// Each scenario should render within reasonable time
EXPECT_LT(duration.count(), 5000) << "Scenario " << scenario.name
<< " performance below expectations: "
<< duration.count() << "ms";
}
}
// Memory usage tests with realistic scenarios
TEST_F(DungeonObjectRenderingTests, ScenarioMemoryUsage) {
size_t initial_memory = renderer_->GetMemoryUsage();
// Render all scenarios multiple times
for (int round = 0; round < 3; round++) {
for (const auto& scenario : scenarios_) {
auto result = renderer_->RenderObjects(scenario.objects, scenario.palette);
ASSERT_TRUE(result.ok()) << "Scenario memory test failed: " << result.status().message();
}
}
size_t final_memory = renderer_->GetMemoryUsage();
// Memory usage should not grow excessively
EXPECT_LT(final_memory, initial_memory * 5) << "Memory leak detected in scenario tests: "
<< initial_memory << " -> " << final_memory;
// Clear cache and verify memory reduction
renderer_->ClearCache();
size_t memory_after_clear = renderer_->GetMemoryUsage();
EXPECT_LT(memory_after_clear, final_memory) << "Cache clear did not reduce memory usage";
}
// Object interaction tests
TEST_F(DungeonObjectRenderingTests, ObjectOverlapHandling) {
// Create objects that overlap
std::vector<zelda3::RoomObject> overlapping_objects;
// Two objects at the same position
overlapping_objects.emplace_back(0x10, 5, 5, 0x12, 0);
overlapping_objects.emplace_back(0x20, 5, 5, 0x12, 1); // Different layer
// Objects that partially overlap
overlapping_objects.emplace_back(0x30, 3, 3, 0x12, 0);
overlapping_objects.emplace_back(0x31, 4, 4, 0x12, 0);
// Set ROM references and load tiles
for (auto& obj : overlapping_objects) {
obj.set_rom(test_rom_.get());
obj.EnsureTilesLoaded();
}
auto result = renderer_->RenderObjects(overlapping_objects, test_palettes_[0]);
ASSERT_TRUE(result.ok()) << "Overlapping objects rendering failed: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_TRUE(bitmap.is_active()) << "Overlapping objects bitmap not active";
EXPECT_GT(bitmap.size(), 0) << "Overlapping objects bitmap has no content";
}
TEST_F(DungeonObjectRenderingTests, LayerRenderingOrder) {
// Create objects on different layers
std::vector<zelda3::RoomObject> layered_objects;
// Background layer (0)
layered_objects.emplace_back(0x10, 5, 5, 0x12, 0);
// Middle layer (1)
layered_objects.emplace_back(0x20, 5, 5, 0x12, 1);
// Foreground layer (2)
layered_objects.emplace_back(0x30, 5, 5, 0x12, 2);
// Set ROM references and load tiles
for (auto& obj : layered_objects) {
obj.set_rom(test_rom_.get());
obj.EnsureTilesLoaded();
}
auto result = renderer_->RenderObjects(layered_objects, test_palettes_[0]);
ASSERT_TRUE(result.ok()) << "Layered objects rendering failed: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_TRUE(bitmap.is_active()) << "Layered objects bitmap not active";
EXPECT_GT(bitmap.size(), 0) << "Layered objects bitmap has no content";
}
// Cache efficiency with realistic scenarios
TEST_F(DungeonObjectRenderingTests, ScenarioCacheEfficiency) {
renderer_->ClearCache();
// Render scenarios multiple times to test cache
for (int round = 0; round < 5; round++) {
for (const auto& scenario : scenarios_) {
auto result = renderer_->RenderObjects(scenario.objects, scenario.palette);
ASSERT_TRUE(result.ok()) << "Cache efficiency test failed: " << result.status().message();
}
}
auto stats = renderer_->GetPerformanceStats();
// Cache hit rate should be high after multiple renders
EXPECT_GT(stats.cache_hits, 0) << "No cache hits in scenario test";
EXPECT_GT(stats.cache_hit_rate(), 0.3) << "Cache hit rate too low: " << stats.cache_hit_rate();
}
// Edge cases in dungeon editing
TEST_F(DungeonObjectRenderingTests, BoundaryObjectPlacement) {
// Create objects at room boundaries
std::vector<zelda3::RoomObject> boundary_objects;
// Objects at exact boundaries
boundary_objects.emplace_back(0x10, 0, 0, 0x12, 0); // Top-left
boundary_objects.emplace_back(0x11, 15, 0, 0x12, 0); // Top-right
boundary_objects.emplace_back(0x12, 0, 10, 0x12, 0); // Bottom-left
boundary_objects.emplace_back(0x13, 15, 10, 0x12, 0); // Bottom-right
// Objects just outside boundaries (should be handled gracefully)
boundary_objects.emplace_back(0x14, -1, 5, 0x12, 0); // Left edge
boundary_objects.emplace_back(0x15, 16, 5, 0x12, 0); // Right edge
boundary_objects.emplace_back(0x16, 5, -1, 0x12, 0); // Top edge
boundary_objects.emplace_back(0x17, 5, 11, 0x12, 0); // Bottom edge
// Set ROM references and load tiles
for (auto& obj : boundary_objects) {
obj.set_rom(test_rom_.get());
obj.EnsureTilesLoaded();
}
auto result = renderer_->RenderObjects(boundary_objects, test_palettes_[0]);
ASSERT_TRUE(result.ok()) << "Boundary objects rendering failed: " << result.status().message();
auto bitmap = std::move(result.value());
EXPECT_TRUE(bitmap.is_active()) << "Boundary objects bitmap not active";
EXPECT_GT(bitmap.size(), 0) << "Boundary objects bitmap has no content";
}
} // namespace test
} // namespace yaze

View File

@@ -1,96 +0,0 @@
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include "app/rom.h"
#include "app/zelda3/overworld/overworld_map.h"
#include "app/zelda3/overworld/overworld.h"
using namespace yaze::zelda3;
using namespace yaze;
int main() {
// Load the vanilla ROM
Rom rom;
if (!rom.LoadFromFile("zelda3.sfc").ok()) {
std::cerr << "Failed to load ROM file" << std::endl;
return 1;
}
std::cout << "// Vanilla ROM values extracted from zelda3.sfc" << std::endl;
std::cout << "// Generated on " << __DATE__ << " " << __TIME__ << std::endl;
std::cout << std::endl;
// Extract ASM version
uint8_t asm_version = rom[OverworldCustomASMHasBeenApplied];
std::cout << "constexpr uint8_t kVanillaASMVersion = 0x" << std::hex << std::setw(2) << std::setfill('0') << (int)asm_version << ";" << std::endl;
std::cout << std::endl;
// Extract area graphics for first 10 maps
std::cout << "// Area graphics for first 10 maps" << std::endl;
for (int i = 0; i < 10; i++) {
uint8_t area_gfx = rom[kAreaGfxIdPtr + i];
std::cout << "constexpr uint8_t kVanillaAreaGraphics" << i << " = 0x" << std::hex << std::setw(2) << std::setfill('0') << (int)area_gfx << ";" << std::endl;
}
std::cout << std::endl;
// Extract area palettes for first 10 maps
std::cout << "// Area palettes for first 10 maps" << std::endl;
for (int i = 0; i < 10; i++) {
uint8_t area_pal = rom[kOverworldMapPaletteIds + i];
std::cout << "constexpr uint8_t kVanillaAreaPalette" << i << " = 0x" << std::hex << std::setw(2) << std::setfill('0') << (int)area_pal << ";" << std::endl;
}
std::cout << std::endl;
// Extract message IDs for first 10 maps
std::cout << "// Message IDs for first 10 maps" << std::endl;
for (int i = 0; i < 10; i++) {
uint16_t message_id = rom[kOverworldMessageIds + (i * 2)] | (rom[kOverworldMessageIds + (i * 2) + 1] << 8);
std::cout << "constexpr uint16_t kVanillaMessageId" << i << " = 0x" << std::hex << std::setw(4) << std::setfill('0') << message_id << ";" << std::endl;
}
std::cout << std::endl;
// Extract screen sizes for first 10 maps
std::cout << "// Screen sizes for first 10 maps" << std::endl;
for (int i = 0; i < 10; i++) {
uint8_t screen_size = rom[kOverworldScreenSize + i];
std::cout << "constexpr uint8_t kVanillaScreenSize" << i << " = 0x" << std::hex << std::setw(2) << std::setfill('0') << (int)screen_size << ";" << std::endl;
}
std::cout << std::endl;
// Extract sprite sets for first 10 maps
std::cout << "// Sprite sets for first 10 maps" << std::endl;
for (int i = 0; i < 10; i++) {
uint8_t sprite_set = rom[kOverworldSpriteset + i];
std::cout << "constexpr uint8_t kVanillaSpriteSet" << i << " = 0x" << std::hex << std::setw(2) << std::setfill('0') << (int)sprite_set << ";" << std::endl;
}
std::cout << std::endl;
// Extract sprite palettes for first 10 maps
std::cout << "// Sprite palettes for first 10 maps" << std::endl;
for (int i = 0; i < 10; i++) {
uint8_t sprite_pal = rom[kOverworldSpritePaletteIds + i];
std::cout << "constexpr uint8_t kVanillaSpritePalette" << i << " = 0x" << std::hex << std::setw(2) << std::setfill('0') << (int)sprite_pal << ";" << std::endl;
}
std::cout << std::endl;
// Extract music for first 10 maps
std::cout << "// Music for first 10 maps" << std::endl;
for (int i = 0; i < 10; i++) {
uint8_t music = rom[kOverworldMusicBeginning + i];
std::cout << "constexpr uint8_t kVanillaMusic" << i << " = 0x" << std::hex << std::setw(2) << std::setfill('0') << (int)music << ";" << std::endl;
}
std::cout << std::endl;
// Extract some special world values
std::cout << "// Special world graphics and palettes" << std::endl;
for (int i = 0; i < 5; i++) {
uint8_t special_gfx = rom[kOverworldSpecialGfxGroup + i];
uint8_t special_pal = rom[kOverworldSpecialPalGroup + i];
std::cout << "constexpr uint8_t kVanillaSpecialGfx" << i << " = 0x" << std::hex << std::setw(2) << std::setfill('0') << (int)special_gfx << ";" << std::endl;
std::cout << "constexpr uint8_t kVanillaSpecialPal" << i << " = 0x" << std::hex << std::setw(2) << std::setfill('0') << (int)special_pal << ";" << std::endl;
}
return 0;
}

View File

@@ -1,261 +0,0 @@
#include <gtest/gtest.h>
#include <memory>
#include <fstream>
#include "app/rom.h"
#include "app/zelda3/overworld/overworld.h"
#include "app/zelda3/overworld/overworld_map.h"
namespace yaze {
namespace zelda3 {
class OverworldIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
// Try to load a vanilla ROM for integration testing
// This would typically be a known good ROM file
rom_ = std::make_unique<Rom>();
// For now, we'll create a mock ROM with known values
// In a real integration test, this would load an actual ROM file
CreateMockVanillaROM();
overworld_ = std::make_unique<Overworld>(rom_.get());
}
void TearDown() override {
overworld_.reset();
rom_.reset();
}
void CreateMockVanillaROM() {
// Create a 2MB ROM with known vanilla values
std::vector<uint8_t> rom_data(0x200000, 0xFF);
// Set up some known vanilla values for testing
// These would be actual values from a vanilla ROM
// OverworldCustomASMHasBeenApplied = 0xFF (vanilla)
rom_data[0x140145] = 0xFF;
// Some sample area graphics values
rom_data[0x7C9C] = 0x00; // Map 0 area graphics
rom_data[0x7C9D] = 0x01; // Map 1 area graphics
// Some sample palette values
rom_data[0x7D1C] = 0x00; // Map 0 area palette
rom_data[0x7D1D] = 0x01; // Map 1 area palette
// Some sample message IDs
rom_data[0x3F51D] = 0x00; // Map 0 message ID (low byte)
rom_data[0x3F51E] = 0x00; // Map 0 message ID (high byte)
rom_data[0x3F51F] = 0x01; // Map 1 message ID (low byte)
rom_data[0x3F520] = 0x00; // Map 1 message ID (high byte)
rom_->LoadFromData(rom_data);
}
std::unique_ptr<Rom> rom_;
std::unique_ptr<Overworld> overworld_;
};
// Test that verifies vanilla ROM behavior
TEST_F(OverworldIntegrationTest, VanillaROMAreaGraphics) {
// Test that area graphics are loaded correctly from vanilla ROM
OverworldMap map0(0, rom_.get());
OverworldMap map1(1, rom_.get());
// These would be the actual expected values from a vanilla ROM
// For now, we're testing the loading mechanism
EXPECT_EQ(map0.area_graphics(), 0x00);
EXPECT_EQ(map1.area_graphics(), 0x01);
}
TEST_F(OverworldIntegrationTest, VanillaROMPalettes) {
// Test that palettes are loaded correctly from vanilla ROM
OverworldMap map0(0, rom_.get());
OverworldMap map1(1, rom_.get());
EXPECT_EQ(map0.area_palette(), 0x00);
EXPECT_EQ(map1.area_palette(), 0x01);
}
TEST_F(OverworldIntegrationTest, VanillaROMMessageIds) {
// Test that message IDs are loaded correctly from vanilla ROM
OverworldMap map0(0, rom_.get());
OverworldMap map1(1, rom_.get());
EXPECT_EQ(map0.message_id(), 0x0000);
EXPECT_EQ(map1.message_id(), 0x0001);
}
TEST_F(OverworldIntegrationTest, VanillaROMASMVersion) {
// Test that ASM version is correctly detected as vanilla
uint8_t asm_version = (*rom_)[OverworldCustomASMHasBeenApplied];
EXPECT_EQ(asm_version, 0xFF); // 0xFF means vanilla ROM
}
// Test that verifies v3 ROM behavior
class OverworldV3IntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
rom_ = std::make_unique<Rom>();
CreateMockV3ROM();
overworld_ = std::make_unique<Overworld>(rom_.get());
}
void TearDown() override {
overworld_.reset();
rom_.reset();
}
void CreateMockV3ROM() {
std::vector<uint8_t> rom_data(0x200000, 0xFF);
// Set up v3 ROM values
rom_data[0x140145] = 0x03; // v3 ROM
// v3 expanded message IDs
rom_data[0x1417F8] = 0x00; // Map 0 message ID (low byte)
rom_data[0x1417F9] = 0x00; // Map 0 message ID (high byte)
rom_data[0x1417FA] = 0x01; // Map 1 message ID (low byte)
rom_data[0x1417FB] = 0x00; // Map 1 message ID (high byte)
// v3 area sizes
rom_data[0x1788D] = 0x00; // Map 0 area size (Small)
rom_data[0x1788E] = 0x01; // Map 1 area size (Large)
// v3 main palettes
rom_data[0x140160] = 0x05; // Map 0 main palette
rom_data[0x140161] = 0x06; // Map 1 main palette
// v3 area-specific background colors
rom_data[0x140000] = 0x00; // Map 0 bg color (low byte)
rom_data[0x140001] = 0x00; // Map 0 bg color (high byte)
rom_data[0x140002] = 0xFF; // Map 1 bg color (low byte)
rom_data[0x140003] = 0x7F; // Map 1 bg color (high byte)
// v3 subscreen overlays
rom_data[0x140340] = 0x00; // Map 0 overlay (low byte)
rom_data[0x140341] = 0x00; // Map 0 overlay (high byte)
rom_data[0x140342] = 0x01; // Map 1 overlay (low byte)
rom_data[0x140343] = 0x00; // Map 1 overlay (high byte)
// v3 animated GFX
rom_data[0x1402A0] = 0x10; // Map 0 animated GFX
rom_data[0x1402A1] = 0x11; // Map 1 animated GFX
// v3 custom tile GFX groups (8 bytes per map)
for (int i = 0; i < 8; i++) {
rom_data[0x140480 + i] = i; // Map 0 custom tiles
rom_data[0x140488 + i] = i + 10; // Map 1 custom tiles
}
rom_->LoadFromData(rom_data);
}
std::unique_ptr<Rom> rom_;
std::unique_ptr<Overworld> overworld_;
};
TEST_F(OverworldV3IntegrationTest, V3ROMAreaSizes) {
// Test that v3 area sizes are loaded correctly
OverworldMap map0(0, rom_.get());
OverworldMap map1(1, rom_.get());
EXPECT_EQ(map0.area_size(), AreaSizeEnum::SmallArea);
EXPECT_EQ(map1.area_size(), AreaSizeEnum::LargeArea);
}
TEST_F(OverworldV3IntegrationTest, V3ROMMainPalettes) {
// Test that v3 main palettes are loaded correctly
OverworldMap map0(0, rom_.get());
OverworldMap map1(1, rom_.get());
EXPECT_EQ(map0.main_palette(), 0x05);
EXPECT_EQ(map1.main_palette(), 0x06);
}
TEST_F(OverworldV3IntegrationTest, V3ROMAreaSpecificBackgroundColors) {
// Test that v3 area-specific background colors are loaded correctly
OverworldMap map0(0, rom_.get());
OverworldMap map1(1, rom_.get());
EXPECT_EQ(map0.area_specific_bg_color(), 0x0000);
EXPECT_EQ(map1.area_specific_bg_color(), 0x7FFF);
}
TEST_F(OverworldV3IntegrationTest, V3ROMSubscreenOverlays) {
// Test that v3 subscreen overlays are loaded correctly
OverworldMap map0(0, rom_.get());
OverworldMap map1(1, rom_.get());
EXPECT_EQ(map0.subscreen_overlay(), 0x0000);
EXPECT_EQ(map1.subscreen_overlay(), 0x0001);
}
TEST_F(OverworldV3IntegrationTest, V3ROMAnimatedGFX) {
// Test that v3 animated GFX are loaded correctly
OverworldMap map0(0, rom_.get());
OverworldMap map1(1, rom_.get());
EXPECT_EQ(map0.animated_gfx(), 0x10);
EXPECT_EQ(map1.animated_gfx(), 0x11);
}
TEST_F(OverworldV3IntegrationTest, V3ROMCustomTileGFXGroups) {
// Test that v3 custom tile GFX groups are loaded correctly
OverworldMap map0(0, rom_.get());
OverworldMap map1(1, rom_.get());
for (int i = 0; i < 8; i++) {
EXPECT_EQ(map0.custom_tileset(i), i);
EXPECT_EQ(map1.custom_tileset(i), i + 10);
}
}
TEST_F(OverworldV3IntegrationTest, V3ROMASMVersion) {
// Test that ASM version is correctly detected as v3
uint8_t asm_version = (*rom_)[OverworldCustomASMHasBeenApplied];
EXPECT_EQ(asm_version, 0x03); // 0x03 means v3 ROM
}
// Test that verifies backwards compatibility
TEST_F(OverworldV3IntegrationTest, BackwardsCompatibility) {
// Test that v3 ROMs can still access vanilla properties
OverworldMap map0(0, rom_.get());
OverworldMap map1(1, rom_.get());
// These should still work even in v3 ROMs
EXPECT_EQ(map0.area_graphics(), 0x00);
EXPECT_EQ(map1.area_graphics(), 0x01);
EXPECT_EQ(map0.area_palette(), 0x00);
EXPECT_EQ(map1.area_palette(), 0x01);
}
// Performance test for large numbers of maps
TEST_F(OverworldIntegrationTest, PerformanceTest) {
// Test that we can handle the full number of overworld maps efficiently
const int kNumMaps = 160;
auto start_time = std::chrono::high_resolution_clock::now();
for (int i = 0; i < kNumMaps; i++) {
OverworldMap map(i, rom_.get());
// Access various properties to simulate real usage
map.area_graphics();
map.area_palette();
map.message_id();
map.area_size();
map.main_palette();
}
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
// Should complete in reasonable time (less than 1 second for 160 maps)
EXPECT_LT(duration.count(), 1000);
}
} // namespace zelda3
} // namespace yaze

View File

@@ -1,108 +0,0 @@
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <vector>
#include "app/rom.h"
#include "app/zelda3/overworld/overworld.h"
#include "app/zelda3/overworld/overworld_map.h"
using namespace yaze::zelda3;
using namespace yaze;
class ROMPatchUtility {
public:
static absl::Status CreateV3PatchedROM(const std::string& input_rom_path,
const std::string& output_rom_path) {
// Load the vanilla ROM
Rom rom;
RETURN_IF_ERROR(rom.LoadFromFile(input_rom_path));
// Apply ZSCustomOverworld v3 settings
RETURN_IF_ERROR(ApplyV3Patch(rom));
// Save the patched ROM
return rom.SaveToFile(Rom::SaveSettings{.filename = output_rom_path});
}
private:
static absl::Status ApplyV3Patch(Rom& rom) {
// Set ASM version to v3
rom.WriteByte(OverworldCustomASMHasBeenApplied, 0x03);
// Enable v3 features
rom.WriteByte(OverworldCustomAreaSpecificBGEnabled, 0x01);
rom.WriteByte(OverworldCustomSubscreenOverlayEnabled, 0x01);
rom.WriteByte(OverworldCustomAnimatedGFXEnabled, 0x01);
rom.WriteByte(OverworldCustomTileGFXGroupEnabled, 0x01);
rom.WriteByte(OverworldCustomMosaicEnabled, 0x01);
rom.WriteByte(OverworldCustomMainPaletteEnabled, 0x01);
// Apply v3 settings to first 10 maps for testing
for (int i = 0; i < 10; i++) {
// Set area sizes (mix of different sizes)
AreaSizeEnum area_size = static_cast<AreaSizeEnum>(i % 4);
rom.WriteByte(kOverworldScreenSize + i, static_cast<uint8_t>(area_size));
// Set main palettes
rom.WriteByte(OverworldCustomMainPaletteArray + i, i % 8);
// Set area-specific background colors
uint16_t bg_color = 0x0000 + (i * 0x1000);
rom.WriteByte(OverworldCustomAreaSpecificBGPalette + (i * 2),
bg_color & 0xFF);
rom.WriteByte(OverworldCustomAreaSpecificBGPalette + (i * 2) + 1,
(bg_color >> 8) & 0xFF);
// Set subscreen overlays
uint16_t overlay = 0x0090 + i;
rom.WriteByte(OverworldCustomSubscreenOverlayArray + (i * 2),
overlay & 0xFF);
rom.WriteByte(OverworldCustomSubscreenOverlayArray + (i * 2) + 1,
(overlay >> 8) & 0xFF);
// Set animated GFX
rom.WriteByte(OverworldCustomAnimatedGFXArray + i, 0x50 + i);
// Set custom tile GFX groups (8 bytes per map)
for (int j = 0; j < 8; j++) {
rom.WriteByte(OverworldCustomTileGFXGroupArray + (i * 8) + j,
0x20 + j + i);
}
// Set mosaic settings
rom.WriteByte(OverworldCustomMosaicArray + i, i % 16);
// Set expanded message IDs
uint16_t message_id = 0x1000 + i;
rom.WriteByte(kOverworldMessagesExpanded + (i * 2), message_id & 0xFF);
rom.WriteByte(kOverworldMessagesExpanded + (i * 2) + 1,
(message_id >> 8) & 0xFF);
}
return absl::OkStatus();
}
};
int main(int argc, char* argv[]) {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " <input_rom> <output_rom>"
<< std::endl;
return 1;
}
std::string input_rom = argv[1];
std::string output_rom = argv[2];
auto status = ROMPatchUtility::CreateV3PatchedROM(input_rom, output_rom);
if (!status.ok()) {
std::cerr << "Failed to create patched ROM: " << status.message()
<< std::endl;
return 1;
}
std::cout << "Successfully created v3 patched ROM: " << output_rom
<< std::endl;
return 0;
}

View File

@@ -1,366 +0,0 @@
#include "test_dungeon_objects.h"
#include "mocks/mock_rom.h"
#include "app/zelda3/dungeon/object_parser.h"
#include "app/zelda3/dungeon/object_renderer.h"
#include "app/zelda3/dungeon/room_object.h"
#include "app/zelda3/dungeon/room_layout.h"
#include "app/gfx/snes_color.h"
#include "app/gfx/snes_palette.h"
#include "testing.h"
#include <vector>
#include <cstring>
#include "gtest/gtest.h"
namespace yaze {
namespace test {
void TestDungeonObjects::SetUp() {
test_rom_ = std::make_unique<MockRom>();
ASSERT_TRUE(CreateTestRom().ok());
ASSERT_TRUE(SetupObjectData().ok());
}
void TestDungeonObjects::TearDown() {
test_rom_.reset();
}
absl::Status TestDungeonObjects::CreateTestRom() {
// Create basic ROM data
std::vector<uint8_t> rom_data(kTestRomSize, 0x00);
// Set up ROM header
std::string title = "ZELDA3 TEST";
std::memcpy(&rom_data[0x7FC0], title.c_str(), std::min(title.length(), size_t(21)));
rom_data[0x7FD7] = 0x21; // 2MB ROM
// Set up object tables
auto subtype1_table = CreateObjectSubtypeTable(0x8000, 0x100);
auto subtype2_table = CreateObjectSubtypeTable(0x83F0, 0x80);
auto subtype3_table = CreateObjectSubtypeTable(0x84F0, 0x100);
// Copy tables to ROM data
std::copy(subtype1_table.begin(), subtype1_table.end(), rom_data.begin() + 0x8000);
std::copy(subtype2_table.begin(), subtype2_table.end(), rom_data.begin() + 0x83F0);
std::copy(subtype3_table.begin(), subtype3_table.end(), rom_data.begin() + 0x84F0);
// Set up tile data
auto tile_data = CreateTileData(0x1B52, 0x400);
std::copy(tile_data.begin(), tile_data.end(), rom_data.begin() + 0x1B52);
return test_rom_->SetTestData(rom_data);
}
absl::Status TestDungeonObjects::SetupObjectData() {
// Set up test object data
std::vector<uint8_t> object_data = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
test_rom_->SetObjectData(kTestObjectId, object_data);
// Set up test room data
auto room_header = CreateRoomHeader(kTestRoomId);
test_rom_->SetRoomData(kTestRoomId, room_header);
return absl::OkStatus();
}
std::vector<uint8_t> TestDungeonObjects::CreateObjectSubtypeTable(int base_addr, int count) {
std::vector<uint8_t> table(count * 2, 0x00);
for (int i = 0; i < count; i++) {
int addr = i * 2;
// Point to tile data at 0x1B52 + (i * 8)
int tile_offset = (i * 8) & 0xFFFF;
table[addr] = tile_offset & 0xFF;
table[addr + 1] = (tile_offset >> 8) & 0xFF;
}
return table;
}
std::vector<uint8_t> TestDungeonObjects::CreateTileData(int base_addr, int tile_count) {
std::vector<uint8_t> data(tile_count * 8, 0x00);
for (int i = 0; i < tile_count; i++) {
int addr = i * 8;
// Create simple tile data
for (int j = 0; j < 8; j++) {
data[addr + j] = (i + j) & 0xFF;
}
}
return data;
}
std::vector<uint8_t> TestDungeonObjects::CreateRoomHeader(int room_id) {
std::vector<uint8_t> header(32, 0x00);
// Basic room properties
header[0] = 0x00; // Background type, collision, light
header[1] = 0x00; // Palette
header[2] = 0x01; // Blockset
header[3] = 0x01; // Spriteset
header[4] = 0x00; // Effect
header[5] = 0x00; // Tag1
header[6] = 0x00; // Tag2
return header;
}
// Test cases
TEST_F(TestDungeonObjects, ObjectParserBasicTest) {
zelda3::ObjectParser parser(test_rom_.get());
auto result = parser.ParseObject(kTestObjectId);
ASSERT_TRUE(result.ok());
EXPECT_FALSE(result->empty());
}
TEST_F(TestDungeonObjects, ObjectRendererBasicTest) {
zelda3::ObjectRenderer renderer(test_rom_.get());
// Create test object
auto room_object = zelda3::RoomObject(kTestObjectId, 0, 0, 0x12, 0);
room_object.set_rom(test_rom_.get());
room_object.EnsureTilesLoaded();
// Create test palette
gfx::SnesPalette palette;
for (int i = 0; i < 16; i++) {
palette.AddColor(gfx::SnesColor(i * 16, i * 16, i * 16));
}
auto result = renderer.RenderObject(room_object, palette);
ASSERT_TRUE(result.ok());
EXPECT_GT(result->width(), 0);
EXPECT_GT(result->height(), 0);
}
TEST_F(TestDungeonObjects, RoomObjectTileLoadingTest) {
auto room_object = zelda3::RoomObject(kTestObjectId, 5, 5, 0x12, 0);
room_object.set_rom(test_rom_.get());
// Test tile loading
room_object.EnsureTilesLoaded();
EXPECT_FALSE(room_object.tiles().empty());
}
TEST_F(TestDungeonObjects, MockRomDataTest) {
auto* mock_rom = static_cast<MockRom*>(test_rom_.get());
EXPECT_TRUE(mock_rom->HasObjectData(kTestObjectId));
EXPECT_TRUE(mock_rom->HasRoomData(kTestRoomId));
EXPECT_TRUE(mock_rom->IsValid());
}
TEST_F(TestDungeonObjects, RoomObjectTileAccessTest) {
auto room_object = zelda3::RoomObject(kTestObjectId, 5, 5, 0x12, 0);
room_object.set_rom(test_rom_.get());
room_object.EnsureTilesLoaded();
// Test new tile access methods
auto tiles_result = room_object.GetTiles();
EXPECT_TRUE(tiles_result.ok());
if (tiles_result.ok()) {
EXPECT_FALSE(tiles_result->empty());
}
// Test individual tile access
auto tile_result = room_object.GetTile(0);
EXPECT_TRUE(tile_result.ok());
if (tile_result.ok()) {
const auto* tile = tile_result.value();
EXPECT_NE(tile, nullptr);
}
// Test tile count
EXPECT_GT(room_object.GetTileCount(), 0);
// Test out of range access
auto bad_tile_result = room_object.GetTile(999);
EXPECT_FALSE(bad_tile_result.ok());
}
TEST_F(TestDungeonObjects, ObjectRendererGraphicsSheetTest) {
zelda3::ObjectRenderer renderer(test_rom_.get());
// Create test object
auto room_object = zelda3::RoomObject(kTestObjectId, 0, 0, 0x12, 0);
room_object.set_rom(test_rom_.get());
room_object.EnsureTilesLoaded();
// Create test palette
gfx::SnesPalette palette;
for (int i = 0; i < 16; i++) {
palette.AddColor(gfx::SnesColor(i * 16, i * 16, i * 16));
}
// Test rendering with graphics sheet lookup
auto result = renderer.RenderObject(room_object, palette);
ASSERT_TRUE(result.ok());
auto bitmap = std::move(result.value());
EXPECT_TRUE(bitmap.is_active());
EXPECT_NE(bitmap.surface(), nullptr);
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
}
TEST_F(TestDungeonObjects, BitmapCopySemanticsTest) {
// Test bitmap copying works correctly
std::vector<uint8_t> data(32 * 32, 0x42);
gfx::Bitmap original(32, 32, 8, data);
// Test copy constructor
gfx::Bitmap copy = original;
EXPECT_EQ(copy.width(), original.width());
EXPECT_EQ(copy.height(), original.height());
EXPECT_TRUE(copy.is_active());
EXPECT_NE(copy.surface(), nullptr);
// Test copy assignment
gfx::Bitmap assigned;
assigned = original;
EXPECT_EQ(assigned.width(), original.width());
EXPECT_EQ(assigned.height(), original.height());
EXPECT_TRUE(assigned.is_active());
EXPECT_NE(assigned.surface(), nullptr);
}
TEST_F(TestDungeonObjects, BitmapMoveSemanticsTest) {
// Test bitmap moving works correctly
std::vector<uint8_t> data(32 * 32, 0x42);
gfx::Bitmap original(32, 32, 8, data);
// Test move constructor
gfx::Bitmap moved = std::move(original);
EXPECT_EQ(moved.width(), 32);
EXPECT_EQ(moved.height(), 32);
EXPECT_TRUE(moved.is_active());
EXPECT_NE(moved.surface(), nullptr);
// Original should be in a valid but empty state
EXPECT_EQ(original.width(), 0);
EXPECT_EQ(original.height(), 0);
EXPECT_FALSE(original.is_active());
EXPECT_EQ(original.surface(), nullptr);
}
TEST_F(TestDungeonObjects, PaletteHandlingTest) {
// Test palette handling and hash calculation
gfx::SnesPalette palette;
for (int i = 0; i < 16; i++) {
palette.AddColor(gfx::SnesColor(i * 16, i * 16, i * 16));
}
EXPECT_EQ(palette.size(), 16);
// Test palette hash calculation (used in caching)
uint64_t hash1 = 0;
for (size_t i = 0; i < palette.size(); ++i) {
hash1 ^= std::hash<uint16_t>{}(palette[i].snes()) + 0x9e3779b9 + (hash1 << 6) + (hash1 >> 2);
}
// Same palette should produce same hash
uint64_t hash2 = 0;
for (size_t i = 0; i < palette.size(); ++i) {
hash2 ^= std::hash<uint16_t>{}(palette[i].snes()) + 0x9e3779b9 + (hash2 << 6) + (hash2 >> 2);
}
EXPECT_EQ(hash1, hash2);
EXPECT_NE(hash1, 0); // Hash should not be zero
}
TEST_F(TestDungeonObjects, ObjectSizeCalculationTest) {
zelda3::ObjectParser parser(test_rom_.get());
// Test object size parsing
auto size_result = parser.ParseObjectSize(0x01, 0x12);
EXPECT_TRUE(size_result.ok());
if (size_result.ok()) {
const auto& size_info = size_result.value();
EXPECT_GT(size_info.width_tiles, 0);
EXPECT_GT(size_info.height_tiles, 0);
EXPECT_TRUE(size_info.is_repeatable);
}
}
TEST_F(TestDungeonObjects, ObjectSubtypeDeterminationTest) {
zelda3::ObjectParser parser(test_rom_.get());
// Test subtype determination
EXPECT_EQ(parser.DetermineSubtype(0x01), 1);
EXPECT_EQ(parser.DetermineSubtype(0x100), 2);
EXPECT_EQ(parser.DetermineSubtype(0x200), 3);
// Test object subtype info
auto subtype_result = parser.GetObjectSubtype(0x01);
EXPECT_TRUE(subtype_result.ok());
if (subtype_result.ok()) {
EXPECT_EQ(subtype_result->subtype, 1);
EXPECT_GT(subtype_result->max_tile_count, 0);
}
}
TEST_F(TestDungeonObjects, RoomLayoutObjectCreationTest) {
zelda3::RoomLayoutObject obj(0x01, 5, 10, zelda3::RoomLayoutObject::Type::kWall, 0);
EXPECT_EQ(obj.id(), 0x01);
EXPECT_EQ(obj.x(), 5);
EXPECT_EQ(obj.y(), 10);
EXPECT_EQ(obj.type(), zelda3::RoomLayoutObject::Type::kWall);
EXPECT_EQ(obj.layer(), 0);
// Test type name
EXPECT_EQ(obj.GetTypeName(), "Wall");
// Test tile creation
auto tile_result = obj.GetTile();
EXPECT_TRUE(tile_result.ok());
}
TEST_F(TestDungeonObjects, RoomLayoutLoadingTest) {
zelda3::RoomLayout layout(test_rom_.get());
// Test loading layout for room 0
auto status = layout.LoadLayout(0);
// This might fail due to missing layout data, which is expected
// We're testing that the method doesn't crash
// Test getting objects by type
auto walls = layout.GetObjectsByType(zelda3::RoomLayoutObject::Type::kWall);
auto floors = layout.GetObjectsByType(zelda3::RoomLayoutObject::Type::kFloor);
// Test dimensions
auto [width, height] = layout.GetDimensions();
EXPECT_GT(width, 0);
EXPECT_GT(height, 0);
// Test object access
auto obj_result = layout.GetObjectAt(0, 0, 0);
// This might fail if no object exists at that position, which is expected
}
TEST_F(TestDungeonObjects, RoomLayoutCollisionTest) {
zelda3::RoomLayout layout(test_rom_.get());
// Test collision detection methods
EXPECT_FALSE(layout.HasWall(0, 0, 0)); // Should be false for empty layout
EXPECT_FALSE(layout.HasFloor(0, 0, 0)); // Should be false for empty layout
// Test with a simple layout
std::vector<uint8_t> layout_data = {
0x01, 0x01, 0x00, 0x00, // Wall, Wall, Empty, Empty
0x21, 0x21, 0x21, 0x21, // Floor, Floor, Floor, Floor
0x00, 0x00, 0x00, 0x00, // Empty, Empty, Empty, Empty
};
// This would require the layout to be properly set up
// For now, we just test that the methods don't crash
}
} // namespace test
} // namespace yaze