tests: use macros.h in more places
authorEric Blake <ebb9@byu.net>
Fri, 25 Dec 2009 23:06:48 +0000 (16:06 -0700)
committerEric Blake <ebb9@byu.net>
Sat, 26 Dec 2009 18:03:54 +0000 (11:03 -0700)
Make the ASSERT macro a bit more reusable.

* tests/macros.h (ASSERT): Depend on ASSERT_STREAM.
(ASSERT_STREAM): Provide default of stderr.
* tests/test-dirent-safer.c: Include macros.h, using alternate
stream for assertions.
* tests/test-dup-safer.c: Likewise.
* tests/test-freopen-safer.c: Likewise.
* tests/test-getopt.c: Likewise.
* tests/test-openat-safer.c: Likewise.
* tests/test-pipe.c: Likewise.
* tests/test-popen-safer.c: Likewise.
* modules/dirent-safer-tests (Files): Include macros.h.
* modules/unistd-safer-tests (Files): Likewise.
* modules/freopen-safer-tests (Files): Likewise.
* modules/getopt-posix-tests (Files): Likewise.
* modules/openat-safer-tests (Files): Likewise.
* modules/pipe-tests (Files): Likewise.

Signed-off-by: Eric Blake <ebb9@byu.net>
15 files changed:
ChangeLog
modules/dirent-safer-tests
modules/freopen-safer-tests
modules/getopt-posix-tests
modules/openat-safer-tests
modules/pipe-tests
modules/unistd-safer-tests
tests/macros.h
tests/test-dirent-safer.c
tests/test-dup-safer.c
tests/test-freopen-safer.c
tests/test-getopt.c
tests/test-openat-safer.c
tests/test-pipe.c
tests/test-popen-safer.c

index f17fd95..fbf1354 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2009-12-26  Eric Blake  <ebb9@byu.net>
+
+       tests: use macros.h in more places
+       * tests/macros.h (ASSERT): Depend on ASSERT_STREAM.
+       (ASSERT_STREAM): Provide default of stderr.
+       * tests/test-dirent-safer.c: Include macros.h, using alternate
+       stream for assertions.
+       * tests/test-dup-safer.c: Likewise.
+       * tests/test-freopen-safer.c: Likewise.
+       * tests/test-getopt.c: Likewise.
+       * tests/test-openat-safer.c: Likewise.
+       * tests/test-pipe.c: Likewise.
+       * tests/test-popen-safer.c: Likewise.
+       * modules/dirent-safer-tests (Files): Include macros.h.
+       * modules/unistd-safer-tests (Files): Likewise.
+       * modules/freopen-safer-tests (Files): Likewise.
+       * modules/getopt-posix-tests (Files): Likewise.
+       * modules/openat-safer-tests (Files): Likewise.
+       * modules/pipe-tests (Files): Likewise.
+
 2009-12-26  Bruno Haible  <bruno@clisp.org>
 
        javacomp: Portability fix.
index da87778..2bc9593 100644 (file)
@@ -1,5 +1,6 @@
 Files:
 tests/test-dirent-safer.c
+tests/macros.h
 
 Depends-on:
 dup2
index 9511880..048d8a2 100644 (file)
@@ -1,5 +1,6 @@
 Files:
 tests/test-freopen-safer.c
+tests/macros.h
 
 Depends-on:
 
index 438b6e4..9c73d08 100644 (file)
@@ -1,4 +1,5 @@
 Files:
+tests/macros.h
 tests/signature.h
 tests/test-getopt.c
 tests/test-getopt.h
index 20bf382..1f0b158 100644 (file)
@@ -1,5 +1,6 @@
 Files:
 tests/test-openat-safer.c
+tests/macros.h
 
 Depends-on:
 
index 14d1e0f..0e31a86 100644 (file)
@@ -1,6 +1,7 @@
 Files:
 tests/test-pipe.sh
 tests/test-pipe.c
+tests/macros.h
 
 Depends-on:
 progname
index a6da5a9..cc4b97a 100644 (file)
@@ -1,5 +1,6 @@
 Files:
 tests/test-dup-safer.c
+tests/macros.h
 
 Depends-on:
 binary-io
index dc55fbe..b29378e 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 
+/* Define ASSERT_STREAM before including this file if ASSERT must
+   target a stream other than stderr.  */
+#ifndef ASSERT_STREAM
+# define ASSERT_STREAM stderr
+#endif
+
 /* ASSERT (condition);
    verifies that the specified condition is fulfilled.  If not, a message
-   is printed to stderr and the program is terminated with an error code.
+   is printed to ASSERT_STREAM if defined (defaulting to stderr if
+   undefined) and the program is terminated with an error code.
 
    This macro has the following properties:
      - The programmer specifies the expected condition, not the failure
      - On Unix platforms, the tester can debug the test program with a
        debugger (provided core dumps are enabled: "ulimit -c unlimited").
      - For the sake of platforms where no debugger is available (such as
-       some mingw systems), an error message is printed on stderr that
-       includes the source location of the ASSERT invocation.
+       some mingw systems), an error message is printed on the error
+       stream that includes the source location of the ASSERT invocation.
  */
 #define ASSERT(expr) \
   do                                                                         \
     {                                                                        \
       if (!(expr))                                                           \
         {                                                                    \
-          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
-          fflush (stderr);                                                   \
+          fprintf (ASSERT_STREAM, "%s:%d: assertion failed\n",               \
+                   __FILE__, __LINE__);                                      \
+          fflush (ASSERT_STREAM);                                            \
           abort ();                                                          \
         }                                                                    \
     }                                                                        \
