diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-09 20:27:04 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-09 20:27:04 +0100 |
| commit | eff8d43479e7704df65fae2a80eefa787213f502 (patch) | |
| tree | 76f2fb8fe8d3db2c15179449df2cf12f7f54e0bf /src/tests/gpu/test_shader_composer.cc | |
| parent | 12378b1b7e9091ba59895b4360b2fa959180a56a (diff) | |
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 <subsystem> filters work correctly
- make run_<subsystem>_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.
Diffstat (limited to 'src/tests/gpu/test_shader_composer.cc')
| -rw-r--r-- | src/tests/gpu/test_shader_composer.cc | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/src/tests/gpu/test_shader_composer.cc b/src/tests/gpu/test_shader_composer.cc new file mode 100644 index 0000000..a98a259 --- /dev/null +++ b/src/tests/gpu/test_shader_composer.cc @@ -0,0 +1,136 @@ +// This file is part of the 64k demo project. +// It tests the ShaderComposer utility. + +#include "gpu/effects/shader_composer.h" +#include <cassert> +#include <iostream> +#include <string> + +#if defined(USE_TEST_ASSETS) +#include "test_assets.h" +#else +#include "generated/assets.h" +#endif + +// Forward declaration for asset loading +const uint8_t* GetAsset(AssetId asset_id, size_t* out_size); + +void test_composition() { + std::cout << "Testing Shader Composition..." << std::endl; + auto& sc = ShaderComposer::Get(); + + sc.RegisterSnippet("math", "fn add(a: f32, b: f32) -> f32 { return a + b; }"); + sc.RegisterSnippet("util", "fn square(a: f32) -> f32 { return a * a; }"); + + std::string main_code = "fn main() { let x = add(1.0, square(2.0)); }"; + std::string result = sc.Compose({"math", "util"}, main_code); + + // Verify order and presence + assert(result.find("Dependency: math") != std::string::npos); + assert(result.find("Dependency: util") != std::string::npos); + assert(result.find("Main Code") != std::string::npos); + + size_t pos_math = result.find("Dependency: math"); + size_t pos_util = result.find("Dependency: util"); + size_t pos_main = result.find("Main Code"); + + assert(pos_math < pos_util); + assert(pos_util < pos_main); + + std::cout << "Composition logic verified." << std::endl; +} + +void test_asset_composition() { + std::cout << "Testing Asset-Based Shader Composition..." << std::endl; + + // Use test assets + auto& sc = ShaderComposer::Get(); + + size_t snippet_a_size; + const char* snippet_a_code = + (const char*)GetAsset(AssetId::ASSET_SHADER_SNIPPET_A, &snippet_a_size); + assert(snippet_a_code != nullptr); + sc.RegisterSnippet("SNIPPET_A", std::string(snippet_a_code, snippet_a_size)); + + size_t snippet_b_size; + const char* snippet_b_code = + (const char*)GetAsset(AssetId::ASSET_SHADER_SNIPPET_B, &snippet_b_size); + sc.RegisterSnippet("SNIPPET_B", std::string(snippet_b_code, snippet_b_size)); + + std::string main_code = + "fn main() -> f32 { return snippet_a() + snippet_b(); }"; + std::string result = sc.Compose({"SNIPPET_A", "SNIPPET_B"}, main_code); + + assert(result.find("fn snippet_a()") != std::string::npos); + assert(result.find("fn snippet_b()") != std::string::npos); + assert(result.find("fn main()") != std::string::npos); + + size_t pos_a = result.find("snippet_a"); + size_t pos_b = result.find("snippet_b"); + size_t pos_main = result.find("main"); + + assert(pos_a < pos_b); + assert(pos_b < pos_main); + + std::cout << "Asset-based composition logic verified." << std::endl; +} + +void test_recursive_composition() { + std::cout << "Testing Recursive Shader Composition..." << std::endl; + auto& sc = ShaderComposer::Get(); + + sc.RegisterSnippet("base", "fn base() {}"); + sc.RegisterSnippet("mid", "#include \"base\"\nfn mid() { base(); }"); + sc.RegisterSnippet( + "top", + "#include \"mid\"\n#include \"base\"\nfn top() { mid(); base(); }"); + + std::string main_code = "#include \"top\"\nfn main() { top(); }"; + std::string result = sc.Compose({}, main_code); + + // Verify each is included exactly once despite multiple includes + size_t count_base = 0; + size_t pos = result.find("fn base()"); + while (pos != std::string::npos) { + count_base++; + pos = result.find("fn base()", pos + 1); + } + assert(count_base == 1); + + assert(result.find("Included: top") != std::string::npos); + assert(result.find("Included: mid") != std::string::npos); + assert(result.find("Included: base") != std::string::npos); + + std::cout << "Recursive composition logic verified." << std::endl; +} + +void test_renderer_composition() { + std::cout << "Testing Renderer Shader Composition..." << std::endl; + auto& sc = ShaderComposer::Get(); + + sc.RegisterSnippet("common_uniforms", + "struct GlobalUniforms { view_proj: mat4x4<f32> };"); + sc.RegisterSnippet("math/sdf_shapes", "fn sdSphere() {}"); + sc.RegisterSnippet("render/scene_query", + "#include \"math/sdf_shapes\"\nfn map_scene() {}"); + + std::string main_code = + "#include \"common_uniforms\"\n#include \"render/scene_query\"\nfn " + "main() {}"; + std::string result = sc.Compose({}, main_code); + + assert(result.find("struct GlobalUniforms") != std::string::npos); + assert(result.find("fn sdSphere") != std::string::npos); + assert(result.find("fn map_scene") != std::string::npos); + + std::cout << "Renderer composition logic verified." << std::endl; +} + +int main() { + test_composition(); + test_asset_composition(); + test_recursive_composition(); + test_renderer_composition(); + std::cout << "--- ALL SHADER COMPOSER TESTS PASSED ---" << std::endl; + return 0; +} |
