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

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

1.1       maekawa     1: /* Test mpz_add, mpz_add_ui, mpz_cmp, mpz_cmp, mpz_mul, mpz_sqrtrem.
                      2:
                      3: Copyright (C) 1991, 1993, 1994, 1996, 2000 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 "gmp.h"
                     24: #include "gmp-impl.h"
                     25: #include "urandom.h"
                     26:
                     27: void debug_mp ();
                     28:
                     29: #ifndef SIZE
                     30: #define SIZE 100
                     31: #endif
                     32:
                     33: main (argc, argv)
                     34:      int argc;
                     35:      char **argv;
                     36: {
                     37:   mpz_t x2;
                     38:   mpz_t x;
                     39:   mpz_t temp, temp2;
                     40:   mp_size_t x2_size;
                     41:   int i;
                     42:   int reps = 10000;
                     43:   unsigned long nth;
                     44:
                     45:   if (argc == 2)
                     46:      reps = atoi (argv[1]);
                     47:
                     48:   mpz_init (x2);
                     49:   mpz_init (x);
                     50:   mpz_init (temp);
                     51:   mpz_init (temp2);
                     52:
                     53:   for (i = 0; i < reps; i++)
                     54:     {
                     55:       x2_size = urandom () % SIZE + 1;
                     56:       mpz_random2 (x2, x2_size);
                     57:       nth = urandom () % mpz_sizeinbase (x2, 2) + 1;
                     58:       if ((urandom() & 1) == 0)
                     59:        {
                     60:          /* With 50% probability, set x2 just below a perfect power.  */
                     61:          mpz_root (x, x2, nth);
                     62:          mpz_pow_ui (x2, x, nth);
                     63:          if (mpz_sgn (x2) != 0)
                     64:            mpz_sub_ui (x2, x2, 1L);
                     65:        }
                     66:
                     67:       mpz_root (x, x2, nth);
                     68:       mpz_pow_ui (temp, x, nth);
                     69:
                     70:       /* Is power of result > argument?  */
                     71:       if (mpz_cmp (temp, x2) > 0)
                     72:        {
                     73:          fprintf (stderr, "ERROR after test %d\n", i);
                     74:          debug_mp (x2, 10);
                     75:          debug_mp (x, 10);
                     76:          fprintf (stderr, "nth: %lu\n", nth);
                     77:          abort ();
                     78:        }
                     79:
                     80:       if (nth > 1 && mpz_cmp_ui (temp, 1L) > 0 && ! mpz_perfect_power_p (temp))
                     81:        {
                     82:          fprintf (stderr, "ERROR in mpz_perfect_power_p after test %d\n", i);
                     83:          debug_mp (temp, 10);
                     84:          debug_mp (x, 10);
                     85:          fprintf (stderr, "nth: %lu\n", nth);
                     86:          abort ();
                     87:        }
                     88:
                     89:       if (nth > 10000)
                     90:        continue;               /* skip too expensive test */
                     91:
                     92:       mpz_add_ui (temp2, x, 1L);
                     93:       mpz_pow_ui (temp2, temp2, nth);
                     94:
                     95:       /* Is square of (result + 1) <= argument?  */
                     96:       if (mpz_cmp (temp2, x2) <= 0)
                     97:        {
                     98:          fprintf (stderr, "ERROR after test %d\n", i);
                     99:          debug_mp (x2, 10);
                    100:          debug_mp (x, 10);
                    101:          fprintf (stderr, "nth: %lu\n", nth);
                    102:          abort ();
                    103:        }
                    104:     }
                    105:
                    106:   exit (0);
                    107: }
                    108:
                    109: void
                    110: debug_mp (x, base)
                    111:      mpz_t x;
                    112: {
                    113:   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
                    114: }

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