github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpq/io.c (about)

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