github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpq/t-cmp_z.c (about) 1 /* Test mpq_cmp_z. 2 3 Copyright 1996, 2001, 2015 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 <stdio.h> 21 #include <stdlib.h> 22 23 #include "gmp.h" 24 #include "gmp-impl.h" 25 #include "tests.h" 26 27 #define SGN(x) ((x) < 0 ? -1 : (x) > 0 ? 1 : 0) 28 29 int 30 ref_mpq_cmp_z (mpq_t a, mpz_t b) 31 { 32 mpz_t bi; 33 int cc; 34 35 mpz_init (bi); 36 37 mpz_mul (bi, b, DEN (a)); 38 cc = mpz_cmp (NUM (a), bi); 39 mpz_clear (bi); 40 return cc; 41 } 42 43 #ifndef SIZE 44 #define SIZE 8 /* increasing this lowers the probability of finding an error */ 45 #endif 46 47 #ifndef MAXN 48 #define MAXN 5 /* increasing this impatcs on total timing */ 49 #endif 50 51 void 52 sizes_test (int m) 53 { 54 mpq_t a; 55 mpz_t b; 56 int i, j, k, s; 57 int cc, ccref; 58 59 mpq_init (a); 60 mpz_init (b); 61 62 for (i = 0; i <= MAXN ; ++i) 63 { 64 mpz_setbit (DEN (a), i*m); /* \sum_0^i 2^(i*m) */ 65 for (j = 0; j <= MAXN; ++j) 66 { 67 mpz_set_ui (NUM (a), 0); 68 mpz_setbit (NUM (a), j*m); /* 2^(j*m) */ 69 for (k = 0; k <= MAXN; ++k) 70 { 71 mpz_set_ui (b, 0); 72 mpz_setbit (b, k*m); /* 2^(k*m) */ 73 if (i == 0) /* Denominator is 1, compare the two exponents */ 74 ccref = (j>k)-(j<k); 75 else 76 ccref = j-i > k ? 1 : -1; 77 for (s = 1; s >= -1; s -= 2) 78 { 79 cc = mpq_cmp_z (a, b); 80 81 if (ccref != SGN (cc)) 82 { 83 fprintf (stderr, "i=%i, j=%i, k=%i, m=%i, s=%i\n; ccref= %i, cc= %i\n", i, j, k, m, s, ccref, cc); 84 abort (); 85 } 86 87 mpq_neg (a, a); 88 mpz_neg (b, b); 89 ccref = - ccref; 90 } 91 } 92 } 93 } 94 95 mpq_clear (a); 96 mpz_clear (b); 97 } 98 99 int 100 main (int argc, char **argv) 101 { 102 mpq_t a; 103 mpz_t b; 104 mp_size_t size; 105 int reps = 10000; 106 int i; 107 int cc, ccref; 108 109 tests_start (); 110 111 if (argc == 2) 112 reps = atoi (argv[1]); 113 114 mpq_init (a); 115 mpz_init (b); 116 117 for (i = 0; i < reps; i++) 118 { 119 if (i % 8192 == 0) 120 sizes_test (urandom () % (i + 1) + 1); 121 size = urandom () % SIZE - SIZE/2; 122 mpz_random2 (NUM (a), size); 123 do 124 { 125 size = urandom () % (SIZE/2); 126 mpz_random2 (DEN (a), size); 127 } 128 while (mpz_cmp_ui (DEN (a), 0) == 0); 129 130 size = urandom () % SIZE - SIZE/2; 131 mpz_random2 (b, size); 132 133 mpq_canonicalize (a); 134 135 ccref = ref_mpq_cmp_z (a, b); 136 cc = mpq_cmp_z (a, b); 137 138 if (SGN (ccref) != SGN (cc)) 139 abort (); 140 } 141 142 mpq_clear (a); 143 mpz_clear (b); 144 145 tests_end (); 146 exit (0); 147 }