1 /* test mpz_congruent_p and mpz_congruent_ui_p
3 Copyright 2001, 2002 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
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 3 of the License, or (at your
10 option) any later version.
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.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
28 check_one (mpz_srcptr a, mpz_srcptr c, mpz_srcptr d, int want)
33 for (swap = 0; swap <= 1; swap++)
35 got = (mpz_congruent_p (a, c, d) != 0);
38 printf ("mpz_congruent_p wrong\n");
39 printf (" expected %d got %d\n", want, got);
50 if (mpz_fits_ulong_p (c) && mpz_fits_ulong_p (d))
52 unsigned long uc = mpz_get_ui (c);
53 unsigned long ud = mpz_get_ui (d);
54 got = (mpz_congruent_ui_p (a, uc, ud) != 0);
57 printf ("mpz_congruent_ui_p wrong\n");
58 printf (" expected %d got %d\n", want, got);
60 printf (" c=%lu\n", uc);
61 printf (" d=%lu\n", ud);
64 printf (" c=0x%lX\n", uc);
65 printf (" d=0x%lX\n", ud);
70 MPZ_SRCPTR_SWAP (a, c);
86 /* anything congruent mod 1 */
90 { "123", "456", "1", 1 },
91 { "0x123456789123456789", "0x987654321987654321", "1", 1 },
93 /* csize==1, dsize==2 changing to 1 after stripping 2s */
94 { "0x3333333333333333", "0x33333333",
96 { "0x33333333333333333333333333333333", "0x3333333333333333",
97 "0x18000000000000000", 1 },
99 /* another dsize==2 becoming 1, with opposite signs this time */
103 { "0x44444444444444441",
104 "-0x2222222222222221F",
105 "0x33333333333333330", 1 },
115 for (i = 0; i < numberof (data); i++)
117 mpz_set_str_or_abort (a, data[i].a, 0);
118 mpz_set_str_or_abort (c, data[i].c, 0);
119 mpz_set_str_or_abort (d, data[i].d, 0);
120 check_one (a, c, d, data[i].want);
130 check_random (int argc, char *argv[])
132 gmp_randstate_ptr rands = RANDS;
133 mpz_t a, c, d, ra, rc;
139 reps = atoi (argv[1]);
147 for (i = 0; i < reps; i++)
149 mpz_errandomb (a, rands, 8*GMP_LIMB_BITS);
150 MPZ_CHECK_FORMAT (a);
151 mpz_errandomb (c, rands, 8*GMP_LIMB_BITS);
152 MPZ_CHECK_FORMAT (c);
153 mpz_errandomb_nonzero (d, rands, 8*GMP_LIMB_BITS);
155 mpz_negrandom (a, rands);
156 MPZ_CHECK_FORMAT (a);
157 mpz_negrandom (c, rands);
158 MPZ_CHECK_FORMAT (c);
159 mpz_negrandom (d, rands);
161 mpz_fdiv_r (ra, a, d);
162 mpz_fdiv_r (rc, c, d);
164 want = (mpz_cmp (ra, rc) == 0);
165 check_one (a, c, d, want);
167 mpz_sub (ra, ra, rc);
169 MPZ_CHECK_FORMAT (a);
170 check_one (a, c, d, 1);
172 if (! mpz_pow2abs_p (d))
174 refmpz_combit (a, urandom() % (8*GMP_LIMB_BITS));
175 check_one (a, c, d, 0);
188 main (int argc, char *argv[])
193 check_random (argc, argv);