]>
Commit | Line | Data |
---|---|---|
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 | ||
14 | #include <support/xstdio.h> | |
15 | ||
16 | jmp_buf rec; | |
17 | char buf[160]; | |
18 | ||
19 | static void | |
20 | sigabrt (int unused) | |
21 | { | |
22 | longjmp (rec, 1); /* recover control */ | |
23 | } | |
24 | ||
25 | #undef NDEBUG | |
26 | #include <assert.h> | |
27 | static void | |
28 | assert1 (void) | |
29 | { | |
30 | assert_perror (1); | |
31 | } | |
32 | ||
33 | static void | |
34 | assert2 (void) | |
35 | { | |
36 | assert_perror (0); | |
37 | } | |
38 | ||
39 | #define NDEBUG | |
40 | #include <assert.h> | |
41 | static void | |
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); | |
75 | xfgets (buf, 160, stderr); | |
76 | if (!strstr(buf, strerror (1))) | |
77 | failed = 1; | |
78 | ||
79 | xfgets (buf, 160, stderr); | |
80 | if (strstr (buf, strerror (0))) | |
81 | failed = 1; | |
82 | ||
83 | xfgets (buf, 160, stderr); | |
84 | if (strstr (buf, strerror (2))) | |
85 | failed = 1; | |
86 | ||
87 | return failed; | |
88 | } |