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/audio/test_jittered_audio.cc | 161 +++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 src/tests/audio/test_jittered_audio.cc (limited to 'src/tests/audio/test_jittered_audio.cc') diff --git a/src/tests/audio/test_jittered_audio.cc b/src/tests/audio/test_jittered_audio.cc new file mode 100644 index 0000000..d8260ec --- /dev/null +++ b/src/tests/audio/test_jittered_audio.cc @@ -0,0 +1,161 @@ +// This file is part of the 64k demo project. +// It tests the ring buffer under jittered consumption (stress test). + +#include + +#if !defined(STRIP_ALL) + +#include "audio/audio.h" +#include "audio/backend/jittered_audio_backend.h" +#include "audio/synth.h" +#include "audio/tracker.h" +#include +#include +#include + +void test_jittered_audio_basic() { + printf("Test: Basic jittered audio consumption...\n"); + + // Initialize audio system + synth_init(); + tracker_init(); + + // Set up jittered backend with realistic parameters + // At 32kHz, 10ms = 320 samples = 160 frames (stereo) + // Jitter of ±5ms means 5-15ms intervals, or 80-240 frames + JitteredAudioBackend jittered_backend; + jittered_backend.set_base_interval(10.0f); // 10ms base interval + jittered_backend.set_jitter_amount(5.0f); // ±5ms jitter + jittered_backend.set_chunk_size_range( + 80, 240); // Realistic chunk sizes for 5-15ms + + audio_set_backend(&jittered_backend); + audio_init(); + + // Start audio thread + audio_start(); + assert(jittered_backend.is_running()); + + // Simulate main loop for 0.1 seconds (quick stress test) + const float total_time = 0.1f; + const float dt = 1.0f / 60.0f; // 60fps + float music_time = 0.0f; + + for (float t = 0.0f; t < total_time; t += dt) { + music_time += dt; // Normal tempo + + // Update tracker and fill buffer + tracker_update(music_time, dt); + audio_render_ahead(music_time, dt); + + // Sleep minimal time to let audio thread run + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + + // Stop audio + audio_shutdown(); + + // Check results + const int frames_consumed = jittered_backend.get_total_frames_consumed(); + const int underruns = jittered_backend.get_underrun_count(); + + printf(" Frames consumed: %d\n", frames_consumed); + printf(" Underruns: %d\n", underruns); + + // Should have consumed some audio (exact amount depends on timing/jitter) + // With minimal sleeps and 0.1s sim time, expect 50-1000 frames + assert(frames_consumed > 50); // At least some audio consumed + assert(frames_consumed < 2000); // Not excessive + + // Underruns are acceptable in this test, but shouldn't be excessive + assert(underruns < 5); // Less than 5 underruns in 0.1 seconds + + printf(" ✓ Basic jittered audio consumption PASSED\n"); +} + +void test_jittered_audio_with_acceleration() { + printf("Test: Jittered audio with tempo acceleration...\n"); + + // Initialize audio system + synth_init(); + tracker_init(); + + // Set up jittered backend with aggressive settings for stress test + // At 32kHz, 15ms = 480 samples = 240 frames (stereo) + // Jitter of ±10ms means 5-25ms intervals, or 80-400 frames + JitteredAudioBackend jittered_backend; + jittered_backend.set_base_interval(15.0f); // Slower consumption + jittered_backend.set_jitter_amount(10.0f); // High jitter + jittered_backend.set_chunk_size_range(80, 400); // Realistic stress test range + + audio_set_backend(&jittered_backend); + audio_init(); + + // Start audio thread + audio_start(); + + // Simulate acceleration scenario (similar to real demo) + const float total_time = 0.6f; + const float dt = 1.0f / 60.0f; + float music_time = 0.0f; + float physical_time = 0.0f; + + for (int frame = 0; frame < 36; ++frame) { // 0.6 seconds @ 60fps + physical_time = frame * dt; + + // Variable tempo (accelerate from 0.3-0.6s) + float tempo_scale = 1.0f; + if (physical_time >= 0.3f && physical_time < 0.6f) { + const float progress = (physical_time - 0.3f) / 0.3f; + tempo_scale = 1.0f + progress * 1.0f; // 1.0 → 2.0 + } + + music_time += dt * tempo_scale; + + // Update tracker and fill buffer + tracker_update(music_time, dt * tempo_scale); + audio_render_ahead(music_time, dt); + + // Sleep minimal time to let audio thread run + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + printf("\n"); + + // Stop audio + audio_shutdown(); + + // Check results + const int frames_consumed = jittered_backend.get_total_frames_consumed(); + const int underruns = jittered_backend.get_underrun_count(); + + printf(" Total frames consumed: %d\n", frames_consumed); + printf(" Total underruns: %d\n", underruns); + + // Should have consumed some audio (exact amount depends on timing/jitter) + // With minimal sleeps and 0.6s sim time, expect more than basic test + assert(frames_consumed > 200); // At least some audio consumed + assert(frames_consumed < 5000); // Not excessive + + // During acceleration with jitter, some underruns are expected but not + // excessive + assert(underruns < 10); // Less than 10 underruns in 0.6 seconds + + printf(" ✓ Jittered audio with acceleration PASSED\n"); +} + +int main() { + printf("Running Jittered Audio Backend tests...\n\n"); + test_jittered_audio_basic(); + test_jittered_audio_with_acceleration(); + printf("\n✅ All Jittered Audio Backend tests PASSED\n"); + return 0; +} + +#else + +int main() { + printf("Jittered Audio Backend tests skipped (STRIP_ALL enabled)\n"); + return 0; +} + +#endif /* !defined(STRIP_ALL) */ -- cgit v1.2.3