]>
Commit | Line | Data |
---|---|---|
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 |
16 | jmp_buf rec; |
17 | char buf[160]; | |
18 | ||
cf3141a5 | 19 | static void |
ba9234d9 UD |
20 | sigabrt (int unused) |
21 | { | |
22 | longjmp (rec, 1); /* recover control */ | |
23 | } | |
24 | ||
25 | #undef NDEBUG | |
26 | #include <assert.h> | |
cf3141a5 | 27 | static void |
ba9234d9 UD |
28 | assert1 (void) |
29 | { | |
30 | assert_perror (1); | |
31 | } | |
32 | ||
cf3141a5 | 33 | static void |
ba9234d9 UD |
34 | assert2 (void) |
35 | { | |
36 | assert_perror (0); | |
37 | } | |
38 | ||
39 | #define NDEBUG | |
40 | #include <assert.h> | |
cf3141a5 | 41 | static void |
ba9234d9 UD |
42 | assert3 (void) |
43 | { | |
44 | assert_perror (2); | |
45 | } | |
46 | ||
47 | int | |
48 | main(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 | } |