1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
// 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 "../common/audio_test_fixture.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");
AudioTestFixture fixture;
printf(" Created and initialized AudioEngine...\n");
// Verify initialization
assert(fixture.engine().get_active_voice_count() == 0);
printf(" Verified voice count is 0...\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");
AudioTestFixture fixture;
// Load global music data
fixture.load_music(&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 = fixture.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);
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");
AudioTestFixture fixture;
// Load music data
fixture.load_music(&g_tracker_score, g_tracker_samples,
g_tracker_sample_assets, g_tracker_samples_count);
SpectrogramResourceManager* res_mgr = fixture.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);
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");
AudioTestFixture fixture;
fixture.load_music(&g_tracker_score, g_tracker_samples,
g_tracker_sample_assets, g_tracker_samples_count);
SpectrogramResourceManager* res_mgr = fixture.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
fixture.engine().reset();
// After reset, state should be cleared
assert(fixture.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);
printf(" ✓ AudioEngine reset test passed\n");
}
#if !defined(STRIP_ALL)
// Test 5: Seeking
void test_audio_engine_seeking() {
printf("Test: AudioEngine seeking...\n");
AudioTestFixture fixture;
fixture.load_music(&g_tracker_score, g_tracker_samples,
g_tracker_sample_assets, g_tracker_samples_count);
// Seek to t=5.0s
fixture.engine().seek(5.0f);
assert(fixture.engine().get_time() == 5.0f);
// Seek backward to t=2.0s
fixture.engine().seek(2.0f);
assert(fixture.engine().get_time() == 2.0f);
// Seek to beginning
fixture.engine().seek(0.0f);
assert(fixture.engine().get_time() == 0.0f);
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) */
}
|