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

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

1.1       maekawa     1: /* Test mpq_cmp_ui.
                      2:
1.1.1.2 ! maekawa     3: Copyright (C) 1996, 1997 Free Software Foundation, Inc.
1.1       maekawa     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
1.1.1.2 ! maekawa     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
1.1       maekawa    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
1.1.1.2 ! maekawa    14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    15: License for more details.
                     16:
1.1.1.2 ! maekawa    17: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    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 "gmp.h"
                     23: #include "gmp-impl.h"
                     24: #include "urandom.h"
                     25:
                     26: #define NUM(x) (&((x)->_mp_num))
                     27: #define DEN(x) (&((x)->_mp_den))
                     28:
                     29: #define SGN(x) ((x) < 0 ? -1 : (x) > 0 ? 1 : 0)
                     30:
                     31: ref_mpq_cmp_ui (a, bn, bd)
                     32:      mpq_t a;
                     33:      unsigned long int bn, bd;
                     34: {
                     35:   mpz_t ai, bi;
                     36:   int cc;
                     37:
                     38:   mpz_init (ai);
                     39:   mpz_init (bi);
                     40:
                     41:   mpz_mul_ui (ai, NUM (a), bd);
                     42:   mpz_mul_ui (bi, DEN (a), bn);
                     43:   cc = mpz_cmp (ai, bi);
                     44:   mpz_clear (ai);
                     45:   mpz_clear (bi);
                     46:   return cc;
                     47: }
                     48:
                     49: #ifndef SIZE
                     50: #define SIZE 8 /* increasing this lowers the probabilty of finding an error */
                     51: #endif
                     52:
                     53: main (argc, argv)
                     54:      int argc;
                     55:      char **argv;
                     56: {
                     57:   mpq_t a, b;
                     58:   mp_size_t size;
                     59:   int reps = 100000;
                     60:   int i;
                     61:   int cc, ccref;
                     62:   unsigned long int bn, bd;
                     63:
                     64:   if (argc == 2)
                     65:      reps = atoi (argv[1]);
                     66:
                     67:   mpq_init (a);
                     68:   mpq_init (b);
                     69:
                     70:   for (i = 0; i < reps; i++)
                     71:     {
                     72:       size = urandom () % SIZE - SIZE/2;
                     73:       mpz_random2 (NUM (a), size);
                     74:       do
                     75:        {
                     76:          size = urandom () % SIZE - SIZE/2;
                     77:          mpz_random2 (DEN (a), size);
                     78:        }
                     79:       while (mpz_cmp_ui (DEN (a), 0) == 0);
                     80:
                     81:       mpz_random2 (NUM (b), 1);
1.1.1.2 ! maekawa    82:       mpz_mod_ui (NUM (b), NUM (b), ~(unsigned long int) 0);
        !            83:       mpz_add_ui (NUM (b), NUM (b), 1);
        !            84:
        !            85:       mpz_random2 (DEN (b), 1);
        !            86:       mpz_mod_ui (DEN (b), DEN (b), ~(unsigned long int) 0);
        !            87:       mpz_add_ui (DEN (b), DEN (b), 1);
1.1       maekawa    88:
                     89:       mpq_canonicalize (a);
                     90:       mpq_canonicalize (b);
                     91:
                     92:       bn = mpz_get_ui (NUM (b));
                     93:       bd = mpz_get_ui (DEN (b));
                     94:
                     95:       ccref = ref_mpq_cmp_ui (a, bn, bd);
                     96:       cc = mpq_cmp_ui (a, bn, bd);
                     97:
                     98:       if (SGN (ccref) != SGN (cc))
                     99:        abort ();
                    100:     }
                    101:
                    102:   exit (0);
                    103: }

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