index 8d5d529..045d29b 100644 (file)
@@ -23,7 +23,6 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <unistd.h>
 
 #include "unistd-safer.h"
    duplicate the original stderr.  */
 
 #define BACKUP_STDERR_FILENO 10
-static FILE *myerr;
+#define ASSERT_STREAM myerr
+#include "macros.h"
 
-#define ASSERT(expr) \
-  do                                                                         \
-    {                                                                        \
-      if (!(expr))                                                           \
-        {                                                                    \
-          fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
-          fflush (myerr);                                                    \
-          abort ();                                                          \
-        }                                                                    \
-    }                                                                        \
-  while (0)
+static FILE *myerr;
 
 int
 main (void)
index 24cc9e5..6a5c69d 100644 (file)
@@ -24,8 +24,6 @@
 #include <errno.h>
 #include <stdbool.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <sys/stat.h>
 
 #include "binary-io.h"
 #include "cloexec.h"
@@ -49,19 +47,10 @@ static int zero (void) { return 0; }
    duplicate the original stderr.  */
 
 #define BACKUP_STDERR_FILENO 10
-static FILE *myerr;
+#define ASSERT_STREAM myerr
+#include "macros.h"
 
-#define ASSERT(expr) \
-  do                                                                         \
-    {                                                                        \
-      if (!(expr))                                                           \
-        {                                                                    \
-          fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
-          fflush (myerr);                                                    \
-          abort ();                                                          \
-        }                                                                    \
-    }                                                                        \
-  while (0)
+static FILE *myerr;
 
 /* Return true if FD is open.  */
 static bool
index 40ba887..41d47da 100644 (file)
@@ -22,7 +22,6 @@
 #include "stdio--.h"
 
 /* Helpers.  */
-#include <stdlib.h>
 #include <unistd.h>
 
 /* This test intentionally closes stderr.  So, we arrange to have fd 10
    duplicate the original stderr.  */
 
 #define BACKUP_STDERR_FILENO 10
-static FILE *myerr;
+#define ASSERT_STREAM myerr
+#include "macros.h"
 
-#define ASSERT(expr) \
-  do                                                                         \
-    {                                                                        \
-      if (!(expr))                                                           \
-        {                                                                    \
-          fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
-          fflush (myerr);                                                    \
-          abort ();                                                          \
-        }                                                                    \
-    }                                                                        \
-  while (0)
+static FILE *myerr;
 
 int
 main (void)
index 9502a44..8896590 100644 (file)
@@ -55,19 +55,10 @@ SIGNATURE_CHECK (getopt, int, (int, char * const[], char const *));
    duplicate the original stderr.  */
 
 #define BACKUP_STDERR_FILENO 10
-static FILE *myerr;
+#define ASSERT_STREAM myerr
+#include "macros.h"
 
-#define ASSERT(expr) \
-  do                                                                         \
-    {                                                                        \
-      if (!(expr))                                                           \
-        {                                                                    \
-          fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
-          fflush (myerr);                                                    \
-          abort ();                                                          \
-        }                                                                    \
-    }                                                                        \
-  while (0)
+static FILE *myerr;
 
 #include "test-getopt.h"
 #if GNULIB_GETOPT_GNU
index f709180..7a49600 100644 (file)
@@ -22,7 +22,6 @@
 
 #include <errno.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
    duplicate the original stderr.  */
 
 #define BACKUP_STDERR_FILENO 10
-static FILE *myerr;
+#define ASSERT_STREAM myerr
+#include "macros.h"
 
-#define ASSERT(expr) \
-  do                                                                         \
-    {                                                                        \
-      if (!(expr))                                                           \
-        {                                                                    \
-          fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
-          fflush (myerr);                                                    \
-          abort ();                                                          \
-        }                                                                    \
-    }                                                                        \
-  while (0)
+static FILE *myerr;
 
 #define witness "test-openat-safer.txt"
 
index 4c298a7..ec498f4 100644 (file)
    duplicate the original stderr.  */
 
 #define BACKUP_STDERR_FILENO 10
-static FILE *myerr;
+#define ASSERT_STREAM myerr
+#include "macros.h"
 
-#define ASSERT(expr) \
-  do                                                                         \
-    {                                                                        \
-      if (!(expr))                                                           \
-        {                                                                    \
-          fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
-          fflush (myerr);                                                    \
-          abort ();                                                          \
-        }                                                                    \
-    }                                                                        \
-  while (0)
+static FILE *myerr;
 
 /* Code executed by the child process.  argv[1] = "child".  */
 static int
index 281dae9..d9ab831 100644 (file)
@@ -22,7 +22,6 @@
 #include "stdio--.h"
 
 /* Helpers.  */
-#include <stdlib.h>
 #include <sys/wait.h>
 #include <unistd.h>
 
    duplicate the original stderr.  */
 
 #define BACKUP_STDERR_FILENO 10
-static FILE *myerr;
+#define ASSERT_STREAM myerr
+#include "macros.h"
 
-#define ASSERT(expr) \
-  do                                                                         \
-    {                                                                        \
-      if (!(expr))                                                           \
-        {                                                                    \
-          fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
-          fflush (myerr);                                                    \
-          abort ();                                                          \
-        }                                                                    \
-    }                                                                        \
-  while (0)
+static FILE *myerr;
 
 int
 main (int argc, char **argv)