File: terror.c

package info (click to toggle)
mpfi 1.5.4%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,592 kB
  • sloc: ansic: 15,869; makefile: 158; sh: 6
file content (142 lines) | stat: -rw-r--r-- 3,889 bytes parent folder | download | duplicates (7)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* terror.c -- Frightening test of error handling.

Copyright 2010,
                     Spaces project, Inria Lorraine
                     and Salsa project, INRIA Rocquencourt,
                     and Arenaire project, Inria Rhone-Alpes, France
                     and Lab. ANO, USTL (Univ. of Lille),  France


This file is part of the MPFI Library.

The MPFI Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The MPFI Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the MPFI Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
MA 02110-1301, USA. */

#include "mpfi-tests.h"

#include "mpfi_config.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

int
read_stderr (FILE *stream)
{
  int ret = 0;

  /* something to read? */
  ret = (fgetc (stream) != EOF);
  if (fseek (stream, 0, SEEK_END) != 0) {
    printf ("Internal error: cannot reset stream.\n");
    exit (1);
  }

  return ret;
}

int
main (int argc, char **argv)
{
  FILE *stream;

  if (freopen ("error.tmp", "w", stderr) == NULL) {
    printf ("Internal error: cannot redirect stderr to a file.\n");
    exit (1);
  }
  stream = fopen ("error.tmp", "r");
  if (stream == NULL) {
    printf ("Internal error: cannot open the file \"error.tmp\".\n");
    exit (1);
  }

  mpfi_reset_error ();
  if (mpfi_is_error () == 1) {
    printf ("Error: mpfi_is_error() returns 1 after a call to "
            "mpfi_reset_error.\n");
    exit (1);
  }

  MPFI_ERROR ("[1] this should be printed!");
  fflush (stderr);
  if (!read_stderr (stream)) {
    printf ("Error: MPFI_ERROR does not print message after a call to "
            "mpfi_reset_error.\n");
    exit (1);
  }
  if (mpfi_is_error () != 1) {
    printf ("Error: mpfi_is_error does not set return 1 after a call to "
            "MPFI_ERROR.\n");
    exit (1);
  }

  mpfi_set_error (2);
  if (mpfi_is_error () == 1) {
    printf ("Error: mpfi_is_error() returns 1 after calling "
            "mpfi_set_error(2).\n");
    exit (1);
  }
  MPFI_ERROR ("[2] this should not be printed!");
  fflush (stderr);
  if (read_stderr (stream)) {
    printf ("Error: MPFI_ERROR prints a message while error number is "
            "set.\n");
    exit (1);
  }
  if (mpfi_is_error () == 1) {
    printf ("Error: MPFI_ERROR set error number to 1 while it was already "
            "set.\n");
    exit (1);
  }

  mpfi_set_error (1);
  if (mpfi_is_error () != 1) {
    printf ("Error: mpfi_is_error() does not return 1 after calling "
            "mpfi_set_error(1).\n");
    exit (1);
  }
  MPFI_ERROR ("[3] this should not be printed!");
  fflush (stderr);
  if (read_stderr (stream)) {
    printf ("Error: MPFI_ERROR prints a message while error number is "
            "set.\n");
    exit (1);
  }

  mpfi_reset_error ();
  if (mpfi_is_error () == 1) {
    printf ("Error: mpfi_is_error() returns 1 after a call to "
            "mpfi_reset_error.\n");
    exit (1);
  }
  MPFI_ERROR ("[4] this should be printed!");
  fflush (stderr);
  if (!read_stderr (stream)) {
    printf ("Error: MPFI_ERROR does not print message after a call to "
            "mpfi_reset_error.\n");
    exit (1);
  }
  if (mpfi_is_error () != 1) {
    printf ("Error: MPFI_ERROR dos not set error number to 1 after a call to "
            "mpfi_reset_error.\n");
    exit (1);
  }

  fclose (stream);
  fclose (stderr);
  unlink ("error.tmp");

  return 0;
}