]> Savannah Git Hosting - gnulib.git/commitdiff
at-init: Add tests.
authorBruno Haible <bruno@clisp.org>
Tue, 18 Feb 2025 11:03:37 +0000 (12:03 +0100)
committerBruno Haible <bruno@clisp.org>
Tue, 18 Feb 2025 11:03:37 +0000 (12:03 +0100)
* 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.

ChangeLog
modules/at-init-tests [new file with mode: 0644]
tests/test-at-init-2.c [new file with mode: 0644]
tests/test-at-init-3.c [new file with mode: 0644]
tests/test-at-init.c [new file with mode: 0644]
tests/test-at-init.sh [new file with mode: 0755]

index ed82d503460533fdb8cabb751f768f5b7ab0b164..fd7b76b9103c95c7edc9a1c93216773421f72e3a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 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.
diff --git a/modules/at-init-tests b/modules/at-init-tests
new file mode 100644 (file)
index 0000000..5bf6e73
--- /dev/null
@@ -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 (file)
index 0000000..9b0ca7f
--- /dev/null
@@ -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 <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;
+}
diff --git a/tests/test-at-init-3.c b/tests/test-at-init-3.c
new file mode 100644 (file)
index 0000000..57c39f2
--- /dev/null
@@ -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 <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;
+}
diff --git a/tests/test-at-init.c b/tests/test-at-init.c
new file mode 100644 (file)
index 0000000..238a54d
--- /dev/null
@@ -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 <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));
+}
diff --git a/tests/test-at-init.sh b/tests/test-at-init.sh
new file mode 100755 (executable)
index 0000000..1f07834
--- /dev/null
@@ -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