diff options
Diffstat (limited to 'assert')
-rw-r--r-- | assert/Makefile | 3 | ||||
-rw-r--r-- | assert/test-assert-perr.c | 86 | ||||
-rw-r--r-- | assert/test-assert.c | 88 |
3 files changed, 176 insertions, 1 deletions
diff --git a/assert/Makefile b/assert/Makefile index 5780457502..63fb9253ff 100644 --- a/assert/Makefile +++ b/assert/Makefile @@ -1,4 +1,4 @@ -# Copyright (C) 1991, 1994, 1997 Free Software Foundation, Inc. +# Copyright (C) 1991, 1994, 1997, 1998 Free Software Foundation, Inc. # This file is part of the GNU C Library. # The GNU C Library is free software; you can redistribute it and/or @@ -23,5 +23,6 @@ subdir := assert headers := assert.h routines := assert assert-perr +tests := test-assert test-assert-perr include ../Rules diff --git a/assert/test-assert-perr.c b/assert/test-assert-perr.c new file mode 100644 index 0000000000..824c1a9f97 --- /dev/null +++ b/assert/test-assert-perr.c @@ -0,0 +1,86 @@ +/* Test assert_perror(). + * + * This is hairier than you'd think, involving games with + * stdio and signals. + * + */ + +#include <signal.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <setjmp.h> + +jmp_buf rec; +char buf[160]; + +void +sigabrt (int unused) +{ + longjmp (rec, 1); /* recover control */ +} + +#undef NDEBUG +#include <assert.h> +void +assert1 (void) +{ + assert_perror (1); +} + +void +assert2 (void) +{ + assert_perror (0); +} + +#define NDEBUG +#include <assert.h> +void +assert3 (void) +{ + assert_perror (2); +} + +int +main(void) +{ + volatile int failed = 1; /* safety in presence of longjmp() */ + + fclose (stderr); + stderr = tmpfile (); + if (!stderr) + abort (); + + signal (SIGABRT, sigabrt); + + if (!setjmp (rec)) + assert1 (); + else + failed = 0; /* should happen */ + + if (!setjmp (rec)) + assert2 (); + else + failed = 1; /* should not happen */ + + if (!setjmp (rec)) + assert3 (); + else + failed = 1; /* should not happen */ + + rewind (stderr); + fgets (buf, 160, stderr); + if (!strstr(buf, strerror (1))) + failed = 1; + + fgets (buf, 160, stderr); + if (strstr (buf, strerror (0))) + failed = 1; + + fgets (buf, 160, stderr); + if (strstr (buf, strerror (2))) + failed = 1; + + return failed; +} diff --git a/assert/test-assert.c b/assert/test-assert.c new file mode 100644 index 0000000000..045721e9cd --- /dev/null +++ b/assert/test-assert.c @@ -0,0 +1,88 @@ +/* Test assert(). + * + * This is hairier than you'd think, involving games with + * stdio and signals. + * + */ + +#include <signal.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <setjmp.h> + +jmp_buf rec; +char buf[160]; + +void +sigabrt (int unused) +{ + longjmp (rec, 1); /* recover control */ +} + +#undef NDEBUG +#include <assert.h> +void +assert1 (void) +{ + assert (1 == 2); +} + +void +assert2 (void) +{ + assert (1 == 1); +} + + +#define NDEBUG +#include <assert.h> +void +assert3 (void) +{ + assert (2 == 3); +} + +int +main (void) +{ + + volatile int failed = 1; + + fclose (stderr); + stderr = tmpfile (); + if(!stderr) + abort (); + + signal (SIGABRT, sigabrt); + + if (!setjmp (rec)) + assert1 (); + else + failed = 0; /* should happen */ + + if (!setjmp (rec)) + assert2 (); + else + failed = 1; /* should not happen */ + + if (!setjmp (rec)) + assert3 (); + else + failed = 1; /* should not happen */ + + rewind (stderr); + fgets (buf, 160, stderr); + if (!strstr (buf, "1 == 2")) + failed = 1; + + fgets (buf, 160, stderr); + if (strstr (buf, "1 == 1")) + failed = 1; + + fgets (buf, 160, stderr); + if (strstr (buf, "2 == 3")) + failed = 1; + + return failed; +} |