From 80dba8647a4e4bbb727aeee66ae36cca34d5590d Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Thu, 9 Nov 2023 22:18:35 +0100 Subject: [PATCH] CC Attribute Test: cleanup. A more thorough test. --- lib/Makefile | 2 +- lib/attribute_cleanup_test.c | 83 ++++++++++++++++++++++++++++++++++++ lib/resource.c | 7 --- 3 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 lib/attribute_cleanup_test.c diff --git a/lib/Makefile b/lib/Makefile index 812f721c..5a90e6b4 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -2,6 +2,6 @@ src := bitmap.c bitops.c blake2s.c blake2b.c checksum.c event.c flowspec.c idm.c obj := $(src-o-files) $(all-daemon) -tests_src := bitmap_test.c heap_test.c buffer_test.c event_test.c flowspec_test.c bitops_test.c patmatch_test.c fletcher16_test.c slist_test.c checksum_test.c lists_test.c mac_test.c ip_test.c hash_test.c printf_test.c slab_test.c +tests_src := attribute_cleanup_test.c bitmap_test.c heap_test.c buffer_test.c event_test.c flowspec_test.c bitops_test.c patmatch_test.c fletcher16_test.c slist_test.c checksum_test.c lists_test.c mac_test.c ip_test.c hash_test.c printf_test.c slab_test.c tests_targets := $(tests_targets) $(tests-target-files) tests_objs := $(tests_objs) $(src-o-files) diff --git a/lib/attribute_cleanup_test.c b/lib/attribute_cleanup_test.c new file mode 100644 index 00000000..31b8cf09 --- /dev/null +++ b/lib/attribute_cleanup_test.c @@ -0,0 +1,83 @@ +/* + * BIRD Library -- Auto storage attribute cleanup test + * + * (c) 2023 Maria Matejka + * (c) 2023 CZ.NIC z.s.p.o. + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#include "test/birdtest.h" + +static int order_pos; +//#define PUSH(n) bt_assert(order_pos++ == (n)) +#define CHECK(n) ASSERT_DIE(order_pos++ == (n)) + +static void +tacd_cleanup(int *val) +{ + CHECK(*val); +} + +static void +tacd_aux(int pos) +{ + CHECK(pos + 0); + + __attribute__((cleanup(tacd_cleanup))) int upmost = pos + 18; + + if (order_pos > 0) + { + CHECK(pos + 1); + __attribute__((cleanup(tacd_cleanup))) int inner_if = pos + 3; + CHECK(pos + 2); + } + + for (int i=0; i<3; i++) + { + CHECK(pos + 4 + 3*i); + __attribute__((cleanup(tacd_cleanup))) int inner_for = pos + 6 + 3*i; + CHECK(pos + 5 + 3*i); + } + + for ( + __attribute__((cleanup(tacd_cleanup))) int i = pos + 15; + i < pos + 16; i++) + { + CHECK(pos + 13); + __attribute__((cleanup(tacd_cleanup))) int inner_for = pos + 15; + CHECK(pos + 14); + } + + CHECK(pos + 17); +} + +#define CHECKCNT 19 + +static int +t_attribute_cleanup_direct(void) +{ + order_pos = 0; + CHECK(0); + + for (int i=0; i<3; i++) + { + CHECK(i*(CHECKCNT+3) + 1); + __attribute__((cleanup(tacd_cleanup))) int inner_for = (i+1) * (CHECKCNT+3); + tacd_aux(i*(CHECKCNT+3) + 2); + CHECK((i+1) * (CHECKCNT+3) - 1); + } + + CHECK(3 * (CHECKCNT+3) + 1); + + return 1; +} + +int main(int argc, char **argv) +{ + bt_init(argc, argv); + + bt_test_suite(t_attribute_cleanup_direct, "Directly using __attribute__((cleanup))"); + + return bt_exit_value(); +} diff --git a/lib/resource.c b/lib/resource.c index 7632dfff..89e559b4 100644 --- a/lib/resource.c +++ b/lib/resource.c @@ -274,12 +274,6 @@ rlookup(unsigned long a) debug("Not found.\n"); } -static void -my_check(int *whatever UNUSED) -{ - printf("Everything works well.\n"); -} - /** * resource_init - initialize the resource manager * @@ -290,7 +284,6 @@ my_check(int *whatever UNUSED) void resource_init(void) { - __attribute__((cleanup(my_check))) int test = 42; resource_sys_init(); root_pool.r.class = &pool_class;