From 9532606e6bc39ffe5128cec60a762545e4550052 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Wed, 1 Jan 2025 14:03:40 -0500 Subject: array: Add offset() to address the data pointer Signed-off-by: Andrew Opalach --- tests/lib.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'tests') diff --git a/tests/lib.c b/tests/lib.c index 64f886d..0923cf1 100644 --- a/tests/lib.c +++ b/tests/lib.c @@ -1,6 +1,12 @@ +#include #include +#include #include +struct test_t { + u32 d; +}; + static bool lib_test_add_wrap(void) { AL_TEST_START("lib_add_wrap"); @@ -30,6 +36,39 @@ static bool lib_test_add_wrap(void) AL_TEST_END(); } +static bool lib_test_offset_vs_at_and_ref(void) +{ + AL_TEST_START("lib_offset_vs_at_and_ref"); + + array(struct test_t *) ar; + al_array_init(ar); + + for (u32 i = 0; i < 10; i++) { + if (i == 4) { + al_array_push(ar, NULL); + } else { + al_array_push(ar, al_alloc_object(struct test_t)); + } + } + + AL_TEST_EQ(&al_array_at(ar, 2), al_array_offset(ar, 2), ptr); + AL_TEST_EQ(&al_array_at(ar, 3), al_array_offset(ar, 3), ptr); + AL_TEST_NEQ(*&al_array_at(ar, 3), NULL, ptr); + AL_TEST_EQ(&al_array_at(ar, 4), al_array_offset(ar, 4), ptr); + AL_TEST_EQ(*&al_array_at(ar, 4), *al_array_offset(ar, 4), ptr); + AL_TEST_NEQ(*&al_array_at(ar, 4), *al_array_offset(ar, 2), ptr); + AL_TEST_EQ(*&al_array_at(ar, 4), NULL, ptr); + AL_TEST_EQ(&al_array_at(ar, 5), al_array_offset(ar, 5), ptr); + + for (u32 i = 0; i < 10; i++) { + if (i != 4) { + al_free(al_array_at(ar, i)); + } + } + + AL_TEST_END(); +} + /* static bool lib_test_add_wrap_atomic(void) { @@ -63,6 +102,7 @@ bool lib_tests_run(void) AL_TEST_START_GROUP("lib"); AL_TEST_RUN(lib_test_add_wrap); + AL_TEST_RUN(lib_test_offset_vs_at_and_ref); //AL_TEST_RUN(lib_test_add_wrap_atomic); AL_TEST_END_GROUP(); -- cgit v1.2.3-101-g0448