bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ASSERT macro


From: Eric Blake
Subject: Re: ASSERT macro
Date: Fri, 25 Dec 2009 22:31:17 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Bruno Haible on 12/24/2009 1:08 PM:
>>> Either way we create macros.h and put ASSERT there
> 
> Done this this big commit, now.
> 
> 
> 2009-12-24  Bruno Haible  <address@hidden>
> 
>       Refactor common macros used in tests.
>       * tests/macros.h: New file.

Here's a way to use ASSERT in a few more files.  I'll probably push this
tomorrow.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAks1n6UACgkQ84KuGfSFAYCqxwCgx6oJ+2/U+BksCZUWqR+TTgis
gj0Anjjp8VoSlW3SNIIZpSfpanuitOtT
=Q3Bi
-----END PGP SIGNATURE-----
>From e4e6ad125d7d0675c4f9e6b3c0aeac4d726c6b5c Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Fri, 25 Dec 2009 16:06:48 -0700
Subject: [PATCH] tests: use macros.h in more places

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 <address@hidden>
---
 ChangeLog                   |   20 ++++++++++++++++++++
 modules/dirent-safer-tests  |    1 +
 modules/freopen-safer-tests |    1 +
 modules/getopt-posix-tests  |    1 +
 modules/openat-safer-tests  |    1 +
 modules/pipe-tests          |    1 +
 modules/unistd-safer-tests  |    1 +
 tests/macros.h              |   18 +++++++++++++-----
 tests/test-dirent-safer.c   |   15 +++------------
 tests/test-dup-safer.c      |   15 +++------------
 tests/test-freopen-safer.c  |   15 +++------------
 tests/test-getopt.c         |   15 +++------------
 tests/test-openat-safer.c   |   15 +++------------
 tests/test-pipe.c           |   15 +++------------
 tests/test-popen-safer.c    |   15 +++------------
 15 files changed, 60 insertions(+), 89 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index acdc7c1..acdd8e0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2009-12-25  Eric Blake  <address@hidden>
+
+       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-25  Bruno Haible  <address@hidden>

        selinux-h: Make generated .h files standalone.
diff --git a/modules/dirent-safer-tests b/modules/dirent-safer-tests
index da87778..2bc9593 100644
--- a/modules/dirent-safer-tests
+++ b/modules/dirent-safer-tests
@@ -1,5 +1,6 @@
 Files:
 tests/test-dirent-safer.c
+tests/macros.h

 Depends-on:
 dup2
diff --git a/modules/freopen-safer-tests b/modules/freopen-safer-tests
index 9511880..048d8a2 100644
--- a/modules/freopen-safer-tests
+++ b/modules/freopen-safer-tests
@@ -1,5 +1,6 @@
 Files:
 tests/test-freopen-safer.c
+tests/macros.h

 Depends-on:

diff --git a/modules/getopt-posix-tests b/modules/getopt-posix-tests
index 438b6e4..9c73d08 100644
--- a/modules/getopt-posix-tests
+++ b/modules/getopt-posix-tests
@@ -1,4 +1,5 @@
 Files:
+tests/macros.h
 tests/signature.h
 tests/test-getopt.c
 tests/test-getopt.h
diff --git a/modules/openat-safer-tests b/modules/openat-safer-tests
index 20bf382..1f0b158 100644
--- a/modules/openat-safer-tests
+++ b/modules/openat-safer-tests
@@ -1,5 +1,6 @@
 Files:
 tests/test-openat-safer.c
+tests/macros.h

 Depends-on:

diff --git a/modules/pipe-tests b/modules/pipe-tests
index 14d1e0f..0e31a86 100644
--- a/modules/pipe-tests
+++ b/modules/pipe-tests
@@ -1,6 +1,7 @@
 Files:
 tests/test-pipe.sh
 tests/test-pipe.c
+tests/macros.h

 Depends-on:
 progname
diff --git a/modules/unistd-safer-tests b/modules/unistd-safer-tests
index a6da5a9..cc4b97a 100644
--- a/modules/unistd-safer-tests
+++ b/modules/unistd-safer-tests
@@ -1,5 +1,6 @@
 Files:
 tests/test-dup-safer.c
+tests/macros.h

 Depends-on:
 binary-io
diff --git a/tests/macros.h b/tests/macros.h
index dc55fbe..b29378e 100644
--- a/tests/macros.h
+++ b/tests/macros.h
@@ -21,9 +21,16 @@
 #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
@@ -33,16 +40,17 @@
      - 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 ();                                                          \
         }                                                                    \
     }                                                                        \
diff --git a/tests/test-dirent-safer.c b/tests/test-dirent-safer.c
index 8d5d529..35cfb8a 100644
--- a/tests/test-dirent-safer.c
+++ b/tests/test-dirent-safer.c
@@ -33,19 +33,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)
diff --git a/tests/test-dup-safer.c b/tests/test-dup-safer.c
index 24cc9e5..72a7a78 100644
--- a/tests/test-dup-safer.c
+++ b/tests/test-dup-safer.c
@@ -49,19 +49,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
diff --git a/tests/test-freopen-safer.c b/tests/test-freopen-safer.c
index 40ba887..494045d 100644
--- a/tests/test-freopen-safer.c
+++ b/tests/test-freopen-safer.c
@@ -30,19 +30,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)
diff --git a/tests/test-getopt.c b/tests/test-getopt.c
index 9502a44..8896590 100644
--- a/tests/test-getopt.c
+++ b/tests/test-getopt.c
@@ -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
diff --git a/tests/test-openat-safer.c b/tests/test-openat-safer.c
index f709180..4ec13e0 100644
--- a/tests/test-openat-safer.c
+++ b/tests/test-openat-safer.c
@@ -31,19 +31,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;

 #define witness "test-openat-safer.txt"

diff --git a/tests/test-pipe.c b/tests/test-pipe.c
index 4c298a7..ec498f4 100644
--- a/tests/test-pipe.c
+++ b/tests/test-pipe.c
@@ -33,19 +33,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;

 /* Code executed by the child process.  argv[1] = "child".  */
 static int
diff --git a/tests/test-popen-safer.c b/tests/test-popen-safer.c
index 281dae9..20bb0ac 100644
--- a/tests/test-popen-safer.c
+++ b/tests/test-popen-safer.c
@@ -31,19 +31,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 (int argc, char **argv)
-- 
1.6.4.2


reply via email to

[Prev in Thread] Current Thread [Next in Thread]