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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/* tprintf.c -- test file for mpfr_printf and mpfr_vprintf
Copyright 2008 Free Software Foundation, Inc.
Contributed by the Arenaire and Cacao projects, INRIA.
The GNU MPFR 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 GNU MPFR 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 GNU MPFR 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. */
#if defined HAVE_STDARG
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#ifdef _MPFR_H_HAVE_INTMAX_T
#include <stdint.h>
#endif
#include "mpfr-test.h"
#define STDOUT_FILENO 1
#if MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0)
/* limit for random precision in random() */
const int prec_max_printf = 5000;
int stdout_redirect;
static void
check (char *fmt, mpfr_t x)
{
if (mpfr_printf (fmt, x) == -1)
{
printf ("Error in mpfr_printf(\"%s\", ...)\n", fmt);
if (stdout_redirect)
{
if ((fflush (stdout) == EOF) || (fclose (stdout) == -1))
{
perror ("check");
exit (1);
}
}
exit (1);
}
putchar ('\n');
}
static void
check_vprintf (char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
if (mpfr_vprintf (fmt, ap) == -1)
{
printf ("Error in mpfr_vprintf(\"%s\", ...)\n", fmt);
if (stdout_redirect)
{
if ((fflush (stdout) == EOF) || (fclose (stdout) == -1))
{
perror ("check_vprintf");
exit (1);
}
}
va_end (ap);
exit (1);
}
putchar ('\n');
va_end (ap);
}
static void
check_special ()
{
mpfr_t x;
mpfr_init (x);
mpfr_set_inf (x, 1);
check ("%Ra", x);
check ("%Rb", x);
check ("%Re", x);
check ("%Rf", x);
check ("%Rg", x);
check_vprintf ("%Ra", x);
check_vprintf ("%Rb", x);
check_vprintf ("%Re", x);
check_vprintf ("%Rf", x);
check_vprintf ("%Rg", x);
mpfr_set_inf (x, -1);
check ("%Ra", x);
check ("%Rb", x);
check ("%Re", x);
check ("%Rf", x);
check ("%Rg", x);
check_vprintf ("%Ra", x);
check_vprintf ("%Rb", x);
check_vprintf ("%Re", x);
check_vprintf ("%Rf", x);
check_vprintf ("%Rg", x);
mpfr_set_nan (x);
check ("%Ra", x);
check ("%Rb", x);
check ("%Re", x);
check ("%Rf", x);
check ("%Rg", x);
check_vprintf ("%Ra", x);
check_vprintf ("%Rb", x);
check_vprintf ("%Re", x);
check_vprintf ("%Rf", x);
check_vprintf ("%Rg", x);
mpfr_clear (x);
}
static void
check_mixed ()
{
char ch = 'a';
signed char sch = -1;
unsigned char uch = 1;
short sh = -1;
unsigned short ush = 1;
int i = -1;
unsigned int ui = 1;
long lo = -1;
unsigned long ulo = 1;
float f = -1.25;
double d = -1.25;
long double ld = -1.25;
ptrdiff_t p = 1;
size_t si = 1;
#ifdef HAVE_LONG_LONG
long long llo = -1;
unsigned long long ullo = -1;
#endif
#ifdef _MPFR_H_HAVE_INTMAX_T
intmax_t im = -1;
uintmax_t uim = 1;
#endif
mpz_t mpz;
mpq_t mpq;
mpf_t mpf;
mp_rnd_t rnd = GMP_RNDN;
mpfr_t mpfr;
mpfr_prec_t prec;
mpz_init (mpz);
mpz_set_ui (mpz, ulo);
mpq_init (mpq);
mpq_set_si (mpq, lo, ulo);
mpf_init (mpf);
mpf_set_q (mpf, mpq);
mpfr_init (mpfr);
mpfr_set_f (mpfr, mpf, GMP_RNDN);
prec = mpfr_get_prec (mpfr);
check_vprintf ("a. %Ra, b. %hhu, c. %u, d. %lx%hhn", mpfr, uch, ui,
ulo, &uch);
MPFR_ASSERTN (uch == 28);
check_vprintf ("a. %hhi, b. %Rb, c. %u, d. %li%ln", sch, mpfr, i,
lo, &ulo);
MPFR_ASSERTN (ulo == 37);
check_vprintf ("a. %hi, b. %*f, c. %Re%hn", ush, 3, f, mpfr, &ush);
MPFR_ASSERTN (ush == 29);
check_vprintf ("a. %hi, b. %e, c. %#.2Rf%n", sh, d, mpfr, &i);
MPFR_ASSERTN (i == 33);
check_vprintf ("a. %R*A, b. %Fe, c. %i%zn", rnd, mpfr, mpf, si,
&si);
MPFR_ASSERTN (si == 34);
check_vprintf ("a. %Pu, b. %c, c. %Lf, d. %Zi%Zn", prec, ch, ld,
mpz, &mpz);
MPFR_ASSERTN (mpz_cmp_ui (mpz, 31) == 0);
check_vprintf ("%% a. %#.0RNg, b. %Qx%Rn, c. %td, d. %p", mpfr, mpq,
&mpfr, p, &i);
MPFR_ASSERTN (mpfr_cmp_ui (mpfr, 16) == 0);
#ifdef HAVE_LONG_LONG
check_vprintf ("a. %Re, b. %llx%Qn", mpfr, ullo, &mpq);
MPFR_ASSERTN (mpq_cmp_ui (mpq, 31, 1) == 0);
check_vprintf ("a. %lli, b. %Rf%Fn", llo, mpfr, &mpf);
MPFR_ASSERTN (mpf_cmp_ui (mpf, 12) == 0);
check_vprintf ("a. %qi, b. %Rf%qn", llo, mpfr, &ullo);
MPFR_ASSERTN (ullo == 12);
#endif
#ifdef _MPFR_H_HAVE_INTMAX_T
check_vprintf ("a. %*RA, b. %ji%Qn", 10, mpfr, im, &mpq);
MPFR_ASSERTN (mpq_cmp_ui (mpq, 20, 1) == 0);
check_vprintf ("a. %.*Re, b. %jx%Fn", 10, mpfr, uim, &mpf);
MPFR_ASSERTN (mpf_cmp_ui (mpf, 25) == 0);
#endif
mpfr_clear (mpfr);
mpf_clear (mpf);
mpq_clear (mpq);
mpz_clear (mpz);
}
static void
check_random (int nb_tests)
{
int i;
mpfr_t x;
mp_rnd_t rnd;
char flag[] =
{
'-',
'+',
' ',
'#',
'0', /* no ambiguity: first zeros are flag zero*/
'\''
};
char specifier[] =
{
'a',
'b',
'e',
'f',
'g'
};
mp_exp_t old_emin, old_emax;
old_emin = mpfr_get_emin ();
old_emax = mpfr_get_emax ();
mpfr_init (x);
for (i = 0; i < nb_tests; ++i)
{
int ret;
int j, jmax;
int spec, prec;
#define FMT_SIZE 13
char fmt[FMT_SIZE]; /* at most something like "%-+ #0'.*R*f" */
char *ptr = fmt;
tests_default_random (x, 256, MPFR_EMIN_MIN, MPFR_EMAX_MAX);
rnd = (mp_rnd_t) RND_RAND ();
spec = (int) (randlimb () % 5);
jmax = (spec == 3 || spec == 4) ? 6 : 5; /* ' flag only with %f or %g */
/* advantage small precision */
prec = (randlimb () % 2) ? 10 : prec_max_printf;
prec = (int) (randlimb () % prec);
if (spec == 3 && mpfr_get_exp (x) > prec_max_printf)
/* change style 'f' to style 'e' when number x is large */
--spec;
*ptr++ = '%';
for (j = 0; j < jmax; j++)
{
if (randlimb () % 3 == 0)
*ptr++ = flag[j];
}
*ptr++ = '.';
*ptr++ = '*';
*ptr++ = 'R';
*ptr++ = '*';
*ptr++ = specifier[spec];
*ptr = '\0';
MPFR_ASSERTD (ptr - fmt < FMT_SIZE);
mpfr_printf ("mpfr_printf(\"%s\", %d, %s, %Re)\n", fmt, prec,
mpfr_print_rnd_mode (rnd), x);
ret = mpfr_printf (fmt, prec, rnd, x);
if (ret == -1)
{
if (spec == 3
&& (MPFR_GET_EXP (x) > INT_MAX || MPFR_GET_EXP (x) < -INT_MAX))
/* normal failure: x is too large to be output with full precision */
{
mpfr_printf ("too large !");
}
else
{
printf ("Error in mpfr_printf(\"%s\", %d, %s, ...)",
fmt, prec, mpfr_print_rnd_mode (rnd));
if (stdout_redirect)
{
if ((fflush (stdout) == EOF) || (fclose (stdout) == -1))
{
perror ("check_random");
exit (1);
}
}
exit (1);
}
}
putchar ('\n');
}
mpfr_set_emin (old_emin);
mpfr_set_emax (old_emax);
mpfr_clear (x);
}
int
main (int argc, char *argv[])
{
int N;
tests_start_mpfr ();
/* with no argument: prints to /dev/null,
tprintf N: prints N tests to stdout */
if (argc == 1)
{
N = 1000;
stdout_redirect = -1;
if (freopen ("/dev/null", "w", stdout) == NULL)
{
/* We failed to open this device, try with a dummy file */
if (freopen ("mpfrtest.txt", "w", stdout) == NULL)
{
printf ("Can't open /dev/null or a temporary file\n");
exit (1);
}
}
}
else
{
stdout_redirect = 0;
N = atoi (argv[1]);
}
check_special ();
check_mixed ();
check_random (N);
if (stdout_redirect)
{
if ((fflush (stdout) == EOF) || (fclose (stdout) == -1))
perror ("main");
}
tests_end_mpfr ();
return 0;
}
#else /* MPFR_VERSION */
int
main (void)
{
printf ("Warning! Test disabled for this MPFR version.\n");
return 0;
}
#endif /* MPFR_VERSION */
#else /* HAVE_STDARG */
int
main (void)
{
/* We have nothing to test. */
return 0;
}
#endif /* HAVE_STDARG */
|