1 /* Test conversion and I/O using mpq_out_str and mpq_inp_str.
2 
3 Copyright 1993, 1994, 1996, 2000, 2001, 2012 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 "config.h"
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #if HAVE_UNISTD_H
25 #include <unistd.h>           /* for unlink */
26 #endif
27 
28 #include "gmp-impl.h"
29 #include "tests.h"
30 
31 #define FILENAME  "io.tmp"
32 
33 void
debug_mp(mpq_t x,int base)34 debug_mp (mpq_t x, int base)
35 {
36   mpq_out_str (stdout, base, x); fputc ('\n', stdout);
37 }
38 
39 int
main(int argc,char ** argv)40 main (int argc, char **argv)
41 {
42   mpq_t  op1, op2;
43   mp_size_t size;
44   int i;
45   int reps = 10000;
46   FILE *fp;
47   int base;
48   gmp_randstate_ptr rands;
49   mpz_t bs;
50   unsigned long bsi, size_range;
51   size_t nread;
52 
53   tests_start ();
54   rands = RANDS;
55 
56   mpz_init (bs);
57 
58   if (argc == 2)
59     reps = atoi (argv[1]);
60 
61   mpq_init (op1);
62   mpq_init (op2);
63 
64   fp = fopen (FILENAME, "w+");
65 
66   for (i = 0; i < reps; i++)
67     {
68       mpz_urandomb (bs, rands, 32);
69       size_range = mpz_get_ui (bs) % 10 + 2;
70 
71       mpz_urandomb (bs, rands, size_range);
72       size = mpz_get_ui (bs);
73       mpz_errandomb (mpq_numref(op1), rands, 512L);
74       mpz_errandomb_nonzero (mpq_denref(op1), rands, 512L);
75       mpq_canonicalize (op1);
76 
77       mpz_urandomb (bs, rands, 1);
78       bsi = mpz_get_ui (bs);
79       if ((bsi & 1) != 0)
80           mpq_neg (op1, op1);
81 
82       mpz_urandomb (bs, rands, 16);
83       bsi = mpz_get_ui (bs);
84       base = bsi % 36 + 1;
85       if (base == 1)
86           base = 0;
87 
88       rewind (fp);
89       if (mpq_out_str (fp, base, op1) == 0
90             || putc (' ', fp) == EOF
91             || fflush (fp) != 0)
92           {
93             printf ("mpq_out_str write error\n");
94             abort ();
95           }
96 
97       rewind (fp);
98       nread = mpq_inp_str (op2, fp, base);
99       if (nread == 0)
100           {
101             if (ferror (fp))
102               printf ("mpq_inp_str stream read error\n");
103             else
104               printf ("mpq_inp_str data conversion error\n");
105             abort ();
106           }
107 
108       if (nread != ftell(fp))
109           {
110             printf ("mpq_inp_str nread doesn't match ftell\n");
111             printf ("  nread  %lu\n", (unsigned long) nread);
112             printf ("  ftell  %ld\n", ftell(fp));
113             abort ();
114           }
115 
116       if (mpq_cmp (op1, op2))
117           {
118             printf ("ERROR\n");
119             printf ("op1  = "); debug_mp (op1, -16);
120             printf ("op2  = "); debug_mp (op2, -16);
121             printf ("base = %d\n", base);
122             abort ();
123           }
124     }
125 
126   fclose (fp);
127 
128   unlink (FILENAME);
129 
130   mpz_clear (bs);
131   mpq_clear (op1);
132   mpq_clear (op2);
133 
134   tests_end ();
135   exit (0);
136 }
137