From 8da917bce6e937ab092bb2f68c0bcf6039c3a3d7 Mon Sep 17 00:00:00 2001 From: skal Date: Wed, 4 Feb 2026 10:37:37 +0100 Subject: test(procedural): Improve test coverage (Task #45) Added tests for gen_perlin and make_periodic. Improved parameter handling checks. Coverage for src/procedural/generator.cc increased to 96%. --- PROJECT_CONTEXT.md | 1 + TODO.md | 3 ++ src/tests/test_procedural.cc | 96 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 95 insertions(+), 5 deletions(-) diff --git a/PROJECT_CONTEXT.md b/PROJECT_CONTEXT.md index 15b5090..7634ad9 100644 --- a/PROJECT_CONTEXT.md +++ b/PROJECT_CONTEXT.md @@ -28,6 +28,7 @@ Style: ## Project Roadmap ### Recently Completed +- **Task #45: Improve Procedural Generation Coverage**: Achieved 96% coverage for `src/procedural/` by implementing comprehensive tests for Perlin noise, periodic blending, and parameter handling. - **Task #44: Developer Tooling (Coverage)**: Added `DEMO_ENABLE_COVERAGE` CMake option and created `scripts/gen_coverage_report.sh` to generate HTML coverage reports using `lcov` on macOS. - **Skybox & Two-pass Rendering Stability**: Resolved "black screen" and validation errors by implementing a robust two-pass rendering architecture (Pass 1: Skybox/Clear, Pass 2: Scene Objects). Implemented a rotating skybox using world-space ray unprojection (`inv_view_proj`) and a multi-octave procedural noise generator. - **Task #20: Platform & Code Hygiene**: Consolidated platform-specific shims and WebGPU headers into `platform.h`. Refactored `platform_init` and `platform_poll` for better abstraction. Removed STL containers from initial hot paths (`AssetManager`, `procedural`). Full STL removal for CRT replacement is deferred to the final optimization phase. diff --git a/TODO.md b/TODO.md index 2d7eb90..1597129 100644 --- a/TODO.md +++ b/TODO.md @@ -3,6 +3,9 @@ This file tracks prioritized tasks with detailed attack plans. ## Recently Completed (February 4, 2026) +- [x] **Task #45: Improve Procedural Generation Coverage**: + - [x] **Unit Tests:** Implemented comprehensive tests for `gen_perlin`, `make_periodic`, and default parameter handling in `src/tests/test_procedural.cc`. + - [x] **Coverage Boost:** Increased `src/procedural/generator.cc` coverage from 38% to 96%. - [x] **Task #44: Developer Tooling (Coverage)**: - [x] **Implement Code Coverage:** Added `DEMO_ENABLE_COVERAGE` CMake option and created `scripts/gen_coverage_report.sh` to generate HTML coverage reports using `lcov` on macOS. - [x] **Documentation:** Updated `doc/HOWTO.md` with usage instructions. diff --git a/src/tests/test_procedural.cc b/src/tests/test_procedural.cc index 3b82fa0..0373402 100644 --- a/src/tests/test_procedural.cc +++ b/src/tests/test_procedural.cc @@ -5,6 +5,7 @@ #include #include #include +#include void test_noise() { std::cout << "Testing Noise Generator..." << std::endl; @@ -12,11 +13,40 @@ void test_noise() { std::vector buffer(w * h * 4); float params[] = {12345, 1.0f}; // Seed, Intensity - procedural::gen_noise(buffer.data(), w, h, params, 2); + // 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 +} - // Check simple properties: alpha should be 255 +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); - // Check that not all pixels are black (very unlikely with noise) + bool nonzero = false; for (size_t i = 0; i < buffer.size(); i += 4) { if (buffer[i] > 0) { @@ -25,6 +55,16 @@ void test_noise() { } } 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() { @@ -33,7 +73,9 @@ void test_grid() { std::vector buffer(w * h * 4); float params[] = {10, 1}; // Size 10, Thickness 1 - procedural::gen_grid(buffer.data(), w, h, params, 2); + // 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); @@ -41,11 +83,55 @@ void test_grid() { 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 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