diff --git a/stdlib/Makefile b/stdlib/Makefile index 0314d5926b..cc9f9215e4 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -80,7 +80,7 @@ tests := tst-strtol tst-strtod testmb testrand testsort testdiv \ tst-strtol-locale tst-strtod-nan-locale tst-strfmon_l \ tst-quick_exit tst-thread-quick_exit tst-width \ tst-width-stdint tst-strfrom tst-strfrom-locale \ - tst-getrandom + tst-getrandom tst-on_exit tests-internal := tst-strtod1i tst-strtod3 tst-strtod4 tst-strtod5i \ tst-tls-atexit tst-tls-atexit-nodelete tests-static := tst-secure-getenv diff --git a/stdlib/tst-on_exit.c b/stdlib/tst-on_exit.c new file mode 100644 index 0000000000..0de3b68525 --- /dev/null +++ b/stdlib/tst-on_exit.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include + +#define MAX_ON_EXIT 10 +static int expected[MAX_ON_EXIT]; +static int next_slot = 0; +static int next_expected = 0; + +static void my_on_exit (void (*fn) (int status, void *)); + +static void +fn1 (int status, void *arg) +{ + intptr_t k = (intptr_t) arg; + + printf ("fn1:\t\t%p %d\n", fn1, (int) k); + if (next_slot < 1 || expected[--next_slot] != k) + _exit (1); +} + +static void +fn2 (int status, void *arg) +{ + intptr_t k = (intptr_t) arg; + + printf ("fn2:\t\t%p %d\n", fn2, (int) k); + if (next_slot < 1 || expected[--next_slot] != k) + _exit (1); + my_on_exit (fn1); +} + +static void +fn3 (int status, void *arg) +{ + intptr_t k = (intptr_t) arg; + + printf ("fn3:\t\t%p %d\n", fn3, (int) k); + if (next_slot < 1 || expected[--next_slot] != k) + _exit (1); + my_on_exit (fn2); +} + +static void +my_on_exit (void (*fn) (int, void *)) +{ + intptr_t k = ++next_expected; + + printf ("on_exit:\t%p %d\n", fn, (int) k); + on_exit (fn, (void *) k); + assert (next_slot < MAX_ON_EXIT); + expected[next_slot++] = k; +} + +static int +do_test (void) +{ + my_on_exit (fn2); + my_on_exit (fn1); + my_on_exit (fn2); + my_on_exit (fn3); + return 0; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c"