2004-11-03 Marcus Brinkmann <marcus@gnu.org>
[glibc.git] / assert / test-assert-perr.c
blob8496db6ffd8e5c11a166b8bc7e757c127458193c
1 /* Test assert_perror().
3 * This is hairier than you'd think, involving games with
4 * stdio and signals.
6 */
8 #include <signal.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <setjmp.h>
14 jmp_buf rec;
15 char buf[160];
17 static void
18 sigabrt (int unused)
20 longjmp (rec, 1); /* recover control */
23 #undef NDEBUG
24 #include <assert.h>
25 static void
26 assert1 (void)
28 assert_perror (1);
31 static void
32 assert2 (void)
34 assert_perror (0);
37 #define NDEBUG
38 #include <assert.h>
39 static void
40 assert3 (void)
42 assert_perror (2);
45 int
46 main(void)
48 volatile int failed = 1; /* safety in presence of longjmp() */
50 fclose (stderr);
51 stderr = tmpfile ();
52 if (!stderr)
53 abort ();
55 signal (SIGABRT, sigabrt);
57 if (!setjmp (rec))
58 assert1 ();
59 else
60 failed = 0; /* should happen */
62 if (!setjmp (rec))
63 assert2 ();
64 else
65 failed = 1; /* should not happen */
67 if (!setjmp (rec))
68 assert3 ();
69 else
70 failed = 1; /* should not happen */
72 rewind (stderr);
73 fgets (buf, 160, stderr);
74 if (!strstr(buf, strerror (1)))
75 failed = 1;
77 fgets (buf, 160, stderr);
78 if (strstr (buf, strerror (0)))
79 failed = 1;
81 fgets (buf, 160, stderr);
82 if (strstr (buf, strerror (2)))
83 failed = 1;
85 return failed;