-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_FastModeGeneration.cpp
More file actions
351 lines (286 loc) · 12.4 KB
/
test_FastModeGeneration.cpp
File metadata and controls
351 lines (286 loc) · 12.4 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
==============================================================================
test_FastModeGeneration.cpp
Created: 2 Aug 2025
Author: Epic 7 Story 7.1 Task 7.1.6 Implementation
Unit tests for Fast Mode generation quality and timing validation.
Tests ensure sub-2-second generation and musical quality standards.
==============================================================================
*/
#include <gtest/gtest.h>
#include <juce_audio_processors/juce_audio_processors.h>
#include <juce_core/juce_core.h>
#include <chrono>
#include "ai/AIGenerationEngine.h"
#include "GenerationParameters.h"
#include "MIDIPattern.h"
#include "PatternManager.h"
#include "ThreadManager.h"
//==============================================================================
class FastModeGenerationTest : public ::testing::Test
{
protected:
void SetUp() override
{
threadManager = std::make_unique<ThreadManager>();
patternManager = std::make_unique<PatternManager>();
aiEngine = std::make_unique<AIGenerationEngine>(*threadManager, *patternManager);
// Setup default parameters for Fast Mode
setupDefaultParameters();
}
void TearDown() override
{
aiEngine.reset();
patternManager.reset();
threadManager.reset();
}
void setupDefaultParameters()
{
params.rhythmicComplexity = 0.5f;
params.tempo = 120.0f;
params.key = 0; // C
params.scale = GenerationParameters::ScaleType::Major;
params.generationType = GenerationParameters::GenerationType::Melody;
params.patternLengthBeats = 16.0f; // 4 bars * 4 beats/bar
params.aiMode = GenerationParameters::AIMode::Fast;
params.generationSeed = 12345; // Deterministic for testing
}
std::unique_ptr<ThreadManager> threadManager;
std::unique_ptr<PatternManager> patternManager;
std::unique_ptr<AIGenerationEngine> aiEngine;
GenerationParameters params;
};
//==============================================================================
// Task 7.1.6: Fast Mode Generation Timing Tests
TEST_F(FastModeGenerationTest, GenerationTimingUnder2Seconds)
{
// Test requirement: Fast Mode must generate patterns in under 2 seconds
auto startTime = std::chrono::high_resolution_clock::now();
// Trigger generation
aiEngine->generatePattern(params);
// Wait for completion (with timeout)
int waitCount = 0;
const int maxWaitMs = 3000; // 3 second timeout
const int checkIntervalMs = 10;
while (!patternManager->getCurrentPattern().has_value() && waitCount < maxWaitMs)
{
juce::Thread::sleep(checkIntervalMs);
waitCount += checkIntervalMs;
}
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
// Verify timing requirement
EXPECT_LT(duration.count(), 2000) << "Fast Mode generation took " << duration.count() << "ms, exceeding 2-second target";
// Verify pattern was generated
EXPECT_TRUE(patternManager->getCurrentPattern().has_value()) << "No pattern was generated";
EXPECT_FALSE(patternManager->getCurrentPattern()->notes.empty()) << "No pattern was generated";
}
TEST_F(FastModeGenerationTest, ConsistentTimingAcrossComplexity)
{
// Test timing consistency across different complexity levels
std::vector<float> complexityLevels = {0.1f, 0.3f, 0.5f, 0.7f, 0.9f};
std::vector<long> timings;
for (float complexity : complexityLevels)
{
params.rhythmicComplexity = complexity;
patternManager->clear(); // Reset for next test
auto startTime = std::chrono::high_resolution_clock::now();
aiEngine->generatePattern(params);
// Wait for completion
int waitCount = 0;
while (!patternManager->getCurrentPattern().has_value() && waitCount < 2500)
{
juce::Thread::sleep(10);
waitCount += 10;
}
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
timings.push_back(duration.count());
EXPECT_LT(duration.count(), 2000) << "Complexity " << complexity << " took " << duration.count() << "ms";
}
// Verify timing consistency (no outliers)
for (auto timing : timings)
{
EXPECT_LT(timing, 2000) << "Timing outlier detected: " << timing << "ms";
}
}
//==============================================================================
// Task 7.1.6: Fast Mode Generation Quality Tests
TEST_F(FastModeGenerationTest, GeneratedPatternStructure)
{
aiEngine->generatePattern(params);
// Wait for generation
int waitCount = 0;
while (!patternManager->getCurrentPattern().has_value() && waitCount < 2000)
{
juce::Thread::sleep(10);
waitCount += 10;
}
const auto& patternOpt = patternManager->getCurrentPattern();
ASSERT_TRUE(patternOpt.has_value());
const auto& pattern = *patternOpt;
// Verify basic pattern structure
EXPECT_FALSE(pattern.notes.empty()) << "Pattern should contain notes";
EXPECT_GT(pattern.notes.size(), 3) << "Pattern should have reasonable note count";
EXPECT_LT(pattern.notes.size(), 200) << "Pattern should not be excessively dense";
// Verify timing bounds
for (const auto& note : pattern.notes)
{
EXPECT_GE(note.startTime, 0.0) << "Note start time should be non-negative";
EXPECT_GT(note.duration, 0.0) << "Note duration should be positive";
EXPECT_GE(note.pitch, 21) << "Note pitch should be within MIDI range (>= A0)";
EXPECT_LE(note.pitch, 108) << "Note pitch should be within MIDI range (<= C8)";
EXPECT_GE(note.velocity, 1) << "Note velocity should be positive";
EXPECT_LE(note.velocity, 127) << "Note velocity should be within MIDI range";
}
}
TEST_F(FastModeGenerationTest, GenreSpecificPatterns)
{
// Test genre-specific pattern generation
std::vector<GenerationParameters::GenerationType> genres = {
GenerationParameters::GenerationType::Melody,
GenerationParameters::GenerationType::Chords,
GenerationParameters::GenerationType::Bassline,
GenerationParameters::GenerationType::Drums
};
for (const auto& genre : genres)
{
params.generationType = genre;
patternManager->clear();
aiEngine->generatePattern(params);
// Wait for generation
int waitCount = 0;
while (!patternManager->getCurrentPattern().has_value() && waitCount < 2000)
{
juce::Thread::sleep(10);
waitCount += 10;
}
const auto& patternOpt = patternManager->getCurrentPattern();
ASSERT_TRUE(patternOpt.has_value());
const auto& pattern = *patternOpt;
EXPECT_FALSE(pattern.notes.empty()) << "Genre " << (int)genre << " should generate valid pattern";
// Genre-specific validation could be added here
// For now, verify basic structure
EXPECT_GT(pattern.notes.size(), 2) << "Genre " << (int)genre << " should generate sufficient notes";
}
}
TEST_F(FastModeGenerationTest, DeterministicGeneration)
{
// Test that same seed produces same pattern
// Generate first pattern
aiEngine->generatePattern(params);
int waitCount = 0;
while (!patternManager->getCurrentPattern().has_value() && waitCount < 2000)
{
juce::Thread::sleep(10);
waitCount += 10;
}
ASSERT_TRUE(patternManager->getCurrentPattern().has_value());
auto firstPattern = *patternManager->getCurrentPattern();
// Generate second pattern with same seed
patternManager->clear();
aiEngine->generatePattern(params);
waitCount = 0;
while (!patternManager->getCurrentPattern().has_value() && waitCount < 2000)
{
juce::Thread::sleep(10);
waitCount += 10;
}
ASSERT_TRUE(patternManager->getCurrentPattern().has_value());
auto secondPattern = *patternManager->getCurrentPattern();
// Verify patterns are identical
EXPECT_EQ(firstPattern.notes.size(), secondPattern.notes.size())
<< "Patterns generated with the same seed should have the same number of notes.";
if (firstPattern.notes.size() == secondPattern.notes.size())
{
for (size_t i = 0; i < firstPattern.notes.size(); ++i)
{
EXPECT_FLOAT_EQ(firstPattern.notes[i].startTime, secondPattern.notes[i].startTime)
<< "Note " << i << " start time mismatch.";
EXPECT_FLOAT_EQ(firstPattern.notes[i].duration, secondPattern.notes[i].duration)
<< "Note " << i << " duration mismatch.";
EXPECT_EQ(firstPattern.notes[i].pitch, secondPattern.notes[i].pitch)
<< "Note " << i << " pitch mismatch.";
EXPECT_EQ(firstPattern.notes[i].velocity, secondPattern.notes[i].velocity)
<< "Note " << i << " velocity mismatch.";
}
}
}
TEST_F(FastModeGenerationTest, ComplexityScaling)
{
// Test that complexity parameter affects pattern generation
// Low complexity
params.rhythmicComplexity = 0.1f;
aiEngine->generatePattern(params);
int waitCount = 0;
while (!patternManager->getCurrentPattern().has_value() && waitCount < 2000)
{
juce::Thread::sleep(10);
waitCount += 10;
}
ASSERT_TRUE(patternManager->getCurrentPattern().has_value());
auto lowComplexityPattern = *patternManager->getCurrentPattern();
// High complexity
params.rhythmicComplexity = 0.9f;
params.generationSeed = 12346; // Different seed to avoid identical patterns
patternManager->clear();
aiEngine->generatePattern(params);
waitCount = 0;
while (!patternManager->getCurrentPattern().has_value() && waitCount < 2000)
{
juce::Thread::sleep(10);
waitCount += 10;
}
ASSERT_TRUE(patternManager->getCurrentPattern().has_value());
auto highComplexityPattern = *patternManager->getCurrentPattern();
// Verify both patterns exist
EXPECT_FALSE(lowComplexityPattern.notes.empty()) << "Low complexity should generate pattern";
EXPECT_FALSE(highComplexityPattern.notes.empty()) << "High complexity should generate pattern";
// Generally, higher complexity should produce more notes or more varied timing
// This is a basic heuristic test
EXPECT_GT(highComplexityPattern.notes.size(), 0) << "High complexity should produce notes";
}
//==============================================================================
// Performance Benchmarking Tests
TEST_F(FastModeGenerationTest, PerformanceBenchmark)
{
// Benchmark multiple generations to verify consistent performance
const int iterations = 5;
std::vector<long> timings;
for (int i = 0; i < iterations; ++i)
{
params.generationSeed = 10000 + i; // Vary seed
patternManager->clear();
auto startTime = std::chrono::high_resolution_clock::now();
aiEngine->generatePattern(params);
int waitCount = 0;
while (!patternManager->getCurrentPattern().has_value() && waitCount < 2500)
{
juce::Thread::sleep(10);
waitCount += 10;
}
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
timings.push_back(duration.count());
}
// Calculate statistics
long totalTime = 0;
long maxTime = 0;
long minTime = LONG_MAX;
for (auto time : timings)
{
totalTime += time;
maxTime = std::max(maxTime, time);
minTime = std::min(minTime, time);
}
double avgTime = static_cast<double>(totalTime) / iterations;
// Performance assertions
EXPECT_LT(avgTime, 1500.0) << "Average generation time should be under 1.5 seconds";
EXPECT_LT(maxTime, 2000) << "Maximum generation time should be under 2 seconds";
EXPECT_GT(minTime, 50) << "Minimum time should be reasonable (not suspiciously fast)";
// Log performance metrics for analysis
std::cout << "\nFast Mode Performance Metrics:\n";
std::cout << "Average: " << avgTime << "ms\n";
std::cout << "Min: " << minTime << "ms\n";
std::cout << "Max: " << maxTime << "ms\n";
}