Move c-stack test into testsuite.
authorEric Blake <ebb9@byu.net>
Tue, 17 Jun 2008 14:43:38 +0000 (08:43 -0600)
committerEric Blake <ebb9@byu.net>
Tue, 17 Jun 2008 15:53:57 +0000 (09:53 -0600)
* modules/c-stack-tests: New file.
* lib/c-stack.c [DEBUG]: Move test program...
* tests/test-c-stack.c: ...into this new file.  Skip rather than
fail test if sigaltstack is lacking.
* tests/test-c-stack.sh: New driver file.

Signed-off-by: Eric Blake <ebb9@byu.net>
ChangeLog
lib/c-stack.c
modules/c-stack-tests [new file with mode: 0644]
tests/test-c-stack.c [new file with mode: 0644]
tests/test-c-stack.sh [new file with mode: 0755]

index 0976ec3..80c51b8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2008-06-17  Eric Blake  <ebb9@byu.net>
+
+       Move c-stack test into testsuite.
+       * modules/c-stack-tests: New file.
+       * lib/c-stack.c [DEBUG]: Move test program...
+       * tests/test-c-stack.c: ...into this new file.  Skip rather than
+       fail test if sigaltstack is lacking.
+       * tests/test-c-stack.sh: New driver file.
+
 2008-06-16  Eric Blake  <ebb9@byu.net>
 
        Use raise module consistently.
index a7ebafa..7129036 100644 (file)
@@ -76,10 +76,6 @@ typedef struct sigaltstack stack_t;
 # define STDERR_FILENO 2
 #endif
 
-#if DEBUG
-# include <stdio.h>
-#endif
-
 #include "c-stack.h"
 #include "exitfail.h"
 
@@ -265,39 +261,3 @@ c_stack_action (void (*action) (int)  __attribute__ ((unused)))
 }
 
 #endif
-
-\f
-
-#if DEBUG
-
-int volatile exit_failure;
-
-static long
-recurse (char *p)
-{
-  char array[500];
-  array[0] = 1;
-  return *p + recurse (array);
-}
-
-char *program_name;
-
-int
-main (int argc __attribute__ ((unused)), char **argv)
-{
-  program_name = argv[0];
-  fprintf (stderr,
-          "The last output line should contain \"stack overflow\".\n");
-  if (c_stack_action (0) == 0)
-    return recurse ("\1");
-  perror ("c_stack_action");
-  return 1;
-}
-
-#endif /* DEBUG */
-\f
-/*
-Local Variables:
-compile-command: "gcc -DDEBUG -g -O -Wall -W c-stack.c"
-End:
-*/
diff --git a/modules/c-stack-tests b/modules/c-stack-tests
new file mode 100644 (file)
index 0000000..291f58d
--- /dev/null
@@ -0,0 +1,15 @@
+Files:
+tests/test-c-stack.c
+tests/test-c-stack.sh
+
+Depends-on:
+exitfail
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-c-stack.sh
+TESTS_ENVIRONMENT += EXEEXT='@EXEEXT@'
+check_PROGRAMS += test-c-stack
+test_c_stack_LDADD = $(LDADD) @LIBINTL@
+MOSTLYCLEANFILES += t-c-stack.tmp
diff --git a/tests/test-c-stack.c b/tests/test-c-stack.c
new file mode 100644 (file)
index 0000000..13c1a12
--- /dev/null
@@ -0,0 +1,68 @@
+/* Test of c-stack module.
+   Copyright (C) 2002, 2004, 2006, 2008 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 <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include "c-stack.h"
+
+#include "exitfail.h"
+#include <stdio.h>
+#include <stdlib.h>
+#if HAVE_SETRLIMIT
+# include <sys/resource.h>
+#endif
+
+#define ASSERT(expr) \
+  do                                                                        \
+    {                                                                       \
+      if (!(expr))                                                          \
+        {                                                                   \
+          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
+          fflush (stderr);                                                  \
+          abort ();                                                         \
+        }                                                                   \
+    }                                                                       \
+  while (0)
+
+static long
+recurse (char *p)
+{
+  char array[500];
+  array[0] = 1;
+  return *p + recurse (array);
+}
+
+char *program_name;
+
+int
+main (int argc, char **argv)
+{
+   program_name = argv[0];
+#if HAVE_SETRLIMIT && defined RLIMIT_STACK
+   /* Before starting the endless recursion, try to be friendly to the
+      user's machine.  On some Linux 2.2.x systems, there is no stack
+      limit for user processes at all.  We don't want to kill such
+      systems.  */
+   struct rlimit rl;
+   rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
+   setrlimit (RLIMIT_STACK, &rl);
+#endif
+
+  if (c_stack_action (0) == 0)
+    return recurse ("\1");
+  perror ("c_stack_action");
+  return 77;
+}
diff --git a/tests/test-c-stack.sh b/tests/test-c-stack.sh
new file mode 100755 (executable)
index 0000000..f979065
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+tmpfiles=""
+trap 'rm -fr $tmpfiles' 1 2 3 15
+
+tmpfiles="t-c-stack.tmp"
+./test-c-stack${EXEEXT} 2> t-c-stack.tmp
+case $? in
+  77) cat t-c-stack.tmp >&2; (exit 77); exit 77 ;;
+  1) ;;
+  *) (exit 1); exit 1 ;;
+esac
+if grep 'stack overflow' t-c-stack.tmp >/dev/null ; then
+  :
+else
+  (exit 1); exit 1
+fi
+
+rm -fr $tmpfiles
+
+exit 0