diff options
author | Daan De Meyer <daan.j.demeyer@gmail.com> | 2023-05-12 08:36:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-12 08:36:43 +0200 |
commit | 32b0be0eb865ede98fafa5cfb5f1871fa402d299 (patch) | |
tree | d5fd716e81dfaf9d71c7622facaa6ce059d7771d /src/test | |
parent | 2bc161dddb9d355d81ca1d2126c94b9c18b9e3cf (diff) | |
parent | 9695b0c01bf3d4b260432fb6754c7fbe9173c7db (diff) | |
download | systemd-32b0be0eb865ede98fafa5cfb5f1871fa402d299.tar.gz |
Merge pull request #27565 from yuwata/static-destruct
static-destruct: support clearing array on exit
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test-static-destruct.c | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/src/test/test-static-destruct.c b/src/test/test-static-destruct.c index cb518ea362..ef8648f588 100644 --- a/src/test/test-static-destruct.c +++ b/src/test/test-static-destruct.c @@ -2,17 +2,38 @@ #include "alloc-util.h" #include "static-destruct.h" +#include "strv.h" #include "tests.h" static int foo = 0; static int bar = 0; static int baz = 0; -static char* memory = NULL; +static char *memory = NULL; +static char **strings = NULL; +static size_t n_strings = 0; +static int *integers = NULL; +static size_t n_integers = 0; static void test_destroy(int *b) { (*b)++; } +static void test_strings_destroy(char **array, size_t n) { + assert_se(n == 3); + assert_se(strv_equal(array, STRV_MAKE("a", "bbb", "ccc"))); + + strv_free(array); +} + +static void test_integers_destroy(int *array, size_t n) { + assert_se(n == 10); + + for (size_t i = 0; i < n; i++) + assert_se(array[i] == (int)(i * i)); + + free(array); +} + STATIC_DESTRUCTOR_REGISTER(foo, test_destroy); STATIC_DESTRUCTOR_REGISTER(bar, test_destroy); STATIC_DESTRUCTOR_REGISTER(bar, test_destroy); @@ -20,15 +41,27 @@ STATIC_DESTRUCTOR_REGISTER(baz, test_destroy); STATIC_DESTRUCTOR_REGISTER(baz, test_destroy); STATIC_DESTRUCTOR_REGISTER(baz, test_destroy); STATIC_DESTRUCTOR_REGISTER(memory, freep); +STATIC_ARRAY_DESTRUCTOR_REGISTER(strings, n_strings, test_strings_destroy); +STATIC_ARRAY_DESTRUCTOR_REGISTER(integers, n_integers, test_integers_destroy); TEST(static_destruct) { + assert_se(foo == 0 && bar == 0 && baz == 0); assert_se(memory = strdup("hallo")); + assert_se(strings = strv_new("a", "bbb", "ccc")); + n_strings = strv_length(strings); + n_integers = 10; + assert_se(integers = new(int, n_integers)); + for (size_t i = 0; i < n_integers; i++) + integers[i] = i * i; - assert_se(foo == 0 && bar == 0 && baz == 0); static_destruct(); - assert_se(foo == 1 && bar == 2 && baz == 3); - assert_se(memory == NULL); + assert_se(foo == 1 && bar == 2 && baz == 3); + assert_se(!memory); + assert_se(!strings); + assert_se(n_strings == 0); + assert_se(!integers); + assert_se(n_integers == 0); } DEFINE_TEST_MAIN(LOG_INFO); |