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

Annotation of OpenXM_contrib/gmp/tests/mpz/t-cong.c, Revision 1.1

1.1     ! ohara       1: /* test mpz_congruent_p and mpz_congruent_ui_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, mpz_srcptr d, int want)
        !            33: {
        !            34:   int   got;
        !            35:   int   swap;
        !            36:
        !            37:   for (swap = 0; swap <= 1; swap++)
        !            38:     {
        !            39:       got = (mpz_congruent_p (a, c, d) != 0);
        !            40:       if (want != got)
        !            41:         {
        !            42:           printf ("mpz_congruent_p wrong\n");
        !            43:           printf ("   expected %d got %d\n", want, got);
        !            44:           mpz_trace ("   a", a);
        !            45:           mpz_trace ("   c", c);
        !            46:           mpz_trace ("   d", d);
        !            47:           mp_trace_base = -16;
        !            48:           mpz_trace ("   a", a);
        !            49:           mpz_trace ("   c", c);
        !            50:           mpz_trace ("   d", d);
        !            51:           abort ();
        !            52:         }
        !            53:
        !            54:       if (mpz_fits_ulong_p (c) && mpz_fits_ulong_p (d))
        !            55:         {
        !            56:           unsigned long  uc = mpz_get_ui (c);
        !            57:           unsigned long  ud = mpz_get_ui (d);
        !            58:           got = (mpz_congruent_ui_p (a, uc, ud) != 0);
        !            59:           if (want != got)
        !            60:             {
        !            61:               printf    ("mpz_congruent_ui_p wrong\n");
        !            62:               printf    ("   expected %d got %d\n", want, got);
        !            63:               mpz_trace ("   a", a);
        !            64:               printf    ("   c=%lu\n", uc);
        !            65:               printf    ("   d=%lu\n", ud);
        !            66:               mp_trace_base = -16;
        !            67:               mpz_trace ("   a", a);
        !            68:               printf    ("   c=0x%lX\n", uc);
        !            69:               printf    ("   d=0x%lX\n", ud);
        !            70:               abort ();
        !            71:             }
        !            72:         }
        !            73:
        !            74:       MPZ_SRCPTR_SWAP (a, c);
        !            75:     }
        !            76: }
        !            77:
        !            78:
        !            79: void
        !            80: check_data (void)
        !            81: {
        !            82:   static const struct {
        !            83:     const char *a;
        !            84:     const char *c;
        !            85:     const char *d;
        !            86:     int        want;
        !            87:
        !            88:   } data[] = {
        !            89:
        !            90:     /* anything congruent mod 1 */
        !            91:     { "0", "0", "1", 1 },
        !            92:     { "1", "0", "1", 1 },
        !            93:     { "0", "1", "1", 1 },
        !            94:     { "123", "456", "1", 1 },
        !            95:     { "0x123456789123456789", "0x987654321987654321", "1", 1 },
        !            96:
        !            97:     /* csize==1, dsize==2 changing to 1 after stripping 2s */
        !            98:     { "0x3333333333333333",  "0x33333333",
        !            99:       "0x180000000", 1 },
        !           100:     { "0x33333333333333333333333333333333", "0x3333333333333333",
        !           101:       "0x18000000000000000", 1 },
        !           102:
        !           103:     /* another dsize==2 becoming 1, with opposite signs this time */
        !           104:     {  "0x444444441",
        !           105:       "-0x22222221F",
        !           106:        "0x333333330", 1 },
        !           107:     {  "0x44444444444444441",
        !           108:       "-0x2222222222222221F",
        !           109:        "0x33333333333333330", 1 },
        !           110:   };
        !           111:
        !           112:   mpz_t   a, c, d;
        !           113:   int     i;
        !           114:
        !           115:   mpz_init (a);
        !           116:   mpz_init (c);
        !           117:   mpz_init (d);
        !           118:
        !           119:   for (i = 0; i < numberof (data); i++)
        !           120:     {
        !           121:       mpz_set_str_or_abort (a, data[i].a, 0);
        !           122:       mpz_set_str_or_abort (c, data[i].c, 0);
        !           123:       mpz_set_str_or_abort (d, data[i].d, 0);
        !           124:       check_one (a, c, d, data[i].want);
        !           125:     }
        !           126:
        !           127:   mpz_clear (a);
        !           128:   mpz_clear (c);
        !           129:   mpz_clear (d);
        !           130: }
        !           131:
        !           132:
        !           133: void
        !           134: check_random (int argc, char *argv[])
        !           135: {
        !           136:   gmp_randstate_ptr rands = RANDS;
        !           137:   mpz_t   a, c, d, ra, rc;
        !           138:   int     i;
        !           139:   int     want;
        !           140:   int     reps = 2000;
        !           141:
        !           142:   if (argc >= 2)
        !           143:     reps = atoi (argv[1]);
        !           144:
        !           145:   mpz_init (a);
        !           146:   mpz_init (c);
        !           147:   mpz_init (d);
        !           148:   mpz_init (ra);
        !           149:   mpz_init (rc);
        !           150:
        !           151:   for (i = 0; i < reps; i++)
        !           152:     {
        !           153:       mpz_errandomb (a, rands, 8*BITS_PER_MP_LIMB);
        !           154:       MPZ_CHECK_FORMAT (a);
        !           155:       mpz_errandomb (c, rands, 8*BITS_PER_MP_LIMB);
        !           156:       MPZ_CHECK_FORMAT (c);
        !           157:       mpz_errandomb_nonzero (d, rands, 8*BITS_PER_MP_LIMB);
        !           158:
        !           159:       mpz_negrandom (a, rands);
        !           160:       MPZ_CHECK_FORMAT (a);
        !           161:       mpz_negrandom (c, rands);
        !           162:       MPZ_CHECK_FORMAT (c);
        !           163:       mpz_negrandom (d, rands);
        !           164:
        !           165:       mpz_fdiv_r (ra, a, d);
        !           166:       mpz_fdiv_r (rc, c, d);
        !           167:
        !           168:       want = (mpz_cmp (ra, rc) == 0);
        !           169:       check_one (a, c, d, want);
        !           170:
        !           171:       mpz_sub (ra, ra, rc);
        !           172:       mpz_sub (a, a, ra);
        !           173:       MPZ_CHECK_FORMAT (a);
        !           174:       check_one (a, c, d, 1);
        !           175:
        !           176:       if (! mpz_pow2abs_p (d))
        !           177:         {
        !           178:           mpz_flipbit (a, urandom() % (8*BITS_PER_MP_LIMB));
        !           179:           check_one (a, c, d, 0);
        !           180:         }
        !           181:     }
        !           182:
        !           183:   mpz_clear (a);
        !           184:   mpz_clear (c);
        !           185:   mpz_clear (d);
        !           186:   mpz_clear (ra);
        !           187:   mpz_clear (rc);
        !           188: }
        !           189:
        !           190:
        !           191: int
        !           192: main (int argc, char *argv[])
        !           193: {
        !           194:   tests_start ();
        !           195:
        !           196:   check_data ();
        !           197:   check_random (argc, argv);
        !           198:
        !           199:   tests_end ();
        !           200:   exit (0);
        !           201: }

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