xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tinp_str.c (revision ec6772edaf0cdcb5f52a48f4aca5e33a8fb8ecfd)
1 /* Test file for mpfr_inp_str.
2 
3 Copyright 2004, 2006-2023 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5 
6 This file is part of the GNU MPFR Library.
7 
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22 
23 #include <ctype.h>
24 
25 #include "mpfr-test.h"
26 
27 #ifdef HAVE_LOCALE_H
28 #include <locale.h>
29 #endif
30 
31 int
main(int argc,char * argv[])32 main (int argc, char *argv[])
33 {
34   mpfr_t x;
35   mpfr_t y;
36   FILE *f;
37   int i, n;
38 
39   tests_start_mpfr ();
40 
41   mpfr_init (x);
42   mpfr_init (y);
43 
44   mpfr_set_prec (x, 15);
45   f = src_fopen ("inp_str.dat", "r");
46   if (f == NULL)
47     {
48       printf ("Error, can't open inp_str.dat\n");
49       exit (1);
50     }
51   i = mpfr_inp_str (x, f, 10, MPFR_RNDN);
52   if (i != 5 || mpfr_cmp_si0 (x, -1700))
53     {
54       printf ("Error in reading 1st line from file inp_str.dat (%d)\n", i);
55       mpfr_dump (x);
56       exit (1);
57     }
58   i = mpfr_inp_str (x, f, 10, MPFR_RNDN);
59   if (i != 10 || mpfr_cmp_ui0 (x, 31415))
60     {
61       printf ("Error in reading 2nd line from file inp_str.dat (%d)\n", i);
62       mpfr_dump (x);
63       exit (1);
64     }
65   i = mpfr_inp_str (x, f, 10, MPFR_RNDN);
66   if (i != 111 || mpfr_cmp_ui0 (x, 31416))
67     {
68       printf ("Error in reading 3rd line from file inp_str.dat (%d)\n", i);
69       mpfr_dump (x);
70       exit (1);
71     }
72   i = mpfr_inp_str (x, f, 10, MPFR_RNDN);
73   if (i != 0)
74     {
75       printf ("Error in reading 4th line from file inp_str.dat (%d)\n", i);
76       mpfr_dump (x);
77       exit (1);
78     }
79 
80   mpfr_set_prec (x, 53);
81   mpfr_set_prec (y, 53);
82   mpfr_set_str (y, "1.0010010100001110100101001110011010111011100001110010e226",
83                 2, MPFR_RNDN);
84   for (n = 2; n < 63; n++)
85     {
86       i = mpfr_inp_str (x, f, n, MPFR_RNDN);
87       if (i == 0 || !mpfr_equal_p (x, y))
88         {
89           printf ("Error in reading %dth line from file inp_str.dat (%d)\n",
90                   n+3, i);
91           mpfr_dump (x);
92           exit (1);
93         }
94       mpfr_set_ui (x, 0, MPFR_RNDN);
95     }
96 
97   /* This last test assumes that isspace(0) is false.
98      If it isn't, set the locale to "C" in order to ensure that;
99      but we check isspace(0) again, just in case. */
100   if (! isspace (0)
101 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
102       || (setlocale (LC_ALL, "C"), ! isspace (0))
103 #endif
104       )
105     {
106       i = mpfr_inp_str (x, f, 10, MPFR_RNDN);
107       if (i != 0)
108         {
109           printf ("Error in the '\\0' test (%d)\n", i);
110           exit (1);
111         }
112       i = mpfr_inp_str (x, f, 10, MPFR_RNDN);
113       if (i != 3 || mpfr_cmp_ui0 (x, 17))
114         {
115           printf ("Error following the '\\0' test (%d)\n", i);
116           mpfr_dump (x);
117           exit (1);
118         }
119     }
120 
121   fclose (f);
122 
123   mpfr_clear (x);
124   mpfr_clear (y);
125 
126   tests_end_mpfr ();
127   return 0;
128 }
129