summaryrefslogtreecommitdiff
path: root/src/tests/audio/test_audio_engine.cc
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-09 20:27:04 +0100
committerskal <pascal.massimino@gmail.com>2026-02-09 20:27:04 +0100
commiteff8d43479e7704df65fae2a80eefa787213f502 (patch)
tree76f2fb8fe8d3db2c15179449df2cf12f7f54e0bf /src/tests/audio/test_audio_engine.cc
parent12378b1b7e9091ba59895b4360b2fa959180a56a (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_engine.cc')
-rw-r--r--src/tests/audio/test_audio_engine.cc182
1 files changed, 182 insertions, 0 deletions
diff --git a/src/tests/audio/test_audio_engine.cc b/src/tests/audio/test_audio_engine.cc
new file mode 100644
index 0000000..3b29dcd
--- /dev/null
+++ b/src/tests/audio/test_audio_engine.cc
@@ -0,0 +1,182 @@
+// This file is part of the 64k demo project.
+// Unit tests for AudioEngine lifecycle and resource management.
+
+#include "audio/audio_engine.h"
+#include "audio/tracker.h"
+#include "generated/assets.h"
+#include <assert.h>
+#include <stdio.h>
+
+#if !defined(STRIP_ALL)
+
+// Test 1: Basic lifecycle (init/shutdown)
+void test_audio_engine_lifecycle() {
+ printf("Test: AudioEngine lifecycle...\n");
+
+ AudioEngine engine;
+ printf(" Created AudioEngine object...\n");
+
+ engine.init();
+ printf(" Initialized AudioEngine...\n");
+
+ // Verify initialization
+ assert(engine.get_active_voice_count() == 0);
+ printf(" Verified voice count is 0...\n");
+
+ engine.shutdown();
+ printf(" Shutdown AudioEngine...\n");
+
+ printf(" ✓ AudioEngine lifecycle test passed\n");
+}
+
+// Test 2: Load music data and verify resource registration
+void test_audio_engine_music_loading() {
+ printf("Test: AudioEngine music data loading...\n");
+
+ AudioEngine engine;
+ engine.init();
+
+ // Load global music data
+ engine.load_music_data(&g_tracker_score, g_tracker_samples,
+ g_tracker_sample_assets, g_tracker_samples_count);
+
+ // Verify resource manager was initialized (samples registered but not loaded
+ // yet)
+ SpectrogramResourceManager* res_mgr = engine.get_resource_manager();
+ assert(res_mgr != nullptr);
+
+ // Initially, no samples should be loaded (lazy loading)
+ assert(res_mgr->get_loaded_count() == 0);
+
+ printf(" ✓ Music data loaded: %u samples registered\n",
+ g_tracker_samples_count);
+
+ engine.shutdown();
+
+ printf(" ✓ AudioEngine music loading test passed\n");
+}
+
+// Test 3: Manual resource loading via resource manager
+void test_audio_engine_manual_resource_loading() {
+ printf("Test: AudioEngine manual resource loading...\n");
+
+ AudioEngine engine;
+ engine.init();
+
+ // Load music data
+ engine.load_music_data(&g_tracker_score, g_tracker_samples,
+ g_tracker_sample_assets, g_tracker_samples_count);
+
+ SpectrogramResourceManager* res_mgr = engine.get_resource_manager();
+ const int initial_loaded = res_mgr->get_loaded_count();
+ assert(initial_loaded == 0); // No samples loaded yet
+
+ // Manually preload first few samples
+ res_mgr->preload(0);
+ res_mgr->preload(1);
+ res_mgr->preload(2);
+
+ const int after_preload = res_mgr->get_loaded_count();
+ printf(" Samples loaded after manual preload: %d\n", after_preload);
+ assert(after_preload == 3); // Should have 3 samples loaded
+
+ // Verify samples are accessible
+ const Spectrogram* spec0 = res_mgr->get_spectrogram(0);
+ const Spectrogram* spec1 = res_mgr->get_spectrogram(1);
+ const Spectrogram* spec2 = res_mgr->get_spectrogram(2);
+
+ assert(spec0 != nullptr);
+ assert(spec1 != nullptr);
+ assert(spec2 != nullptr);
+
+ engine.shutdown();
+
+ printf(" ✓ AudioEngine manual resource loading test passed\n");
+}
+
+// Test 4: Reset and verify state cleanup
+void test_audio_engine_reset() {
+ printf("Test: AudioEngine reset...\n");
+
+ AudioEngine engine;
+ engine.init();
+
+ engine.load_music_data(&g_tracker_score, g_tracker_samples,
+ g_tracker_sample_assets, g_tracker_samples_count);
+
+ SpectrogramResourceManager* res_mgr = engine.get_resource_manager();
+
+ // Manually load some samples
+ res_mgr->preload(0);
+ res_mgr->preload(1);
+ res_mgr->preload(2);
+
+ const int loaded_before_reset = res_mgr->get_loaded_count();
+ assert(loaded_before_reset == 3);
+
+ // Reset engine
+ engine.reset();
+
+ // After reset, state should be cleared
+ assert(engine.get_active_voice_count() == 0);
+
+ // Resources should be marked as unloaded (but memory not freed)
+ const int loaded_after_reset = res_mgr->get_loaded_count();
+ printf(" Loaded count before reset: %d, after reset: %d\n",
+ loaded_before_reset, loaded_after_reset);
+ assert(loaded_after_reset == 0);
+
+ engine.shutdown();
+
+ printf(" ✓ AudioEngine reset test passed\n");
+}
+
+#if !defined(STRIP_ALL)
+// Test 5: Seeking
+void test_audio_engine_seeking() {
+ printf("Test: AudioEngine seeking...\n");
+
+ AudioEngine engine;
+ engine.init();
+
+ engine.load_music_data(&g_tracker_score, g_tracker_samples,
+ g_tracker_sample_assets, g_tracker_samples_count);
+
+ // Seek to t=5.0s
+ engine.seek(5.0f);
+ assert(engine.get_time() == 5.0f);
+
+ // Seek backward to t=2.0s
+ engine.seek(2.0f);
+ assert(engine.get_time() == 2.0f);
+
+ // Seek to beginning
+ engine.seek(0.0f);
+ assert(engine.get_time() == 0.0f);
+
+ engine.shutdown();
+
+ printf(" ✓ AudioEngine seeking test passed\n");
+}
+#endif /* !defined(STRIP_ALL) */
+
+#endif /* !defined(STRIP_ALL) */
+
+int main() {
+#if !defined(STRIP_ALL)
+ printf("Running AudioEngine tests...\n\n");
+
+ test_audio_engine_lifecycle();
+ test_audio_engine_music_loading();
+ test_audio_engine_manual_resource_loading();
+ test_audio_engine_reset();
+ // TODO: Re-enable after debugging
+ // test_audio_engine_seeking();
+
+ printf("\n✅ All AudioEngine tests PASSED\n");
+ return 0;
+#else
+ printf("AudioEngine tests skipped (STRIP_ALL enabled)\n");
+ return 0;
+#endif /* !defined(STRIP_ALL) */
+}