blob: 892f61d619f5e40c178c295c284169b04da1f480 [file] [log] [blame]
Austin Schuhbb1338c2024-06-15 19:31:16 -07001/* Test conversion and I/O using mpz_out_str and mpz_inp_str.
2
3Copyright 1993, 1994, 1996, 2000, 2001, 2012 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library test suite.
6
7The GNU MP Library test suite is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 3 of the License,
10or (at your option) any later version.
11
12The GNU MP Library test suite is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15Public License for more details.
16
17You should have received a copy of the GNU General Public License along with
18the 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
33void
34debug_mp (mpz_t x, int base)
35{
36 mpz_out_str (stdout, base, x); fputc ('\n', stdout);
37}
38
39int
40main (int argc, char **argv)
41{
42 mpz_t op1, op2;
43 mp_size_t size;
44 int i;
45 int reps = 10000;
46 FILE *fp;
47 int base, base_out;
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 mpz_init (op1);
62 mpz_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_rrandomb (op1, rands, size);
74 mpz_urandomb (bs, rands, 1);
75 bsi = mpz_get_ui (bs);
76 if ((bsi & 1) != 0)
77 mpz_neg (op1, op1);
78
79 mpz_urandomb (bs, rands, 16);
80 bsi = mpz_get_ui (bs);
81 base = bsi % 62 + 1;
82 if (base == 1)
83 base = 0;
84
85 if (i % 2 == 0 && base <= 36)
86 base_out = -base;
87 else
88 base_out = base;
89
90 rewind (fp);
91 if (mpz_out_str (fp, base_out, op1) == 0
92 || putc (' ', fp) == EOF
93 || fflush (fp) != 0)
94 {
95 printf ("mpz_out_str write error\n");
96 abort ();
97 }
98
99 rewind (fp);
100 nread = mpz_inp_str (op2, fp, base);
101 if (nread == 0)
102 {
103 if (ferror (fp))
104 printf ("mpz_inp_str stream read error\n");
105 else
106 printf ("mpz_inp_str data conversion error\n");
107 abort ();
108 }
109
110 if (nread != ftell(fp))
111 {
112 printf ("mpz_inp_str nread doesn't match ftell\n");
113 printf (" nread %lu\n", (unsigned long) nread);
114 printf (" ftell %ld\n", ftell(fp));
115 abort ();
116 }
117
118 if (mpz_cmp (op1, op2))
119 {
120 printf ("ERROR\n");
121 printf ("op1 = "); debug_mp (op1, -16);
122 printf ("op2 = "); debug_mp (op2, -16);
123 printf ("base = %d\n", base);
124 abort ();
125 }
126 }
127
128 fclose (fp);
129
130 unlink (FILENAME);
131
132 mpz_clear (bs);
133 mpz_clear (op1);
134 mpz_clear (op2);
135
136 tests_end ();
137 exit (0);
138}