1 /* Test mpz_inp_raw and mpz_out_raw.
3 Copyright 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/. */
33 #define FILENAME "t-io_raw.tmp"
36 /* In the fopen, "b" selects binary mode on DOS systems, meaning no
37 conversion of '\n' to and from CRLF. It's believed systems without such
38 nonsense will simply ignore the "b", but in case that's not so a plain
39 "w+" is attempted if "w+b" fails. */
42 fopen_wplusb_or_die (const char *filename)
45 fp = fopen (filename, "w+b");
47 fp = fopen (filename, "w+");
51 printf ("Cannot create file %s\n", filename);
57 /* use 0x80 to check nothing bad happens with sign extension etc */
58 #define BYTEVAL(i) (((i) + 1) | 0x80)
63 int i, j, zeros, neg, error = 0;
65 size_t want_ret, got_ret;
72 for (i = 0; i < 32; i++)
74 for (zeros = 0; zeros < 8; zeros++)
76 for (neg = 0; neg <= 1; neg++)
78 want_ret = i + zeros + 4;
80 /* need this to get the twos complement right */
81 ASSERT_ALWAYS (sizeof (size) >= 4);
87 fp = fopen_wplusb_or_die (FILENAME);
88 for (j = 3; j >= 0; j--)
89 ASSERT_ALWAYS (putc ((size >> (j*8)) & 0xFF, fp) != EOF);
90 for (j = 0; j < zeros; j++)
91 ASSERT_ALWAYS (putc ('\0', fp) != EOF);
92 for (j = 0; j < i; j++)
93 ASSERT_ALWAYS (putc (BYTEVAL (j), fp) != EOF);
94 /* and some trailing garbage */
95 ASSERT_ALWAYS (putc ('x', fp) != EOF);
96 ASSERT_ALWAYS (putc ('y', fp) != EOF);
97 ASSERT_ALWAYS (putc ('z', fp) != EOF);
98 ASSERT_ALWAYS (fflush (fp) == 0);
101 got_ret = mpz_inp_raw (got, fp);
102 ASSERT_ALWAYS (! ferror(fp));
103 ASSERT_ALWAYS (fclose (fp) == 0);
105 MPZ_CHECK_FORMAT (got);
107 if (got_ret != want_ret)
109 printf ("check_in: return value wrong\n");
112 if (mpz_cmp (got, want) != 0)
114 printf ("check_in: result wrong\n");
119 printf (" i=%d zeros=%d neg=%d\n", i, zeros, neg);
120 printf (" got_ret %lu\n", (unsigned long) got_ret);
121 printf (" want_ret %lu\n", (unsigned long) want_ret);
122 mpz_trace (" got ", got);
123 mpz_trace (" want ", want);
127 mpz_neg (want, want);
130 mpz_mul_2exp (want, want, 8);
131 mpz_add_ui (want, want, (unsigned long) BYTEVAL (i));
142 int i, j, neg, error = 0;
144 char want[256], got[256], *p;
145 size_t want_len, got_ret, got_read;
151 for (i = 0; i < 32; i++)
153 for (neg = 0; neg <= 1; neg++)
157 /* need this to get the twos complement right */
158 ASSERT_ALWAYS (sizeof (size) >= 4);
165 for (j = 3; j >= 0; j--)
166 *p++ = size >> (j*8);
167 for (j = 0; j < i; j++)
169 ASSERT_ALWAYS (p <= want + sizeof (want));
171 fp = fopen_wplusb_or_die (FILENAME);
172 got_ret = mpz_out_raw (fp, z);
173 ASSERT_ALWAYS (fflush (fp) == 0);
175 got_read = fread (got, 1, sizeof(got), fp);
176 ASSERT_ALWAYS (! ferror(fp));
177 ASSERT_ALWAYS (fclose (fp) == 0);
179 if (got_ret != want_len)
181 printf ("check_out: wrong return value\n");
184 if (got_read != want_len)
186 printf ("check_out: wrong number of bytes read back\n");
189 if (memcmp (want, got, want_len) != 0)
191 printf ("check_out: wrong data\n");
196 printf (" i=%d neg=%d\n", i, neg);
198 printf (" got_ret %lu\n", (unsigned long) got_ret);
199 printf (" got_read %lu\n", (unsigned long) got_read);
200 printf (" want_len %lu\n", (unsigned long) want_len);
202 for (j = 0; j < want_len; j++)
203 printf (" %02X", (unsigned) (unsigned char) want[j]);
206 for (j = 0; j < want_len; j++)
207 printf (" %02X", (unsigned) (unsigned char) got[j]);
214 mpz_mul_2exp (z, z, 8);
215 mpz_add_ui (z, z, (unsigned long) BYTEVAL (i));
225 gmp_randstate_ptr rands = RANDS;
228 size_t inp_ret, out_ret;
234 for (i = 0; i < 500; i++)
236 mpz_erandomb (want, rands, 10*GMP_LIMB_BITS);
237 mpz_negrandom (want, rands);
239 fp = fopen_wplusb_or_die (FILENAME);
240 out_ret = mpz_out_raw (fp, want);
241 ASSERT_ALWAYS (fflush (fp) == 0);
243 inp_ret = mpz_inp_raw (got, fp);
244 ASSERT_ALWAYS (fclose (fp) == 0);
246 MPZ_CHECK_FORMAT (got);
248 if (inp_ret != out_ret)
250 printf ("check_rand: different inp/out return values\n");
253 if (mpz_cmp (got, want) != 0)
255 printf ("check_rand: wrong result\n");
260 printf (" out_ret %lu\n", (unsigned long) out_ret);
261 printf (" inp_ret %lu\n", (unsigned long) inp_ret);
262 mpz_trace (" want", want);
263 mpz_trace (" got ", got);