1 /* Test mpf_get_str and mpf_set_str.
2 
3 Copyright 1996, 2000, 2001, 2008 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library test suite.
6 
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
11 
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15 Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h> /* for strlen */
23 
24 #include "gmp.h"
25 #include "gmp-impl.h"
26 #include "tests.h"
27 
28 #ifndef SIZE
29 #define SIZE 10
30 #endif
31 
32 #ifndef EXPO
33 #define EXPO 200
34 #endif
35 
36 int
main(int argc,char ** argv)37 main (int argc, char **argv)
38 {
39   mpf_t x, y;
40   int reps = 20000;
41   int i;
42   mp_size_t bprec = 100;
43   mpf_t d, rerr, max_rerr, limit_rerr;
44   char *str;
45   mp_exp_t bexp;
46   long size, exp;
47   int base;
48   char buf[SIZE * GMP_LIMB_BITS + 5];
49 
50   tests_start ();
51 
52   if (argc > 1)
53     {
54       reps = strtol (argv[1], 0, 0);
55       if (argc > 2)
56 	bprec = strtol (argv[2], 0, 0);
57     }
58 
59   mpf_set_default_prec (bprec);
60 
61   mpf_init_set_ui (limit_rerr, 1);
62   mpf_div_2exp (limit_rerr, limit_rerr, bprec);
63 #if VERBOSE
64   mpf_dump (limit_rerr);
65 #endif
66   mpf_init (rerr);
67   mpf_init_set_ui (max_rerr, 0);
68 
69   mpf_init (x);
70   mpf_init (y);
71   mpf_init (d);
72 
73   /* First test some specific values.  */
74 
75   mpf_set_str (y, "1.23456e1000", 0);
76 
77   mpf_set_str (x, "1.23456e1000", 10);
78   if (mpf_cmp (x, y) != 0)
79     abort ();
80   mpf_set_str (x, "1.23456e+1000", 0);
81   if (mpf_cmp (x, y) != 0)
82     abort ();
83   mpf_set_str (x, "1.23456e+1000", 10);
84   if (mpf_cmp (x, y) != 0)
85     abort ();
86 
87   /* Now test random values.  */
88 
89   for (i = 0; i < reps; i++)
90     {
91       if (i == 0)
92         {
93           /* exercise the special case in get_str for for x==0 */
94           mpf_set_ui (x, 0L);
95           base = 10;
96         }
97       else
98         {
99           size = urandom () % (2 * SIZE) - SIZE;
100           exp = urandom () % EXPO;
101           mpf_random2 (x, size, exp);
102           base = urandom () % 61 + 2;
103         }
104 
105       str = mpf_get_str (0, &bexp, base, 0, x);
106 
107       if (str[0] == '-')
108 	sprintf (buf, "-0.%s@%ld", str + 1, bexp);
109       else
110 	sprintf (buf, "0.%s@%ld", str, bexp);
111 
112       mpf_set_str_or_abort (y, buf, -base);
113       (*__gmp_free_func) (str, strlen (str) + 1);
114 
115       mpf_reldiff (rerr, x, y);
116       if (mpf_cmp (rerr, max_rerr) > 0)
117 	{
118 	  mpf_set (max_rerr, rerr);
119 #if VERBOSE
120 	  mpf_dump (max_rerr);
121 #endif
122 	  if (mpf_cmp (rerr, limit_rerr) > 0)
123 	    {
124 	      printf ("ERROR after %d tests\n", i);
125 	      printf ("base = %d\n", base);
126 	      printf ("   x = "); mpf_dump (x);
127 	      printf ("   y = "); mpf_dump (y);
128 	      abort ();
129 	    }
130 	}
131     }
132 
133   mpf_clear (limit_rerr);
134   mpf_clear (rerr);
135   mpf_clear (max_rerr);
136 
137   mpf_clear (x);
138   mpf_clear (y);
139   mpf_clear (d);
140 
141   tests_end ();
142   exit (0);
143 }
144