1 /* Test mpz_root, mpz_rootrem, and mpz_perfect_power_p.
3 Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2009 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/. */
27 void debug_mp __GMP_PROTO ((mpz_t, int));
30 check_one (mpz_t root1, mpz_t x2, unsigned long nth, int i)
40 MPZ_CHECK_FORMAT (root1);
42 mpz_rootrem (root2, rem2, x2, nth);
43 MPZ_CHECK_FORMAT (root2);
44 MPZ_CHECK_FORMAT (rem2);
46 mpz_pow_ui (temp, root1, nth);
47 MPZ_CHECK_FORMAT (temp);
49 mpz_add (temp2, temp, rem2);
51 /* Is power of result > argument? */
52 if (mpz_cmp (root1, root2) != 0 || mpz_cmp (x2, temp2) != 0 || mpz_cmp (temp, x2) > 0)
54 fprintf (stderr, "ERROR after test %d\n", i);
58 fprintf (stderr, "nth: %lu\n", nth);
62 if (nth > 1 && mpz_cmp_ui (temp, 1L) > 0 && ! mpz_perfect_power_p (temp))
64 fprintf (stderr, "ERROR in mpz_perfect_power_p after test %d\n", i);
67 fprintf (stderr, "nth: %lu\n", nth);
71 if (nth <= 10000) /* skip too expensive test */
73 mpz_add_ui (temp2, root1, 1L);
74 mpz_pow_ui (temp2, temp2, nth);
75 MPZ_CHECK_FORMAT (temp2);
77 /* Is square of (result + 1) <= argument? */
78 if (mpz_cmp (temp2, x2) <= 0)
80 fprintf (stderr, "ERROR after test %d\n", i);
83 fprintf (stderr, "nth: %lu\n", nth);
95 main (int argc, char **argv)
103 gmp_randstate_ptr rands;
105 unsigned long bsi, size_range;
108 TESTS_REPS (reps, argv, argc);
117 /* This triggers a gcc 4.3.2 bug */
118 mpz_set_str (x2, "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000000000000002", 16);
119 mpz_root (root1, x2, 2);
120 check_one (root1, x2, 2, -1);
122 for (i = 0; i < reps; i++)
124 mpz_urandomb (bs, rands, 32);
125 size_range = mpz_get_ui (bs) % 17 + 2;
127 mpz_urandomb (bs, rands, size_range);
128 x2_size = mpz_get_ui (bs) + 10;
129 mpz_rrandomb (x2, rands, x2_size);
131 mpz_urandomb (bs, rands, 15);
132 nth = mpz_getlimbn (bs, 0) % mpz_sizeinbase (x2, 2) + 2;
134 mpz_root (root1, x2, nth);
136 mpz_urandomb (bs, rands, 4);
137 bsi = mpz_get_ui (bs);
140 /* With 50% probability, set x2 near a perfect power. */
141 mpz_pow_ui (x2, root1, nth);
144 mpz_sub_ui (x2, x2, bsi >> 2);
148 mpz_add_ui (x2, x2, bsi >> 2);
149 mpz_root (root1, x2, nth);
152 check_one (root1, x2, nth, i);
164 debug_mp (mpz_t x, int base)
166 mpz_out_str (stderr, base, x); fputc ('\n', stderr);