2006-01-09 Roland McGrath <roland@redhat.com>
[glibc.git] / assert / test-assert.c
blob26b58d4dd3b4100229fd9706614d9d823886329a
1 /* Test assert().
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 (1 == 2);
31 static void
32 assert2 (void)
34 assert (1 == 1);
38 #define NDEBUG
39 #include <assert.h>
40 static void
41 assert3 (void)
43 assert (2 == 3);
46 int
47 main (void)
50 volatile int failed = 1;
52 fclose (stderr);
53 stderr = tmpfile ();
54 if(!stderr)
55 abort ();
57 signal (SIGABRT, sigabrt);
59 if (!setjmp (rec))
60 assert1 ();
61 else
62 failed = 0; /* should happen */
64 if (!setjmp (rec))
65 assert2 ();
66 else
67 failed = 1; /* should not happen */
69 if (!setjmp (rec))
70 assert3 ();
71 else
72 failed = 1; /* should not happen */
74 rewind (stderr);
75 fgets (buf, 160, stderr);
76 if (!strstr (buf, "1 == 2"))
77 failed = 1;
79 fgets (buf, 160, stderr);
80 if (strstr (buf, "1 == 1"))
81 failed = 1;
83 fgets (buf, 160, stderr);
84 if (strstr (buf, "2 == 3"))
85 failed = 1;
87 return failed;