Annotation of OpenXM_contrib/gmp/tests/mpz/t-cong_2exp.c, Revision 1.1
1.1 ! ohara 1: /* test mpz_congruent_2exp_p */
! 2:
! 3: /*
! 4: Copyright 2001 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 Lesser General Public License as published by
! 10: the Free Software Foundation; either version 2.1 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 Lesser General Public
! 16: License for more details.
! 17:
! 18: You should have received a copy of the GNU Lesser 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:
! 24: #include <stdio.h>
! 25: #include <stdlib.h>
! 26: #include "gmp.h"
! 27: #include "gmp-impl.h"
! 28: #include "tests.h"
! 29:
! 30:
! 31: void
! 32: check_one (mpz_srcptr a, mpz_srcptr c, unsigned long d, int want)
! 33: {
! 34: mpz_t diff, d2exp;
! 35: int got;
! 36: int swap;
! 37:
! 38: for (swap = 0; swap <= 1; swap++)
! 39: {
! 40: got = (mpz_congruent_2exp_p (a, c, d) != 0);
! 41: if (want != got)
! 42: {
! 43: mpz_init (diff);
! 44: mpz_init (d2exp);
! 45:
! 46: mpz_sub (diff, a, c);
! 47: mpz_set_ui (d2exp, 1L);
! 48: mpz_mul_2exp (d2exp, d2exp, d);
! 49:
! 50: printf ("mpz_congruent_2exp_p wrong\n");
! 51: printf (" expected %d got %d\n", want, got);
! 52: mpz_trace (" a", a);
! 53: mpz_trace (" c", c);
! 54: mpz_trace (" a-c", diff);
! 55: mpz_trace (" 2^d", d2exp);
! 56: printf (" d=%lu\n", d);
! 57:
! 58: mp_trace_base = -16;
! 59: mpz_trace (" a", a);
! 60: mpz_trace (" c", c);
! 61: mpz_trace (" a-c", diff);
! 62: mpz_trace (" 2^d", d2exp);
! 63: printf (" d=0x%lX\n", d);
! 64: abort ();
! 65: }
! 66:
! 67: MPZ_SRCPTR_SWAP (a, c);
! 68: }
! 69: }
! 70:
! 71:
! 72: void
! 73: check_data (void)
! 74: {
! 75: static const struct {
! 76: const char *a;
! 77: const char *c;
! 78: unsigned long d;
! 79: int want;
! 80:
! 81: } data[] = {
! 82:
! 83: /* anything is congruent mod 1 */
! 84: { "0", "0", 0, 1 },
! 85: { "1", "0", 0, 1 },
! 86: { "0", "1", 0, 1 },
! 87: { "123", "456", 0, 1 },
! 88: { "0x123456789123456789", "0x987654321987654321", 0, 1 },
! 89:
! 90: };
! 91:
! 92: mpz_t a, c;
! 93: int i;
! 94:
! 95: mpz_init (a);
! 96: mpz_init (c);
! 97:
! 98: for (i = 0; i < numberof (data); i++)
! 99: {
! 100: mpz_set_str_or_abort (a, data[i].a, 0);
! 101: mpz_set_str_or_abort (c, data[i].c, 0);
! 102: check_one (a, c, data[i].d, data[i].want);
! 103: }
! 104:
! 105: mpz_clear (a);
! 106: mpz_clear (c);
! 107: }
! 108:
! 109:
! 110: void
! 111: check_random (int argc, char *argv[])
! 112: {
! 113: gmp_randstate_ptr rands = RANDS;
! 114: unsigned long d;
! 115: mpz_t a, c, ra, rc;
! 116: int i;
! 117: int want;
! 118: int reps = 5000;
! 119:
! 120: if (argc >= 2)
! 121: reps = atoi (argv[1]);
! 122:
! 123: mpz_init (a);
! 124: mpz_init (c);
! 125: mpz_init (ra);
! 126: mpz_init (rc);
! 127:
! 128: for (i = 0; i < reps; i++)
! 129: {
! 130: mpz_errandomb (a, rands, 8*BITS_PER_MP_LIMB);
! 131: mpz_errandomb (c, rands, 8*BITS_PER_MP_LIMB);
! 132: d = urandom() % (8*BITS_PER_MP_LIMB);
! 133:
! 134: mpz_mul_2exp (a, a, urandom() % (2*BITS_PER_MP_LIMB));
! 135: mpz_mul_2exp (c, c, urandom() % (2*BITS_PER_MP_LIMB));
! 136:
! 137: mpz_negrandom (a, rands);
! 138: mpz_negrandom (c, rands);
! 139:
! 140: mpz_fdiv_r_2exp (ra, a, d);
! 141: mpz_fdiv_r_2exp (rc, c, d);
! 142:
! 143: want = (mpz_cmp (ra, rc) == 0);
! 144: check_one (a, c, d, want);
! 145:
! 146: mpz_sub (ra, ra, rc);
! 147: mpz_sub (a, a, ra);
! 148: check_one (a, c, d, 1);
! 149: }
! 150:
! 151: mpz_clear (a);
! 152: mpz_clear (c);
! 153: mpz_clear (ra);
! 154: mpz_clear (rc);
! 155: }
! 156:
! 157:
! 158: int
! 159: main (int argc, char *argv[])
! 160: {
! 161: tests_start ();
! 162:
! 163: check_data ();
! 164: check_random (argc, argv);
! 165:
! 166: tests_end ();
! 167: exit (0);
! 168: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>