Commit | Line | Data |
---|---|---|
e59cb199 HS |
1 | # -*- mode: perl; -*- |
2 | ||
3 | # Note that this does not test Math::BigRat upgrading. | |
4 | ||
5 | use strict; | |
6 | use warnings; | |
7 | ||
d18b40da | 8 | use Test::More tests => 141; |
e59cb199 HS |
9 | |
10 | use Math::BigInt upgrade => 'Math::BigRat'; | |
11 | use Math::BigRat downgrade => 'Math::BigInt'; | |
12 | ||
13 | is(Math::BigRat->downgrade(), 'Math::BigInt', 'Math::BigRat->downgrade()'); | |
14 | is(Math::BigInt->upgrade(), 'Math::BigRat', 'Math::BigInt->upgrade()'); | |
15 | ||
16 | ||
17 | ################################################################################ | |
18 | # Verify that constructors downgrade when they should. | |
19 | ||
20 | note("Enable downgrading, and see if constructors downgrade"); | |
21 | ||
22 | my $x; | |
23 | ||
24 | # new() | |
25 | ||
26 | $x = Math::BigRat -> new("0.5"); | |
27 | cmp_ok($x, "==", 0.5); | |
28 | is(ref $x, "Math::BigRat", "Creating a 0.5 does not downgrade"); | |
29 | ||
30 | $x = Math::BigRat -> new("4"); | |
31 | cmp_ok($x, "==", 4, 'new("4")'); | |
32 | is(ref $x, "Math::BigInt", "Creating a 4 downgrades to Math::BigInt"); | |
33 | ||
34 | $x = Math::BigRat -> new("0"); | |
35 | cmp_ok($x, "==", 0, 'new("0")'); | |
36 | is(ref $x, "Math::BigInt", "Creating a 0 downgrades to Math::BigInt"); | |
37 | ||
38 | $x = Math::BigRat -> new("1"); | |
39 | cmp_ok($x, "==", 1, 'new("1")'); | |
40 | is(ref $x, "Math::BigInt", "Creating a 1 downgrades to Math::BigInt"); | |
41 | ||
42 | $x = Math::BigRat -> new("Inf"); | |
43 | cmp_ok($x, "==", "Inf", 'new("inf")'); | |
44 | is(ref $x, "Math::BigInt", "Creating an Inf downgrades to Math::BigInt"); | |
45 | ||
46 | $x = Math::BigRat -> new("NaN"); | |
47 | is($x, "NaN", 'new("NaN")'); | |
48 | is(ref $x, "Math::BigInt", "Creating a NaN downgrades to Math::BigInt"); | |
49 | ||
50 | # bzero() | |
51 | ||
52 | $x = Math::BigRat -> bzero(); | |
53 | cmp_ok($x, "==", 0, "bzero()"); | |
54 | is(ref $x, "Math::BigInt", "Creating a 0 downgrades to Math::BigInt"); | |
55 | ||
56 | # bone() | |
57 | ||
58 | $x = Math::BigRat -> bone(); | |
59 | cmp_ok($x, "==", 1, "bone()"); | |
60 | is(ref $x, "Math::BigInt", "Creating a 1 downgrades to Math::BigInt"); | |
61 | ||
62 | # binf() | |
63 | ||
64 | $x = Math::BigRat -> binf(); | |
65 | cmp_ok($x, "==", "Inf", "binf()"); | |
66 | is(ref $x, "Math::BigInt", "Creating an Inf downgrades to Math::BigInt"); | |
67 | ||
68 | # bnan() | |
69 | ||
70 | $x = Math::BigRat -> bnan(); | |
71 | is($x, "NaN", "bnan()"); | |
72 | is(ref $x, "Math::BigInt", "Creating a NaN downgrades to Math::BigInt"); | |
73 | ||
74 | # from_hex() | |
75 | ||
76 | $x = Math::BigRat -> from_hex("13a"); | |
77 | cmp_ok($x, "==", 314, 'from_hex("13a")'); | |
78 | is(ref $x, "Math::BigInt", 'from_hex("13a") downgrades to Math::BigInt'); | |
79 | ||
80 | # from_oct() | |
81 | ||
82 | $x = Math::BigRat -> from_oct("472"); | |
83 | cmp_ok($x, "==", 314, 'from_oct("472")'); | |
84 | is(ref $x, "Math::BigInt", 'from_oct("472") downgrades to Math::BigInt'); | |
85 | ||
86 | # from_bin() | |
87 | ||
88 | $x = Math::BigRat -> from_bin("100111010"); | |
89 | cmp_ok($x, "==", 314, 'from_bin("100111010")'); | |
90 | is(ref $x, "Math::BigInt", | |
91 | 'from_bin("100111010") downgrades to Math::BigInt'); | |
92 | ||
93 | note("Disable downgrading, and see if constructors downgrade"); | |
94 | ||
95 | Math::BigRat -> downgrade(undef); | |
96 | ||
97 | my $half = Math::BigRat -> new("1/2"); | |
98 | my $four = Math::BigRat -> new("4"); | |
99 | my $zero = Math::BigRat -> bzero(); | |
100 | my $inf = Math::BigRat -> binf(); | |
101 | my $nan = Math::BigRat -> bnan(); | |
102 | ||
103 | is(ref $half, "Math::BigRat", "Creating a 0.5 does not downgrade"); | |
104 | is(ref $four, "Math::BigRat", "Creating a 4 does not downgrade"); | |
105 | is(ref $zero, "Math::BigRat", "Creating a 0 does not downgrade"); | |
106 | is(ref $inf, "Math::BigRat", "Creating an Inf does not downgrade"); | |
107 | is(ref $nan, "Math::BigRat", "Creating a NaN does not downgrade"); | |
d18b40da N |
108 | |
109 | ################################################################################ | |
110 | # Verify that other methods downgrade when they should. | |
111 | ||
112 | Math::BigRat -> downgrade("Math::BigInt"); | |
113 | ||
114 | note("bneg()"); | |
115 | ||
116 | $x = $zero -> copy() -> bneg(); | |
117 | cmp_ok($x, "==", 0, "-(0) = 0"); | |
118 | is(ref($x), "Math::BigInt", "-(0) => Math::BigInt"); | |
119 | ||
120 | $x = $four -> copy() -> bneg(); | |
121 | cmp_ok($x, "==", -4, "-(4) = -4"); | |
122 | is(ref($x), "Math::BigInt", "-(4) => Math::BigInt"); | |
123 | ||
124 | $x = $inf -> copy() -> bneg(); | |
125 | cmp_ok($x, "==", "-inf", "-(Inf) = -Inf"); | |
126 | is(ref($x), "Math::BigInt", "-(Inf) => Math::BigInt"); | |
127 | ||
128 | $x = $nan -> copy() -> bneg(); | |
129 | is($x, "NaN", "-(NaN) = NaN"); | |
130 | is(ref($x), "Math::BigInt", "-(NaN) => Math::BigInt"); | |
131 | ||
132 | note("bnorm()"); | |
133 | ||
134 | $x = $zero -> copy() -> bnorm(); | |
135 | cmp_ok($x, "==", 0, "bnorm(0)"); | |
136 | is(ref($x), "Math::BigInt", "bnorm(0) => Math::BigInt"); | |
137 | ||
138 | $x = $four -> copy() -> bnorm(); | |
139 | cmp_ok($x, "==", 4, "bnorm(4)"); | |
140 | is(ref($x), "Math::BigInt", "bnorm(4) => Math::BigInt"); | |
141 | ||
142 | $x = $inf -> copy() -> bnorm(); | |
143 | cmp_ok($x, "==", "inf", "bnorm(Inf)"); | |
144 | is(ref($x), "Math::BigInt", "bnorm(Inf) => Math::BigInt"); | |
145 | ||
146 | $x = $nan -> copy() -> bnorm(); | |
147 | is($x, "NaN", "bnorm(NaN)"); | |
148 | is(ref($x), "Math::BigInt", "bnorm(NaN) => Math::BigInt"); | |
149 | ||
150 | note("binc()"); | |
151 | ||
152 | $x = $zero -> copy() -> binc(); | |
153 | cmp_ok($x, "==", 1, "binc(0)"); | |
154 | is(ref($x), "Math::BigInt", "binc(0) => Math::BigInt"); | |
155 | ||
156 | $x = $four -> copy() -> binc(); | |
157 | cmp_ok($x, "==", 5, "binc(4)"); | |
158 | is(ref($x), "Math::BigInt", "binc(4) => Math::BigInt"); | |
159 | ||
160 | $x = $inf -> copy() -> binc(); | |
161 | cmp_ok($x, "==", "inf", "binc(Inf)"); | |
162 | is(ref($x), "Math::BigInt", "binc(Inf) => Math::BigInt"); | |
163 | ||
164 | $x = $nan -> copy() -> binc(); | |
165 | is($x, "NaN", "binc(NaN)"); | |
166 | is(ref($x), "Math::BigInt", "binc(NaN) => Math::BigInt"); | |
167 | ||
168 | note("bdec()"); | |
169 | ||
170 | $x = $zero -> copy() -> bdec(); | |
171 | cmp_ok($x, "==", -1, "bdec(0)"); | |
172 | is(ref($x), "Math::BigInt", "bdec(0) => Math::BigInt"); | |
173 | ||
174 | $x = $four -> copy() -> bdec(); | |
175 | cmp_ok($x, "==", 3, "bdec(4)"); | |
176 | is(ref($x), "Math::BigInt", "bdec(4) => Math::BigInt"); | |
177 | ||
178 | $x = $inf -> copy() -> bdec(); | |
179 | cmp_ok($x, "==", "inf", "bdec(Inf)"); | |
180 | is(ref($x), "Math::BigInt", "bdec(Inf) => Math::BigInt"); | |
181 | ||
182 | $x = $nan -> copy() -> bdec(); | |
183 | is($x, "NaN", "bdec(NaN)"); | |
184 | is(ref($x), "Math::BigInt", "bdec(NaN) => Math::BigInt"); | |
185 | ||
186 | note("badd()"); | |
187 | ||
188 | $x = $half -> copy() -> badd($nan); | |
189 | is($x, "NaN", "0.5 + NaN = NaN"); | |
190 | is(ref($x), "Math::BigInt", "0.5 + NaN => Math::BigInt"); | |
191 | ||
192 | $x = $half -> copy() -> badd($inf); | |
193 | cmp_ok($x, "==", "+Inf", "0.5 + Inf = Inf"); | |
194 | is(ref($x), "Math::BigInt", "2.5 + Inf => Math::BigInt"); | |
195 | ||
196 | $x = $half -> copy() -> badd($half); | |
197 | cmp_ok($x, "==", 1, "0.5 + 0.5 = 1"); | |
198 | is(ref($x), "Math::BigInt", "0.5 + 0.5 => Math::BigInt"); | |
199 | ||
200 | $x = $half -> copy() -> badd($half -> copy() -> bneg()); | |
201 | cmp_ok($x, "==", 0, "0.5 + -0.5 = 0"); | |
202 | is(ref($x), "Math::BigInt", "0.5 + -0.5 => Math::BigInt"); | |
203 | ||
204 | $x = $four -> copy() -> badd($zero); | |
205 | cmp_ok($x, "==", 4, "4 + 0 = 4"); | |
206 | is(ref($x), "Math::BigInt", "4 + 0 => Math::BigInt"); | |
207 | ||
208 | $x = $zero -> copy() -> badd($four); | |
209 | cmp_ok($x, "==", 4, "0 + 4 = 4"); | |
210 | is(ref($x), "Math::BigInt", "0 + 4 => Math::BigInt"); | |
211 | ||
212 | $x = $inf -> copy() -> badd($four); | |
213 | cmp_ok($x, "==", "+Inf", "Inf + 4 = Inf"); | |
214 | is(ref($x), "Math::BigInt", "Inf + 4 => Math::BigInt"); | |
215 | ||
216 | $x = $nan -> copy() -> badd($four); | |
217 | is($x, "NaN", "NaN + 4 = NaN"); | |
218 | is(ref($x), "Math::BigInt", "NaN + 4 => Math::BigInt"); | |
219 | ||
220 | note("bsub()"); | |
221 | ||
222 | $x = $half -> copy() -> bsub($nan); | |
223 | is($x, "NaN", "0.5 - NaN = NaN"); | |
224 | is(ref($x), "Math::BigInt", "0.5 - NaN => Math::BigInt"); | |
225 | ||
226 | $x = $half -> copy() -> bsub($inf); | |
227 | cmp_ok($x, "==", "-Inf", "2.5 - Inf = -Inf"); | |
228 | is(ref($x), "Math::BigInt", "2.5 - Inf => Math::BigInt"); | |
229 | ||
230 | $x = $half -> copy() -> bsub($half); | |
231 | cmp_ok($x, "==", 0, "0.5 - 0.5 = 0"); | |
232 | is(ref($x), "Math::BigInt", "0.5 - 0.5 => Math::BigInt"); | |
233 | ||
234 | $x = $half -> copy() -> bsub($half -> copy() -> bneg()); | |
235 | cmp_ok($x, "==", 1, "0.5 - -0.5 = 1"); | |
236 | is(ref($x), "Math::BigInt", "0.5 - -0.5 => Math::BigInt"); | |
237 | ||
238 | $x = $four -> copy() -> bsub($zero); | |
239 | cmp_ok($x, "==", 4, "4 - 0 = 4"); | |
240 | is(ref($x), "Math::BigInt", "4 - 0 => Math::BigInt"); | |
241 | ||
242 | $x = $zero -> copy() -> bsub($four); | |
243 | cmp_ok($x, "==", -4, "0 - 4 = -4"); | |
244 | is(ref($x), "Math::BigInt", "0 - 4 => Math::BigInt"); | |
245 | ||
246 | $x = $inf -> copy() -> bsub($four); | |
247 | cmp_ok($x, "==", "Inf", "Inf - 4 = Inf"); | |
248 | is(ref($x), "Math::BigInt", "Inf - 4 => Math::BigInt"); | |
249 | ||
250 | $x = $nan -> copy() -> bsub($four); | |
251 | is($x, "NaN", "NaN - 4 = NaN"); | |
252 | is(ref($x), "Math::BigInt", "NaN - 4 => Math::BigInt"); | |
253 | ||
254 | note("bmul()"); | |
255 | ||
256 | $x = $zero -> copy() -> bmul($four); | |
257 | cmp_ok($x, "==", 0, "bmul(0, 4) = 0"); | |
258 | is(ref($x), "Math::BigInt", "bmul(0, 4) => Math::BigInt"); | |
259 | ||
260 | $x = $four -> copy() -> bmul($four); | |
261 | cmp_ok($x, "==", 16, "bmul(4, 4) = 16"); | |
262 | is(ref($x), "Math::BigInt", "bmul(4, 4) => Math::BigInt"); | |
263 | ||
264 | $x = $inf -> copy() -> bmul($four); | |
265 | cmp_ok($x, "==", "inf", "bmul(Inf, 4) = Inf"); | |
266 | is(ref($x), "Math::BigInt", "bmul(Inf, 4) => Math::BigInt"); | |
267 | ||
268 | $x = $nan -> copy() -> bmul($four); | |
269 | is($x, "NaN", "bmul(NaN, 4) = NaN"); | |
270 | is(ref($x), "Math::BigInt", "bmul(NaN, 4) => Math::BigInt"); | |
271 | ||
272 | $x = $four -> copy() -> bmul("0.5"); | |
273 | cmp_ok($x, "==", 2, "bmul(4, 0.5) = 2"); | |
274 | is(ref($x), "Math::BigInt", "bmul(4, 0.5) => Math::BigInt"); | |
275 | ||
276 | # bmuladd() | |
277 | ||
278 | note("bdiv()"); | |
279 | ||
280 | note("bmod()"); | |
281 | ||
282 | note("bmodpow()"); | |
283 | ||
284 | note("bpow()"); | |
285 | ||
286 | note("blog()"); | |
287 | ||
288 | note("bexp()"); | |
289 | ||
290 | note("bnok()"); | |
291 | ||
292 | note("bsin()"); | |
293 | ||
294 | note("bcos()"); | |
295 | ||
296 | note("batan()"); | |
297 | ||
298 | note("batan()"); | |
299 | ||
300 | note("bsqrt()"); | |
301 | ||
302 | note("broot()"); | |
303 | ||
304 | note("bfac()"); | |
305 | ||
306 | note("bdfac()"); | |
307 | ||
308 | note("btfac()"); | |
309 | ||
310 | note("bmfac()"); | |
311 | ||
312 | note("blsft()"); | |
313 | ||
314 | note("brsft()"); | |
315 | ||
316 | note("band()"); | |
317 | ||
318 | note("bior()"); | |
319 | ||
320 | note("bxor()"); | |
321 | ||
322 | note("bnot()"); | |
323 | ||
324 | note("bround()"); | |
325 | ||
326 | # Add tests for rounding a non-integer to an integer. Fixme! | |
327 | ||
328 | $x = $zero -> copy() -> bround(); | |
329 | cmp_ok($x, "==", 0, "bround(0)"); | |
330 | is(ref($x), "Math::BigInt", "bround(0) => Math::BigInt"); | |
331 | ||
332 | $x = $four -> copy() -> bround(); | |
333 | cmp_ok($x, "==", 4, "bround(4)"); | |
334 | is(ref($x), "Math::BigInt", "bround(4) => Math::BigInt"); | |
335 | ||
336 | $x = $inf -> copy() -> bround(); | |
337 | cmp_ok($x, "==", "inf", "bround(Inf)"); | |
338 | is(ref($x), "Math::BigInt", "bround(Inf) => Math::BigInt"); | |
339 | ||
340 | $x = $nan -> copy() -> bround(); | |
341 | is($x, "NaN", "bround(NaN)"); | |
342 | is(ref($x), "Math::BigInt", "bround(NaN) => Math::BigInt"); | |
343 | ||
344 | note("bfround()"); | |
345 | ||
346 | # Add tests for rounding a non-integer to an integer. Fixme! | |
347 | ||
348 | $x = $zero -> copy() -> bfround(); | |
349 | cmp_ok($x, "==", 0, "bfround(0)"); | |
350 | is(ref($x), "Math::BigInt", "bfround(0) => Math::BigInt"); | |
351 | ||
352 | $x = $four -> copy() -> bfround(); | |
353 | cmp_ok($x, "==", 4, "bfround(4)"); | |
354 | is(ref($x), "Math::BigInt", "bfround(4) => Math::BigInt"); | |
355 | ||
356 | $x = $inf -> copy() -> bfround(); | |
357 | cmp_ok($x, "==", "inf", "bfround(Inf)"); | |
358 | is(ref($x), "Math::BigInt", "bfround(Inf) => Math::BigInt"); | |
359 | ||
360 | $x = $nan -> copy() -> bfround(); | |
361 | is($x, "NaN", "bfround(NaN)"); | |
362 | is(ref($x), "Math::BigInt", "bfround(NaN) => Math::BigInt"); | |
363 | ||
364 | note("bfloor()"); | |
365 | ||
366 | $x = $half -> copy() -> bfloor(); | |
367 | cmp_ok($x, "==", 0, "bfloor(0)"); | |
368 | is(ref($x), "Math::BigInt", "bfloor(0) => Math::BigInt"); | |
369 | ||
370 | $x = $inf -> copy() -> bfloor(); | |
371 | cmp_ok($x, "==", "Inf", "bfloor(Inf)"); | |
372 | is(ref($x), "Math::BigInt", "bfloor(Inf) => Math::BigInt"); | |
373 | ||
374 | $x = $nan -> copy() -> bfloor(); | |
375 | is($x, "NaN", "bfloor(NaN)"); | |
376 | is(ref($x), "Math::BigInt", "bfloor(NaN) => Math::BigInt"); | |
377 | ||
378 | note("bceil()"); | |
379 | ||
380 | $x = $half -> copy() -> bceil(); | |
381 | cmp_ok($x, "==", 1, "bceil(0)"); | |
382 | is(ref($x), "Math::BigInt", "bceil(0) => Math::BigInt"); | |
383 | ||
384 | $x = $inf -> copy() -> bceil(); | |
385 | cmp_ok($x, "==", "Inf", "bceil(Inf)"); | |
386 | is(ref($x), "Math::BigInt", "bceil(Inf) => Math::BigInt"); | |
387 | ||
388 | $x = $nan -> copy() -> bceil(); | |
389 | is($x, "NaN", "bceil(NaN)"); | |
390 | is(ref($x), "Math::BigInt", "bceil(NaN) => Math::BigInt"); | |
391 | ||
392 | note("bint()"); | |
393 | ||
394 | $x = $half -> copy() -> bint(); | |
395 | cmp_ok($x, "==", 0, "bint(0)"); | |
396 | is(ref($x), "Math::BigInt", "bint(0) => Math::BigInt"); | |
397 | ||
398 | $x = $inf -> copy() -> bint(); | |
399 | cmp_ok($x, "==", "Inf", "bint(Inf)"); | |
400 | is(ref($x), "Math::BigInt", "bint(Inf) => Math::BigInt"); | |
401 | ||
402 | $x = $nan -> copy() -> bint(); | |
403 | is($x, "NaN", "bint(NaN)"); | |
404 | is(ref($x), "Math::BigInt", "bint(NaN) => Math::BigInt"); | |
405 | ||
406 | note("bgcd()"); | |
407 | ||
408 | note("blcm()"); | |
409 | ||
410 | # mantissa() ? | |
411 | ||
412 | # exponent() ? | |
413 | ||
414 | # parts() ? | |
415 | ||
416 | # sparts() | |
417 | ||
418 | # nparts() | |
419 | ||
420 | # eparts() | |
421 | ||
422 | # dparts() | |
423 | ||
424 | # fparts() | |
425 | ||
426 | # numerator() | |
427 | ||
428 | # denominator() | |
429 | ||
430 | #require 'upgrade.inc'; # all tests here for sharing |