1 /* Test mpq_cmp_ui. 2 3 Copyright 1996, 1997, 2001, 2002 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 http://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_ui (mpq_t a, unsigned long int bn, unsigned long int bd) 31 { 32 mpz_t ai, bi; 33 int cc; 34 35 mpz_init (ai); 36 mpz_init (bi); 37 38 mpz_mul_ui (ai, NUM (a), bd); 39 mpz_mul_ui (bi, DEN (a), bn); 40 cc = mpz_cmp (ai, bi); 41 mpz_clear (ai); 42 mpz_clear (bi); 43 return cc; 44 } 45 46 #ifndef SIZE 47 #define SIZE 8 /* increasing this lowers the probabilty of finding an error */ 48 #endif 49 50 int 51 main (int argc, char **argv) 52 { 53 mpq_t a, b; 54 mp_size_t size; 55 int reps = 10000; 56 int i; 57 int cc, ccref; 58 unsigned long int bn, bd; 59 60 tests_start (); 61 62 if (argc == 2) 63 reps = atoi (argv[1]); 64 65 mpq_init (a); 66 mpq_init (b); 67 68 for (i = 0; i < reps; i++) 69 { 70 size = urandom () % SIZE - SIZE/2; 71 mpz_random2 (NUM (a), size); 72 do 73 { 74 size = urandom () % SIZE - SIZE/2; 75 mpz_random2 (DEN (a), size); 76 } 77 while (mpz_cmp_ui (DEN (a), 0) == 0); 78 79 mpz_random2 (NUM (b), (mp_size_t) 1); 80 mpz_mod_ui (NUM (b), NUM (b), ~(unsigned long int) 0); 81 mpz_add_ui (NUM (b), NUM (b), 1); 82 83 mpz_random2 (DEN (b), (mp_size_t) 1); 84 mpz_mod_ui (DEN (b), DEN (b), ~(unsigned long int) 0); 85 mpz_add_ui (DEN (b), DEN (b), 1); 86 87 mpq_canonicalize (a); 88 mpq_canonicalize (b); 89 90 bn = mpz_get_ui (NUM (b)); 91 bd = mpz_get_ui (DEN (b)); 92 93 ccref = ref_mpq_cmp_ui (a, bn, bd); 94 cc = mpq_cmp_ui (a, bn, bd); 95 96 if (SGN (ccref) != SGN (cc)) 97 abort (); 98 } 99 100 mpq_clear (a); 101 mpq_clear (b); 102 103 tests_end (); 104 exit (0); 105 } 106