Files
glibc/assert/test-assert-perr.c
T

87 lines
1.2 KiB
C
Raw Normal View History

1998-07-04 10:39:13 +00:00
/* 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];
2000-12-05 08:17:58 +00:00
static void
1998-07-04 10:39:13 +00:00
sigabrt (int unused)
{
longjmp (rec, 1); /* recover control */
}
#undef NDEBUG
#include <assert.h>
2000-12-05 08:17:58 +00:00
static void
1998-07-04 10:39:13 +00:00
assert1 (void)
{
assert_perror (1);
}
2000-12-05 08:17:58 +00:00
static void
1998-07-04 10:39:13 +00:00
assert2 (void)
{
assert_perror (0);
}
#define NDEBUG
#include <assert.h>
2000-12-05 08:17:58 +00:00
static void
1998-07-04 10:39:13 +00:00
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;
}