From: Bruno Haible Date: Tue, 18 Feb 2025 11:03:37 +0000 (+0100) Subject: at-init: Add tests. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=4cba83c6240dbf85dfcd663953f558b4ef2920ae;p=gnulib.git at-init: Add tests. * tests/test-at-init.sh: New file. * tests/test-at-init.c: New file. * tests/test-at-init-2.c: New file. * tests/test-at-init-3.c: New file. * modules/at-init-tests: New file. --- diff --git a/ChangeLog b/ChangeLog index ed82d50346..fd7b76b910 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2025-02-18 Bruno Haible + at-init: Add tests. + * tests/test-at-init.sh: New file. + * tests/test-at-init.c: New file. + * tests/test-at-init-2.c: New file. + * tests/test-at-init-3.c: New file. + * modules/at-init-tests: New file. + at-init: New module. * lib/at-init.h: New file. * lib/at-init.c: New file. diff --git a/modules/at-init-tests b/modules/at-init-tests new file mode 100644 index 0000000000..5bf6e73ac1 --- /dev/null +++ b/modules/at-init-tests @@ -0,0 +1,15 @@ +Files: +tests/test-at-init.sh +tests/test-at-init.c +tests/test-at-init-2.c +tests/test-at-init-3.c + +Depends-on: +write + +configure.ac: + +Makefile.am: +TESTS += test-at-init.sh +check_PROGRAMS += test-at-init +test_at_init_SOURCES = test-at-init.c test-at-init-2.c test-at-init-3.c diff --git a/tests/test-at-init-2.c b/tests/test-at-init-2.c new file mode 100644 index 0000000000..9b0ca7f6e2 --- /dev/null +++ b/tests/test-at-init-2.c @@ -0,0 +1,43 @@ +/* Test of computed initialization. + Copyright (C) 2025 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +/* Specification. */ +#include "at-init.h" + +static int sum_of_squares; + +AT_INIT (init_squares); +#ifdef __SUNPRO_C +# pragma init (init_squares) +#endif + +static void +init_squares (void) +{ + int i; + + sum_of_squares = 0; + for (i = 0; i <= 100; i++) + sum_of_squares += i * i; +} + +int +get_squares (void) +{ + return sum_of_squares; +} diff --git a/tests/test-at-init-3.c b/tests/test-at-init-3.c new file mode 100644 index 0000000000..57c39f2e02 --- /dev/null +++ b/tests/test-at-init-3.c @@ -0,0 +1,43 @@ +/* Test of computed initialization. + Copyright (C) 2025 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +/* Specification. */ +#include "at-init.h" + +static int sum_of_cubes; + +AT_INIT (init_cubes); +#ifdef __SUNPRO_C +# pragma init (init_cubes) +#endif + +static void +init_cubes (void) +{ + int i; + + sum_of_cubes = 0; + for (i = 0; i <= 100; i++) + sum_of_cubes += i * i *i; +} + +int +get_cubes (void) +{ + return sum_of_cubes; +} diff --git a/tests/test-at-init.c b/tests/test-at-init.c new file mode 100644 index 0000000000..238a54d1df --- /dev/null +++ b/tests/test-at-init.c @@ -0,0 +1,62 @@ +/* Test of computed initialization. + Copyright (C) 2025 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +/* Specification. */ +#include "at-init.h" + +#include +#include +#include + +/* Avoid an error in the pragma on MSVC. */ +#undef read + +extern int get_squares (void); +extern int get_cubes (void); + +AT_INIT (initial_message); +#ifdef __SUNPRO_C +# pragma init (initial_message) +#endif + +static void +initial_message (void) +{ + const char message[] = "Initializing..."; + write (STDOUT_FILENO, message, strlen (message)); +} + +AT_FINI (final_message); +#ifdef __SUNPRO_C +# pragma fini (final_message) +#endif + +static void +final_message (void) +{ + const char message[] = "Finishing..."; + write (STDOUT_FILENO, message, strlen (message)); +} + +int +main () +{ + char message[100]; + sprintf (message, "Main...%d...%d...", get_squares (), get_cubes ()); + write (STDOUT_FILENO, message, strlen (message)); +} diff --git a/tests/test-at-init.sh b/tests/test-at-init.sh new file mode 100755 index 0000000000..1f078344de --- /dev/null +++ b/tests/test-at-init.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +output=`${CHECKER} ./test-at-init${EXEEXT}` +if test "$output" != 'Initializing...Main...338350...25502500...Finishing...'; then + echo "Got output: $output" 1>&2 + exit 1 +fi + +exit 0