summaryrefslogtreecommitdiff
path: root/src/tests/audio/test_audio_engine.cc
blob: 98c6aa7549dee7ec73762a7c4687470f82ffed30 (plain)
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
// This file is part of the 64k demo project.
// Unit tests for AudioEngine lifecycle and resource management.

#include "../common/audio_test_fixture.h"
#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");

  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
  SpectrogramResourceManager* res_mgr = fixture.engine().get_resource_manager();
  assert(res_mgr != nullptr);

  // After load_music_data, samples are registered (may be eagerly loaded
  // by tracker_init if already initialized)

  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();

  // After init + load_music, tracker_init eagerly loads all samples
  const int initial_loaded = res_mgr->get_loaded_count();
  printf("  Samples eagerly loaded by tracker_init: %d\n", initial_loaded);
  assert(initial_loaded > 0); // Samples loaded eagerly at init

  // Verify first samples are accessible
  const Spectrogram* spec0 = res_mgr->get_spectrogram(0);
  const Spectrogram* spec1 = res_mgr->get_spectrogram(1);

  assert(spec0 != nullptr);
  assert(spec1 != 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();

  const int loaded_before_reset = res_mgr->get_loaded_count();
  assert(loaded_before_reset > 0);

  // Reset engine
  fixture.engine().reset();

  // After reset, state should be cleared
  assert(fixture.engine().get_active_voice_count() == 0);

  // After reset, tracker re-initializes and reloads samples eagerly
  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);
  // Samples are re-loaded by tracker_init after reset
  assert(loaded_after_reset == loaded_before_reset);

  printf("  ✓ AudioEngine reset test passed\n");
}

#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();

  printf("\n✅ All AudioEngine tests PASSED\n");
  return 0;
#else
  printf("AudioEngine tests skipped (STRIP_ALL enabled)\n");
  return 0;
#endif /* !defined(STRIP_ALL) */
}