1 /* Test assert().
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 (1 == 2);
31 }
32
33 static void
34 assert2 (void)
35 {
36 assert (1 == 1);
37 }
38
39
40 #define NDEBUG
41 #include <assert.h>
42 static void
43 assert3 (void)
44 {
45 assert (2 == 3);
46 }
47
48 int
49 main (void)
50 {
51
52 volatile int failed = 1;
53
54 fclose (stderr);
55 stderr = tmpfile ();
56 if(!stderr)
57 abort ();
58
59 signal (SIGABRT, sigabrt);
60
61 if (!setjmp (rec))
62 assert1 ();
63 else
64 failed = 0; /* should happen */
65
66 if (!setjmp (rec))
67 assert2 ();
68 else
69 failed = 1; /* should not happen */
70
71 if (!setjmp (rec))
72 assert3 ();
73 else
74 failed = 1; /* should not happen */
75
76 rewind (stderr);
77 xfgets (buf, 160, stderr);
78 if (!strstr (buf, "1 == 2"))
79 failed = 1;
80
81 xfgets (buf, 160, stderr);
82 if (strstr (buf, "1 == 1"))
83 failed = 1;
84
85 xfgets (buf, 160, stderr);
86 if (strstr (buf, "2 == 3"))
87 failed = 1;
88
89 return failed;
90 }