github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpz/io.c (about) 1 /* Test conversion and I/O using mpz_out_str and mpz_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 (mpz_t x, int base) 36 { 37 mpz_out_str (stdout, base, x); fputc ('\n', stdout); 38 } 39 40 int 41 main (int argc, char **argv) 42 { 43 mpz_t op1, op2; 44 mp_size_t size; 45 int i; 46 int reps = 10000; 47 FILE *fp; 48 int base, base_out; 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 mpz_init (op1); 63 mpz_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_rrandomb (op1, rands, size); 75 mpz_urandomb (bs, rands, 1); 76 bsi = mpz_get_ui (bs); 77 if ((bsi & 1) != 0) 78 mpz_neg (op1, op1); 79 80 mpz_urandomb (bs, rands, 16); 81 bsi = mpz_get_ui (bs); 82 base = bsi % 62 + 1; 83 if (base == 1) 84 base = 0; 85 86 if (i % 2 == 0 && base <= 36) 87 base_out = -base; 88 else 89 base_out = base; 90 91 rewind (fp); 92 if (mpz_out_str (fp, base_out, op1) == 0 93 || putc (' ', fp) == EOF 94 || fflush (fp) != 0) 95 { 96 printf ("mpz_out_str write error\n"); 97 abort (); 98 } 99 100 rewind (fp); 101 nread = mpz_inp_str (op2, fp, base); 102 if (nread == 0) 103 { 104 if (ferror (fp)) 105 printf ("mpz_inp_str stream read error\n"); 106 else 107 printf ("mpz_inp_str data conversion error\n"); 108 abort (); 109 } 110 111 if (nread != ftell(fp)) 112 { 113 printf ("mpz_inp_str nread doesn't match ftell\n"); 114 printf (" nread %lu\n", (unsigned long) nread); 115 printf (" ftell %ld\n", ftell(fp)); 116 abort (); 117 } 118 119 if (mpz_cmp (op1, op2)) 120 { 121 printf ("ERROR\n"); 122 printf ("op1 = "); debug_mp (op1, -16); 123 printf ("op2 = "); debug_mp (op2, -16); 124 printf ("base = %d\n", base); 125 abort (); 126 } 127 } 128 129 fclose (fp); 130 131 unlink (FILENAME); 132 133 mpz_clear (bs); 134 mpz_clear (op1); 135 mpz_clear (op2); 136 137 tests_end (); 138 exit (0); 139 }