source: trunk/third/gmp/tests/mpz/io.c @ 18191

Revision 18191, 3.0 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* Test conversion and I/O using mpz_out_str and mpz_inp_str.
2
3Copyright 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 02111-1307, USA. */
21
22#include "config.h"
23
24#include <stdio.h>
25#include <stdlib.h>
26#if HAVE_UNISTD_H
27#include <unistd.h>             /* for unlink */
28#endif
29
30#include "gmp.h"
31#include "gmp-impl.h"
32#include "tests.h"
33
34#define FILENAME  "io.tmp"
35
36void
37debug_mp (mpz_t x, int base)
38{
39  mpz_out_str (stdout, base, x); fputc ('\n', stdout);
40}
41
42int
43main (int argc, char **argv)
44{
45  mpz_t  op1, op2;
46  mp_size_t size;
47  int i;
48  int reps = 10000;
49  FILE *fp;
50  int base;
51  gmp_randstate_ptr rands;
52  mpz_t bs;
53  unsigned long bsi, size_range;
54  size_t nread;
55
56  tests_start ();
57  rands = RANDS;
58
59  mpz_init (bs);
60
61  if (argc == 2)
62    reps = atoi (argv[1]);
63
64  mpz_init (op1);
65  mpz_init (op2);
66
67  fp = fopen (FILENAME, "w+");
68
69  for (i = 0; i < reps; i++)
70    {
71      mpz_urandomb (bs, rands, 32);
72      size_range = mpz_get_ui (bs) % 10 + 2;
73
74      mpz_urandomb (bs, rands, size_range);
75      size = mpz_get_ui (bs);
76      mpz_rrandomb (op1, rands, size);
77      mpz_urandomb (bs, rands, 1);
78      bsi = mpz_get_ui (bs);
79      if ((bsi & 1) != 0)
80        mpz_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 (mpz_out_str (fp, base, op1) == 0
90          || putc (' ', fp) == EOF
91          || fflush (fp) != 0)
92        {
93          printf ("mpz_out_str write error\n");
94          abort ();
95        }
96
97      rewind (fp); 
98      nread = mpz_inp_str (op2, fp, base);
99      if (nread == 0)
100        {
101          if (ferror (fp))
102            printf ("mpz_inp_str stream read error\n");
103          else
104            printf ("mpz_inp_str data conversion error\n");
105          abort ();
106        }
107
108      if (nread != ftell(fp))
109        {
110          printf ("mpz_inp_str nread doesn't match ftell\n");
111          printf ("  nread  %u\n", nread);
112          printf ("  ftell  %ld\n", ftell(fp));
113          abort ();
114        }
115
116      if (mpz_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  mpz_clear (op1);
132  mpz_clear (op2);
133
134  tests_end ();
135  exit (0);
136}
Note: See TracBrowser for help on using the repository browser.