stdlib: Optimization qsort{_r} swap implementation
[glibc.git] / assert / test-assert-perr.c
blob09a4fcb6efe2eb9b08bfe16bd1b33b03b484fdb7
1 /* Test assert_perror().
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 #include <support/xstdio.h>
16 jmp_buf rec;
17 char buf[160];
19 static void
20 sigabrt (int unused)
22 longjmp (rec, 1); /* recover control */
25 #undef NDEBUG
26 #include <assert.h>
27 static void
28 assert1 (void)
30 assert_perror (1);
33 static void
34 assert2 (void)
36 assert_perror (0);
39 #define NDEBUG
40 #include <assert.h>
41 static void
42 assert3 (void)
44 assert_perror (2);
47 int
48 main(void)
50 volatile int failed = 1; /* safety in presence of longjmp() */
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 xfgets (buf, 160, stderr);
76 if (!strstr(buf, strerror (1)))
77 failed = 1;
79 xfgets (buf, 160, stderr);
80 if (strstr (buf, strerror (0)))
81 failed = 1;
83 xfgets (buf, 160, stderr);
84 if (strstr (buf, strerror (2)))
85 failed = 1;
87 return failed;