[BACK]Return to t-cmp_ui.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / tests / mpq

Annotation of OpenXM_contrib/gmp/tests/mpq/t-cmp_ui.c, Revision 1.1.1.1

1.1       ohara       1: /* Test mpq_cmp_ui.
                      2:
                      3: Copyright 1996, 1997, 2001 Free Software Foundation, Inc.
                      4:
                      5: This file is part of the GNU MP Library.
                      6:
                      7: The GNU MP Library is free software; you can redistribute it and/or modify
                      8: it under the terms of the GNU Lesser General Public License as published by
                      9: the Free Software Foundation; either version 2.1 of the License, or (at your
                     10: option) any later version.
                     11:
                     12: The GNU MP Library is distributed in the hope that it will be useful, but
                     13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
                     15: License for more details.
                     16:
                     17: You should have received a copy of the GNU Lesser General Public License
                     18: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24:
                     25: #include "gmp.h"
                     26: #include "gmp-impl.h"
                     27: #include "tests.h"
                     28:
                     29: #define NUM(x) (&((x)->_mp_num))
                     30: #define DEN(x) (&((x)->_mp_den))
                     31:
                     32: #define SGN(x) ((x) < 0 ? -1 : (x) > 0 ? 1 : 0)
                     33:
                     34: int
                     35: ref_mpq_cmp_ui (mpq_t a, unsigned long int bn, unsigned long int bd)
                     36: {
                     37:   mpz_t ai, bi;
                     38:   int cc;
                     39:
                     40:   mpz_init (ai);
                     41:   mpz_init (bi);
                     42:
                     43:   mpz_mul_ui (ai, NUM (a), bd);
                     44:   mpz_mul_ui (bi, DEN (a), bn);
                     45:   cc = mpz_cmp (ai, bi);
                     46:   mpz_clear (ai);
                     47:   mpz_clear (bi);
                     48:   return cc;
                     49: }
                     50:
                     51: #ifndef SIZE
                     52: #define SIZE 8 /* increasing this lowers the probabilty of finding an error */
                     53: #endif
                     54:
                     55: int
                     56: main (int argc, char **argv)
                     57: {
                     58:   mpq_t a, b;
                     59:   mp_size_t size;
                     60:   int reps = 100000;
                     61:   int i;
                     62:   int cc, ccref;
                     63:   unsigned long int bn, bd;
                     64:
                     65:   tests_start ();
                     66:
                     67:   if (argc == 2)
                     68:      reps = atoi (argv[1]);
                     69:
                     70:   mpq_init (a);
                     71:   mpq_init (b);
                     72:
                     73:   for (i = 0; i < reps; i++)
                     74:     {
                     75:       size = urandom () % SIZE - SIZE/2;
                     76:       mpz_random2 (NUM (a), size);
                     77:       do
                     78:        {
                     79:          size = urandom () % SIZE - SIZE/2;
                     80:          mpz_random2 (DEN (a), size);
                     81:        }
                     82:       while (mpz_cmp_ui (DEN (a), 0) == 0);
                     83:
                     84:       mpz_random2 (NUM (b), 1);
                     85:       mpz_mod_ui (NUM (b), NUM (b), ~(unsigned long int) 0);
                     86:       mpz_add_ui (NUM (b), NUM (b), 1);
                     87:
                     88:       mpz_random2 (DEN (b), 1);
                     89:       mpz_mod_ui (DEN (b), DEN (b), ~(unsigned long int) 0);
                     90:       mpz_add_ui (DEN (b), DEN (b), 1);
                     91:
                     92:       mpq_canonicalize (a);
                     93:       mpq_canonicalize (b);
                     94:
                     95:       bn = mpz_get_ui (NUM (b));
                     96:       bd = mpz_get_ui (DEN (b));
                     97:
                     98:       ccref = ref_mpq_cmp_ui (a, bn, bd);
                     99:       cc = mpq_cmp_ui (a, bn, bd);
                    100:
                    101:       if (SGN (ccref) != SGN (cc))
                    102:        abort ();
                    103:     }
                    104:
                    105:   mpq_clear (a);
                    106:   mpq_clear (b);
                    107:
                    108:   tests_end ();
                    109:   exit (0);
                    110: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>