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/audio/test_audio_backend.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/audio/test_audio_backend.cc')
| -rw-r--r-- | src/tests/audio/test_audio_backend.cc | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/src/tests/audio/test_audio_backend.cc b/src/tests/audio/test_audio_backend.cc new file mode 100644 index 0000000..6a748aa --- /dev/null +++ b/src/tests/audio/test_audio_backend.cc @@ -0,0 +1,130 @@ +// This file is part of the 64k demo project. +// It tests the audio backend abstraction layer. +// Verifies backend injection and event hooks work correctly. + +#include "audio/audio.h" +#include "audio/audio_backend.h" +#include "audio/synth.h" +#include <assert.h> +#include <stdio.h> +#include <vector> + +#if !defined(STRIP_ALL) + +// Simple test backend that records events +class TestBackend : public AudioBackend { + public: + struct Event { + float timestamp; + int spectrogram_id; + float volume; + float pan; + }; + + std::vector<Event> events; + bool init_called = false; + bool start_called = false; + bool shutdown_called = false; + + void init() override { + init_called = true; + } + + void start() override { + start_called = true; + } + + void shutdown() override { + shutdown_called = true; + } + + float get_realtime_peak() override { + // Test backend: return synthetic peak + return 0.5f; + } + + void on_voice_triggered(float timestamp, int spectrogram_id, float volume, + float pan) override { + events.push_back({timestamp, spectrogram_id, volume, pan}); + } +}; + +void test_backend_injection() { + TestBackend backend; + + // Inject test backend before audio_init + audio_set_backend(&backend); + + audio_init(); + assert(backend.init_called); + + audio_start(); + assert(backend.start_called); + + audio_shutdown(); + assert(backend.shutdown_called); + + printf("Backend injection test PASSED\n"); +} + +void test_event_recording() { + TestBackend backend; + audio_set_backend(&backend); + + synth_init(); + + // Create a dummy spectrogram + float data[DCT_SIZE * 2] = {0}; + Spectrogram spec = {data, data, 2}; + int id = synth_register_spectrogram(&spec); + + // Trigger a voice + synth_trigger_voice(id, 0.8f, -0.5f); + + // Render some frames to advance time + float output[1024] = {0}; + synth_render(output, 256); // ~0.008 sec at 32kHz + + // Verify event was recorded + assert(backend.events.size() == 1); + assert(backend.events[0].spectrogram_id == id); + assert(backend.events[0].volume == 0.8f); + assert(backend.events[0].pan == -0.5f); + assert(backend.events[0].timestamp == 0.0f); // Triggered before any render + + // Trigger another voice after rendering + synth_trigger_voice(id, 1.0f, 0.0f); + + assert(backend.events.size() == 2); + assert(backend.events[1].timestamp > 0.0f); // Should be > 0 now + + printf("Event recording test PASSED\n"); +} + +void test_default_backend() { + // Reset backend to nullptr to test default + audio_set_backend(nullptr); + + // This should use MiniaudioBackend by default + audio_init(); + audio_start(); + audio_shutdown(); + + printf("Default backend test PASSED\n"); +} + +#endif /* !defined(STRIP_ALL) */ + +int main() { +#if !defined(STRIP_ALL) + printf("Running Audio Backend tests...\n"); + test_backend_injection(); + test_event_recording(); + test_default_backend(); + printf("All Audio Backend tests PASSED\n"); + return 0; +#else + printf("Audio Backend tests skipped (STRIP_ALL enabled)\n"); + return 0; +#endif /* !defined(STRIP_ALL) */ +} |
