1 /* Test conversion and I/O using mpz_out_str and mpz_inp_str.
3 Copyright 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
25 #include <unistd.h> /* for unlink */
32 #define FILENAME "io.tmp"
35 debug_mp (mpz_t x, int base)
37 mpz_out_str (stdout, base, x); fputc ('\n', stdout);
41 main (int argc, char **argv)
49 gmp_randstate_ptr rands;
51 unsigned long bsi, size_range;
60 reps = atoi (argv[1]);
65 fp = fopen (FILENAME, "w+");
67 for (i = 0; i < reps; i++)
69 mpz_urandomb (bs, rands, 32);
70 size_range = mpz_get_ui (bs) % 10 + 2;
72 mpz_urandomb (bs, rands, size_range);
73 size = mpz_get_ui (bs);
74 mpz_rrandomb (op1, rands, size);
75 mpz_urandomb (bs, rands, 1);
76 bsi = mpz_get_ui (bs);
80 mpz_urandomb (bs, rands, 16);
81 bsi = mpz_get_ui (bs);
87 if (mpz_out_str (fp, base, op1) == 0
88 || putc (' ', fp) == EOF
91 printf ("mpz_out_str write error\n");
96 nread = mpz_inp_str (op2, fp, base);
100 printf ("mpz_inp_str stream read error\n");
102 printf ("mpz_inp_str data conversion error\n");
106 if (nread != ftell(fp))
108 printf ("mpz_inp_str nread doesn't match ftell\n");
109 printf (" nread %lu\n", (unsigned long) nread);
110 printf (" ftell %ld\n", ftell(fp));
114 if (mpz_cmp (op1, op2))
117 printf ("op1 = "); debug_mp (op1, -16);
118 printf ("op2 = "); debug_mp (op2, -16);
119 printf ("base = %d\n", base);