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

Annotation of OpenXM_contrib/gmp/mpz/tests/t-gcd.c, Revision 1.1.1.1

1.1       maekawa     1: /* Test mpz_gcd, mpz_gcdext, mpz_mul, mpz_tdiv_r, mpz_add, mpz_cmp,
                      2:    mpz_cmp_ui, mpz_init_set, mpz_set, mpz_clear.
                      3:
                      4: Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
                      5:
                      6: This file is part of the GNU MP Library.
                      7:
                      8: The GNU MP Library is free software; you can redistribute it and/or modify
                      9: it under the terms of the GNU Library General Public License as published by
                     10: the Free Software Foundation; either version 2 of the License, or (at your
                     11: option) any later version.
                     12:
                     13: The GNU MP Library is distributed in the hope that it will be useful, but
                     14: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
                     16: License for more details.
                     17:
                     18: You should have received a copy of the GNU Library General Public License
                     19: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     21: MA 02111-1307, USA. */
                     22:
                     23: #include <stdio.h>
                     24: #include "gmp.h"
                     25: #include "gmp-impl.h"
                     26: #include "urandom.h"
                     27:
                     28: void mpz_refgcd (), debug_mp ();
                     29:
                     30: #ifndef SIZE
                     31: #define SIZE 256 /* really needs to be this large to exercise corner cases! */
                     32: #endif
                     33:
                     34: main (argc, argv)
                     35:      int argc;
                     36:      char **argv;
                     37: {
                     38:   mpz_t op1, op2;
                     39:   mpz_t refgcd, gcd, s, t;
                     40:   mpz_t temp1, temp2;
                     41:   mp_size_t op1_size, op2_size;
                     42:   int i;
                     43:   int reps = 1000;
                     44:
                     45:   if (argc == 2)
                     46:      reps = atoi (argv[1]);
                     47:
                     48:   mpz_init (op1);
                     49:   mpz_init (op2);
                     50:   mpz_init (refgcd);
                     51:   mpz_init (gcd);
                     52:   mpz_init (temp1);
                     53:   mpz_init (temp2);
                     54:   mpz_init (s);
                     55:   mpz_init (t);
                     56:
                     57:   for (i = 0; i < reps; i++)
                     58:     {
                     59:       op1_size = urandom () % SIZE - SIZE/2;
                     60:       op2_size = urandom () % SIZE - SIZE/2;
                     61:
                     62:       mpz_random2 (op1, op1_size);
                     63:       mpz_random2 (op2, op2_size);
                     64:
                     65:       mpz_refgcd (refgcd, op1, op2);
                     66:
                     67:       mpz_gcd (gcd, op1, op2);
                     68:       if (mpz_cmp (refgcd, gcd))
                     69:        dump_abort (op1, op2);
                     70:
                     71:       mpz_gcdext (gcd, s, t, op1, op2);
                     72:       if (mpz_cmp (refgcd, gcd))
                     73:        dump_abort (op1, op2);
                     74:
                     75:       mpz_mul (temp1, s, op1);
                     76:       mpz_mul (temp2, t, op2);
                     77:       mpz_add (gcd, temp1, temp2);
                     78:       if (mpz_cmp (refgcd, gcd))
                     79:        dump_abort (op1, op2);
                     80:     }
                     81:
                     82:   exit (0);
                     83: }
                     84:
                     85: void
                     86: mpz_refgcd (g, x, y)
                     87:      mpz_t g;
                     88:      mpz_t x, y;
                     89: {
                     90:   mpz_t xx, yy;
                     91:
                     92:   mpz_init (xx);
                     93:   mpz_init (yy);
                     94:
                     95:   mpz_abs (xx, x);
                     96:   mpz_abs (yy, y);
                     97:
                     98:   for (;;)
                     99:     {
                    100:       if (mpz_cmp_ui (yy, 0) == 0)
                    101:        {
                    102:          mpz_set (g, xx);
                    103:          break;
                    104:        }
                    105:       mpz_tdiv_r (xx, xx, yy);
                    106:       if (mpz_cmp_ui (xx, 0) == 0)
                    107:        {
                    108:          mpz_set (g, yy);
                    109:          break;
                    110:        }
                    111:       mpz_tdiv_r (yy, yy, xx);
                    112:     }
                    113:
                    114:   mpz_clear (xx);
                    115:   mpz_clear (yy);
                    116: }
                    117:
                    118: dump_abort (op1, op2)
                    119:      mpz_t op1, op2;
                    120: {
                    121:   fprintf (stderr, "ERROR\n");
                    122:   fprintf (stderr, "op1 = "); debug_mp (op1, -16);
                    123:   fprintf (stderr, "op2 = "); debug_mp (op2, -16);
                    124:   abort();
                    125: }
                    126:
                    127: void
                    128: debug_mp (x, base)
                    129:      mpz_t x;
                    130: {
                    131:   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
                    132: }

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