Files
jack-looper/test_double_process.c
Loic Coenen f1f1c02314 fix: add missing string.h include in test_double_process.c
Co-authored-by: aider (deepseek/deepseek-coder) <aider@aider.chat>
2026-05-01 16:24:36 +00:00

79 lines
2.5 KiB
C

// TODO: Remove this test after debugging the double status line issue
// test_double_process.c
#include "engine.h"
#include <stdio.h>
#include <assert.h>
#include <string.h>
void test_command_processed_only_once(void) {
printf("Test: Command processed only once...\n");
Engine engine;
memset(&engine, 0, sizeof(engine));
engine.sample_rate = 48000;
engine.control_channel = 0;
engine.quantize_mode = QUANTIZE_OFF;
engine.quantize_threshold = 0;
engine.queued_triggers = NULL;
// Initialize command queue
command_queue_init(&engine.command_queue);
// Initialize clips
for (int i = 0; i < MAX_CLIPS; i++) {
engine.clips[i].state = CLIP_EMPTY;
engine.clips[i].buffer = NULL;
engine.clips[i].buffer_size = 0;
engine.clips[i].write_position = 0;
engine.clips[i].read_position = 0;
}
// Submit a trigger command
engine_trigger_clip(&engine, 0);
// Check queue state
CommandQueue *q = &engine.command_queue;
unsigned int write = atomic_load(&q->write_index);
unsigned int read = atomic_load(&q->read_index);
printf(" Queue before process: write=%u, read=%u, pending=%u\n",
write, read, write - read);
assert(write - read == 1); // One command pending
// Process once
engine_process_commands(&engine);
printf(" After first process: state=%d (expected %d=CLIP_RECORDING)\n",
engine.clips[0].state, CLIP_RECORDING);
assert(engine.clips[0].state == CLIP_RECORDING);
// Check queue state after first process
write = atomic_load(&q->write_index);
read = atomic_load(&q->read_index);
printf(" Queue after first process: write=%u, read=%u, pending=%u\n",
write, read, write - read);
assert(write - read == 0); // No commands pending
// Process again (simulating audio thread calling it)
engine_process_commands(&engine);
printf(" After second process: state=%d (expected %d=CLIP_RECORDING)\n",
engine.clips[0].state, CLIP_RECORDING);
// If state changed, the command was processed twice
if (engine.clips[0].state != CLIP_RECORDING) {
printf(" BUG: State changed to %d on second process!\n", engine.clips[0].state);
} else {
printf(" OK: State unchanged after second process\n");
}
// Cleanup
for (int i = 0; i < MAX_CLIPS; i++) {
free(engine.clips[i].buffer);
}
printf("PASSED\n");
}
int main(void) {
test_command_processed_only_once();
return 0;
}