1 | /* Test mpz_add, mpz_add_ui, mpz_cmp, mpz_cmp, mpz_mul, mpz_sqrtrem. |
---|
2 | |
---|
3 | Copyright 1991, 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc. |
---|
4 | |
---|
5 | This file is part of the GNU MP Library. |
---|
6 | |
---|
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 2.1 of the License, or (at your |
---|
10 | option) any later version. |
---|
11 | |
---|
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. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Lesser General Public License |
---|
18 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
20 | MA 02111-1307, USA. */ |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <stdlib.h> |
---|
24 | |
---|
25 | #include "gmp.h" |
---|
26 | #include "gmp-impl.h" |
---|
27 | #include "tests.h" |
---|
28 | |
---|
29 | void debug_mp _PROTO ((mpz_t, int)); |
---|
30 | |
---|
31 | int |
---|
32 | main (int argc, char **argv) |
---|
33 | { |
---|
34 | mpz_t x2; |
---|
35 | mpz_t x; |
---|
36 | mpz_t temp, temp2; |
---|
37 | mp_size_t x2_size; |
---|
38 | int i; |
---|
39 | int reps = 5000; |
---|
40 | unsigned long nth; |
---|
41 | gmp_randstate_ptr rands; |
---|
42 | mpz_t bs; |
---|
43 | unsigned long bsi, size_range; |
---|
44 | |
---|
45 | tests_start (); |
---|
46 | rands = RANDS; |
---|
47 | |
---|
48 | mpz_init (bs); |
---|
49 | |
---|
50 | if (argc == 2) |
---|
51 | reps = atoi (argv[1]); |
---|
52 | |
---|
53 | mpz_init (x2); |
---|
54 | mpz_init (x); |
---|
55 | mpz_init (temp); |
---|
56 | mpz_init (temp2); |
---|
57 | |
---|
58 | for (i = 0; i < reps; i++) |
---|
59 | { |
---|
60 | mpz_urandomb (bs, rands, 32); |
---|
61 | size_range = mpz_get_ui (bs) % 12 + 2; |
---|
62 | |
---|
63 | mpz_urandomb (bs, rands, size_range); |
---|
64 | x2_size = mpz_get_ui (bs) + 10; |
---|
65 | mpz_rrandomb (x2, rands, x2_size); |
---|
66 | |
---|
67 | mpz_urandomb (bs, rands, 5L); |
---|
68 | nth = mpz_getlimbn (bs, 0) % mpz_sizeinbase (x2, 2) + 1; |
---|
69 | |
---|
70 | mpz_urandomb (bs, rands, 2); |
---|
71 | bsi = mpz_get_ui (bs); |
---|
72 | if ((bsi & 1) != 0) |
---|
73 | { |
---|
74 | /* With 50% probability, set x2 just below a perfect power. */ |
---|
75 | mpz_root (x, x2, nth); |
---|
76 | mpz_pow_ui (x2, x, nth); |
---|
77 | if (mpz_sgn (x2) != 0) |
---|
78 | mpz_sub_ui (x2, x2, 1L); |
---|
79 | } |
---|
80 | |
---|
81 | /* printf ("%ld %lu\n", SIZ (x2), nth); */ |
---|
82 | |
---|
83 | mpz_root (x, x2, nth); |
---|
84 | mpz_pow_ui (temp, x, nth); |
---|
85 | |
---|
86 | /* Is power of result > argument? */ |
---|
87 | if (mpz_cmp (temp, x2) > 0) |
---|
88 | { |
---|
89 | fprintf (stderr, "ERROR after test %d\n", i); |
---|
90 | debug_mp (x2, 10); |
---|
91 | debug_mp (x, 10); |
---|
92 | fprintf (stderr, "nth: %lu\n", nth); |
---|
93 | abort (); |
---|
94 | } |
---|
95 | |
---|
96 | if (nth > 1 && mpz_cmp_ui (temp, 1L) > 0 && ! mpz_perfect_power_p (temp)) |
---|
97 | { |
---|
98 | fprintf (stderr, "ERROR in mpz_perfect_power_p after test %d\n", i); |
---|
99 | debug_mp (temp, 10); |
---|
100 | debug_mp (x, 10); |
---|
101 | fprintf (stderr, "nth: %lu\n", nth); |
---|
102 | abort (); |
---|
103 | } |
---|
104 | |
---|
105 | if (nth > 10000) |
---|
106 | continue; /* skip too expensive test */ |
---|
107 | |
---|
108 | mpz_add_ui (temp2, x, 1L); |
---|
109 | mpz_pow_ui (temp2, temp2, nth); |
---|
110 | |
---|
111 | /* Is square of (result + 1) <= argument? */ |
---|
112 | if (mpz_cmp (temp2, x2) <= 0) |
---|
113 | { |
---|
114 | fprintf (stderr, "ERROR after test %d\n", i); |
---|
115 | debug_mp (x2, 10); |
---|
116 | debug_mp (x, 10); |
---|
117 | fprintf (stderr, "nth: %lu\n", nth); |
---|
118 | abort (); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | mpz_clear (bs); |
---|
123 | mpz_clear (x2); |
---|
124 | mpz_clear (x); |
---|
125 | mpz_clear (temp); |
---|
126 | mpz_clear (temp2); |
---|
127 | |
---|
128 | tests_end (); |
---|
129 | exit (0); |
---|
130 | } |
---|
131 | |
---|
132 | void |
---|
133 | debug_mp (mpz_t x, int base) |
---|
134 | { |
---|
135 | mpz_out_str (stderr, base, x); fputc ('\n', stderr); |
---|
136 | } |
---|