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

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

1.1       ohara       1: /* Test mpq_cmp.
                      2:
                      3: Copyright 1996, 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 (mpq_t a, mpq_t b)
                     36: {
                     37:   mpz_t ai, bi;
                     38:   int cc;
                     39:
                     40:   mpz_init (ai);
                     41:   mpz_init (bi);
                     42:
                     43:   mpz_mul (ai, NUM (a), DEN (b));
                     44:   mpz_mul (bi, NUM (b), DEN (a));
                     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:
                     64:   tests_start ();
                     65:
                     66:   if (argc == 2)
                     67:      reps = atoi (argv[1]);
                     68:
                     69:   mpq_init (a);
                     70:   mpq_init (b);
                     71:
                     72:   for (i = 0; i < reps; i++)
                     73:     {
                     74:       size = urandom () % SIZE - SIZE/2;
                     75:       mpz_random2 (NUM (a), size);
                     76:       do
                     77:        {
                     78:          size = urandom () % SIZE - SIZE/2;
                     79:          mpz_random2 (DEN (a), size);
                     80:        }
                     81:       while (mpz_cmp_ui (DEN (a), 0) == 0);
                     82:
                     83:       size = urandom () % SIZE - SIZE/2;
                     84:       mpz_random2 (NUM (b), size);
                     85:       do
                     86:        {
                     87:          size = urandom () % SIZE - SIZE/2;
                     88:          mpz_random2 (DEN (b), size);
                     89:        }
                     90:       while (mpz_cmp_ui (DEN (b), 0) == 0);
                     91:
                     92:       mpq_canonicalize (a);
                     93:       mpq_canonicalize (b);
                     94:
                     95:       ccref = ref_mpq_cmp (a, b);
                     96:       cc = mpq_cmp (a, b);
                     97:
                     98:       if (SGN (ccref) != SGN (cc))
                     99:        abort ();
                    100:     }
                    101:
                    102:   mpq_clear (a);
                    103:   mpq_clear (b);
                    104:
                    105:   tests_end ();
                    106:   exit (0);
                    107: }

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