From: Lukas Krickl Date: Mon, 2 Mar 2026 11:12:37 +0000 (+0100) Subject: commands: Added tests for command push X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=0ccf9e81766b86d1771c2e213168da4176485350;p=lrts%2F.git commands: Added tests for command push --- diff --git a/src/p_pc/u_assert.c b/src/p_pc/u_assert.c index 445f254..dcfb77d 100644 --- a/src/p_pc/u_assert.c +++ b/src/p_pc/u_assert.c @@ -1,6 +1,6 @@ #include -void p_exit(i8 exit_code) { +void p_impl_exit(i8 exit_code) { exit(exit_code); } diff --git a/src/p_platform.h b/src/p_platform.h index 9657186..73e32bc 100644 --- a/src/p_platform.h +++ b/src/p_platform.h @@ -62,7 +62,15 @@ u64 p_ticks_per_second(void); */ void p_delay(u64 time); -void p_exit(i8 exit_code); +#ifdef LRTS_TESTS +#define p_exit(exit_code) +#else +/* this is just a wrapper macro around exit + * to allow tests to undefine it and do nothing on exit */ +#define p_exit(exit_code) p_impl_exit(exit_code) +#endif + +void p_impl_exit(i8 exit_code); /** diff --git a/src/t_command.c b/src/t_command.c index 519890f..885400e 100644 --- a/src/t_command.c +++ b/src/t_command.c @@ -2,7 +2,8 @@ struct t_command_queue t_command_queue; -void t_command_queue_process(void) { +u32 t_command_queue_process(void) { + return 0; } lrts_bool t_command_exec(struct t_command *c) { @@ -18,6 +19,7 @@ void t_command_push(struct t_command c) { if (t_command_queue.buffer[t_command_queue.write_idx].type != T_COMMAND_NONE) { u_log(U_LOG_CRIT, "Command queue overflow!\n"); p_exit(P_EXIT_FAIL); + return; } t_command_queue.buffer[t_command_queue.write_idx++] = c; diff --git a/src/t_command.h b/src/t_command.h index ec53a4a..5bb9d12 100644 --- a/src/t_command.h +++ b/src/t_command.h @@ -29,6 +29,7 @@ union t_command_data { struct t_command { enum t_command_type type; enum t_command_flags flags; + u32 run_at_tick; union t_command_data d; }; @@ -46,8 +47,11 @@ struct t_command_queue { extern struct t_command_queue t_command_queue; -/* runs the command queue */ -void t_command_queue_process(void); +/* runs the command queue + * returns number of commands run + * commands are only run if the tick in t_command <= current tick + **/ +u32 t_command_queue_process(void); /* executes a command. Returns true if command was executed. */ lrts_bool t_command_exec(struct t_command *c); diff --git a/src/test.c b/src/test.c index 31c53c5..3c84b30 100644 --- a/src/test.c +++ b/src/test.c @@ -1,17 +1,21 @@ #define LRTS_IMPL #define LRTS_RENDERER_TESTS +#define LRTS_TESTS #include "lrts.h" #include "p_platform.h" + #include "tests/t_defs.h" #ifdef LRTS_IMPL #include "tests/t_args.c" #include "tests/t_arena.c" #include "tests/t_math.c" +#include "tests/t_command.c" #endif + int main(int argc, char **argv) { p_io_init(); @@ -25,6 +29,8 @@ int main(int argc, char **argv) { T_TESTCASE("fixed-point-macro", t_test_fp_macros); T_TESTCASE("fixed-point-sqrt", t_test_fp_sqrt); + T_TESTCASE("command push", test_t_command_push); + T_TESTEND("lrts test"); p_io_finish(); diff --git a/src/tests/t_command.c b/src/tests/t_command.c new file mode 100644 index 0000000..2d6a033 --- /dev/null +++ b/src/tests/t_command.c @@ -0,0 +1,20 @@ +#include "t_defs.h" +#include "../t_command.h" + +int test_t_command_push(void) { + struct t_command t; + u32 i = 0; + + t.type = T_COMMAND_MOVE_CAMERA; + t_command_push(t); + T_ASSERT(t_command_queue.write_idx == 1, ("write index moved\n")); + T_ASSERT(t_command_queue.buffer[0].type == T_COMMAND_MOVE_CAMERA, ("command written\n")); + + for (i = 0; i < T_COMMANDS_MAX+1; i++) { + t_command_push(t); + } + + T_ASSERT(t_command_queue.write_idx == 0, ("write index out of bounds")); + + return 0; +}