0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-11-09 20:58:44 +00:00
bird/lib/attribute_cleanup_test.c
Maria Matejka 980ef11016 C Compiler Attribute Cleanup Test
There is a long-known CC attribute cleanup which allows to call a custom
cleanup function when an auto-storage variable ceases to exist. We're
gonna use it for end-of-loop and leave-locked-block macros.

This commit adds a static unit test for this compiler feature to be sure
that it really does what we want. We're looking forward to the next ISO
C norm where this may finally get a nice syntax and standardization.
2023-11-09 23:08:21 +01:00

83 lines
1.4 KiB
C

/*
* BIRD Library -- Auto storage attribute cleanup test
*
* (c) 2023 Maria Matejka <mq@jmq.cz>
* (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 CHECK(n) bt_assert(order_pos++ == (n))
static void
tacd_cleanup(int *val)
{
CHECK(*val);
}
static void
tacd_aux(int pos)
{
CHECK(pos + 0);
UNUSED CLEANUP(tacd_cleanup) int upmost = pos + 18;
if (order_pos > 0)
{
CHECK(pos + 1);
UNUSED CLEANUP(tacd_cleanup) int inner_if = pos + 3;
CHECK(pos + 2);
}
for (int i=0; i<3; i++)
{
CHECK(pos + 4 + 3*i);
UNUSED CLEANUP(tacd_cleanup) int inner_for = pos + 6 + 3*i;
CHECK(pos + 5 + 3*i);
}
for (
CLEANUP(tacd_cleanup) int i = pos + 15;
i < pos + 16; i++)
{
CHECK(pos + 13);
UNUSED CLEANUP(tacd_cleanup) int inner_for = pos + 15;
CHECK(pos + 14);
}
CHECK(pos + 17);
}
#define CHECKCNT 19
static int
t_attribute_cleanup(void)
{
order_pos = 0;
CHECK(0);
for (int i=0; i<3; i++)
{
CHECK(i*(CHECKCNT+3) + 1);
UNUSED 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, "Basic usability of the cleanup attribute");
return bt_exit_value();
}