]> sourceware.org Git - glibc.git/blame - assert/test-assert-perr.c
libio/tst-getdelim: Add new test covering NUL as a delimiter
[glibc.git] / assert / test-assert-perr.c
CommitLineData
ba9234d9
UD
1/* Test assert_perror().
2 *
3 * This is hairier than you'd think, involving games with
4 * stdio and signals.
5 *
6 */
7
8#include <signal.h>
9#include <stdlib.h>
10#include <stdio.h>
11#include <string.h>
12#include <setjmp.h>
13
7ba426a1
FB
14#include <support/xstdio.h>
15
ba9234d9
UD
16jmp_buf rec;
17char buf[160];
18
cf3141a5 19static void
ba9234d9
UD
20sigabrt (int unused)
21{
22 longjmp (rec, 1); /* recover control */
23}
24
25#undef NDEBUG
26#include <assert.h>
cf3141a5 27static void
ba9234d9
UD
28assert1 (void)
29{
30 assert_perror (1);
31}
32
cf3141a5 33static void
ba9234d9
UD
34assert2 (void)
35{
36 assert_perror (0);
37}
38
39#define NDEBUG
40#include <assert.h>
cf3141a5 41static void
ba9234d9
UD
42assert3 (void)
43{
44 assert_perror (2);
45}
46
47int
48main(void)
49{
50 volatile int failed = 1; /* safety in presence of longjmp() */
51
52 fclose (stderr);
53 stderr = tmpfile ();
54 if (!stderr)
55 abort ();
56
57 signal (SIGABRT, sigabrt);
58
59 if (!setjmp (rec))
60 assert1 ();
61 else
62 failed = 0; /* should happen */
63
64 if (!setjmp (rec))
65 assert2 ();
66 else
67 failed = 1; /* should not happen */
68
69 if (!setjmp (rec))
70 assert3 ();
71 else
72 failed = 1; /* should not happen */
73
74 rewind (stderr);
7ba426a1 75 xfgets (buf, 160, stderr);
ba9234d9
UD
76 if (!strstr(buf, strerror (1)))
77 failed = 1;
78
7ba426a1 79 xfgets (buf, 160, stderr);
ba9234d9
UD
80 if (strstr (buf, strerror (0)))
81 failed = 1;
82
7ba426a1 83 xfgets (buf, 160, stderr);
ba9234d9
UD
84 if (strstr (buf, strerror (2)))
85 failed = 1;
86
87 return failed;
88}
This page took 1.335779 seconds and 6 git commands to generate.