aboutsummaryrefslogtreecommitdiff
blob: 824c1a9f97de862e8988320ec88e9e45cbe3c6d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Test assert_perror().
 *
 * This is hairier than you'd think, involving games with
 * stdio and signals.
 *
 */

#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <setjmp.h>

jmp_buf rec;
char buf[160];

void
sigabrt (int unused)
{
  longjmp (rec, 1);  /* recover control */
}

#undef NDEBUG
#include <assert.h>
void
assert1 (void)
{
  assert_perror (1);
}

void
assert2 (void)
{
  assert_perror (0);
}

#define NDEBUG
#include <assert.h>
void
assert3 (void)
{
  assert_perror (2);
}

int
main(void)
{
  volatile int failed = 1;  /* safety in presence of longjmp() */

  fclose (stderr);
  stderr = tmpfile ();
  if (!stderr)
    abort ();

  signal (SIGABRT, sigabrt);

  if (!setjmp (rec))
    assert1 ();
  else
    failed = 0;  /* should happen */

  if (!setjmp (rec))
    assert2 ();
  else
    failed = 1; /* should not happen */

  if (!setjmp (rec))
    assert3 ();
  else
    failed = 1; /* should not happen */

  rewind (stderr);
  fgets (buf, 160, stderr);
  if (!strstr(buf, strerror (1)))
    failed = 1;

  fgets (buf, 160, stderr);
  if (strstr (buf, strerror (0)))
    failed = 1;

  fgets (buf, 160, stderr);
  if (strstr (buf, strerror (2)))
    failed = 1;

  return failed;
}