From e6d59a7b506f610b82340b62457f9dd9ad0cb53d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 19 Feb 2026 13:30:45 +0100 Subject: feat: integrate basic CMocka tests --- src/error.S | 3 ++ src/helpers/function.S | 4 +++ src/snake.S | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 src/snake.S (limited to 'src') diff --git a/src/error.S b/src/error.S index 44be8b0..50597dd 100644 --- a/src/error.S +++ b/src/error.S @@ -1,6 +1,8 @@ #include "helpers/function.S" #include "SDL/error.S" +#ifndef __STDC_VERSION__ + .section .text //! @function _print_sdl_error(char const * format) @@ -19,3 +21,4 @@ function_end +#endif \ No newline at end of file diff --git a/src/helpers/function.S b/src/helpers/function.S index af41905..eeada7c 100644 --- a/src/helpers/function.S +++ b/src/helpers/function.S @@ -1,3 +1,5 @@ +#ifndef __STDC_VERSION__ + .set .L_FUNCTION_IS_IN_FUNCTION_DEFINITION, 0 .set .L_FUNCTION_LOCALS_ALLOCATED, 0 @@ -122,3 +124,5 @@ .cfi_def_cfa_register %rbp .set .L\name\()_FRAME_OFFSET, 0 .endm + +#endif \ No newline at end of file diff --git a/src/snake.S b/src/snake.S new file mode 100644 index 0000000..00d66e2 --- /dev/null +++ b/src/snake.S @@ -0,0 +1,77 @@ +//! @file snake.S +//! +//! Type definitions, sizes, and the interface for the snake. + +#include "helpers/function.S" + +#define OFFSET_snake_t_x 0 +#define SIZE_snake_t_x 4 + +#define OFFSET_snake_t_y OFFSET_snake_t_x + SIZE_snake_t_x +#define SIZE_snake_t_y 4 + +#define OFFSET_snake_t_length OFFSET_snake_t_y + SIZE_snake_t_y +#define SIZE_snake_t_length 4 + +#define OFFSET_snake_t_direction OFFSET_snake_t_length + SIZE_snake_t_length +#define SIZE_snake_t_direction 1 + +#define OFFSET_snake_t_padding OFFSET_snake_t_direction + SIZE_snake_t_direction +#define SIZE_snake_t_padding 3 + +#define SIZE_snake_t OFFSET_snake_t_padding + SIZE_snake_t_padding + +#ifndef __STDC_VERSION__ + +.section .text + + //! @fn void snake_init(snake_t * self, int x, int y, int length) + //! @param (%rdi) self the object to initialize + //! @param (%esi) x the x position of the head of the snake + //! @param (%edx) y the y position of the head of the snake + //! @param (%ecx) length the length of the snake + function_begin snake_init + + test %rdi, %rdi + jz .Lexit_snake_init + + mov %esi, OFFSET_snake_t_x(%rdi) + mov %edx, OFFSET_snake_t_y(%rdi) + mov %ecx, OFFSET_snake_t_length(%rdi) + xor %al, %al + mov %al, OFFSET_snake_t_direction(%rdi) + + function_end + +#else + +#include + +typedef struct { + int x; + int y; + int length; + char direction; + char padding[3]; +} snake_t; + +static_assert(sizeof(snake_t) == SIZE_snake_t); + +static_assert(offsetof(snake_t, x) == OFFSET_snake_t_x); +static_assert(sizeof(((snake_t*)0)->x) == SIZE_snake_t_x); + +static_assert(offsetof(snake_t, y) == OFFSET_snake_t_y); +static_assert(sizeof(((snake_t*)0)->y) == SIZE_snake_t_y); + +static_assert(offsetof(snake_t, length) == OFFSET_snake_t_length); +static_assert(sizeof(((snake_t*)0)->length) == SIZE_snake_t_length); + +static_assert(offsetof(snake_t, direction) == OFFSET_snake_t_direction); +static_assert(sizeof(((snake_t*)0)->direction) == SIZE_snake_t_direction); + +static_assert(offsetof(snake_t, padding) == OFFSET_snake_t_padding); +static_assert(sizeof(((snake_t*)0)->padding) == SIZE_snake_t_padding); + +void snake_init(snake_t * self, int x, int y, int length); + +#endif \ No newline at end of file -- cgit v1.2.3