Update Changes.
[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###############################################################################
574bacfe 522# test whether +inf eq inf
523
524$y = 1e1000000; # create inf, since bareword inf does not work
c525946d 525$x = Math::BigInt->new('+inf'); ok_inf ($x,$y);
574bacfe 526
527###############################################################################
58cde26e 528# all tests done
529
58cde26e 530###############################################################################
c525946d 531
532# libc are confused what to call Infinity
533
534sub ok_inf {
535 my ($x, $y) = @_;
536
c59ea072 537 $x =~ s/^inf(?:inity)?/Inf/i;
538 $y =~ s/^inf(?:inity)?/Inf/i;
c525946d 539
540 ok($x, $y);
541}
542
58cde26e 543# Perl 5.005 does not like ok ($x,undef)
544
545sub ok_undef
546 {
547 my $x = shift;
548
549 ok (1,1) and return if !defined $x;
550 ok ($x,'undef');
551 }
552
553###############################################################################
554# sub to check validity of a BigInt internally, to ensure that no op leaves a
555# number object in an invalid state (f.i. "-0")
556
557sub is_valid
558 {
0716bf9b 559 my ($x,$f) = @_;
58cde26e 560
0716bf9b 561 my $e = 0; # error?
58cde26e 562 # ok as reference?
0716bf9b 563 $e = 'Not a reference to Math::BigInt' if !ref($x);
58cde26e 564
565 # has ok sign?
0716bf9b 566 $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'"
567 if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/;
58cde26e 568
0716bf9b 569 $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0;
570 $e = $CALC->_check($x->{value}) if $e eq '0';
58cde26e 571
0716bf9b 572 # test done, see if error did crop up
573 ok (1,1), return if ($e eq '0');
58cde26e 574
0716bf9b 575 ok (1,$e." op '$f'");
a99f8827 576 }
58cde26e 577
748a9306 578__END__
574bacfe 579&is_negative
5800:0
581-1:1
5821:0
583+inf:0
584-inf:1
585NaNneg:0
586&is_positive
5870:1
588-1:0
5891:1
590+inf:1
591-inf:0
592NaNneg:0
0716bf9b 593&is_odd
594abc:0
5950:0
5961:1
5973:1
598-1:1
599-3:1
60010000001:1
60110000002:0
6022:0
603&is_even
604abc:0
6050:1
6061:0
6073:0
608-1:0
609-3:0
61010000001:0
61110000002:1
6122:1
613&bacmp
614+0:-0:0
615+0:+1:-1
616-1:+1:0
617+1:-1:0
618-1:+2:-1
619+2:-1:1
620-123456789:+987654321:-1
621+123456789:-987654321:-1
622+987654321:+123456789:1
623-987654321:+123456789:1
624-123:+4567889:-1
574bacfe 625# NaNs
626acmpNaN:123:
627123:acmpNaN:
628acmpNaN:acmpNaN:
629# infinity
630+inf:+inf:0
631-inf:-inf:0
632+inf:-inf:0
633-inf:+inf:0
634+inf:123:1
635-inf:123:1
636+inf:-123:1
637-inf:-123:1
638# return undef
639+inf:NaN:
640NaN:inf:
641-inf:NaN:
642NaN:-inf:
748a9306 643&bnorm
0716bf9b 644123:123
58cde26e 645# binary input
6460babc:NaN
6470b123:NaN
6480b0:0
649-0b0:0
650-0b1:-1
6510b0001:1
6520b001:1
6530b011:3
6540b101:5
6550b1000000000000000000000000000000:1073741824
574bacfe 6560b_101:NaN
6570b1_0_1:5
58cde26e 658# hex input
659-0x0:0
6600xabcdefgh:NaN
6610x1234:4660
6620xabcdef:11259375
663-0xABCDEF:-11259375
664-0x1234:-4660
6650x12345678:305419896
574bacfe 6660x1_2_3_4_56_78:305419896
6670x_123:NaN
58cde26e 668# inf input
574bacfe 669+inf:inf
58cde26e 670-inf:-inf
6710inf:NaN
672# normal input
673:NaN
748a9306 674abc:NaN
675 1 a:NaN
6761bcd2:NaN
67711111b:NaN
678+1z:NaN
679-1z:NaN
58cde26e 6800:0
681+0:0
682+00:0
683+000:0
684000000000000000000:0
685-0:0
686-0000:0
687+1:1
688+01:1
689+001:1
690+00000100000:100000
691123456789:123456789
748a9306 692-1:-1
693-01:-1
694-001:-1
695-123456789:-123456789
696-00000100000:-100000
58cde26e 6971_2_3:123
698_123:NaN
699_123_:NaN
700_123_:NaN
7011__23:NaN
70210000000000E-1_0:1
7031E2:100
7041E1:10
7051E0:1
706E1:NaN
707E23:NaN
7081.23E2:123
7091.23E1:NaN
7101.23E-1:NaN
711100E-1:10
712# floating point input
7131.01E2:101
7141010E-1:101
715-1010E0:-1010
716-1010E1:-10100
717-1010E-2:NaN
718-1.01E+1:NaN
719-1.01E-1:NaN
574bacfe 7201234.00:1234
721&bnan
7221:NaN
7232:NaN
724abc:NaN
725&bone
7262:+:+1
7272:-:-1
728boneNaN:-:-1
729boneNaN:+:+1
7302:abc:+1
7313::+1
58cde26e 732&binf
574bacfe 7331:+:inf
58cde26e 7342:-:-inf
574bacfe 7353:abc:inf
58cde26e 736&is_inf
737+inf::1
738-inf::1
739abc::0
7401::0
741NaN::0
742-1::0
743+inf:-:0
744+inf:+:1
745-inf:-:1
746-inf:+:0
0716bf9b 747# it must be exactly /^[+-]inf$/
748+infinity::0
749-infinity::0
58cde26e 750&blsft
751abc:abc:NaN
752+2:+2:+8
753+1:+32:+4294967296
754+1:+48:+281474976710656
755+8:-2:NaN
756# excercise base 10
757+12345:4:10:123450000
758-1234:0:10:-1234
759+1234:0:10:+1234
760+2:2:10:200
761+12:2:10:1200
762+1234:-3:10:NaN
7631234567890123:12:10:1234567890123000000000000
764&brsft
765abc:abc:NaN
766+8:+2:+2
767+4294967296:+32:+1
768+281474976710656:+48:+1
769+2:-2:NaN
770# excercise base 10
771-1234:0:10:-1234
772+1234:0:10:+1234
773+200:2:10:2
774+1234:3:10:1
775+1234:2:10:12
776+1234:-3:10:NaN
777310000:4:10:31
77812300000:5:10:123
7791230000000000:10:10:123
78009876123456789067890:12:10:9876123
7811234561234567890123:13:10:123456
782&bsstr
7831e+34:1e+34
784123.456E3:123456e+0
785100:1e+2
786abc:NaN
748a9306 787&bneg
574bacfe 788bnegNaN:NaN
789+inf:-inf
790-inf:inf
748a9306 791abd:NaN
792+0:+0
793+1:-1
794-1:+1
795+123456789:-123456789
796-123456789:+123456789
797&babs
574bacfe 798babsNaN:NaN
799+inf:inf
800-inf:inf
748a9306 801+0:+0
802+1:+1
803-1:+1
804+123456789:+123456789
805-123456789:+123456789
806&bcmp
574bacfe 807bcmpNaN:bcmpNaN:
808bcmpNaN:+0:
809+0:bcmpNaN:
5d7098d5 810+0:+0:0
748a9306 811-1:+0:-1
5d7098d5 812+0:-1:1
813+1:+0:1
748a9306 814+0:+1:-1
815-1:+1:-1
5d7098d5 816+1:-1:1
817-1:-1:0
818+1:+1:0
819+123:+123:0
820+123:+12:1
748a9306 821+12:+123:-1
5d7098d5 822-123:-123:0
748a9306 823-123:-12:-1
5d7098d5 824-12:-123:1
748a9306 825+123:+124:-1
5d7098d5 826+124:+123:1
827-123:-124:1
748a9306 828-124:-123:-1
5d7098d5 829+100:+5:1
58cde26e 830-123456789:+987654321:-1
831+123456789:-987654321:1
832-987654321:+123456789:-1
0716bf9b 833-inf:5432112345:-1
834+inf:5432112345:1
835-inf:-5432112345:-1
836+inf:-5432112345:1
837+inf:+inf:0
838-inf:-inf:0
574bacfe 839+inf:-inf:1
840-inf:+inf:-1
0716bf9b 841# return undef
842+inf:NaN:
574bacfe 843NaN:inf:
0716bf9b 844-inf:NaN:
845NaN:-inf:
58cde26e 846&binc
847abc:NaN
574bacfe 848+inf:inf
849-inf:-inf
58cde26e 850+0:+1
851+1:+2
852-1:+0
853&bdec
854abc:NaN
574bacfe 855+inf:inf
856-inf:-inf
58cde26e 857+0:-1
858+1:+0
859-1:-2
748a9306 860&badd
861abc:abc:NaN
862abc:+0:NaN
863+0:abc:NaN
574bacfe 864+inf:-inf:0
865-inf:+inf:0
866+inf:+inf:inf
867-inf:-inf:-inf
868baddNaN:+inf:NaN
869baddNaN:+inf:NaN
870+inf:baddNaN:NaN
871-inf:baddNaN:NaN
748a9306 872+0:+0:+0
873+1:+0:+1
874+0:+1:+1
875+1:+1:+2
876-1:+0:-1
877+0:-1:-1
878-1:-1:-2
879-1:+1:+0
880+1:-1:+0
881+9:+1:+10
882+99:+1:+100
883+999:+1:+1000
884+9999:+1:+10000
885+99999:+1:+100000
886+999999:+1:+1000000
887+9999999:+1:+10000000
888+99999999:+1:+100000000
889+999999999:+1:+1000000000
890+9999999999:+1:+10000000000
891+99999999999:+1:+100000000000
892+10:-1:+9
893+100:-1:+99
894+1000:-1:+999
895+10000:-1:+9999
896+100000:-1:+99999
897+1000000:-1:+999999
898+10000000:-1:+9999999
899+100000000:-1:+99999999
900+1000000000:-1:+999999999
901+10000000000:-1:+9999999999
902+123456789:+987654321:+1111111110
903-123456789:+987654321:+864197532
904-123456789:-987654321:-1111111110
905+123456789:-987654321:-864197532
906&bsub
907abc:abc:NaN
908abc:+0:NaN
909+0:abc:NaN
574bacfe 910+inf:-inf:inf
911-inf:+inf:-inf
912+inf:+inf:0
913-inf:-inf:0
748a9306 914+0:+0:+0
915+1:+0:+1
916+0:+1:-1
917+1:+1:+0
918-1:+0:-1
919+0:-1:+1
920-1:-1:+0
921-1:+1:-2
922+1:-1:+2
923+9:+1:+8
924+99:+1:+98
925+999:+1:+998
926+9999:+1:+9998
927+99999:+1:+99998
928+999999:+1:+999998
929+9999999:+1:+9999998
930+99999999:+1:+99999998
931+999999999:+1:+999999998
932+9999999999:+1:+9999999998
933+99999999999:+1:+99999999998
934+10:-1:+11
935+100:-1:+101
936+1000:-1:+1001
937+10000:-1:+10001
938+100000:-1:+100001
939+1000000:-1:+1000001
940+10000000:-1:+10000001
941+100000000:-1:+100000001
942+1000000000:-1:+1000000001
943+10000000000:-1:+10000000001
944+123456789:+987654321:-864197532
945-123456789:+987654321:-1111111110
946-123456789:-987654321:+864197532
947+123456789:-987654321:+1111111110
948&bmul
949abc:abc:NaN
950abc:+0:NaN
951+0:abc:NaN
574bacfe 952NaNmul:+inf:NaN
953NaNmul:-inf:NaN
954-inf:NaNmul:NaN
955+inf:NaNmul:NaN
956+inf:+inf:inf
957+inf:-inf:-inf
958-inf:+inf:-inf
959-inf:-inf:inf
748a9306 960+0:+0:+0
961+0:+1:+0
962+1:+0:+0
963+0:-1:+0
964-1:+0:+0
965+123456789123456789:+0:+0
966+0:+123456789123456789:+0
967-1:-1:+1
968-1:+1:-1
969+1:-1:-1
970+1:+1:+1
971+2:+3:+6
972-2:+3:-6
973+2:-3:-6
974-2:-3:+6
975+111:+111:+12321
976+10101:+10101:+102030201
977+1001001:+1001001:+1002003002001
978+100010001:+100010001:+10002000300020001
979+10000100001:+10000100001:+100002000030000200001
980+11111111111:+9:+99999999999
981+22222222222:+9:+199999999998
982+33333333333:+9:+299999999997
983+44444444444:+9:+399999999996
984+55555555555:+9:+499999999995
985+66666666666:+9:+599999999994
986+77777777777:+9:+699999999993
987+88888888888:+9:+799999999992
988+99999999999:+9:+899999999991
58cde26e 989+25:+25:+625
990+12345:+12345:+152399025
991+99999:+11111:+1111088889
574bacfe 992&bdiv-list
993100:20:5,0
9944095:4095:1,0
995-4095:-4095:1,0
9964095:-4095:-1,0
997-4095:4095:-1,0
748a9306 998&bdiv
999abc:abc:NaN
1000abc:+1:abc:NaN
1001+1:abc:NaN
1002+0:+0:NaN
574bacfe 1003+5:0:inf
1004-5:0:-inf
1005+1:+0:inf
748a9306 1006+0:+1:+0
748a9306 1007+0:-1:+0
574bacfe 1008-1:+0:-inf
748a9306 1009+1:+1:+1
1010-1:-1:+1
1011+1:-1:-1
1012-1:+1:-1
1013+1:+2:+0
1014+2:+1:+2
58cde26e 1015+1:+26:+0
748a9306 1016+1000000000:+9:+111111111
1017+2000000000:+9:+222222222
1018+3000000000:+9:+333333333
1019+4000000000:+9:+444444444
1020+5000000000:+9:+555555555
1021+6000000000:+9:+666666666
1022+7000000000:+9:+777777777
1023+8000000000:+9:+888888888
1024+9000000000:+9:+1000000000
1025+35500000:+113:+314159
1026+71000000:+226:+314159
1027+106500000:+339:+314159
1028+1000000000:+3:+333333333
1029+10:+5:+2
1030+100:+4:+25
1031+1000:+8:+125
1032+10000:+16:+625
1033+999999999999:+9:+111111111111
1034+999999999999:+99:+10101010101
1035+999999999999:+999:+1001001001
1036+999999999999:+9999:+100010001
1037+999999999999999:+99999:+10000100001
58cde26e 1038+1111088889:+99999:+11111
1039-5:-3:1
10404:3:1
10411:3:0
1042-2:-3:0
1043-2:3:-1
10441:-3:-1
1045-5:3:-2
10464:-3:-2
574bacfe 1047123:+inf:0
1048123:-inf:0
748a9306 1049&bmod
1050abc:abc:NaN
1051abc:+1:abc:NaN
1052+1:abc:NaN
1053+0:+0:NaN
1054+0:+1:+0
1055+1:+0:NaN
1056+0:-1:+0
1057-1:+0:NaN
1058+1:+1:+0
1059-1:-1:+0
1060+1:-1:+0
1061-1:+1:+0
1062+1:+2:+1
1063+2:+1:+0
1064+1000000000:+9:+1
1065+2000000000:+9:+2
1066+3000000000:+9:+3
1067+4000000000:+9:+4
1068+5000000000:+9:+5
1069+6000000000:+9:+6
1070+7000000000:+9:+7
1071+8000000000:+9:+8
1072+9000000000:+9:+0
1073+35500000:+113:+33
1074+71000000:+226:+66
1075+106500000:+339:+99
1076+1000000000:+3:+1
1077+10:+5:+0
1078+100:+4:+0
1079+1000:+8:+0
1080+10000:+16:+0
1081+999999999999:+9:+0
1082+999999999999:+99:+0
1083+999999999999:+999:+0
1084+999999999999:+9999:+0
1085+999999999999999:+99999:+0
58cde26e 1086-9:+5:+1
1087+9:-5:-1
1088-9:-5:-4
1089-5:3:1
1090-2:3:1
10914:3:1
10921:3:1
1093-5:-3:-2
1094-2:-3:-2
10954:-3:-2
10961:-3:-2
574bacfe 10974095:4095:0
748a9306 1098&bgcd
1099abc:abc:NaN
1100abc:+0:NaN
1101+0:abc:NaN
1102+0:+0:+0
1103+0:+1:+1
1104+1:+0:+1
1105+1:+1:+1
1106+2:+3:+1
1107+3:+2:+1
58cde26e 1108-3:+2:+1
748a9306 1109+100:+625:+25
1110+4096:+81:+1
58cde26e 1111+1034:+804:+2
1112+27:+90:+56:+1
1113+27:+90:+54:+9
1114&blcm
e16b8f49 1115abc:abc:NaN
58cde26e 1116abc:+0:NaN
1117+0:abc:NaN
1118+0:+0:NaN
1119+1:+0:+0
1120+0:+1:+0
1121+27:+90:+270
1122+1034:+804:+415668
e16b8f49 1123&band
1124abc:abc:NaN
58cde26e 1125abc:0:NaN
11260:abc:NaN
0716bf9b 11271:2:0
11283:2:2
e16b8f49 1129+8:+2:+0
1130+281474976710656:+0:+0
1131+281474976710656:+1:+0
1132+281474976710656:+281474976710656:+281474976710656
574bacfe 1133-2:-3:-4
1134-1:-1:-1
1135-6:-6:-6
1136-7:-4:-8
1137-7:4:0
1138-4:7:4
e16b8f49 1139&bior
1140abc:abc:NaN
58cde26e 1141abc:0:NaN
11420:abc:NaN
0716bf9b 11431:2:3
e16b8f49 1144+8:+2:+10
1145+281474976710656:+0:+281474976710656
1146+281474976710656:+1:+281474976710657
1147+281474976710656:+281474976710656:+281474976710656
574bacfe 1148-2:-3:-1
1149-1:-1:-1
1150-6:-6:-6
1151-7:4:-3
1152-4:7:-1
e16b8f49 1153&bxor
1154abc:abc:NaN
58cde26e 1155abc:0:NaN
11560:abc:NaN
0716bf9b 11571:2:3
e16b8f49 1158+8:+2:+10
1159+281474976710656:+0:+281474976710656
1160+281474976710656:+1:+281474976710657
1161+281474976710656:+281474976710656:+0
574bacfe 1162-2:-3:3
1163-1:-1:0
1164-6:-6:0
1165-7:4:-3
1166-4:7:-5
11674:-7:-3
1168-4:-7:5
e16b8f49 1169&bnot
1170abc:NaN
1171+0:-1
1172+8:-9
1173+281474976710656:-281474976710657
574bacfe 1174-1:0
1175-2:1
1176-12:11
58cde26e 1177&digit
11780:0:0
117912:0:2
118012:1:1
1181123:0:3
1182123:1:2
1183123:2:1
1184123:-1:1
1185123:-2:2
1186123:-3:3
1187123456:0:6
1188123456:1:5
1189123456:2:4
1190123456:3:3
1191123456:4:2
1192123456:5:1
1193123456:-1:1
1194123456:-2:2
1195123456:-3:3
1196100000:-3:0
1197100000:0:0
1198100000:1:0
1199&mantissa
1200abc:NaN
12011e4:1
12022e0:2
1203123:123
1204-1:-1
1205-2:-2
1206&exponent
1207abc:NaN
12081e4:4
12092e0:0
1210123:0
1211-1:0
1212-2:0
12130:1
1214&parts
1215abc:NaN,NaN
12161e4:1,4
12172e0:2,0
1218123:123,0
1219-1:-1,0
1220-2:-2,0
12210:0,1
1222&bpow
0716bf9b 1223abc:12:NaN
122412:abc:NaN
58cde26e 12250:0:1
12260:1:0
12270:2:0
12280:-1:NaN
12290:-2:NaN
12301:0:1
12311:1:1
12321:2:1
12331:3:1
12341:-1:1
12351:-2:1
12361:-3:1
12372:0:1
12382:1:2
12392:2:4
12402:3:8
12413:3:27
12422:-1:NaN
1243-2:-1:NaN
12442:-2:NaN
1245-2:-2:NaN
574bacfe 1246+inf:1234500012:inf
0716bf9b 1247-inf:1234500012:-inf
574bacfe 1248+inf:-12345000123:inf
0716bf9b 1249-inf:-12345000123:-inf
58cde26e 1250# 1 ** -x => 1 / (1 ** x)
1251-1:0:1
1252-2:0:1
1253-1:1:-1
1254-1:2:1
1255-1:3:-1
1256-1:4:1
1257-1:5:-1
1258-1:-1:-1
1259-1:-2:1
1260-1:-3:-1
1261-1:-4:1
126210:2:100
126310:3:1000
126410:4:10000
126510:5:100000
126610:6:1000000
126710:7:10000000
126810:8:100000000
126910:9:1000000000
127010:20:100000000000000000000
1271123456:2:15241383936
1272&length
1273100:3
127410:2
12751:1
12760:1
127712345:5
127810000000000000000:17
1279-123:3
1280&bsqrt
1281144:12
128216:4
12834:2
12842:1
128512:3
1286256:16
1287100000000:10000
12884000000000000:2000000
12891:1
12900:0
1291-2:NaN
1292Nan:NaN
1293&bround
1294$round_mode('trunc')
574bacfe 12950:12:0
1296NaNbround:12:NaN
1297+inf:12:inf
1298-inf:12:-inf
58cde26e 12991234:0:1234
13001234:2:1200
1301123456:4:123400
1302123456:5:123450
1303123456:6:123456
1304+10123456789:5:+10123000000
1305-10123456789:5:-10123000000
1306+10123456789:9:+10123456700
1307-10123456789:9:-10123456700
1308+101234500:6:+101234000
1309-101234500:6:-101234000
1310#+101234500:-4:+101234000
1311#-101234500:-4:-101234000
1312$round_mode('zero')
1313+20123456789:5:+20123000000
1314-20123456789:5:-20123000000
1315+20123456789:9:+20123456800
1316-20123456789:9:-20123456800
1317+201234500:6:+201234000
1318-201234500:6:-201234000
1319#+201234500:-4:+201234000
1320#-201234500:-4:-201234000
1321+12345000:4:12340000
1322-12345000:4:-12340000
1323$round_mode('+inf')
1324+30123456789:5:+30123000000
1325-30123456789:5:-30123000000
1326+30123456789:9:+30123456800
1327-30123456789:9:-30123456800
1328+301234500:6:+301235000
1329-301234500:6:-301234000
1330#+301234500:-4:+301235000
1331#-301234500:-4:-301234000
1332+12345000:4:12350000
1333-12345000:4:-12340000
1334$round_mode('-inf')
1335+40123456789:5:+40123000000
1336-40123456789:5:-40123000000
1337+40123456789:9:+40123456800
1338-40123456789:9:-40123456800
1339+401234500:6:+401234000
1340+401234500:6:+401234000
1341#-401234500:-4:-401235000
1342#-401234500:-4:-401235000
1343+12345000:4:12340000
1344-12345000:4:-12350000
1345$round_mode('odd')
1346+50123456789:5:+50123000000
1347-50123456789:5:-50123000000
1348+50123456789:9:+50123456800
1349-50123456789:9:-50123456800
1350+501234500:6:+501235000
1351-501234500:6:-501235000
1352#+501234500:-4:+501235000
1353#-501234500:-4:-501235000
1354+12345000:4:12350000
1355-12345000:4:-12350000
1356$round_mode('even')
1357+60123456789:5:+60123000000
1358-60123456789:5:-60123000000
1359+60123456789:9:+60123456800
1360-60123456789:9:-60123456800
1361+601234500:6:+601234000
1362-601234500:6:-601234000
1363#+601234500:-4:+601234000
1364#-601234500:-4:-601234000
1365#-601234500:-9:0
1366#-501234500:-9:0
1367#-601234500:-8:0
1368#-501234500:-8:0
1369+1234567:7:1234567
1370+1234567:6:1234570
1371+12345000:4:12340000
1372-12345000:4:-12340000
58cde26e 1373&is_zero
13740:1
1375NaNzero:0
574bacfe 1376+inf:0
1377-inf:0
58cde26e 1378123:0
1379-1:0
13801:0
58cde26e 1381&is_one
13820:0
574bacfe 1383NaNone:0
1384+inf:0
1385-inf:0
58cde26e 13861:1
13872:0
1388-1:0
1389-2:0
1390# floor and ceil tests are pretty pointless in integer space...but play safe
1391&bfloor
13920:0
574bacfe 1393NaNfloor:NaN
1394+inf:inf
1395-inf:-inf
f216259d 1396-1:-1
58cde26e 1397-2:-2
13982:2
13993:3
1400abc:NaN
1401&bceil
574bacfe 1402NaNceil:NaN
1403+inf:inf
1404-inf:-inf
58cde26e 14050:0
1406-1:-1
1407-2:-2
14082:2
14093:3
1410abc:NaN