From 3cc1265adcb39a8c0e76f6ff06ade54f338b0d17 Mon Sep 17 00:00:00 2001 From: matt335672 <30179339+matt335672@users.noreply.github.com> Date: Mon, 24 Feb 2025 14:12:44 +0000 Subject: [PATCH] Add test suite calls for list16 Following a re-write of the list16 module to avoid memory allocation issues, a test suite is added for the module. --- common/list16.c | 1 + common/list16.h | 3 + tests/common/Makefile.am | 1 + tests/common/test_common.h | 1 + tests/common/test_common_main.c | 1 + tests/common/test_list16_calls.c | 113 +++++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+) create mode 100644 tests/common/test_list16_calls.c diff --git a/common/list16.c b/common/list16.c index 77eccd3c..f9bd3273 100644 --- a/common/list16.c +++ b/common/list16.c @@ -66,6 +66,7 @@ list16_deinit(struct list16 *self) if (self->items != self->mitems) { g_free(self->items); + self->items = self->mitems; // Prevent double=free } } diff --git a/common/list16.h b/common/list16.h index 85e38c14..58b6df2c 100644 --- a/common/list16.h +++ b/common/list16.h @@ -36,8 +36,11 @@ struct list16 * list16_create(void); void list16_delete(struct list16 *self); +/* Initialise a stack-based list16 */ void list16_init(struct list16 *self); +/* Free any memory allocated to a list16. + * After this call, list16_init() must be called again to re-use the list */ void list16_deinit(struct list16 *self); /* Returns != 0 if item added successfully */ diff --git a/tests/common/Makefile.am b/tests/common/Makefile.am index 07259d62..52de5565 100644 --- a/tests/common/Makefile.am +++ b/tests/common/Makefile.am @@ -16,6 +16,7 @@ test_common_SOURCES = \ test_common_main.c \ test_fifo_calls.c \ test_list_calls.c \ + test_list16_calls.c \ test_parse.c \ test_string_calls.c \ test_string_calls_unicode.c \ diff --git a/tests/common/test_common.h b/tests/common/test_common.h index c496b425..12995411 100644 --- a/tests/common/test_common.h +++ b/tests/common/test_common.h @@ -9,6 +9,7 @@ bin_to_hex(const char *input, int length); Suite *make_suite_test_fifo(void); Suite *make_suite_test_list(void); +Suite *make_suite_test_list16(void); Suite *make_suite_test_parse(void); Suite *make_suite_test_string(void); Suite *make_suite_test_string_unicode(void); diff --git a/tests/common/test_common_main.c b/tests/common/test_common_main.c index 6bdda940..6107e2b5 100644 --- a/tests/common/test_common_main.c +++ b/tests/common/test_common_main.c @@ -48,6 +48,7 @@ int main (void) sr = srunner_create (make_suite_test_fifo()); srunner_add_suite(sr, make_suite_test_list()); + srunner_add_suite(sr, make_suite_test_list16()); srunner_add_suite(sr, make_suite_test_parse()); srunner_add_suite(sr, make_suite_test_string()); srunner_add_suite(sr, make_suite_test_string_unicode()); diff --git a/tests/common/test_list16_calls.c b/tests/common/test_list16_calls.c new file mode 100644 index 00000000..cc1d81d4 --- /dev/null +++ b/tests/common/test_list16_calls.c @@ -0,0 +1,113 @@ +#if defined(HAVE_CONFIG_H) +#include "config_ac.h" +#endif + +#include "list16.h" + +#include "os_calls.h" +#include "test_common.h" +#include "string_calls.h" + +#define TEST_LIST16_SIZE 1000 + +START_TEST(test_list16__simple) +{ + struct list16 *lst = list16_create(); + int i; + tui16 val; + int res; + for (i = 0 ; i < TEST_LIST16_SIZE ; ++i) + { + res = list16_add_item(lst, (tui16)i); + ck_assert_int_ne(res, 0); + } + + ck_assert_int_eq(lst->count, TEST_LIST16_SIZE); + for (i = 0 ; i < TEST_LIST16_SIZE ; ++i) + { + ck_assert_int_eq(lst->items[i], (tui16)i); + // Also check get method + val = list16_get_item(lst, i); + ck_assert_int_eq(val, (tui16)i); + } + + /* Out-of-bounds test */ + val = list16_get_item(lst, TEST_LIST16_SIZE); + ck_assert_int_eq(val, 0); + + i = list16_index_of(lst, 50); + ck_assert_int_eq(i, 50); + + list16_remove_item(lst, 10); + ck_assert_int_eq(lst->count, TEST_LIST16_SIZE - 1); + /* Check values before the deleted item */ + for (i = 0; i < 10; ++i) + { + val = list16_get_item(lst, i); + ck_assert_int_eq(val, (tui16)i); + } + /* Check values after the deleted item */ + for (i = 10; i < lst->count; ++i) + { + val = list16_get_item(lst, i); + ck_assert_int_eq(val, (tui16)(i + 1)); + } + + list16_insert_item(lst, 10, 10); + ck_assert_int_eq(lst->count, TEST_LIST16_SIZE); + /* Re-check all values */ + for (i = 0; i < lst->count; ++i) + { + val = list16_get_item(lst, i); + ck_assert_int_eq(val, (tui16)i); + } + + list16_insert_item(lst, 0, 99); + ck_assert_int_eq(lst->count, TEST_LIST16_SIZE + 1); + val = list16_get_item(lst, 10); + ck_assert_int_eq(val, 9); + + list16_clear(lst); + ck_assert_int_eq(lst->count, 0); + list16_delete(lst); +} +END_TEST + +/* Tests stack calls work as expected. Run under valgrind to check memory */ +START_TEST(test_list16__stack) +{ + int i; + int res; + struct list16 lst; + + list16_init(&lst); + ck_assert_int_eq(lst.count, 0); + list16_deinit(&lst); + list16_deinit(&lst); // Shouldn't happen, but could result in double-free + + list16_init(&lst); + for (i = 0 ; i < TEST_LIST16_SIZE ; ++i) + { + res = list16_add_item(&lst, (tui16)i); + ck_assert_int_ne(res, 0); + } + ck_assert_int_eq(lst.count, TEST_LIST16_SIZE); + list16_deinit(&lst); +} + +/******************************************************************************/ +Suite * +make_suite_test_list16(void) +{ + Suite *s; + TCase *tc_simple; + + s = suite_create("List16"); + + tc_simple = tcase_create("simple"); + suite_add_tcase(s, tc_simple); + tcase_add_test(tc_simple, test_list16__simple); + tcase_add_test(tc_simple, test_list16__stack); + + return s; +}