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