1 | /* Test mpz_lcm and mpz_lcm_ui. |
---|
2 | |
---|
3 | Copyright 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 | |
---|
23 | |
---|
24 | #include <stdio.h> |
---|
25 | #include <stdlib.h> |
---|
26 | #include <string.h> |
---|
27 | |
---|
28 | #include "gmp.h" |
---|
29 | #include "gmp-impl.h" |
---|
30 | #include "tests.h" |
---|
31 | |
---|
32 | |
---|
33 | void |
---|
34 | check_all (mpz_ptr want, mpz_srcptr x_orig, mpz_srcptr y_orig) |
---|
35 | { |
---|
36 | mpz_t got, x, y; |
---|
37 | int negx, negy, swap, inplace; |
---|
38 | |
---|
39 | mpz_init (got); |
---|
40 | mpz_init_set (x, x_orig); |
---|
41 | mpz_init_set (y, y_orig); |
---|
42 | |
---|
43 | for (swap = 0; swap < 2; swap++) |
---|
44 | { |
---|
45 | mpz_swap (x, y); |
---|
46 | |
---|
47 | for (negx = 0; negx < 2; negx++) |
---|
48 | { |
---|
49 | mpz_neg (x, x); |
---|
50 | |
---|
51 | for (negy = 0; negy < 2; negy++) |
---|
52 | { |
---|
53 | mpz_neg (y, y); |
---|
54 | |
---|
55 | for (inplace = 0; inplace <= 1; inplace++) |
---|
56 | { |
---|
57 | if (inplace) |
---|
58 | { mpz_set (got, x); mpz_lcm (got, got, y); } |
---|
59 | else |
---|
60 | mpz_lcm (got, x, y); |
---|
61 | MPZ_CHECK_FORMAT (got); |
---|
62 | |
---|
63 | if (mpz_cmp (got, want) != 0) |
---|
64 | { |
---|
65 | printf ("mpz_lcm wrong, inplace=%d\n", inplace); |
---|
66 | fail: |
---|
67 | mpz_trace ("x", x); |
---|
68 | mpz_trace ("y", y); |
---|
69 | mpz_trace ("got", got); |
---|
70 | mpz_trace ("want", want); |
---|
71 | abort (); |
---|
72 | } |
---|
73 | |
---|
74 | if (mpz_fits_ulong_p (y)) |
---|
75 | { |
---|
76 | unsigned long yu = mpz_get_ui (y); |
---|
77 | if (inplace) |
---|
78 | { mpz_set (got, x); mpz_lcm_ui (got, got, yu); } |
---|
79 | else |
---|
80 | mpz_lcm_ui (got, x, yu); |
---|
81 | |
---|
82 | if (mpz_cmp (got, want) != 0) |
---|
83 | { |
---|
84 | printf ("mpz_lcm_ui wrong, inplace=%d\n", inplace); |
---|
85 | printf ("yu=%lu\n", yu); |
---|
86 | goto fail; |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | mpz_clear (got); |
---|
95 | mpz_clear (x); |
---|
96 | mpz_clear (y); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | void |
---|
101 | check_primes (void) |
---|
102 | { |
---|
103 | static unsigned long prime[] = { |
---|
104 | 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97, |
---|
105 | 101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181, |
---|
106 | 191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277, |
---|
107 | 281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383, |
---|
108 | 389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487, |
---|
109 | }; |
---|
110 | mpz_t want, x, y; |
---|
111 | int i; |
---|
112 | |
---|
113 | mpz_init (want); |
---|
114 | mpz_init (x); |
---|
115 | mpz_init (y); |
---|
116 | |
---|
117 | /* New prime each time. */ |
---|
118 | mpz_set_ui (want, 1L); |
---|
119 | for (i = 0; i < numberof (prime); i++) |
---|
120 | { |
---|
121 | mpz_set (x, want); |
---|
122 | mpz_set_ui (y, prime[i]); |
---|
123 | mpz_mul_ui (want, want, prime[i]); |
---|
124 | check_all (want, x, y); |
---|
125 | } |
---|
126 | |
---|
127 | /* Old prime each time. */ |
---|
128 | mpz_set (x, want); |
---|
129 | for (i = 0; i < numberof (prime); i++) |
---|
130 | { |
---|
131 | mpz_set_ui (y, prime[i]); |
---|
132 | check_all (want, x, y); |
---|
133 | } |
---|
134 | |
---|
135 | /* One old, one new each time. */ |
---|
136 | mpz_set_ui (want, prime[0]); |
---|
137 | for (i = 1; i < numberof (prime); i++) |
---|
138 | { |
---|
139 | mpz_set (x, want); |
---|
140 | mpz_set_ui (y, prime[i] * prime[i-1]); |
---|
141 | mpz_mul_ui (want, want, prime[i]); |
---|
142 | check_all (want, x, y); |
---|
143 | } |
---|
144 | |
---|
145 | /* Triplets with A,B in x and B,C in y. */ |
---|
146 | mpz_set_ui (want, 1L); |
---|
147 | mpz_set_ui (x, 1L); |
---|
148 | mpz_set_ui (y, 1L); |
---|
149 | for (i = 0; i+2 < numberof (prime); i += 3) |
---|
150 | { |
---|
151 | mpz_mul_ui (want, want, prime[i]); |
---|
152 | mpz_mul_ui (want, want, prime[i+1]); |
---|
153 | mpz_mul_ui (want, want, prime[i+2]); |
---|
154 | |
---|
155 | mpz_mul_ui (x, x, prime[i]); |
---|
156 | mpz_mul_ui (x, x, prime[i+1]); |
---|
157 | |
---|
158 | mpz_mul_ui (y, y, prime[i+1]); |
---|
159 | mpz_mul_ui (y, y, prime[i+2]); |
---|
160 | |
---|
161 | check_all (want, x, y); |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | mpz_clear (want); |
---|
166 | mpz_clear (x); |
---|
167 | mpz_clear (y); |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | int |
---|
173 | main (int argc, char *argv[]) |
---|
174 | { |
---|
175 | tests_start (); |
---|
176 | |
---|
177 | check_primes (); |
---|
178 | |
---|
179 | tests_end (); |
---|
180 | exit (0); |
---|
181 | } |
---|