* 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.
2025-02-18 Bruno Haible <bruno@clisp.org>
+ 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.
--- /dev/null
+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
--- /dev/null
+/* 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 <https://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+/* 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;
+}
--- /dev/null
+/* 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 <https://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+/* 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;
+}
--- /dev/null
+/* 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 <https://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+/* Specification. */
+#include "at-init.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+/* 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));
+}
--- /dev/null
+#!/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