commands: Added tests for command push
authorLukas Krickl <lukas@krickl.dev>
Mon, 2 Mar 2026 11:12:37 +0000 (12:12 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 2 Mar 2026 11:12:37 +0000 (12:12 +0100)
src/p_pc/u_assert.c
src/p_platform.h
src/t_command.c
src/t_command.h
src/test.c
src/tests/t_command.c [new file with mode: 0644]

index 445f254336b0551d060bb6b92088a8a422b64f8a..dcfb77d983d6acf9d70762019a3b08af5c13542c 100644 (file)
@@ -1,6 +1,6 @@
 #include <assert.h>
 
 
-void p_exit(i8 exit_code) {
+void p_impl_exit(i8 exit_code) {
        exit(exit_code);
 }
index 96571869c2048a7310ea4b446fa8ecfae99b9cae..73e32bc8c89d73d9af29933fcbd4aee7987c0bb7 100644 (file)
@@ -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);
 
 
 /**
index 519890fe79f899c5f23e05c415946ad8125d2c1d..885400e62f44a61648e6edc4b4d5572d185567cc 100644 (file)
@@ -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;
index ec53a4ad407295d3f6e232d718b266048d8cc6e7..5bb9d12840fdd9a899424d98384c088b756e83d1 100644 (file)
@@ -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);
index 31c53c59c782ba10a15294fe35d864a0b74535f0..3c84b30e59c78cfe98e62a490c6cc8aa6f0fe4db 100644 (file)
@@ -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 (file)
index 0000000..2d6a033
--- /dev/null
@@ -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;
+}