1
/* test mpz_congruent_p and mpz_congruent_ui_p */
4
Copyright 2001 Free Software Foundation, Inc.
6
This file is part of the GNU MP Library.
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.
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.
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,
32
check_one (mpz_srcptr a, mpz_srcptr c, mpz_srcptr d, int want)
37
for (swap = 0; swap <= 1; swap++)
39
got = (mpz_congruent_p (a, c, d) != 0);
42
printf ("mpz_congruent_p wrong\n");
43
printf (" expected %d got %d\n", want, got);
54
if (mpz_fits_ulong_p (c) && mpz_fits_ulong_p (d))
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);
61
printf ("mpz_congruent_ui_p wrong\n");
62
printf (" expected %d got %d\n", want, got);
64
printf (" c=%lu\n", uc);
65
printf (" d=%lu\n", ud);
68
printf (" c=0x%lX\n", uc);
69
printf (" d=0x%lX\n", ud);
74
MPZ_SRCPTR_SWAP (a, c);
90
/* anything congruent mod 1 */
94
{ "123", "456", "1", 1 },
95
{ "0x123456789123456789", "0x987654321987654321", "1", 1 },
97
/* csize==1, dsize==2 changing to 1 after stripping 2s */
98
{ "0x3333333333333333", "0x33333333",
100
{ "0x33333333333333333333333333333333", "0x3333333333333333",
101
"0x18000000000000000", 1 },
103
/* another dsize==2 becoming 1, with opposite signs this time */
107
{ "0x44444444444444441",
108
"-0x2222222222222221F",
109
"0x33333333333333330", 1 },
119
for (i = 0; i < numberof (data); i++)
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);
134
check_random (int argc, char *argv[])
136
gmp_randstate_ptr rands = RANDS;
137
mpz_t a, c, d, ra, rc;
143
reps = atoi (argv[1]);
151
for (i = 0; i < reps; i++)
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);
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);
165
mpz_fdiv_r (ra, a, d);
166
mpz_fdiv_r (rc, c, d);
168
want = (mpz_cmp (ra, rc) == 0);
169
check_one (a, c, d, want);
171
mpz_sub (ra, ra, rc);
173
MPZ_CHECK_FORMAT (a);
174
check_one (a, c, d, 1);
176
if (! mpz_pow2abs_p (d))
178
mpz_flipbit (a, urandom() % (8*BITS_PER_MP_LIMB));
179
check_one (a, c, d, 0);
192
main (int argc, char *argv[])
197
check_random (argc, argv);