1 | /* Test mpz_add, mpz_add_ui, mpz_cmp, mpz_cmp, mpz_mul, mpz_sqrtrem. |
---|
2 | |
---|
3 | Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002 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 = 1000; |
---|
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, 15); |
---|
68 | nth = mpz_getlimbn (bs, 0) % mpz_sizeinbase (x2, 2) + 2; |
---|
69 | |
---|
70 | mpz_root (x, x2, nth); |
---|
71 | |
---|
72 | mpz_urandomb (bs, rands, 4); |
---|
73 | bsi = mpz_get_ui (bs); |
---|
74 | if ((bsi & 1) != 0) |
---|
75 | { |
---|
76 | /* With 50% probability, set x2 near a perfect power. */ |
---|
77 | mpz_pow_ui (x2, x, nth); |
---|
78 | if ((bsi & 2) != 0) |
---|
79 | { |
---|
80 | mpz_sub_ui (x2, x2, bsi >> 2); |
---|
81 | mpz_abs (x2, x2); |
---|
82 | } |
---|
83 | else |
---|
84 | mpz_add_ui (x2, x2, bsi >> 2); |
---|
85 | mpz_root (x, x2, nth); |
---|
86 | } |
---|
87 | |
---|
88 | /* printf ("%ld %lu\n", SIZ (x2), nth); */ |
---|
89 | |
---|
90 | mpz_pow_ui (temp, x, nth); |
---|
91 | |
---|
92 | /* Is power of result > argument? */ |
---|
93 | if (mpz_cmp (temp, x2) > 0) |
---|
94 | { |
---|
95 | fprintf (stderr, "ERROR after test %d\n", i); |
---|
96 | debug_mp (x2, 10); |
---|
97 | debug_mp (x, 10); |
---|
98 | fprintf (stderr, "nth: %lu\n", nth); |
---|
99 | abort (); |
---|
100 | } |
---|
101 | |
---|
102 | if (nth > 1 && mpz_cmp_ui (temp, 1L) > 0 && ! mpz_perfect_power_p (temp)) |
---|
103 | { |
---|
104 | fprintf (stderr, "ERROR in mpz_perfect_power_p after test %d\n", i); |
---|
105 | debug_mp (temp, 10); |
---|
106 | debug_mp (x, 10); |
---|
107 | fprintf (stderr, "nth: %lu\n", nth); |
---|
108 | abort (); |
---|
109 | } |
---|
110 | |
---|
111 | if (nth > 10000) |
---|
112 | continue; /* skip too expensive test */ |
---|
113 | |
---|
114 | mpz_add_ui (temp2, x, 1L); |
---|
115 | mpz_pow_ui (temp2, temp2, nth); |
---|
116 | |
---|
117 | /* Is square of (result + 1) <= argument? */ |
---|
118 | if (mpz_cmp (temp2, x2) <= 0) |
---|
119 | { |
---|
120 | fprintf (stderr, "ERROR after test %d\n", i); |
---|
121 | debug_mp (x2, 10); |
---|
122 | debug_mp (x, 10); |
---|
123 | fprintf (stderr, "nth: %lu\n", nth); |
---|
124 | abort (); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | mpz_clear (bs); |
---|
129 | mpz_clear (x2); |
---|
130 | mpz_clear (x); |
---|
131 | mpz_clear (temp); |
---|
132 | mpz_clear (temp2); |
---|
133 | |
---|
134 | tests_end (); |
---|
135 | exit (0); |
---|
136 | } |
---|
137 | |
---|
138 | void |
---|
139 | debug_mp (mpz_t x, int base) |
---|
140 | { |
---|
141 | mpz_out_str (stderr, base, x); fputc ('\n', stderr); |
---|
142 | } |
---|