[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.2

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:
1.1.1.2 ! maekawa     4: Copyright (C) 1991, 1993, 1994, 1996, 1997, 2000 Free Software Foundation, Inc.
1.1       maekawa     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
1.1.1.2 ! maekawa     9: it under the terms of the GNU Lesser General Public License as published by
        !            10: the Free Software Foundation; either version 2.1 of the License, or (at your
1.1       maekawa    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
1.1.1.2 ! maekawa    15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    16: License for more details.
                     17:
1.1.1.2 ! maekawa    18: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    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:
1.1.1.2 ! maekawa    28: void debug_mp ();
1.1       maekawa    29:
                     30: #ifndef SIZE
1.1.1.2 ! maekawa    31: #define SIZE 128
1.1       maekawa    32: #endif
                     33:
                     34: main (argc, argv)
                     35:      int argc;
                     36:      char **argv;
                     37: {
1.1.1.2 ! maekawa    38:   mpz_t op1, op2, x;
        !            39:   mpz_t gcd, gcd2, s, t;
1.1       maekawa    40:   mpz_t temp1, temp2;
1.1.1.2 ! maekawa    41:   mp_size_t op1_size, op2_size, x_size;
1.1       maekawa    42:   int i;
1.1.1.2 ! maekawa    43:   int reps = 2000;
1.1       maekawa    44:
                     45:   if (argc == 2)
                     46:      reps = atoi (argv[1]);
                     47:
                     48:   mpz_init (op1);
                     49:   mpz_init (op2);
1.1.1.2 ! maekawa    50:   mpz_init (x);
1.1       maekawa    51:   mpz_init (gcd);
1.1.1.2 ! maekawa    52:   mpz_init (gcd2);
1.1       maekawa    53:   mpz_init (temp1);
                     54:   mpz_init (temp2);
                     55:   mpz_init (s);
                     56:   mpz_init (t);
                     57:
                     58:   for (i = 0; i < reps; i++)
                     59:     {
                     60:       op1_size = urandom () % SIZE - SIZE/2;
                     61:       op2_size = urandom () % SIZE - SIZE/2;
1.1.1.2 ! maekawa    62:       x_size = urandom () % SIZE/2;
1.1       maekawa    63:
                     64:       mpz_random2 (op1, op1_size);
                     65:       mpz_random2 (op2, op2_size);
1.1.1.2 ! maekawa    66:       mpz_random2 (x, x_size);
        !            67:       mpz_mul (op1, op1, x);
        !            68:       mpz_mul (op2, op2, x);
1.1       maekawa    69:
                     70:       mpz_gcd (gcd, op1, op2);
1.1.1.2 ! maekawa    71:       /* We know GCD will be at least X, since we multiplied in that factor.  */
        !            72:       if (mpz_cmp (gcd, x) < 0 && mpz_sgn (op1) != 0 && mpz_sgn (op2) != 0)
        !            73:        dump_abort (op1, op2);
        !            74:
        !            75:       mpz_gcdext (gcd2, s, t, op1, op2);
        !            76:       if (mpz_cmp (gcd, gcd2))
1.1       maekawa    77:        dump_abort (op1, op2);
                     78:
1.1.1.2 ! maekawa    79:       mpz_gcdext (gcd2, s, NULL, op1, op2);
        !            80:       if (mpz_cmp (gcd, gcd2))
1.1       maekawa    81:        dump_abort (op1, op2);
                     82:
                     83:       mpz_mul (temp1, s, op1);
                     84:       mpz_mul (temp2, t, op2);
1.1.1.2 ! maekawa    85:       mpz_add (gcd2, temp1, temp2);
        !            86:       if (mpz_cmp (gcd, gcd2))
1.1       maekawa    87:        dump_abort (op1, op2);
                     88:     }
                     89:
                     90:   exit (0);
                     91: }
                     92:
                     93: dump_abort (op1, op2)
                     94:      mpz_t op1, op2;
                     95: {
                     96:   fprintf (stderr, "ERROR\n");
                     97:   fprintf (stderr, "op1 = "); debug_mp (op1, -16);
                     98:   fprintf (stderr, "op2 = "); debug_mp (op2, -16);
                     99:   abort();
                    100: }
                    101:
                    102: void
                    103: debug_mp (x, base)
                    104:      mpz_t x;
                    105: {
                    106:   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
                    107: }

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