1 /* Test mpz_add, mpz_add_ui, mpz_cmp, mpz_cmp, mpz_mul, mpz_sqrtrem.
3 Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002 Free Software Foundation,
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 3 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. If not, see http://www.gnu.org/licenses/. */
28 void dump_abort __GMP_PROTO ((mpz_t, mpz_t, mpz_t));
29 void debug_mp __GMP_PROTO ((mpz_t, int));
32 main (int argc, char **argv)
40 gmp_randstate_ptr rands;
42 unsigned long size_range;
45 TESTS_REPS (reps, argv, argc);
57 for (i = 0; i < reps; i++)
59 mpz_urandomb (bs, rands, 32);
60 size_range = mpz_get_ui (bs) % 17 + 2; /* 0..262144 bit operands */
62 mpz_urandomb (bs, rands, size_range);
63 x2_size = mpz_get_ui (bs);
64 mpz_rrandomb (x2, rands, x2_size);
66 /* printf ("%ld\n", SIZ (x2)); */
68 mpz_sqrtrem (x, rem, x2);
70 MPZ_CHECK_FORMAT (rem);
74 /* Is square of result > argument? */
75 if (mpz_cmp (temp, x2) > 0)
76 dump_abort (x2, x, rem);
78 mpz_add_ui (temp2, x, 1);
79 mpz_mul (temp2, temp2, temp2);
81 /* Is square of (result + 1) <= argument? */
82 if (mpz_cmp (temp2, x2) <= 0)
83 dump_abort (x2, x, rem);
85 mpz_add (temp2, temp, rem);
87 /* Is the remainder wrong? */
88 if (mpz_cmp (x2, temp2) != 0)
89 dump_abort (x2, x, rem);
104 dump_abort (mpz_t x2, mpz_t x, mpz_t rem)
106 fprintf (stderr, "ERROR\n");
107 fprintf (stderr, "x2 = "); debug_mp (x2, -16);
108 fprintf (stderr, "x = "); debug_mp (x, -16);
109 fprintf (stderr, "remainder = "); debug_mp (rem, -16);
114 debug_mp (mpz_t x, int base)
116 mpz_out_str (stderr, base, x); fputc ('\n', stderr);