From 1169000ee0e763a6d09a298eba3eb4d560ff030d Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Fri, 4 Oct 2024 12:42:56 -0400 Subject: lib: Cleanup and add tests Signed-off-by: Andrew Opalach --- include/al/array_typed.h | 5 +++++ include/al/lib.h | 13 ++++--------- include/al/test.h | 13 +++++++++++++ meson.build | 1 + tests/lib.c | 39 +++++++++++++++++++++++++++++++++++++++ tests/lib.h | 8 ++++++++ tests/main.c | 2 ++ 7 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 tests/lib.c create mode 100644 tests/lib.h diff --git a/include/al/array_typed.h b/include/al/array_typed.h index 156dbcb..a48b230 100644 --- a/include/al/array_typed.h +++ b/include/al/array_typed.h @@ -5,6 +5,11 @@ #error "missing required GNU C extensions" #endif +#define AL_EMPTY_DEFAULT(...) AL_EMPTY_DEFAULT_0(__VA_ARGS__, , ) +#define AL_EMPTY_DEFAULT_0(FUNC, T, S, ...) FUNC(T, S) +#define AL_PASTER_EVALUATOR(X, Y) X##_##Y +#define AL_PASTER(X, Y) AL_PASTER_EVALUATOR(X, Y) + #define _array(N, ...) AL_PASTER(array, AL_PASTER(N, __VA_ARGS__)) #define _al_array_init(N, ...) AL_PASTER(_array_init, AL_PASTER(N, __VA_ARGS__)) #define _al_array_data(N, ...) AL_PASTER(_array_data, AL_PASTER(N, __VA_ARGS__)) diff --git a/include/al/lib.h b/include/al/lib.h index 71e0672..32b91ad 100644 --- a/include/al/lib.h +++ b/include/al/lib.h @@ -143,13 +143,6 @@ AL_UNUSED_FUNCTION_POP #define AL_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#ifndef _MSVC_ -#define AL_EMPTY_DEFAULT(...) AL_EMPTY_DEFAULT_0(__VA_ARGS__, , ) -#define AL_EMPTY_DEFAULT_0(FUNC, T, S, ...) FUNC(T, S) -#define AL_PASTER_EVALUATOR(X, Y) X##_##Y -#define AL_PASTER(X, Y) AL_PASTER_EVALUATOR(X, Y) -#endif - #define AL_BOOLSTR(b) ((b) ? "true" : "false") #ifndef _MSVC_ @@ -158,6 +151,8 @@ AL_UNUSED_FUNCTION_POP #define __al_thread __declspec(thread) #endif +#define __al_fallthrough __attribute__((fallthrough)) + #if __has_attribute(__counted_by__) #define __al_counted_by(member) __attribute__((__counted_by__(member))) #else @@ -199,12 +194,12 @@ static inline u16 al_u16_inc_wrap(u16 v) static inline u32 al_u32_add_wrap(u32 v, u32 add) { - return (((u64)v + add) > UINT32_MAX) ? (add - (UINT32_MAX - v)) : v + add; + return (v > UINT32_MAX - add) ? (add - (UINT32_MAX - v)) : v + add; } static inline u16 al_u16_add_wrap(u16 v, u16 add) { - return (((u32)v + add) > UINT16_MAX) ? (add - (UINT16_MAX - v)) : v + add; + return (v > UINT16_MAX - add) ? (add - (UINT16_MAX - v)) : v + add; } AL_UNUSED_FUNCTION_POP diff --git a/include/al/test.h b/include/al/test.h index 532c839..aa4d28b 100644 --- a/include/al/test.h +++ b/include/al/test.h @@ -73,6 +73,19 @@ static inline s32 al_test_bool_cmp(bool a, bool b) #define AL_TEST_FMT_bool "%s" #define AL_TEST_PRINTF_bool(b) (b ? "true" : "false") +static inline s32 al_test_u16_cmp(u16 a, u16 b) +{ + if (a == b) return 0; + else if (a < b) return -1; + else return 1; +} + +#define AL_TEST_CMP_u16 al_test_u16_cmp +#define AL_TEST_TYPE_u16 u16 + +#define AL_TEST_FMT_u16 "%hu" +#define AL_TEST_PRINTF_u16(i) i + static inline s32 al_test_u32_cmp(u32 a, u32 b) { if (a == b) return 0; diff --git a/meson.build b/meson.build index 7251227..a05df28 100644 --- a/meson.build +++ b/meson.build @@ -57,6 +57,7 @@ alabaster = declare_dependency(include_directories: alabaster_inc, if get_option('tests') and not meson.is_subproject() tests_src = [ 'tests/main.c', + 'tests/lib.c', 'tests/array.c', 'tests/str.c', 'tests/ring_buffer.c' diff --git a/tests/lib.c b/tests/lib.c new file mode 100644 index 0000000..f194abc --- /dev/null +++ b/tests/lib.c @@ -0,0 +1,39 @@ +#include + +static bool lib_test_add_wrap(void) +{ + AL_TEST_START("lib_add_wrap"); + + u32 x = UINT32_MAX - 5; + x = al_u32_add_wrap(x, 15); + AL_TEST_EQ(x, 10, u32); + + x = UINT32_MAX; + x = al_u32_add_wrap(x, 1); + AL_TEST_EQ(x, 1, u32); + + x = UINT32_MAX - 25; + x = al_u32_add_wrap(x, 20); + AL_TEST_EQ(x, UINT32_MAX - 5, u32); + x = al_u32_add_wrap(x, 6); + AL_TEST_EQ(x, 1, u32); + + u16 y = UINT16_MAX - 5; + y = al_u16_add_wrap(y, 15); + AL_TEST_EQ(y, 10, u16); + + y = UINT16_MAX; + y = al_u16_add_wrap(y, 1); + AL_TEST_EQ(y, 1, u16); + + AL_TEST_END(); +} + +bool lib_tests_run(void) +{ + AL_TEST_START_GROUP("lib"); + + AL_TEST_RUN(lib_test_add_wrap); + + AL_TEST_END_GROUP(); +} diff --git a/tests/lib.h b/tests/lib.h new file mode 100644 index 0000000..bdb6efc --- /dev/null +++ b/tests/lib.h @@ -0,0 +1,8 @@ +#ifndef _AL_LIB_TESTS_H +#define _AL_LIB_TESTS_H + +#include + +bool lib_tests_run(void); + +#endif // _AL_LIB_TESTS_H diff --git a/tests/main.c b/tests/main.c index 2d50d72..1c8afe2 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,5 +1,6 @@ #include +#include "lib.h" #include "array.h" #ifdef AL_HAVE_GNU_EXTENSIONS #include "array_typed.h" @@ -9,6 +10,7 @@ s32 main(void) { + if (!lib_tests_run()) goto fail; if (!array_tests_run()) goto fail; #ifdef AL_HAVE_GNU_EXTENSIONS if (!array_typed_tests_run()) goto fail; -- cgit v1.2.3-101-g0448