xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/toutimpl.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /* Test file for internal debugging-out functions:
2    mpfr_dump, mpfr_print_binary, mpfr_print_rnd_mode.
3 
4 Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 Contributed by the Arenaire and Cacao projects, INRIA.
6 
7 This file is part of the GNU MPFR Library.
8 
9 The GNU MPFR Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13 
14 The GNU MPFR Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17 License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
21 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 
24 #include <stdlib.h>
25 #include "mpfr-test.h"
26 
27 /* We output stdout to a file to check if it is correct
28    Is it a good idea?
29    We can't use tmpname since it is insecure */
30 #define FILE_NAME "dummy.tmp"
31 
32 static const char Buffer[] =
33 "@NaN@\n"
34 "-@Inf@\n"
35 "-0\n"
36 "0.10101010101011111001000110001100010000100000000000000E32\n";
37 
38 int
39 main (void)
40 {
41   mpfr_t x;
42   FILE *f;
43   int i;
44 
45   tests_start_mpfr ();
46 
47   /* Check RND_MODE */
48   if (strcmp (mpfr_print_rnd_mode(MPFR_RNDN), "MPFR_RNDN"))
49     {
50       printf ("Error for printing MPFR_RNDN\n");
51       exit (1);
52     }
53   if (strcmp (mpfr_print_rnd_mode(MPFR_RNDU), "MPFR_RNDU"))
54     {
55       printf ("Error for printing MPFR_RNDU\n");
56       exit (1);
57     }
58   if (strcmp (mpfr_print_rnd_mode(MPFR_RNDD), "MPFR_RNDD"))
59     {
60       printf ("Error for printing MPFR_RNDD\n");
61       exit (1);
62     }
63   if (strcmp (mpfr_print_rnd_mode(MPFR_RNDZ), "MPFR_RNDZ"))
64     {
65       printf ("Error for printing MPFR_RNDZ\n");
66       exit (1);
67     }
68   if (mpfr_print_rnd_mode ((mpfr_rnd_t) -1) != NULL ||
69       mpfr_print_rnd_mode (MPFR_RND_MAX) != NULL)
70     {
71       printf ("Error for illegal rounding mode values.\n");
72       exit (1);
73     }
74 
75   /* Reopen stdout to a file. All errors will be put to stderr
76      Can't use tmpname since it is unsecure */
77   if (freopen (FILE_NAME, "w", stdout) == NULL)
78     {
79       printf ("Error can't redirect stdout\n");
80       exit (1);
81     }
82   mpfr_init (x);
83   mpfr_set_nan (x);
84   mpfr_dump (x);
85   mpfr_set_inf (x, -1);
86   mpfr_dump (x);
87   MPFR_SET_ZERO (x); MPFR_SET_NEG (x);
88   mpfr_dump (x);
89   mpfr_set_str_binary (x, "0.101010101010111110010001100011000100001E32");
90   mpfr_dump (x);
91   mpfr_print_mant_binary ("x=",MPFR_MANT(x), MPFR_PREC(x));
92 
93 
94   mpfr_clear (x);
95   fclose (stdout);
96   /* Open it and check for it */
97   f = fopen (FILE_NAME, "r");
98   if (f == NULL)
99     {
100       fprintf (stderr, "Can't reopen file!\n");
101       exit (1);
102     }
103   for(i = 0 ; i < sizeof(Buffer)-1 ; i++)
104     {
105       if (feof (f))
106         {
107           fprintf (stderr, "Error EOF\n");
108           exit (1);
109         }
110       if (Buffer[i] != fgetc (f))
111         {
112           fprintf (stderr, "Character mismatch for i=%d / %lu\n",
113                   i, (unsigned long) sizeof(Buffer));
114           exit (1);
115         }
116     }
117   fclose (f);
118 
119   remove (FILE_NAME);
120   tests_end_mpfr ();
121   return 0;
122 }
123