diff --git a/test_double_process.c b/test_double_process.c new file mode 100644 index 0000000..0f4fbb8 --- /dev/null +++ b/test_double_process.c @@ -0,0 +1,77 @@ +// TODO: Remove this test after debugging the double status line issue +// test_double_process.c +#include "engine.h" +#include +#include + +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; +}