Upgrade to Math::BigInt 1.41.
[p5sagit/p5-mst-13.2.git] / lib / Math / BigInt / t / bigintpm.t
CommitLineData
58cde26e 1#!/usr/bin/perl -w
748a9306 2
58cde26e 3use strict;
4use Test;
5
6BEGIN
7 {
8 $| = 1;
9 # chdir 't' if -d 't';
10 unshift @INC, '../lib'; # for running manually
574bacfe 11 plan tests => 1424;
58cde26e 12 }
574bacfe 13my $version = '1.40'; # for $VERSION tests, match current release (by hand!)
58cde26e 14
15##############################################################################
16# for testing inheritance of _swap
17
18package Math::Foo;
19
20use Math::BigInt;
574bacfe 21#use Math::BigInt lib => 'BitVect'; # for testing
58cde26e 22use vars qw/@ISA/;
23@ISA = (qw/Math::BigInt/);
24
25use overload
26# customized overload for sub, since original does not use swap there
27'-' => sub { my @a = ref($_[0])->_swap(@_);
28 $a[0]->bsub($a[1])};
29
30sub _swap
31 {
32 # a fake _swap, which reverses the params
33 my $self = shift; # for override in subclass
34 if ($_[2])
35 {
36 my $c = ref ($_[0] ) || 'Math::Foo';
37 return ( $_[0]->copy(), $_[1] );
38 }
39 else
40 {
41 return ( Math::Foo->new($_[1]), $_[0] );
42 }
43 }
44
45##############################################################################
46package main;
d1f8c7a4 47
748a9306 48use Math::BigInt;
0716bf9b 49#use Math::BigInt lib => 'BitVect'; # for testing
0716bf9b 50
574bacfe 51my $CALC = Math::BigInt::_core_lib(); ok ($CALC,'Math::BigInt::Calc');
748a9306 52
58cde26e 53my (@args,$f,$try,$x,$y,$z,$a,$exp,$ans,$ans1,@a,$m,$e,$round_mode);
54
55while (<DATA>)
56 {
57 chop;
58 next if /^#/; # skip comments
59 if (s/^&//)
60 {
61 $f = $_;
62 }
63 elsif (/^\$/)
64 {
65 $round_mode = $_;
66 $round_mode =~ s/^\$/Math::BigInt->/;
67 # print "$round_mode\n";
68 }
69 else
70 {
71 @args = split(/:/,$_,99);
72 $ans = pop(@args);
73 $try = "\$x = Math::BigInt->new(\"$args[0]\");";
74 if ($f eq "bnorm"){
75 # $try .= '$x+0;';
58cde26e 76 } elsif ($f eq "is_zero") {
77 $try .= '$x->is_zero()+0;';
78 } elsif ($f eq "is_one") {
79 $try .= '$x->is_one()+0;';
80 } elsif ($f eq "is_odd") {
81 $try .= '$x->is_odd()+0;';
82 } elsif ($f eq "is_even") {
83 $try .= '$x->is_even()+0;';
574bacfe 84 } elsif ($f eq "is_negative") {
85 $try .= '$x->is_negative()+0;';
86 } elsif ($f eq "is_positive") {
87 $try .= '$x->is_positive()+0;';
0716bf9b 88 } elsif ($f eq "is_inf") {
89 $try .= "\$x->is_inf('$args[1]')+0;";
58cde26e 90 } elsif ($f eq "binf") {
91 $try .= "\$x->binf('$args[1]');";
574bacfe 92 } elsif ($f eq "bone") {
93 $try .= "\$x->bone('$args[1]');";
94 } elsif ($f eq "bnan") {
95 $try .= "\$x->bnan();";
58cde26e 96 } elsif ($f eq "bfloor") {
97 $try .= '$x->bfloor();';
98 } elsif ($f eq "bceil") {
99 $try .= '$x->bceil();';
58cde26e 100 } elsif ($f eq "bsstr") {
101 $try .= '$x->bsstr();';
102 } elsif ($f eq "bneg") {
574bacfe 103 $try .= '$x->bneg();';
58cde26e 104 } elsif ($f eq "babs") {
574bacfe 105 $try .= '$x->babs();';
58cde26e 106 } elsif ($f eq "binc") {
107 $try .= '++$x;';
108 } elsif ($f eq "bdec") {
109 $try .= '--$x;';
110 }elsif ($f eq "bnot") {
111 $try .= '~$x;';
112 }elsif ($f eq "bsqrt") {
113 $try .= '$x->bsqrt();';
114 }elsif ($f eq "length") {
115 $try .= "\$x->length();";
58cde26e 116 }elsif ($f eq "exponent"){
117 $try .= '$x = $x->exponent()->bstr();';
118 }elsif ($f eq "mantissa"){
119 $try .= '$x = $x->mantissa()->bstr();';
120 }elsif ($f eq "parts"){
121 $try .= "(\$m,\$e) = \$x->parts();";
122 $try .= '$m = $m->bstr(); $m = "NaN" if !defined $m;';
123 $try .= '$e = $e->bstr(); $e = "NaN" if !defined $e;';
124 $try .= '"$m,$e";';
125 } else {
0716bf9b 126 $try .= "\$y = new Math::BigInt ('$args[1]');";
58cde26e 127 if ($f eq "bcmp"){
128 $try .= '$x <=> $y;';
0716bf9b 129 }elsif ($f eq "bround") {
130 $try .= "$round_mode; \$x->bround(\$y);";
58cde26e 131 }elsif ($f eq "bacmp"){
0716bf9b 132 $try .= "\$x->bacmp(\$y);";
58cde26e 133 }elsif ($f eq "badd"){
134 $try .= "\$x + \$y;";
135 }elsif ($f eq "bsub"){
136 $try .= "\$x - \$y;";
137 }elsif ($f eq "bmul"){
138 $try .= "\$x * \$y;";
139 }elsif ($f eq "bdiv"){
140 $try .= "\$x / \$y;";
574bacfe 141 }elsif ($f eq "bdiv-list"){
142 $try .= 'join (",",$x->bdiv($y));';
58cde26e 143 }elsif ($f eq "bmod"){
144 $try .= "\$x % \$y;";
145 }elsif ($f eq "bgcd")
146 {
147 if (defined $args[2])
148 {
149 $try .= " \$z = new Math::BigInt \"$args[2]\"; ";
150 }
151 $try .= "Math::BigInt::bgcd(\$x, \$y";
152 $try .= ", \$z" if (defined $args[2]);
153 $try .= " );";
154 }
155 elsif ($f eq "blcm")
156 {
157 if (defined $args[2])
158 {
159 $try .= " \$z = new Math::BigInt \"$args[2]\"; ";
160 }
161 $try .= "Math::BigInt::blcm(\$x, \$y";
162 $try .= ", \$z" if (defined $args[2]);
163 $try .= " );";
164 }elsif ($f eq "blsft"){
165 if (defined $args[2])
166 {
167 $try .= "\$x->blsft(\$y,$args[2]);";
168 }
169 else
170 {
171 $try .= "\$x << \$y;";
172 }
173 }elsif ($f eq "brsft"){
174 if (defined $args[2])
175 {
176 $try .= "\$x->brsft(\$y,$args[2]);";
177 }
178 else
179 {
180 $try .= "\$x >> \$y;";
181 }
182 }elsif ($f eq "band"){
183 $try .= "\$x & \$y;";
184 }elsif ($f eq "bior"){
185 $try .= "\$x | \$y;";
186 }elsif ($f eq "bxor"){
187 $try .= "\$x ^ \$y;";
188 }elsif ($f eq "bpow"){
189 $try .= "\$x ** \$y;";
190 }elsif ($f eq "digit"){
191 $try = "\$x = Math::BigInt->new(\"$args[0]\"); \$x->digit($args[1]);";
192 } else { warn "Unknown op '$f'"; }
193 }
194 # print "trying $try\n";
195 $ans1 = eval $try;
196 $ans =~ s/^[+]([0-9])/$1/; # remove leading '+'
197 if ($ans eq "")
198 {
199 ok_undef ($ans1);
200 }
201 else
202 {
203 #print "try: $try ans: $ans1 $ans\n";
204 print "# Tried: '$try'\n" if !ok ($ans1, $ans);
205 }
206 # check internal state of number objects
0716bf9b 207 is_valid($ans1,$f) if ref $ans1;
58cde26e 208 }
209 } # endwhile data tests
210close DATA;
211
574bacfe 212# XXX Tels 06/29/2001 following tests never fail or do not work :( !?
0716bf9b 213
214# test whether use Math::BigInt qw/version/ works
215$try = "use Math::BigInt ($version.'1');";
216$try .= ' $x = Math::BigInt->new(123); $x = "$x";';
58cde26e 217$ans1 = eval $try;
0716bf9b 218ok_undef ( $_ ); # should result in error!
58cde26e 219
0716bf9b 220# test whether constant works or not, also test for qw($version)
221$try = "use Math::BigInt ($version,'babs',':constant');";
222$try .= ' $x = 2**150; babs($x); $x = "$x";';
223$ans1 = eval $try;
58cde26e 224ok ( $ans1, "1427247692705959881058285969449495136382746624");
225
0716bf9b 226# test wether Math::BigInt::Small via use works (w/ dff. spellings of calc)
574bacfe 227#$try = "use Math::BigInt ($version,'lib','Small');";
0716bf9b 228#$try .= ' $x = 2**10; $x = "$x";';
229#$ans1 = eval $try;
230#ok ( $ans1, "1024");
574bacfe 231#$try = "use Math::BigInt ($version,'LiB','Math::BigInt::Small');";
0716bf9b 232#$try .= ' $x = 2**10; $x = "$x";';
233#$ans1 = eval $try;
234#ok ( $ans1, "1024");
235# test wether calc => undef (array element not existing) works
574bacfe 236#$try = "use Math::BigInt ($version,'LIB');";
0716bf9b 237#$try = "require Math::BigInt; Math::BigInt::import($version,'CALC');";
238#$try .= ' $x = Math::BigInt->new(2)**10; $x = "$x";';
239#$ans1 = eval $try;
240#ok ( $ans1, 1024);
241
574bacfe 242# test whether fallback to calc works
243$try = "use Math::BigInt ($version,'lib','foo, bar , ');";
244$try .= ' Math::BigInt::_core_lib();';
245$ans1 = eval $try;
246ok ( $ans1, "Math::BigInt::Calc");
247
58cde26e 248# test some more
249@a = ();
250for (my $i = 1; $i < 10; $i++)
251 {
252 push @a, $i;
253 }
254ok "@a", "1 2 3 4 5 6 7 8 9";
255
574bacfe 256# test whether self-multiplication works correctly (result is 2**64)
58cde26e 257$try = '$x = new Math::BigInt "+4294967296";';
258$try .= '$a = $x->bmul($x);';
259$ans1 = eval $try;
260print "# Tried: '$try'\n" if !ok ($ans1, Math::BigInt->new(2) ** 64);
574bacfe 261# test self-pow
262$try = '$x = Math::BigInt->new(10);';
263$try .= '$a = $x->bpow($x);';
264$ans1 = eval $try;
265print "# Tried: '$try'\n" if !ok ($ans1, Math::BigInt->new(10) ** 10);
58cde26e 266
0716bf9b 267# test whether op destroys args or not (should better not)
58cde26e 268
269$x = new Math::BigInt (3);
270$y = new Math::BigInt (4);
271$z = $x & $y;
272ok ($x,3);
273ok ($y,4);
274ok ($z,0);
275$z = $x | $y;
276ok ($x,3);
277ok ($y,4);
278ok ($z,7);
279$x = new Math::BigInt (1);
280$y = new Math::BigInt (2);
281$z = $x | $y;
282ok ($x,1);
283ok ($y,2);
284ok ($z,3);
285
286$x = new Math::BigInt (5);
287$y = new Math::BigInt (4);
288$z = $x ^ $y;
289ok ($x,5);
290ok ($y,4);
291ok ($z,1);
292
293$x = new Math::BigInt (-5); $y = -$x;
294ok ($x, -5);
295
296$x = new Math::BigInt (-5); $y = abs($x);
297ok ($x, -5);
298
299# check whether overloading cmp works
300$try = "\$x = Math::BigInt->new(0);";
301$try .= "\$y = 10;";
302$try .= "'false' if \$x ne \$y;";
303$ans = eval $try;
304print "# For '$try'\n" if (!ok "$ans" , "false" );
305
306# we cant test for working cmpt with other objects here, we would need a dummy
307# object with stringify overload for this. see Math::String tests
308
309###############################################################################
310# check shortcuts
311$try = "\$x = Math::BigInt->new(1); \$x += 9;";
312$try .= "'ok' if \$x == 10;";
313$ans = eval $try;
314print "# For '$try'\n" if (!ok "$ans" , "ok" );
315
316$try = "\$x = Math::BigInt->new(1); \$x -= 9;";
317$try .= "'ok' if \$x == -8;";
318$ans = eval $try;
319print "# For '$try'\n" if (!ok "$ans" , "ok" );
320
321$try = "\$x = Math::BigInt->new(1); \$x *= 9;";
322$try .= "'ok' if \$x == 9;";
323$ans = eval $try;
324print "# For '$try'\n" if (!ok "$ans" , "ok" );
325
326$try = "\$x = Math::BigInt->new(10); \$x /= 2;";
327$try .= "'ok' if \$x == 5;";
328$ans = eval $try;
329print "# For '$try'\n" if (!ok "$ans" , "ok" );
330
331###############################################################################
332# check reversed order of arguments
333$try = "\$x = Math::BigInt->new(10); \$x = 2 ** \$x;";
334$try .= "'ok' if \$x == 1024;"; $ans = eval $try;
335print "# For '$try'\n" if (!ok "$ans" , "ok" );
336
337$try = "\$x = Math::BigInt->new(10); \$x = 2 * \$x;";
338$try .= "'ok' if \$x == 20;"; $ans = eval $try;
339print "# For '$try'\n" if (!ok "$ans" , "ok" );
340
341$try = "\$x = Math::BigInt->new(10); \$x = 2 + \$x;";
342$try .= "'ok' if \$x == 12;"; $ans = eval $try;
343print "# For '$try'\n" if (!ok "$ans" , "ok" );
344
345$try = "\$x = Math::BigInt->new(10); \$x = 2 - \$x;";
346$try .= "'ok' if \$x == -8;"; $ans = eval $try;
347print "# For '$try'\n" if (!ok "$ans" , "ok" );
348
349$try = "\$x = Math::BigInt->new(10); \$x = 20 / \$x;";
350$try .= "'ok' if \$x == 2;"; $ans = eval $try;
351print "# For '$try'\n" if (!ok "$ans" , "ok" );
352
353###############################################################################
354# check badd(4,5) form
355
356$try = "\$x = Math::BigInt::badd(4,5);";
357$try .= "'ok' if \$x == 9;";
358$ans = eval $try;
359print "# For '$try'\n" if (!ok "$ans" , "ok" );
360
361$try = "\$x = Math::BigInt->badd(4,5);";
362$try .= "'ok' if \$x == 9;";
363$ans = eval $try;
364print "# For '$try'\n" if (!ok "$ans" , "ok" );
365
366###############################################################################
574bacfe 367# the followin tests only make sense with Math::BigInt::Calc
368
369###############################################################################
58cde26e 370# check proper length of internal arrays
371
0716bf9b 372$x = Math::BigInt->new(99999); is_valid($x);
373$x += 1; ok ($x,100000); is_valid($x);
374$x -= 1; ok ($x,99999); is_valid($x);
58cde26e 375
376###############################################################################
574bacfe 377# check numify
58cde26e 378
0716bf9b 379my $BASE = int(1e5); # should access Math::BigInt::Calc::BASE
58cde26e 380$x = Math::BigInt->new($BASE-1); ok ($x->numify(),$BASE-1);
381$x = Math::BigInt->new(-($BASE-1)); ok ($x->numify(),-($BASE-1));
382$x = Math::BigInt->new($BASE); ok ($x->numify(),$BASE);
383$x = Math::BigInt->new(-$BASE); ok ($x->numify(),-$BASE);
384$x = Math::BigInt->new( -($BASE*$BASE*1+$BASE*1+1) );
385ok($x->numify(),-($BASE*$BASE*1+$BASE*1+1));
386
387###############################################################################
388# test bug in _digits with length($c[-1]) where $c[-1] was "00001" instead of 1
389
390$x = Math::BigInt->new(99998); $x++; $x++; $x++; $x++;
391if ($x > 100000) { ok (1,1) } else { ok ("$x < 100000","$x > 100000"); }
392
393$x = Math::BigInt->new(100003); $x++;
394$y = Math::BigInt->new(1000000);
395if ($x < 1000000) { ok (1,1) } else { ok ("$x > 1000000","$x < 1000000"); }
396
397###############################################################################
398# bug in sub where number with at least 6 trailing zeros after any op failed
399
400$x = Math::BigInt->new(123456); $z = Math::BigInt->new(10000); $z *= 10;
401$x -= $z;
402ok ($z, 100000);
403ok ($x, 23456);
404
405###############################################################################
574bacfe 406# bug in shortcut in mul()
407
408# construct a number with a zero-hole of BASE_LEN
409my $bl = Math::BigInt::Calc::_base_len();
410$x = '1' x $bl . '0' x $bl . '1' x $bl . '0' x $bl;
411$y = '1' x (2*$bl);
412#print "$x * $y\n";
413$x = Math::BigInt->new($x)->bmul($y);
414# result is 123..$bl . $bl x (3*bl-1) . $bl...321 . '0' x $bl
415$y = ''; my $d = '';
416for (my $i = 1; $i <= $bl; $i++)
417 {
418 $y .= $i; $d = $i.$d;
419 }
420#print "$y $d\n";
421$y .= $bl x (3*$bl-1) . $d . '0' x $bl;
422ok ($x,$y);
423
424###############################################################################
58cde26e 425# bug with rest "-0" in div, causing further div()s to fail
426
0716bf9b 427$x = Math::BigInt->new('-322056000'); ($x,$y) = $x->bdiv('-12882240');
58cde26e 428
0716bf9b 429ok ($y,'0','not -0'); # not '-0'
58cde26e 430is_valid($y);
431
432###############################################################################
433# check undefs: NOT DONE YET
434
435###############################################################################
436# bool
437
438$x = Math::BigInt->new(1); if ($x) { ok (1,1); } else { ok($x,'to be true') }
439$x = Math::BigInt->new(0); if (!$x) { ok (1,1); } else { ok($x,'to be false') }
440
441###############################################################################
442# objectify()
443
444@args = Math::BigInt::objectify(2,4,5);
445ok (scalar @args,3); # 'Math::BigInt', 4, 5
446ok ($args[0],'Math::BigInt');
447ok ($args[1],4);
448ok ($args[2],5);
449
450@args = Math::BigInt::objectify(0,4,5);
451ok (scalar @args,3); # 'Math::BigInt', 4, 5
452ok ($args[0],'Math::BigInt');
453ok ($args[1],4);
454ok ($args[2],5);
455
456@args = Math::BigInt::objectify(2,4,5);
457ok (scalar @args,3); # 'Math::BigInt', 4, 5
458ok ($args[0],'Math::BigInt');
459ok ($args[1],4);
460ok ($args[2],5);
461
462@args = Math::BigInt::objectify(2,4,5,6,7);
463ok (scalar @args,5); # 'Math::BigInt', 4, 5, 6, 7
464ok ($args[0],'Math::BigInt');
465ok ($args[1],4); ok (ref($args[1]),$args[0]);
466ok ($args[2],5); ok (ref($args[2]),$args[0]);
467ok ($args[3],6); ok (ref($args[3]),'');
468ok ($args[4],7); ok (ref($args[4]),'');
469
470@args = Math::BigInt::objectify(2,'Math::BigInt',4,5,6,7);
471ok (scalar @args,5); # 'Math::BigInt', 4, 5, 6, 7
472ok ($args[0],'Math::BigInt');
473ok ($args[1],4); ok (ref($args[1]),$args[0]);
474ok ($args[2],5); ok (ref($args[2]),$args[0]);
475ok ($args[3],6); ok (ref($args[3]),'');
476ok ($args[4],7); ok (ref($args[4]),'');
477
478###############################################################################
b22b3e31 479# test for floating-point input (other tests in bnorm() below)
58cde26e 480
481$z = 1050000000000000; # may be int on systems with 64bit?
0716bf9b 482$x = Math::BigInt->new($z); ok ($x->bsstr(),'105e+13'); # not 1.03e+15
483$z = 1e+129; # definitely a float (may fail on UTS)
484$x = Math::BigInt->new($z); ok ($x->bsstr(),$z);
58cde26e 485
486###############################################################################
487# prime number tests, also test for **= and length()
488# found on: http://www.utm.edu/research/primes/notes/by_year.html
489
490# ((2^148)-1)/17
491$x = Math::BigInt->new(2); $x **= 148; $x++; $x = $x / 17;
492ok ($x,"20988936657440586486151264256610222593863921");
493ok ($x->length(),length "20988936657440586486151264256610222593863921");
494
495# MM7 = 2^127-1
496$x = Math::BigInt->new(2); $x **= 127; $x--;
497ok ($x,"170141183460469231731687303715884105727");
498
499# I am afraid the following is not yet possible due to slowness
500# Also, testing for 2 meg output is a bit hard ;)
501#$x = new Math::BigInt(2); $x **= 6972593; $x--;
502
0716bf9b 503# 593573509*2^332162+1 has exactly 1,000,000 digits
b22b3e31 504# takes about 24 mins on 300 Mhz, so cannot be done yet ;)
58cde26e 505#$x = Math::BigInt->new(2); $x **= 332162; $x *= "593573509"; $x++;
0716bf9b 506#ok ($x->length(),1_000_000);
58cde26e 507
508###############################################################################
509# inheritance and overriding of _swap
510
511$x = Math::Foo->new(5);
512$x = $x - 8; # 8 - 5 instead of 5-8
513ok ($x,3);
514ok (ref($x),'Math::Foo');
515
516$x = Math::Foo->new(5);
517$x = 8 - $x; # 5 - 8 instead of 8 - 5
518ok ($x,-3);
519ok (ref($x),'Math::Foo');
520
521###############################################################################
17baacb7 522# Test whether +inf eq inf
523# This tried to test whether BigInt inf equals Perl inf. Unfortunately, Perl
524# hasn't (before 5.7.3 at least) a consistent way to say inf, and some things
525# like 1e100000 crash on some platforms. So simple test for 'inf'
526$x = Math::BigInt->new('+inf'); ok ($x,'inf');
574bacfe 527
528###############################################################################
58cde26e 529# all tests done
530
58cde26e 531###############################################################################
532# Perl 5.005 does not like ok ($x,undef)
533
534sub ok_undef
535 {
536 my $x = shift;
537
538 ok (1,1) and return if !defined $x;
539 ok ($x,'undef');
540 }
541
542###############################################################################
543# sub to check validity of a BigInt internally, to ensure that no op leaves a
544# number object in an invalid state (f.i. "-0")
545
546sub is_valid
547 {
0716bf9b 548 my ($x,$f) = @_;
58cde26e 549
0716bf9b 550 my $e = 0; # error?
58cde26e 551 # ok as reference?
0716bf9b 552 $e = 'Not a reference to Math::BigInt' if !ref($x);
58cde26e 553
554 # has ok sign?
0716bf9b 555 $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'"
556 if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/;
58cde26e 557
0716bf9b 558 $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0;
559 $e = $CALC->_check($x->{value}) if $e eq '0';
58cde26e 560
0716bf9b 561 # test done, see if error did crop up
562 ok (1,1), return if ($e eq '0');
58cde26e 563
0716bf9b 564 ok (1,$e." op '$f'");
a99f8827 565 }
58cde26e 566
748a9306 567__END__
574bacfe 568&is_negative
5690:0
570-1:1
5711:0
572+inf:0
573-inf:1
574NaNneg:0
575&is_positive
5760:1
577-1:0
5781:1
579+inf:1
580-inf:0
581NaNneg:0
0716bf9b 582&is_odd
583abc:0
5840:0
5851:1
5863:1
587-1:1
588-3:1
58910000001:1
59010000002:0
5912:0
592&is_even
593abc:0
5940:1
5951:0
5963:0
597-1:0
598-3:0
59910000001:0
60010000002:1
6012:1
602&bacmp
603+0:-0:0
604+0:+1:-1
605-1:+1:0
606+1:-1:0
607-1:+2:-1
608+2:-1:1
609-123456789:+987654321:-1
610+123456789:-987654321:-1
611+987654321:+123456789:1
612-987654321:+123456789:1
613-123:+4567889:-1
574bacfe 614# NaNs
615acmpNaN:123:
616123:acmpNaN:
617acmpNaN:acmpNaN:
618# infinity
619+inf:+inf:0
620-inf:-inf:0
621+inf:-inf:0
622-inf:+inf:0
623+inf:123:1
624-inf:123:1
625+inf:-123:1
626-inf:-123:1
627# return undef
628+inf:NaN:
629NaN:inf:
630-inf:NaN:
631NaN:-inf:
748a9306 632&bnorm
0716bf9b 633123:123
58cde26e 634# binary input
6350babc:NaN
6360b123:NaN
6370b0:0
638-0b0:0
639-0b1:-1
6400b0001:1
6410b001:1
6420b011:3
6430b101:5
6440b1000000000000000000000000000000:1073741824
574bacfe 6450b_101:NaN
6460b1_0_1:5
58cde26e 647# hex input
648-0x0:0
6490xabcdefgh:NaN
6500x1234:4660
6510xabcdef:11259375
652-0xABCDEF:-11259375
653-0x1234:-4660
6540x12345678:305419896
574bacfe 6550x1_2_3_4_56_78:305419896
6560x_123:NaN
58cde26e 657# inf input
574bacfe 658+inf:inf
58cde26e 659-inf:-inf
6600inf:NaN
661# normal input
662:NaN
748a9306 663abc:NaN
664 1 a:NaN
6651bcd2:NaN
66611111b:NaN
667+1z:NaN
668-1z:NaN
58cde26e 6690:0
670+0:0
671+00:0
672+000:0
673000000000000000000:0
674-0:0
675-0000:0
676+1:1
677+01:1
678+001:1
679+00000100000:100000
680123456789:123456789
748a9306 681-1:-1
682-01:-1
683-001:-1
684-123456789:-123456789
685-00000100000:-100000
58cde26e 6861_2_3:123
687_123:NaN
688_123_:NaN
689_123_:NaN
6901__23:NaN
69110000000000E-1_0:1
6921E2:100
6931E1:10
6941E0:1
695E1:NaN
696E23:NaN
6971.23E2:123
6981.23E1:NaN
6991.23E-1:NaN
700100E-1:10
701# floating point input
7021.01E2:101
7031010E-1:101
704-1010E0:-1010
705-1010E1:-10100
706-1010E-2:NaN
707-1.01E+1:NaN
708-1.01E-1:NaN
574bacfe 7091234.00:1234
710&bnan
7111:NaN
7122:NaN
713abc:NaN
714&bone
7152:+:+1
7162:-:-1
717boneNaN:-:-1
718boneNaN:+:+1
7192:abc:+1
7203::+1
58cde26e 721&binf
574bacfe 7221:+:inf
58cde26e 7232:-:-inf
574bacfe 7243:abc:inf
58cde26e 725&is_inf
726+inf::1
727-inf::1
728abc::0
7291::0
730NaN::0
731-1::0
732+inf:-:0
733+inf:+:1
734-inf:-:1
735-inf:+:0
0716bf9b 736# it must be exactly /^[+-]inf$/
737+infinity::0
738-infinity::0
58cde26e 739&blsft
740abc:abc:NaN
741+2:+2:+8
742+1:+32:+4294967296
743+1:+48:+281474976710656
744+8:-2:NaN
745# excercise base 10
746+12345:4:10:123450000
747-1234:0:10:-1234
748+1234:0:10:+1234
749+2:2:10:200
750+12:2:10:1200
751+1234:-3:10:NaN
7521234567890123:12:10:1234567890123000000000000
753&brsft
754abc:abc:NaN
755+8:+2:+2
756+4294967296:+32:+1
757+281474976710656:+48:+1
758+2:-2:NaN
759# excercise base 10
760-1234:0:10:-1234
761+1234:0:10:+1234
762+200:2:10:2
763+1234:3:10:1
764+1234:2:10:12
765+1234:-3:10:NaN
766310000:4:10:31
76712300000:5:10:123
7681230000000000:10:10:123
76909876123456789067890:12:10:9876123
7701234561234567890123:13:10:123456
771&bsstr
7721e+34:1e+34
773123.456E3:123456e+0
774100:1e+2
775abc:NaN
748a9306 776&bneg
574bacfe 777bnegNaN:NaN
778+inf:-inf
779-inf:inf
748a9306 780abd:NaN
781+0:+0
782+1:-1
783-1:+1
784+123456789:-123456789
785-123456789:+123456789
786&babs
574bacfe 787babsNaN:NaN
788+inf:inf
789-inf:inf
748a9306 790+0:+0
791+1:+1
792-1:+1
793+123456789:+123456789
794-123456789:+123456789
795&bcmp
574bacfe 796bcmpNaN:bcmpNaN:
797bcmpNaN:+0:
798+0:bcmpNaN:
5d7098d5 799+0:+0:0
748a9306 800-1:+0:-1
5d7098d5 801+0:-1:1
802+1:+0:1
748a9306 803+0:+1:-1
804-1:+1:-1
5d7098d5 805+1:-1:1
806-1:-1:0
807+1:+1:0
808+123:+123:0
809+123:+12:1
748a9306 810+12:+123:-1
5d7098d5 811-123:-123:0
748a9306 812-123:-12:-1
5d7098d5 813-12:-123:1
748a9306 814+123:+124:-1
5d7098d5 815+124:+123:1
816-123:-124:1
748a9306 817-124:-123:-1
5d7098d5 818+100:+5:1
58cde26e 819-123456789:+987654321:-1
820+123456789:-987654321:1
821-987654321:+123456789:-1
0716bf9b 822-inf:5432112345:-1
823+inf:5432112345:1
824-inf:-5432112345:-1
825+inf:-5432112345:1
826+inf:+inf:0
827-inf:-inf:0
574bacfe 828+inf:-inf:1
829-inf:+inf:-1
0716bf9b 830# return undef
831+inf:NaN:
574bacfe 832NaN:inf:
0716bf9b 833-inf:NaN:
834NaN:-inf:
58cde26e 835&binc
836abc:NaN
574bacfe 837+inf:inf
838-inf:-inf
58cde26e 839+0:+1
840+1:+2
841-1:+0
842&bdec
843abc:NaN
574bacfe 844+inf:inf
845-inf:-inf
58cde26e 846+0:-1
847+1:+0
848-1:-2
748a9306 849&badd
850abc:abc:NaN
851abc:+0:NaN
852+0:abc:NaN
574bacfe 853+inf:-inf:0
854-inf:+inf:0
855+inf:+inf:inf
856-inf:-inf:-inf
857baddNaN:+inf:NaN
858baddNaN:+inf:NaN
859+inf:baddNaN:NaN
860-inf:baddNaN:NaN
748a9306 861+0:+0:+0
862+1:+0:+1
863+0:+1:+1
864+1:+1:+2
865-1:+0:-1
866+0:-1:-1
867-1:-1:-2
868-1:+1:+0
869+1:-1:+0
870+9:+1:+10
871+99:+1:+100
872+999:+1:+1000
873+9999:+1:+10000
874+99999:+1:+100000
875+999999:+1:+1000000
876+9999999:+1:+10000000
877+99999999:+1:+100000000
878+999999999:+1:+1000000000
879+9999999999:+1:+10000000000
880+99999999999:+1:+100000000000
881+10:-1:+9
882+100:-1:+99
883+1000:-1:+999
884+10000:-1:+9999
885+100000:-1:+99999
886+1000000:-1:+999999
887+10000000:-1:+9999999
888+100000000:-1:+99999999
889+1000000000:-1:+999999999
890+10000000000:-1:+9999999999
891+123456789:+987654321:+1111111110
892-123456789:+987654321:+864197532
893-123456789:-987654321:-1111111110
894+123456789:-987654321:-864197532
895&bsub
896abc:abc:NaN
897abc:+0:NaN
898+0:abc:NaN
574bacfe 899+inf:-inf:inf
900-inf:+inf:-inf
901+inf:+inf:0
902-inf:-inf:0
748a9306 903+0:+0:+0
904+1:+0:+1
905+0:+1:-1
906+1:+1:+0
907-1:+0:-1
908+0:-1:+1
909-1:-1:+0
910-1:+1:-2
911+1:-1:+2
912+9:+1:+8
913+99:+1:+98
914+999:+1:+998
915+9999:+1:+9998
916+99999:+1:+99998
917+999999:+1:+999998
918+9999999:+1:+9999998
919+99999999:+1:+99999998
920+999999999:+1:+999999998
921+9999999999:+1:+9999999998
922+99999999999:+1:+99999999998
923+10:-1:+11
924+100:-1:+101
925+1000:-1:+1001
926+10000:-1:+10001
927+100000:-1:+100001
928+1000000:-1:+1000001
929+10000000:-1:+10000001
930+100000000:-1:+100000001
931+1000000000:-1:+1000000001
932+10000000000:-1:+10000000001
933+123456789:+987654321:-864197532
934-123456789:+987654321:-1111111110
935-123456789:-987654321:+864197532
936+123456789:-987654321:+1111111110
937&bmul
938abc:abc:NaN
939abc:+0:NaN
940+0:abc:NaN
574bacfe 941NaNmul:+inf:NaN
942NaNmul:-inf:NaN
943-inf:NaNmul:NaN
944+inf:NaNmul:NaN
945+inf:+inf:inf
946+inf:-inf:-inf
947-inf:+inf:-inf
948-inf:-inf:inf
748a9306 949+0:+0:+0
950+0:+1:+0
951+1:+0:+0
952+0:-1:+0
953-1:+0:+0
954+123456789123456789:+0:+0
955+0:+123456789123456789:+0
956-1:-1:+1
957-1:+1:-1
958+1:-1:-1
959+1:+1:+1
960+2:+3:+6
961-2:+3:-6
962+2:-3:-6
963-2:-3:+6
964+111:+111:+12321
965+10101:+10101:+102030201
966+1001001:+1001001:+1002003002001
967+100010001:+100010001:+10002000300020001
968+10000100001:+10000100001:+100002000030000200001
969+11111111111:+9:+99999999999
970+22222222222:+9:+199999999998
971+33333333333:+9:+299999999997
972+44444444444:+9:+399999999996
973+55555555555:+9:+499999999995
974+66666666666:+9:+599999999994
975+77777777777:+9:+699999999993
976+88888888888:+9:+799999999992
977+99999999999:+9:+899999999991
58cde26e 978+25:+25:+625
979+12345:+12345:+152399025
980+99999:+11111:+1111088889
574bacfe 981&bdiv-list
982100:20:5,0
9834095:4095:1,0
984-4095:-4095:1,0
9854095:-4095:-1,0
986-4095:4095:-1,0
748a9306 987&bdiv
988abc:abc:NaN
989abc:+1:abc:NaN
990+1:abc:NaN
991+0:+0:NaN
574bacfe 992+5:0:inf
993-5:0:-inf
994+1:+0:inf
748a9306 995+0:+1:+0
748a9306 996+0:-1:+0
574bacfe 997-1:+0:-inf
748a9306 998+1:+1:+1
999-1:-1:+1
1000+1:-1:-1
1001-1:+1:-1
1002+1:+2:+0
1003+2:+1:+2
58cde26e 1004+1:+26:+0
748a9306 1005+1000000000:+9:+111111111
1006+2000000000:+9:+222222222
1007+3000000000:+9:+333333333
1008+4000000000:+9:+444444444
1009+5000000000:+9:+555555555
1010+6000000000:+9:+666666666
1011+7000000000:+9:+777777777
1012+8000000000:+9:+888888888
1013+9000000000:+9:+1000000000
1014+35500000:+113:+314159
1015+71000000:+226:+314159
1016+106500000:+339:+314159
1017+1000000000:+3:+333333333
1018+10:+5:+2
1019+100:+4:+25
1020+1000:+8:+125
1021+10000:+16:+625
1022+999999999999:+9:+111111111111
1023+999999999999:+99:+10101010101
1024+999999999999:+999:+1001001001
1025+999999999999:+9999:+100010001
1026+999999999999999:+99999:+10000100001
58cde26e 1027+1111088889:+99999:+11111
1028-5:-3:1
10294:3:1
10301:3:0
1031-2:-3:0
1032-2:3:-1
10331:-3:-1
1034-5:3:-2
10354:-3:-2
574bacfe 1036123:+inf:0
1037123:-inf:0
748a9306 1038&bmod
1039abc:abc:NaN
1040abc:+1:abc:NaN
1041+1:abc:NaN
1042+0:+0:NaN
1043+0:+1:+0
1044+1:+0:NaN
1045+0:-1:+0
1046-1:+0:NaN
1047+1:+1:+0
1048-1:-1:+0
1049+1:-1:+0
1050-1:+1:+0
1051+1:+2:+1
1052+2:+1:+0
1053+1000000000:+9:+1
1054+2000000000:+9:+2
1055+3000000000:+9:+3
1056+4000000000:+9:+4
1057+5000000000:+9:+5
1058+6000000000:+9:+6
1059+7000000000:+9:+7
1060+8000000000:+9:+8
1061+9000000000:+9:+0
1062+35500000:+113:+33
1063+71000000:+226:+66
1064+106500000:+339:+99
1065+1000000000:+3:+1
1066+10:+5:+0
1067+100:+4:+0
1068+1000:+8:+0
1069+10000:+16:+0
1070+999999999999:+9:+0
1071+999999999999:+99:+0
1072+999999999999:+999:+0
1073+999999999999:+9999:+0
1074+999999999999999:+99999:+0
58cde26e 1075-9:+5:+1
1076+9:-5:-1
1077-9:-5:-4
1078-5:3:1
1079-2:3:1
10804:3:1
10811:3:1
1082-5:-3:-2
1083-2:-3:-2
10844:-3:-2
10851:-3:-2
574bacfe 10864095:4095:0
748a9306 1087&bgcd
1088abc:abc:NaN
1089abc:+0:NaN
1090+0:abc:NaN
1091+0:+0:+0
1092+0:+1:+1
1093+1:+0:+1
1094+1:+1:+1
1095+2:+3:+1
1096+3:+2:+1
58cde26e 1097-3:+2:+1
748a9306 1098+100:+625:+25
1099+4096:+81:+1
58cde26e 1100+1034:+804:+2
1101+27:+90:+56:+1
1102+27:+90:+54:+9
1103&blcm
e16b8f49 1104abc:abc:NaN
58cde26e 1105abc:+0:NaN
1106+0:abc:NaN
1107+0:+0:NaN
1108+1:+0:+0
1109+0:+1:+0
1110+27:+90:+270
1111+1034:+804:+415668
e16b8f49 1112&band
1113abc:abc:NaN
58cde26e 1114abc:0:NaN
11150:abc:NaN
0716bf9b 11161:2:0
11173:2:2
e16b8f49 1118+8:+2:+0
1119+281474976710656:+0:+0
1120+281474976710656:+1:+0
1121+281474976710656:+281474976710656:+281474976710656
574bacfe 1122-2:-3:-4
1123-1:-1:-1
1124-6:-6:-6
1125-7:-4:-8
1126-7:4:0
1127-4:7:4
e16b8f49 1128&bior
1129abc:abc:NaN
58cde26e 1130abc:0:NaN
11310:abc:NaN
0716bf9b 11321:2:3
e16b8f49 1133+8:+2:+10
1134+281474976710656:+0:+281474976710656
1135+281474976710656:+1:+281474976710657
1136+281474976710656:+281474976710656:+281474976710656
574bacfe 1137-2:-3:-1
1138-1:-1:-1
1139-6:-6:-6
1140-7:4:-3
1141-4:7:-1
e16b8f49 1142&bxor
1143abc:abc:NaN
58cde26e 1144abc:0:NaN
11450:abc:NaN
0716bf9b 11461:2:3
e16b8f49 1147+8:+2:+10
1148+281474976710656:+0:+281474976710656
1149+281474976710656:+1:+281474976710657
1150+281474976710656:+281474976710656:+0
574bacfe 1151-2:-3:3
1152-1:-1:0
1153-6:-6:0
1154-7:4:-3
1155-4:7:-5
11564:-7:-3
1157-4:-7:5
e16b8f49 1158&bnot
1159abc:NaN
1160+0:-1
1161+8:-9
1162+281474976710656:-281474976710657
574bacfe 1163-1:0
1164-2:1
1165-12:11
58cde26e 1166&digit
11670:0:0
116812:0:2
116912:1:1
1170123:0:3
1171123:1:2
1172123:2:1
1173123:-1:1
1174123:-2:2
1175123:-3:3
1176123456:0:6
1177123456:1:5
1178123456:2:4
1179123456:3:3
1180123456:4:2
1181123456:5:1
1182123456:-1:1
1183123456:-2:2
1184123456:-3:3
1185100000:-3:0
1186100000:0:0
1187100000:1:0
1188&mantissa
1189abc:NaN
11901e4:1
11912e0:2
1192123:123
1193-1:-1
1194-2:-2
1195&exponent
1196abc:NaN
11971e4:4
11982e0:0
1199123:0
1200-1:0
1201-2:0
12020:1
1203&parts
1204abc:NaN,NaN
12051e4:1,4
12062e0:2,0
1207123:123,0
1208-1:-1,0
1209-2:-2,0
12100:0,1
1211&bpow
0716bf9b 1212abc:12:NaN
121312:abc:NaN
58cde26e 12140:0:1
12150:1:0
12160:2:0
12170:-1:NaN
12180:-2:NaN
12191:0:1
12201:1:1
12211:2:1
12221:3:1
12231:-1:1
12241:-2:1
12251:-3:1
12262:0:1
12272:1:2
12282:2:4
12292:3:8
12303:3:27
12312:-1:NaN
1232-2:-1:NaN
12332:-2:NaN
1234-2:-2:NaN
574bacfe 1235+inf:1234500012:inf
0716bf9b 1236-inf:1234500012:-inf
574bacfe 1237+inf:-12345000123:inf
0716bf9b 1238-inf:-12345000123:-inf
58cde26e 1239# 1 ** -x => 1 / (1 ** x)
1240-1:0:1
1241-2:0:1
1242-1:1:-1
1243-1:2:1
1244-1:3:-1
1245-1:4:1
1246-1:5:-1
1247-1:-1:-1
1248-1:-2:1
1249-1:-3:-1
1250-1:-4:1
125110:2:100
125210:3:1000
125310:4:10000
125410:5:100000
125510:6:1000000
125610:7:10000000
125710:8:100000000
125810:9:1000000000
125910:20:100000000000000000000
1260123456:2:15241383936
1261&length
1262100:3
126310:2
12641:1
12650:1
126612345:5
126710000000000000000:17
1268-123:3
1269&bsqrt
1270144:12
127116:4
12724:2
12732:1
127412:3
1275256:16
1276100000000:10000
12774000000000000:2000000
12781:1
12790:0
1280-2:NaN
1281Nan:NaN
1282&bround
1283$round_mode('trunc')
574bacfe 12840:12:0
1285NaNbround:12:NaN
1286+inf:12:inf
1287-inf:12:-inf
58cde26e 12881234:0:1234
12891234:2:1200
1290123456:4:123400
1291123456:5:123450
1292123456:6:123456
1293+10123456789:5:+10123000000
1294-10123456789:5:-10123000000
1295+10123456789:9:+10123456700
1296-10123456789:9:-10123456700
1297+101234500:6:+101234000
1298-101234500:6:-101234000
1299#+101234500:-4:+101234000
1300#-101234500:-4:-101234000
1301$round_mode('zero')
1302+20123456789:5:+20123000000
1303-20123456789:5:-20123000000
1304+20123456789:9:+20123456800
1305-20123456789:9:-20123456800
1306+201234500:6:+201234000
1307-201234500:6:-201234000
1308#+201234500:-4:+201234000
1309#-201234500:-4:-201234000
1310+12345000:4:12340000
1311-12345000:4:-12340000
1312$round_mode('+inf')
1313+30123456789:5:+30123000000
1314-30123456789:5:-30123000000
1315+30123456789:9:+30123456800
1316-30123456789:9:-30123456800
1317+301234500:6:+301235000
1318-301234500:6:-301234000
1319#+301234500:-4:+301235000
1320#-301234500:-4:-301234000
1321+12345000:4:12350000
1322-12345000:4:-12340000
1323$round_mode('-inf')
1324+40123456789:5:+40123000000
1325-40123456789:5:-40123000000
1326+40123456789:9:+40123456800
1327-40123456789:9:-40123456800
1328+401234500:6:+401234000
1329+401234500:6:+401234000
1330#-401234500:-4:-401235000
1331#-401234500:-4:-401235000
1332+12345000:4:12340000
1333-12345000:4:-12350000
1334$round_mode('odd')
1335+50123456789:5:+50123000000
1336-50123456789:5:-50123000000
1337+50123456789:9:+50123456800
1338-50123456789:9:-50123456800
1339+501234500:6:+501235000
1340-501234500:6:-501235000
1341#+501234500:-4:+501235000
1342#-501234500:-4:-501235000
1343+12345000:4:12350000
1344-12345000:4:-12350000
1345$round_mode('even')
1346+60123456789:5:+60123000000
1347-60123456789:5:-60123000000
1348+60123456789:9:+60123456800
1349-60123456789:9:-60123456800
1350+601234500:6:+601234000
1351-601234500:6:-601234000
1352#+601234500:-4:+601234000
1353#-601234500:-4:-601234000
1354#-601234500:-9:0
1355#-501234500:-9:0
1356#-601234500:-8:0
1357#-501234500:-8:0
1358+1234567:7:1234567
1359+1234567:6:1234570
1360+12345000:4:12340000
1361-12345000:4:-12340000
58cde26e 1362&is_zero
13630:1
1364NaNzero:0
574bacfe 1365+inf:0
1366-inf:0
58cde26e 1367123:0
1368-1:0
13691:0
58cde26e 1370&is_one
13710:0
574bacfe 1372NaNone:0
1373+inf:0
1374-inf:0
58cde26e 13751:1
13762:0
1377-1:0
1378-2:0
1379# floor and ceil tests are pretty pointless in integer space...but play safe
1380&bfloor
13810:0
574bacfe 1382NaNfloor:NaN
1383+inf:inf
1384-inf:-inf
f216259d 1385-1:-1
58cde26e 1386-2:-2
13872:2
13883:3
1389abc:NaN
1390&bceil
574bacfe 1391NaNceil:NaN
1392+inf:inf
1393-inf:-inf
58cde26e 13940:0
1395-1:-1
1396-2:-2
13972:2
13983:3
1399abc:NaN