From eff8d43479e7704df65fae2a80eefa787213f502 Mon Sep 17 00:00:00 2001 From: skal Date: Mon, 9 Feb 2026 20:27:04 +0100 Subject: refactor: Reorganize tests into subsystem subdirectories Restructured test suite for better organization and targeted testing: **Structure:** - src/tests/audio/ - 15 audio system tests - src/tests/gpu/ - 12 GPU/shader tests - src/tests/3d/ - 6 3D rendering tests - src/tests/assets/ - 2 asset system tests - src/tests/util/ - 3 utility tests - src/tests/common/ - 3 shared test helpers - src/tests/scripts/ - 2 bash test scripts (moved conceptually, not physically) **CMake changes:** - Updated add_demo_test macro to accept LABEL parameter - Applied CTest labels to all 36 tests for subsystem filtering - Updated all test file paths in CMakeLists.txt - Fixed common helper paths (webgpu_test_fixture, etc.) - Added custom targets for subsystem testing: - run_audio_tests, run_gpu_tests, run_3d_tests - run_assets_tests, run_util_tests, run_all_tests **Include path updates:** - Fixed relative includes in GPU tests to reference ../common/ **Documentation:** - Updated doc/HOWTO.md with subsystem test commands - Updated doc/CONTRIBUTING.md with new test organization - Updated scripts/check_all.sh to reflect new structure **Verification:** - All 36 tests passing (100%) - ctest -L filters work correctly - make run__tests targets functional - scripts/check_all.sh passes Backward compatible: make test and ctest continue to work unchanged. handoff(Gemini): Test reorganization complete. 36/36 tests passing. --- src/tests/util/test_procedural.cc | 137 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/tests/util/test_procedural.cc (limited to 'src/tests/util/test_procedural.cc') diff --git a/src/tests/util/test_procedural.cc b/src/tests/util/test_procedural.cc new file mode 100644 index 0000000..e9f9a02 --- /dev/null +++ b/src/tests/util/test_procedural.cc @@ -0,0 +1,137 @@ +// This file is part of the 64k demo project. +// It tests the procedural generation system. + +#include "procedural/generator.h" +#include +#include +#include +#include + +void test_noise() { + std::cout << "Testing Noise Generator..." << std::endl; + int w = 64, h = 64; + std::vector buffer(w * h * 4); + float params[] = {12345, 1.0f}; // Seed, Intensity + + // Test with explicit params + bool res = procedural::gen_noise(buffer.data(), w, h, params, 2); + assert(res); + assert(buffer[3] == 255); + + // Check that not all pixels are black + bool nonzero = false; + for (size_t i = 0; i < buffer.size(); i += 4) { + if (buffer[i] > 0) { + nonzero = true; + break; + } + } + assert(nonzero); + + // Test with default params + std::fill(buffer.begin(), buffer.end(), 0); + res = procedural::gen_noise(buffer.data(), w, h, nullptr, 0); + assert(res); + assert(buffer[3] == 255); // Alpha should still be set +} + +void test_perlin() { + std::cout << "Testing Perlin Generator..." << std::endl; + int w = 64, h = 64; + std::vector buffer(w * h * 4); + + // Test with explicit params + // Params: Seed, Freq, Amp, Decay, Octaves + float params[] = {12345, 4.0f, 1.0f, 0.5f, 4.0f}; + bool res = procedural::gen_perlin(buffer.data(), w, h, params, 5); + assert(res); + assert(buffer[3] == 255); + + bool nonzero = false; + for (size_t i = 0; i < buffer.size(); i += 4) { + if (buffer[i] > 0) { + nonzero = true; + break; + } + } + assert(nonzero); + + // Test with default params + std::fill(buffer.begin(), buffer.end(), 0); + res = procedural::gen_perlin(buffer.data(), w, h, nullptr, 0); + assert(res); + assert(buffer[3] == 255); + + // Test memory allocation failure simulation (large dimensions) + // This is hard to robustly test without mocking, but we can try an + // excessively large allocation if desired. For now, we trust the logic path. +} + +void test_grid() { + std::cout << "Testing Grid Generator..." << std::endl; + int w = 100, h = 100; + std::vector buffer(w * h * 4); + float params[] = {10, 1}; // Size 10, Thickness 1 + + // Test with explicit params + bool res = procedural::gen_grid(buffer.data(), w, h, params, 2); + assert(res); + + // Pixel (0,0) should be white (on line) + assert(buffer[0] == 255); + // Pixel (5,5) should be black (off line, since size=10) + assert(buffer[(5 * w + 5) * 4] == 0); + // Pixel (10,0) should be white (on vertical line) + assert(buffer[(0 * w + 10) * 4] == 255); + + // Test with default params + res = procedural::gen_grid(buffer.data(), w, h, nullptr, 0); + assert(res); + // Default size is 32, thickness 2 + assert(buffer[0] == 255); + assert(buffer[(0 * w + 32) * 4] == 255); +} + +void test_periodic() { + std::cout << "Testing Periodic Blending..." << std::endl; + int w = 64, h = 64; + std::vector buffer(w * h * 4); + + // Fill with horizontal gradient: left=0, right=255 + for (int y = 0; y < h; ++y) { + for (int x = 0; x < w; ++x) { + int idx = (y * w + x) * 4; + buffer[idx] = (uint8_t)(x * 255 / (w - 1)); + buffer[idx + 1] = 0; + buffer[idx + 2] = 0; + buffer[idx + 3] = 255; + } + } + + // Pre-check: edges are different + assert(buffer[0] == 0); + assert(buffer[(w - 1) * 4] == 255); + + float params[] = {0.1f}; // Blend ratio 10% + bool res = procedural::make_periodic(buffer.data(), w, h, params, 1); + assert(res); + + // Post-check: Left edge (x=0) should now be blended with right edge. + // Logic: blend right edge INTO left edge. At x=0, we copy from right side. + // So buffer[0] should be close to 255 (value from right). + assert(buffer[0] > 200); + + // Check invalid ratio + float invalid_params[] = {-1.0f}; + res = procedural::make_periodic(buffer.data(), w, h, invalid_params, 1); + assert(res); // Should return true but do nothing +} + +int main() { + test_noise(); + test_perlin(); + test_grid(); + test_periodic(); + std::cout << "--- PROCEDURAL TESTS PASSED ---" << std::endl; + return 0; +} \ No newline at end of file -- cgit v1.2.3