Math::Big* test tweaks to work better with core:
[p5sagit/p5-mst-13.2.git] / lib / Math / BigInt / t / bigintpm.t
CommitLineData
58cde26e 1#!/usr/bin/perl -w
748a9306 2
c986521f 3BEGIN {
4 $| = 1;
5 if ($ENV{PERL_CORE}) {
6 @INC = qw(../lib);
7 chdir 't' if -d 't';
8 } else {
9 # for running manually with the CPAN distribution
10 unshift @INC, '../lib';
11 }
12 print "# INC = @INC\n";
13}
14
58cde26e 15use strict;
16use Test;
17
18BEGIN
19 {
ee15d750 20 plan tests => 1457;
58cde26e 21 }
c986521f 22
ee15d750 23my $version = '1.43'; # for $VERSION tests, match current release (by hand!)
58cde26e 24
25##############################################################################
26# for testing inheritance of _swap
27
28package Math::Foo;
29
30use Math::BigInt;
574bacfe 31#use Math::BigInt lib => 'BitVect'; # for testing
58cde26e 32use vars qw/@ISA/;
33@ISA = (qw/Math::BigInt/);
34
35use overload
36# customized overload for sub, since original does not use swap there
37'-' => sub { my @a = ref($_[0])->_swap(@_);
38 $a[0]->bsub($a[1])};
39
40sub _swap
41 {
42 # a fake _swap, which reverses the params
43 my $self = shift; # for override in subclass
44 if ($_[2])
45 {
46 my $c = ref ($_[0] ) || 'Math::Foo';
47 return ( $_[0]->copy(), $_[1] );
48 }
49 else
50 {
51 return ( Math::Foo->new($_[1]), $_[0] );
52 }
53 }
54
55##############################################################################
56package main;
d1f8c7a4 57
748a9306 58use Math::BigInt;
0716bf9b 59#use Math::BigInt lib => 'BitVect'; # for testing
0716bf9b 60
574bacfe 61my $CALC = Math::BigInt::_core_lib(); ok ($CALC,'Math::BigInt::Calc');
748a9306 62
58cde26e 63my (@args,$f,$try,$x,$y,$z,$a,$exp,$ans,$ans1,@a,$m,$e,$round_mode);
64
65while (<DATA>)
66 {
67 chop;
68 next if /^#/; # skip comments
69 if (s/^&//)
70 {
71 $f = $_;
72 }
73 elsif (/^\$/)
74 {
75 $round_mode = $_;
76 $round_mode =~ s/^\$/Math::BigInt->/;
77 # print "$round_mode\n";
78 }
79 else
80 {
81 @args = split(/:/,$_,99);
82 $ans = pop(@args);
83 $try = "\$x = Math::BigInt->new(\"$args[0]\");";
84 if ($f eq "bnorm"){
ee15d750 85 $try = "\$x = Math::BigInt::bnorm(\"$args[0]\");";
58cde26e 86 } elsif ($f eq "is_zero") {
ee15d750 87 $try .= '$x->is_zero();';
58cde26e 88 } elsif ($f eq "is_one") {
ee15d750 89 $try .= '$x->is_one();';
58cde26e 90 } elsif ($f eq "is_odd") {
ee15d750 91 $try .= '$x->is_odd();';
58cde26e 92 } elsif ($f eq "is_even") {
ee15d750 93 $try .= '$x->is_even();';
574bacfe 94 } elsif ($f eq "is_negative") {
ee15d750 95 $try .= '$x->is_negative();';
574bacfe 96 } elsif ($f eq "is_positive") {
ee15d750 97 $try .= '$x->is_positive();';
bd05a461 98 } elsif ($f eq "as_hex") {
99 $try .= '$x->as_hex();';
100 } elsif ($f eq "as_bin") {
101 $try .= '$x->as_bin();';
0716bf9b 102 } elsif ($f eq "is_inf") {
ee15d750 103 $try .= "\$x->is_inf('$args[1]');";
58cde26e 104 } elsif ($f eq "binf") {
105 $try .= "\$x->binf('$args[1]');";
574bacfe 106 } elsif ($f eq "bone") {
107 $try .= "\$x->bone('$args[1]');";
108 } elsif ($f eq "bnan") {
109 $try .= "\$x->bnan();";
58cde26e 110 } elsif ($f eq "bfloor") {
111 $try .= '$x->bfloor();';
112 } elsif ($f eq "bceil") {
113 $try .= '$x->bceil();';
58cde26e 114 } elsif ($f eq "bsstr") {
115 $try .= '$x->bsstr();';
116 } elsif ($f eq "bneg") {
574bacfe 117 $try .= '$x->bneg();';
58cde26e 118 } elsif ($f eq "babs") {
574bacfe 119 $try .= '$x->babs();';
58cde26e 120 } elsif ($f eq "binc") {
121 $try .= '++$x;';
122 } elsif ($f eq "bdec") {
123 $try .= '--$x;';
124 }elsif ($f eq "bnot") {
125 $try .= '~$x;';
126 }elsif ($f eq "bsqrt") {
127 $try .= '$x->bsqrt();';
128 }elsif ($f eq "length") {
ee15d750 129 $try .= '$x->length();';
58cde26e 130 }elsif ($f eq "exponent"){
ee15d750 131 # ->bstr() to see if a BigInt is returned
58cde26e 132 $try .= '$x = $x->exponent()->bstr();';
133 }elsif ($f eq "mantissa"){
ee15d750 134 # ->bstr() to see if a BigInt is returned
58cde26e 135 $try .= '$x = $x->mantissa()->bstr();';
136 }elsif ($f eq "parts"){
ee15d750 137 $try .= '($m,$e) = $x->parts();';
138 # ->bstr() to see if a BigInt is returned
58cde26e 139 $try .= '$m = $m->bstr(); $m = "NaN" if !defined $m;';
140 $try .= '$e = $e->bstr(); $e = "NaN" if !defined $e;';
141 $try .= '"$m,$e";';
142 } else {
0716bf9b 143 $try .= "\$y = new Math::BigInt ('$args[1]');";
58cde26e 144 if ($f eq "bcmp"){
145 $try .= '$x <=> $y;';
0716bf9b 146 }elsif ($f eq "bround") {
147 $try .= "$round_mode; \$x->bround(\$y);";
58cde26e 148 }elsif ($f eq "bacmp"){
ee15d750 149 $try .= '$x->bacmp($y);';
58cde26e 150 }elsif ($f eq "badd"){
ee15d750 151 $try .= '$x + $y;';
58cde26e 152 }elsif ($f eq "bsub"){
ee15d750 153 $try .= '$x - $y;';
58cde26e 154 }elsif ($f eq "bmul"){
ee15d750 155 $try .= '$x * $y;';
58cde26e 156 }elsif ($f eq "bdiv"){
ee15d750 157 $try .= '$x / $y;';
574bacfe 158 }elsif ($f eq "bdiv-list"){
159 $try .= 'join (",",$x->bdiv($y));';
58cde26e 160 }elsif ($f eq "bmod"){
ee15d750 161 $try .= '$x % $y;';
58cde26e 162 }elsif ($f eq "bgcd")
163 {
164 if (defined $args[2])
165 {
166 $try .= " \$z = new Math::BigInt \"$args[2]\"; ";
167 }
168 $try .= "Math::BigInt::bgcd(\$x, \$y";
169 $try .= ", \$z" if (defined $args[2]);
170 $try .= " );";
171 }
172 elsif ($f eq "blcm")
173 {
174 if (defined $args[2])
175 {
176 $try .= " \$z = new Math::BigInt \"$args[2]\"; ";
177 }
178 $try .= "Math::BigInt::blcm(\$x, \$y";
179 $try .= ", \$z" if (defined $args[2]);
180 $try .= " );";
181 }elsif ($f eq "blsft"){
182 if (defined $args[2])
183 {
184 $try .= "\$x->blsft(\$y,$args[2]);";
185 }
186 else
187 {
188 $try .= "\$x << \$y;";
189 }
190 }elsif ($f eq "brsft"){
191 if (defined $args[2])
192 {
193 $try .= "\$x->brsft(\$y,$args[2]);";
194 }
195 else
196 {
197 $try .= "\$x >> \$y;";
198 }
199 }elsif ($f eq "band"){
200 $try .= "\$x & \$y;";
201 }elsif ($f eq "bior"){
202 $try .= "\$x | \$y;";
203 }elsif ($f eq "bxor"){
204 $try .= "\$x ^ \$y;";
205 }elsif ($f eq "bpow"){
206 $try .= "\$x ** \$y;";
207 }elsif ($f eq "digit"){
208 $try = "\$x = Math::BigInt->new(\"$args[0]\"); \$x->digit($args[1]);";
209 } else { warn "Unknown op '$f'"; }
210 }
211 # print "trying $try\n";
212 $ans1 = eval $try;
213 $ans =~ s/^[+]([0-9])/$1/; # remove leading '+'
214 if ($ans eq "")
215 {
216 ok_undef ($ans1);
217 }
218 else
219 {
ee15d750 220 # print "try: $try ans: $ans1 $ans\n";
58cde26e 221 print "# Tried: '$try'\n" if !ok ($ans1, $ans);
222 }
223 # check internal state of number objects
0716bf9b 224 is_valid($ans1,$f) if ref $ans1;
58cde26e 225 }
226 } # endwhile data tests
227close DATA;
228
574bacfe 229# XXX Tels 06/29/2001 following tests never fail or do not work :( !?
0716bf9b 230
231# test whether use Math::BigInt qw/version/ works
232$try = "use Math::BigInt ($version.'1');";
233$try .= ' $x = Math::BigInt->new(123); $x = "$x";';
58cde26e 234$ans1 = eval $try;
0716bf9b 235ok_undef ( $_ ); # should result in error!
58cde26e 236
0716bf9b 237# test whether constant works or not, also test for qw($version)
238$try = "use Math::BigInt ($version,'babs',':constant');";
239$try .= ' $x = 2**150; babs($x); $x = "$x";';
240$ans1 = eval $try;
58cde26e 241ok ( $ans1, "1427247692705959881058285969449495136382746624");
242
0716bf9b 243# test wether Math::BigInt::Small via use works (w/ dff. spellings of calc)
574bacfe 244#$try = "use Math::BigInt ($version,'lib','Small');";
0716bf9b 245#$try .= ' $x = 2**10; $x = "$x";';
246#$ans1 = eval $try;
247#ok ( $ans1, "1024");
574bacfe 248#$try = "use Math::BigInt ($version,'LiB','Math::BigInt::Small');";
0716bf9b 249#$try .= ' $x = 2**10; $x = "$x";';
250#$ans1 = eval $try;
251#ok ( $ans1, "1024");
252# test wether calc => undef (array element not existing) works
574bacfe 253#$try = "use Math::BigInt ($version,'LIB');";
0716bf9b 254#$try = "require Math::BigInt; Math::BigInt::import($version,'CALC');";
255#$try .= ' $x = Math::BigInt->new(2)**10; $x = "$x";';
256#$ans1 = eval $try;
257#ok ( $ans1, 1024);
258
574bacfe 259# test whether fallback to calc works
260$try = "use Math::BigInt ($version,'lib','foo, bar , ');";
261$try .= ' Math::BigInt::_core_lib();';
262$ans1 = eval $try;
263ok ( $ans1, "Math::BigInt::Calc");
264
58cde26e 265# test some more
266@a = ();
267for (my $i = 1; $i < 10; $i++)
268 {
269 push @a, $i;
270 }
271ok "@a", "1 2 3 4 5 6 7 8 9";
272
574bacfe 273# test whether self-multiplication works correctly (result is 2**64)
58cde26e 274$try = '$x = new Math::BigInt "+4294967296";';
275$try .= '$a = $x->bmul($x);';
276$ans1 = eval $try;
277print "# Tried: '$try'\n" if !ok ($ans1, Math::BigInt->new(2) ** 64);
574bacfe 278# test self-pow
279$try = '$x = Math::BigInt->new(10);';
280$try .= '$a = $x->bpow($x);';
281$ans1 = eval $try;
282print "# Tried: '$try'\n" if !ok ($ans1, Math::BigInt->new(10) ** 10);
58cde26e 283
0716bf9b 284# test whether op destroys args or not (should better not)
58cde26e 285
286$x = new Math::BigInt (3);
287$y = new Math::BigInt (4);
288$z = $x & $y;
289ok ($x,3);
290ok ($y,4);
291ok ($z,0);
292$z = $x | $y;
293ok ($x,3);
294ok ($y,4);
295ok ($z,7);
296$x = new Math::BigInt (1);
297$y = new Math::BigInt (2);
298$z = $x | $y;
299ok ($x,1);
300ok ($y,2);
301ok ($z,3);
302
303$x = new Math::BigInt (5);
304$y = new Math::BigInt (4);
305$z = $x ^ $y;
306ok ($x,5);
307ok ($y,4);
308ok ($z,1);
309
310$x = new Math::BigInt (-5); $y = -$x;
311ok ($x, -5);
312
313$x = new Math::BigInt (-5); $y = abs($x);
314ok ($x, -5);
315
316# check whether overloading cmp works
317$try = "\$x = Math::BigInt->new(0);";
318$try .= "\$y = 10;";
319$try .= "'false' if \$x ne \$y;";
320$ans = eval $try;
321print "# For '$try'\n" if (!ok "$ans" , "false" );
322
323# we cant test for working cmpt with other objects here, we would need a dummy
324# object with stringify overload for this. see Math::String tests
325
326###############################################################################
327# check shortcuts
328$try = "\$x = Math::BigInt->new(1); \$x += 9;";
329$try .= "'ok' if \$x == 10;";
330$ans = eval $try;
331print "# For '$try'\n" if (!ok "$ans" , "ok" );
332
333$try = "\$x = Math::BigInt->new(1); \$x -= 9;";
334$try .= "'ok' if \$x == -8;";
335$ans = eval $try;
336print "# For '$try'\n" if (!ok "$ans" , "ok" );
337
338$try = "\$x = Math::BigInt->new(1); \$x *= 9;";
339$try .= "'ok' if \$x == 9;";
340$ans = eval $try;
341print "# For '$try'\n" if (!ok "$ans" , "ok" );
342
343$try = "\$x = Math::BigInt->new(10); \$x /= 2;";
344$try .= "'ok' if \$x == 5;";
345$ans = eval $try;
346print "# For '$try'\n" if (!ok "$ans" , "ok" );
347
348###############################################################################
349# check reversed order of arguments
350$try = "\$x = Math::BigInt->new(10); \$x = 2 ** \$x;";
351$try .= "'ok' if \$x == 1024;"; $ans = eval $try;
352print "# For '$try'\n" if (!ok "$ans" , "ok" );
353
354$try = "\$x = Math::BigInt->new(10); \$x = 2 * \$x;";
355$try .= "'ok' if \$x == 20;"; $ans = eval $try;
356print "# For '$try'\n" if (!ok "$ans" , "ok" );
357
358$try = "\$x = Math::BigInt->new(10); \$x = 2 + \$x;";
359$try .= "'ok' if \$x == 12;"; $ans = eval $try;
360print "# For '$try'\n" if (!ok "$ans" , "ok" );
361
362$try = "\$x = Math::BigInt->new(10); \$x = 2 - \$x;";
363$try .= "'ok' if \$x == -8;"; $ans = eval $try;
364print "# For '$try'\n" if (!ok "$ans" , "ok" );
365
366$try = "\$x = Math::BigInt->new(10); \$x = 20 / \$x;";
367$try .= "'ok' if \$x == 2;"; $ans = eval $try;
368print "# For '$try'\n" if (!ok "$ans" , "ok" );
369
370###############################################################################
371# check badd(4,5) form
372
373$try = "\$x = Math::BigInt::badd(4,5);";
374$try .= "'ok' if \$x == 9;";
375$ans = eval $try;
376print "# For '$try'\n" if (!ok "$ans" , "ok" );
377
378$try = "\$x = Math::BigInt->badd(4,5);";
379$try .= "'ok' if \$x == 9;";
380$ans = eval $try;
381print "# For '$try'\n" if (!ok "$ans" , "ok" );
382
383###############################################################################
574bacfe 384# the followin tests only make sense with Math::BigInt::Calc
385
386###############################################################################
58cde26e 387# check proper length of internal arrays
388
0716bf9b 389$x = Math::BigInt->new(99999); is_valid($x);
390$x += 1; ok ($x,100000); is_valid($x);
391$x -= 1; ok ($x,99999); is_valid($x);
58cde26e 392
393###############################################################################
574bacfe 394# check numify
58cde26e 395
0716bf9b 396my $BASE = int(1e5); # should access Math::BigInt::Calc::BASE
58cde26e 397$x = Math::BigInt->new($BASE-1); ok ($x->numify(),$BASE-1);
398$x = Math::BigInt->new(-($BASE-1)); ok ($x->numify(),-($BASE-1));
399$x = Math::BigInt->new($BASE); ok ($x->numify(),$BASE);
400$x = Math::BigInt->new(-$BASE); ok ($x->numify(),-$BASE);
401$x = Math::BigInt->new( -($BASE*$BASE*1+$BASE*1+1) );
402ok($x->numify(),-($BASE*$BASE*1+$BASE*1+1));
403
404###############################################################################
405# test bug in _digits with length($c[-1]) where $c[-1] was "00001" instead of 1
406
407$x = Math::BigInt->new(99998); $x++; $x++; $x++; $x++;
408if ($x > 100000) { ok (1,1) } else { ok ("$x < 100000","$x > 100000"); }
409
410$x = Math::BigInt->new(100003); $x++;
411$y = Math::BigInt->new(1000000);
412if ($x < 1000000) { ok (1,1) } else { ok ("$x > 1000000","$x < 1000000"); }
413
414###############################################################################
415# bug in sub where number with at least 6 trailing zeros after any op failed
416
417$x = Math::BigInt->new(123456); $z = Math::BigInt->new(10000); $z *= 10;
418$x -= $z;
419ok ($z, 100000);
420ok ($x, 23456);
421
422###############################################################################
574bacfe 423# bug in shortcut in mul()
424
425# construct a number with a zero-hole of BASE_LEN
426my $bl = Math::BigInt::Calc::_base_len();
427$x = '1' x $bl . '0' x $bl . '1' x $bl . '0' x $bl;
428$y = '1' x (2*$bl);
429#print "$x * $y\n";
430$x = Math::BigInt->new($x)->bmul($y);
431# result is 123..$bl . $bl x (3*bl-1) . $bl...321 . '0' x $bl
432$y = ''; my $d = '';
433for (my $i = 1; $i <= $bl; $i++)
434 {
435 $y .= $i; $d = $i.$d;
436 }
437#print "$y $d\n";
438$y .= $bl x (3*$bl-1) . $d . '0' x $bl;
439ok ($x,$y);
440
441###############################################################################
58cde26e 442# bug with rest "-0" in div, causing further div()s to fail
443
0716bf9b 444$x = Math::BigInt->new('-322056000'); ($x,$y) = $x->bdiv('-12882240');
58cde26e 445
0716bf9b 446ok ($y,'0','not -0'); # not '-0'
58cde26e 447is_valid($y);
448
449###############################################################################
450# check undefs: NOT DONE YET
451
452###############################################################################
453# bool
454
455$x = Math::BigInt->new(1); if ($x) { ok (1,1); } else { ok($x,'to be true') }
456$x = Math::BigInt->new(0); if (!$x) { ok (1,1); } else { ok($x,'to be false') }
457
458###############################################################################
459# objectify()
460
461@args = Math::BigInt::objectify(2,4,5);
462ok (scalar @args,3); # 'Math::BigInt', 4, 5
463ok ($args[0],'Math::BigInt');
464ok ($args[1],4);
465ok ($args[2],5);
466
467@args = Math::BigInt::objectify(0,4,5);
468ok (scalar @args,3); # 'Math::BigInt', 4, 5
469ok ($args[0],'Math::BigInt');
470ok ($args[1],4);
471ok ($args[2],5);
472
473@args = Math::BigInt::objectify(2,4,5);
474ok (scalar @args,3); # 'Math::BigInt', 4, 5
475ok ($args[0],'Math::BigInt');
476ok ($args[1],4);
477ok ($args[2],5);
478
479@args = Math::BigInt::objectify(2,4,5,6,7);
480ok (scalar @args,5); # 'Math::BigInt', 4, 5, 6, 7
481ok ($args[0],'Math::BigInt');
482ok ($args[1],4); ok (ref($args[1]),$args[0]);
483ok ($args[2],5); ok (ref($args[2]),$args[0]);
484ok ($args[3],6); ok (ref($args[3]),'');
485ok ($args[4],7); ok (ref($args[4]),'');
486
487@args = Math::BigInt::objectify(2,'Math::BigInt',4,5,6,7);
488ok (scalar @args,5); # 'Math::BigInt', 4, 5, 6, 7
489ok ($args[0],'Math::BigInt');
490ok ($args[1],4); ok (ref($args[1]),$args[0]);
491ok ($args[2],5); ok (ref($args[2]),$args[0]);
492ok ($args[3],6); ok (ref($args[3]),'');
493ok ($args[4],7); ok (ref($args[4]),'');
494
495###############################################################################
b22b3e31 496# test for floating-point input (other tests in bnorm() below)
58cde26e 497
498$z = 1050000000000000; # may be int on systems with 64bit?
ee15d750 499$x = Math::BigInt->new($z); ok ($x->bsstr(),'105e+13'); # not 1.05e+15
0716bf9b 500$z = 1e+129; # definitely a float (may fail on UTS)
ee15d750 501# don't compare to $z, since some Perl versions stringify $z into something
502# like '1.e+129' or something equally ugly
503$x = Math::BigInt->new($z); ok ($x->bsstr(),'1e+129');
58cde26e 504
505###############################################################################
506# prime number tests, also test for **= and length()
507# found on: http://www.utm.edu/research/primes/notes/by_year.html
508
509# ((2^148)-1)/17
510$x = Math::BigInt->new(2); $x **= 148; $x++; $x = $x / 17;
511ok ($x,"20988936657440586486151264256610222593863921");
512ok ($x->length(),length "20988936657440586486151264256610222593863921");
513
514# MM7 = 2^127-1
515$x = Math::BigInt->new(2); $x **= 127; $x--;
516ok ($x,"170141183460469231731687303715884105727");
517
bd05a461 518$x = Math::BigInt->new('215960156869840440586892398248');
519($x,$y) = $x->length();
520ok ($x,30); ok ($y,0);
521
522$x = Math::BigInt->new('1_000_000_000_000');
523($x,$y) = $x->length();
524ok ($x,13); ok ($y,0);
525
58cde26e 526# I am afraid the following is not yet possible due to slowness
527# Also, testing for 2 meg output is a bit hard ;)
528#$x = new Math::BigInt(2); $x **= 6972593; $x--;
529
0716bf9b 530# 593573509*2^332162+1 has exactly 1,000,000 digits
b22b3e31 531# takes about 24 mins on 300 Mhz, so cannot be done yet ;)
58cde26e 532#$x = Math::BigInt->new(2); $x **= 332162; $x *= "593573509"; $x++;
0716bf9b 533#ok ($x->length(),1_000_000);
58cde26e 534
535###############################################################################
536# inheritance and overriding of _swap
537
538$x = Math::Foo->new(5);
539$x = $x - 8; # 8 - 5 instead of 5-8
540ok ($x,3);
541ok (ref($x),'Math::Foo');
542
543$x = Math::Foo->new(5);
544$x = 8 - $x; # 5 - 8 instead of 8 - 5
545ok ($x,-3);
546ok (ref($x),'Math::Foo');
547
548###############################################################################
17baacb7 549# Test whether +inf eq inf
550# This tried to test whether BigInt inf equals Perl inf. Unfortunately, Perl
551# hasn't (before 5.7.3 at least) a consistent way to say inf, and some things
ee15d750 552# like 1e100000 crash on some platforms. So simple test for the string 'inf'
17baacb7 553$x = Math::BigInt->new('+inf'); ok ($x,'inf');
574bacfe 554
ee15d750 555### all tests done ############################################################
58cde26e 556
58cde26e 557###############################################################################
558# Perl 5.005 does not like ok ($x,undef)
559
560sub ok_undef
561 {
562 my $x = shift;
563
564 ok (1,1) and return if !defined $x;
565 ok ($x,'undef');
566 }
567
568###############################################################################
569# sub to check validity of a BigInt internally, to ensure that no op leaves a
570# number object in an invalid state (f.i. "-0")
571
572sub is_valid
573 {
0716bf9b 574 my ($x,$f) = @_;
58cde26e 575
0716bf9b 576 my $e = 0; # error?
58cde26e 577 # ok as reference?
0716bf9b 578 $e = 'Not a reference to Math::BigInt' if !ref($x);
58cde26e 579
580 # has ok sign?
0716bf9b 581 $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'"
582 if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/;
58cde26e 583
0716bf9b 584 $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0;
585 $e = $CALC->_check($x->{value}) if $e eq '0';
58cde26e 586
0716bf9b 587 # test done, see if error did crop up
588 ok (1,1), return if ($e eq '0');
58cde26e 589
0716bf9b 590 ok (1,$e." op '$f'");
a99f8827 591 }
58cde26e 592
748a9306 593__END__
574bacfe 594&is_negative
5950:0
596-1:1
5971:0
598+inf:0
599-inf:1
600NaNneg:0
601&is_positive
6020:1
603-1:0
6041:1
605+inf:1
606-inf:0
607NaNneg:0
0716bf9b 608&is_odd
609abc:0
6100:0
6111:1
6123:1
613-1:1
614-3:1
61510000001:1
61610000002:0
6172:0
618&is_even
619abc:0
6200:1
6211:0
6223:0
623-1:0
624-3:0
62510000001:0
62610000002:1
6272:1
628&bacmp
629+0:-0:0
630+0:+1:-1
631-1:+1:0
632+1:-1:0
633-1:+2:-1
634+2:-1:1
635-123456789:+987654321:-1
636+123456789:-987654321:-1
637+987654321:+123456789:1
638-987654321:+123456789:1
639-123:+4567889:-1
574bacfe 640# NaNs
641acmpNaN:123:
642123:acmpNaN:
643acmpNaN:acmpNaN:
644# infinity
645+inf:+inf:0
646-inf:-inf:0
647+inf:-inf:0
648-inf:+inf:0
649+inf:123:1
650-inf:123:1
651+inf:-123:1
652-inf:-123:1
653# return undef
654+inf:NaN:
655NaN:inf:
656-inf:NaN:
657NaN:-inf:
748a9306 658&bnorm
0716bf9b 659123:123
58cde26e 660# binary input
6610babc:NaN
6620b123:NaN
6630b0:0
664-0b0:0
665-0b1:-1
6660b0001:1
6670b001:1
6680b011:3
6690b101:5
6700b1000000000000000000000000000000:1073741824
574bacfe 6710b_101:NaN
6720b1_0_1:5
58cde26e 673# hex input
674-0x0:0
6750xabcdefgh:NaN
6760x1234:4660
6770xabcdef:11259375
678-0xABCDEF:-11259375
679-0x1234:-4660
6800x12345678:305419896
574bacfe 6810x1_2_3_4_56_78:305419896
6820x_123:NaN
58cde26e 683# inf input
ee15d750 684inf:inf
574bacfe 685+inf:inf
58cde26e 686-inf:-inf
6870inf:NaN
688# normal input
689:NaN
748a9306 690abc:NaN
691 1 a:NaN
6921bcd2:NaN
69311111b:NaN
694+1z:NaN
695-1z:NaN
58cde26e 6960:0
697+0:0
698+00:0
699+000:0
700000000000000000000:0
701-0:0
702-0000:0
703+1:1
704+01:1
705+001:1
706+00000100000:100000
707123456789:123456789
748a9306 708-1:-1
709-01:-1
710-001:-1
711-123456789:-123456789
712-00000100000:-100000
58cde26e 7131_2_3:123
714_123:NaN
715_123_:NaN
716_123_:NaN
7171__23:NaN
71810000000000E-1_0:1
7191E2:100
7201E1:10
7211E0:1
722E1:NaN
723E23:NaN
7241.23E2:123
7251.23E1:NaN
7261.23E-1:NaN
727100E-1:10
728# floating point input
7291.01E2:101
7301010E-1:101
731-1010E0:-1010
732-1010E1:-10100
733-1010E-2:NaN
734-1.01E+1:NaN
735-1.01E-1:NaN
574bacfe 7361234.00:1234
737&bnan
7381:NaN
7392:NaN
740abc:NaN
741&bone
7422:+:+1
7432:-:-1
744boneNaN:-:-1
745boneNaN:+:+1
7462:abc:+1
7473::+1
58cde26e 748&binf
574bacfe 7491:+:inf
58cde26e 7502:-:-inf
574bacfe 7513:abc:inf
58cde26e 752&is_inf
753+inf::1
754-inf::1
755abc::0
7561::0
757NaN::0
758-1::0
759+inf:-:0
760+inf:+:1
761-inf:-:1
762-inf:+:0
0716bf9b 763# it must be exactly /^[+-]inf$/
764+infinity::0
765-infinity::0
58cde26e 766&blsft
767abc:abc:NaN
768+2:+2:+8
769+1:+32:+4294967296
770+1:+48:+281474976710656
771+8:-2:NaN
772# excercise base 10
773+12345:4:10:123450000
774-1234:0:10:-1234
775+1234:0:10:+1234
776+2:2:10:200
777+12:2:10:1200
778+1234:-3:10:NaN
7791234567890123:12:10:1234567890123000000000000
780&brsft
781abc:abc:NaN
782+8:+2:+2
783+4294967296:+32:+1
784+281474976710656:+48:+1
785+2:-2:NaN
786# excercise base 10
787-1234:0:10:-1234
788+1234:0:10:+1234
789+200:2:10:2
790+1234:3:10:1
791+1234:2:10:12
792+1234:-3:10:NaN
793310000:4:10:31
79412300000:5:10:123
7951230000000000:10:10:123
79609876123456789067890:12:10:9876123
7971234561234567890123:13:10:123456
798&bsstr
7991e+34:1e+34
800123.456E3:123456e+0
801100:1e+2
802abc:NaN
748a9306 803&bneg
574bacfe 804bnegNaN:NaN
805+inf:-inf
806-inf:inf
748a9306 807abd:NaN
808+0:+0
809+1:-1
810-1:+1
811+123456789:-123456789
812-123456789:+123456789
813&babs
574bacfe 814babsNaN:NaN
815+inf:inf
816-inf:inf
748a9306 817+0:+0
818+1:+1
819-1:+1
820+123456789:+123456789
821-123456789:+123456789
822&bcmp
574bacfe 823bcmpNaN:bcmpNaN:
824bcmpNaN:+0:
825+0:bcmpNaN:
5d7098d5 826+0:+0:0
748a9306 827-1:+0:-1
5d7098d5 828+0:-1:1
829+1:+0:1
748a9306 830+0:+1:-1
831-1:+1:-1
5d7098d5 832+1:-1:1
833-1:-1:0
834+1:+1:0
835+123:+123:0
836+123:+12:1
748a9306 837+12:+123:-1
5d7098d5 838-123:-123:0
748a9306 839-123:-12:-1
5d7098d5 840-12:-123:1
748a9306 841+123:+124:-1
5d7098d5 842+124:+123:1
843-123:-124:1
748a9306 844-124:-123:-1
5d7098d5 845+100:+5:1
58cde26e 846-123456789:+987654321:-1
847+123456789:-987654321:1
848-987654321:+123456789:-1
0716bf9b 849-inf:5432112345:-1
850+inf:5432112345:1
851-inf:-5432112345:-1
852+inf:-5432112345:1
853+inf:+inf:0
854-inf:-inf:0
574bacfe 855+inf:-inf:1
856-inf:+inf:-1
0716bf9b 857# return undef
858+inf:NaN:
574bacfe 859NaN:inf:
0716bf9b 860-inf:NaN:
861NaN:-inf:
58cde26e 862&binc
863abc:NaN
574bacfe 864+inf:inf
865-inf:-inf
58cde26e 866+0:+1
867+1:+2
868-1:+0
869&bdec
870abc:NaN
574bacfe 871+inf:inf
872-inf:-inf
58cde26e 873+0:-1
874+1:+0
875-1:-2
748a9306 876&badd
877abc:abc:NaN
878abc:+0:NaN
879+0:abc:NaN
574bacfe 880+inf:-inf:0
881-inf:+inf:0
882+inf:+inf:inf
883-inf:-inf:-inf
884baddNaN:+inf:NaN
885baddNaN:+inf:NaN
886+inf:baddNaN:NaN
887-inf:baddNaN:NaN
748a9306 888+0:+0:+0
889+1:+0:+1
890+0:+1:+1
891+1:+1:+2
892-1:+0:-1
893+0:-1:-1
894-1:-1:-2
895-1:+1:+0
896+1:-1:+0
897+9:+1:+10
898+99:+1:+100
899+999:+1:+1000
900+9999:+1:+10000
901+99999:+1:+100000
902+999999:+1:+1000000
903+9999999:+1:+10000000
904+99999999:+1:+100000000
905+999999999:+1:+1000000000
906+9999999999:+1:+10000000000
907+99999999999:+1:+100000000000
908+10:-1:+9
909+100:-1:+99
910+1000:-1:+999
911+10000:-1:+9999
912+100000:-1:+99999
913+1000000:-1:+999999
914+10000000:-1:+9999999
915+100000000:-1:+99999999
916+1000000000:-1:+999999999
917+10000000000:-1:+9999999999
918+123456789:+987654321:+1111111110
919-123456789:+987654321:+864197532
920-123456789:-987654321:-1111111110
921+123456789:-987654321:-864197532
922&bsub
923abc:abc:NaN
924abc:+0:NaN
925+0:abc:NaN
574bacfe 926+inf:-inf:inf
927-inf:+inf:-inf
928+inf:+inf:0
929-inf:-inf:0
748a9306 930+0:+0:+0
931+1:+0:+1
932+0:+1:-1
933+1:+1:+0
934-1:+0:-1
935+0:-1:+1
936-1:-1:+0
937-1:+1:-2
938+1:-1:+2
939+9:+1:+8
940+99:+1:+98
941+999:+1:+998
942+9999:+1:+9998
943+99999:+1:+99998
944+999999:+1:+999998
945+9999999:+1:+9999998
946+99999999:+1:+99999998
947+999999999:+1:+999999998
948+9999999999:+1:+9999999998
949+99999999999:+1:+99999999998
950+10:-1:+11
951+100:-1:+101
952+1000:-1:+1001
953+10000:-1:+10001
954+100000:-1:+100001
955+1000000:-1:+1000001
956+10000000:-1:+10000001
957+100000000:-1:+100000001
958+1000000000:-1:+1000000001
959+10000000000:-1:+10000000001
960+123456789:+987654321:-864197532
961-123456789:+987654321:-1111111110
962-123456789:-987654321:+864197532
963+123456789:-987654321:+1111111110
964&bmul
965abc:abc:NaN
966abc:+0:NaN
967+0:abc:NaN
574bacfe 968NaNmul:+inf:NaN
969NaNmul:-inf:NaN
970-inf:NaNmul:NaN
971+inf:NaNmul:NaN
972+inf:+inf:inf
973+inf:-inf:-inf
974-inf:+inf:-inf
975-inf:-inf:inf
748a9306 976+0:+0:+0
977+0:+1:+0
978+1:+0:+0
979+0:-1:+0
980-1:+0:+0
981+123456789123456789:+0:+0
982+0:+123456789123456789:+0
983-1:-1:+1
984-1:+1:-1
985+1:-1:-1
986+1:+1:+1
987+2:+3:+6
988-2:+3:-6
989+2:-3:-6
990-2:-3:+6
991+111:+111:+12321
992+10101:+10101:+102030201
993+1001001:+1001001:+1002003002001
994+100010001:+100010001:+10002000300020001
995+10000100001:+10000100001:+100002000030000200001
996+11111111111:+9:+99999999999
997+22222222222:+9:+199999999998
998+33333333333:+9:+299999999997
999+44444444444:+9:+399999999996
1000+55555555555:+9:+499999999995
1001+66666666666:+9:+599999999994
1002+77777777777:+9:+699999999993
1003+88888888888:+9:+799999999992
1004+99999999999:+9:+899999999991
58cde26e 1005+25:+25:+625
1006+12345:+12345:+152399025
1007+99999:+11111:+1111088889
574bacfe 1008&bdiv-list
1009100:20:5,0
10104095:4095:1,0
1011-4095:-4095:1,0
10124095:-4095:-1,0
1013-4095:4095:-1,0
748a9306 1014&bdiv
1015abc:abc:NaN
1016abc:+1:abc:NaN
1017+1:abc:NaN
1018+0:+0:NaN
574bacfe 1019+5:0:inf
1020-5:0:-inf
1021+1:+0:inf
748a9306 1022+0:+1:+0
748a9306 1023+0:-1:+0
574bacfe 1024-1:+0:-inf
748a9306 1025+1:+1:+1
1026-1:-1:+1
1027+1:-1:-1
1028-1:+1:-1
1029+1:+2:+0
1030+2:+1:+2
58cde26e 1031+1:+26:+0
748a9306 1032+1000000000:+9:+111111111
1033+2000000000:+9:+222222222
1034+3000000000:+9:+333333333
1035+4000000000:+9:+444444444
1036+5000000000:+9:+555555555
1037+6000000000:+9:+666666666
1038+7000000000:+9:+777777777
1039+8000000000:+9:+888888888
1040+9000000000:+9:+1000000000
1041+35500000:+113:+314159
1042+71000000:+226:+314159
1043+106500000:+339:+314159
1044+1000000000:+3:+333333333
1045+10:+5:+2
1046+100:+4:+25
1047+1000:+8:+125
1048+10000:+16:+625
1049+999999999999:+9:+111111111111
1050+999999999999:+99:+10101010101
1051+999999999999:+999:+1001001001
1052+999999999999:+9999:+100010001
1053+999999999999999:+99999:+10000100001
58cde26e 1054+1111088889:+99999:+11111
1055-5:-3:1
10564:3:1
10571:3:0
1058-2:-3:0
1059-2:3:-1
10601:-3:-1
1061-5:3:-2
10624:-3:-2
574bacfe 1063123:+inf:0
1064123:-inf:0
ee15d750 106510000000000000000000000000000000000000000000000000000000000000000000000000000000000:10000000375084540248994272022843165711074:999999962491547381984643365663244474111576
748a9306 1066&bmod
1067abc:abc:NaN
1068abc:+1:abc:NaN
1069+1:abc:NaN
1070+0:+0:NaN
1071+0:+1:+0
1072+1:+0:NaN
1073+0:-1:+0
1074-1:+0:NaN
1075+1:+1:+0
1076-1:-1:+0
1077+1:-1:+0
1078-1:+1:+0
1079+1:+2:+1
1080+2:+1:+0
1081+1000000000:+9:+1
1082+2000000000:+9:+2
1083+3000000000:+9:+3
1084+4000000000:+9:+4
1085+5000000000:+9:+5
1086+6000000000:+9:+6
1087+7000000000:+9:+7
1088+8000000000:+9:+8
1089+9000000000:+9:+0
1090+35500000:+113:+33
1091+71000000:+226:+66
1092+106500000:+339:+99
1093+1000000000:+3:+1
1094+10:+5:+0
1095+100:+4:+0
1096+1000:+8:+0
1097+10000:+16:+0
1098+999999999999:+9:+0
1099+999999999999:+99:+0
1100+999999999999:+999:+0
1101+999999999999:+9999:+0
1102+999999999999999:+99999:+0
58cde26e 1103-9:+5:+1
1104+9:-5:-1
1105-9:-5:-4
1106-5:3:1
1107-2:3:1
11084:3:1
11091:3:1
1110-5:-3:-2
1111-2:-3:-2
11124:-3:-2
11131:-3:-2
574bacfe 11144095:4095:0
748a9306 1115&bgcd
1116abc:abc:NaN
1117abc:+0:NaN
1118+0:abc:NaN
1119+0:+0:+0
1120+0:+1:+1
1121+1:+0:+1
1122+1:+1:+1
1123+2:+3:+1
1124+3:+2:+1
58cde26e 1125-3:+2:+1
748a9306 1126+100:+625:+25
1127+4096:+81:+1
58cde26e 1128+1034:+804:+2
1129+27:+90:+56:+1
1130+27:+90:+54:+9
1131&blcm
e16b8f49 1132abc:abc:NaN
58cde26e 1133abc:+0:NaN
1134+0:abc:NaN
1135+0:+0:NaN
1136+1:+0:+0
1137+0:+1:+0
1138+27:+90:+270
1139+1034:+804:+415668
e16b8f49 1140&band
1141abc:abc:NaN
58cde26e 1142abc:0:NaN
11430:abc:NaN
0716bf9b 11441:2:0
11453:2:2
e16b8f49 1146+8:+2:+0
1147+281474976710656:+0:+0
1148+281474976710656:+1:+0
1149+281474976710656:+281474976710656:+281474976710656
574bacfe 1150-2:-3:-4
1151-1:-1:-1
1152-6:-6:-6
1153-7:-4:-8
1154-7:4:0
1155-4:7:4
e16b8f49 1156&bior
1157abc:abc:NaN
58cde26e 1158abc:0:NaN
11590:abc:NaN
0716bf9b 11601:2:3
e16b8f49 1161+8:+2:+10
1162+281474976710656:+0:+281474976710656
1163+281474976710656:+1:+281474976710657
1164+281474976710656:+281474976710656:+281474976710656
574bacfe 1165-2:-3:-1
1166-1:-1:-1
1167-6:-6:-6
1168-7:4:-3
1169-4:7:-1
e16b8f49 1170&bxor
1171abc:abc:NaN
58cde26e 1172abc:0:NaN
11730:abc:NaN
0716bf9b 11741:2:3
e16b8f49 1175+8:+2:+10
1176+281474976710656:+0:+281474976710656
1177+281474976710656:+1:+281474976710657
1178+281474976710656:+281474976710656:+0
574bacfe 1179-2:-3:3
1180-1:-1:0
1181-6:-6:0
1182-7:4:-3
1183-4:7:-5
11844:-7:-3
1185-4:-7:5
e16b8f49 1186&bnot
1187abc:NaN
1188+0:-1
1189+8:-9
1190+281474976710656:-281474976710657
574bacfe 1191-1:0
1192-2:1
1193-12:11
58cde26e 1194&digit
11950:0:0
119612:0:2
119712:1:1
1198123:0:3
1199123:1:2
1200123:2:1
1201123:-1:1
1202123:-2:2
1203123:-3:3
1204123456:0:6
1205123456:1:5
1206123456:2:4
1207123456:3:3
1208123456:4:2
1209123456:5:1
1210123456:-1:1
1211123456:-2:2
1212123456:-3:3
1213100000:-3:0
1214100000:0:0
1215100000:1:0
1216&mantissa
1217abc:NaN
12181e4:1
12192e0:2
1220123:123
1221-1:-1
1222-2:-2
ee15d750 1223+inf:inf
1224-inf:-inf
58cde26e 1225&exponent
1226abc:NaN
12271e4:4
12282e0:0
1229123:0
1230-1:0
1231-2:0
12320:1
ee15d750 1233+inf:inf
1234-inf:inf
58cde26e 1235&parts
1236abc:NaN,NaN
12371e4:1,4
12382e0:2,0
1239123:123,0
1240-1:-1,0
1241-2:-2,0
12420:0,1
ee15d750 1243+inf:inf,inf
1244-inf:-inf,inf
58cde26e 1245&bpow
0716bf9b 1246abc:12:NaN
124712:abc:NaN
58cde26e 12480:0:1
12490:1:0
12500:2:0
12510:-1:NaN
12520:-2:NaN
12531:0:1
12541:1:1
12551:2:1
12561:3:1
12571:-1:1
12581:-2:1
12591:-3:1
12602:0:1
12612:1:2
12622:2:4
12632:3:8
12643:3:27
12652:-1:NaN
1266-2:-1:NaN
12672:-2:NaN
1268-2:-2:NaN
574bacfe 1269+inf:1234500012:inf
0716bf9b 1270-inf:1234500012:-inf
574bacfe 1271+inf:-12345000123:inf
0716bf9b 1272-inf:-12345000123:-inf
58cde26e 1273# 1 ** -x => 1 / (1 ** x)
1274-1:0:1
1275-2:0:1
1276-1:1:-1
1277-1:2:1
1278-1:3:-1
1279-1:4:1
1280-1:5:-1
1281-1:-1:-1
1282-1:-2:1
1283-1:-3:-1
1284-1:-4:1
128510:2:100
128610:3:1000
128710:4:10000
128810:5:100000
128910:6:1000000
129010:7:10000000
129110:8:100000000
129210:9:1000000000
129310:20:100000000000000000000
1294123456:2:15241383936
1295&length
1296100:3
129710:2
12981:1
12990:1
130012345:5
130110000000000000000:17
1302-123:3
bd05a461 1303215960156869840440586892398248:30
58cde26e 1304&bsqrt
1305144:12
130616:4
13074:2
13082:1
130912:3
1310256:16
1311100000000:10000
13124000000000000:2000000
13131:1
13140:0
1315-2:NaN
1316Nan:NaN
1317&bround
1318$round_mode('trunc')
574bacfe 13190:12:0
1320NaNbround:12:NaN
1321+inf:12:inf
1322-inf:12:-inf
58cde26e 13231234:0:1234
13241234:2:1200
1325123456:4:123400
1326123456:5:123450
1327123456:6:123456
1328+10123456789:5:+10123000000
1329-10123456789:5:-10123000000
1330+10123456789:9:+10123456700
1331-10123456789:9:-10123456700
1332+101234500:6:+101234000
1333-101234500:6:-101234000
1334#+101234500:-4:+101234000
1335#-101234500:-4:-101234000
1336$round_mode('zero')
1337+20123456789:5:+20123000000
1338-20123456789:5:-20123000000
1339+20123456789:9:+20123456800
1340-20123456789:9:-20123456800
1341+201234500:6:+201234000
1342-201234500:6:-201234000
1343#+201234500:-4:+201234000
1344#-201234500:-4:-201234000
1345+12345000:4:12340000
1346-12345000:4:-12340000
1347$round_mode('+inf')
1348+30123456789:5:+30123000000
1349-30123456789:5:-30123000000
1350+30123456789:9:+30123456800
1351-30123456789:9:-30123456800
1352+301234500:6:+301235000
1353-301234500:6:-301234000
1354#+301234500:-4:+301235000
1355#-301234500:-4:-301234000
1356+12345000:4:12350000
1357-12345000:4:-12340000
1358$round_mode('-inf')
1359+40123456789:5:+40123000000
1360-40123456789:5:-40123000000
1361+40123456789:9:+40123456800
1362-40123456789:9:-40123456800
1363+401234500:6:+401234000
1364+401234500:6:+401234000
1365#-401234500:-4:-401235000
1366#-401234500:-4:-401235000
1367+12345000:4:12340000
1368-12345000:4:-12350000
1369$round_mode('odd')
1370+50123456789:5:+50123000000
1371-50123456789:5:-50123000000
1372+50123456789:9:+50123456800
1373-50123456789:9:-50123456800
1374+501234500:6:+501235000
1375-501234500:6:-501235000
1376#+501234500:-4:+501235000
1377#-501234500:-4:-501235000
1378+12345000:4:12350000
1379-12345000:4:-12350000
1380$round_mode('even')
1381+60123456789:5:+60123000000
1382-60123456789:5:-60123000000
1383+60123456789:9:+60123456800
1384-60123456789:9:-60123456800
1385+601234500:6:+601234000
1386-601234500:6:-601234000
1387#+601234500:-4:+601234000
1388#-601234500:-4:-601234000
1389#-601234500:-9:0
1390#-501234500:-9:0
1391#-601234500:-8:0
1392#-501234500:-8:0
1393+1234567:7:1234567
1394+1234567:6:1234570
1395+12345000:4:12340000
1396-12345000:4:-12340000
58cde26e 1397&is_zero
13980:1
1399NaNzero:0
574bacfe 1400+inf:0
1401-inf:0
58cde26e 1402123:0
1403-1:0
14041:0
58cde26e 1405&is_one
14060:0
574bacfe 1407NaNone:0
1408+inf:0
1409-inf:0
58cde26e 14101:1
14112:0
1412-1:0
1413-2:0
1414# floor and ceil tests are pretty pointless in integer space...but play safe
1415&bfloor
14160:0
574bacfe 1417NaNfloor:NaN
1418+inf:inf
1419-inf:-inf
f216259d 1420-1:-1
58cde26e 1421-2:-2
14222:2
14233:3
1424abc:NaN
1425&bceil
574bacfe 1426NaNceil:NaN
1427+inf:inf
1428-inf:-inf
58cde26e 14290:0
1430-1:-1
1431-2:-2
14322:2
14333:3
1434abc:NaN
bd05a461 1435&as_hex
1436128:0x80
1437-128:-0x80
14380:0x0
1439-0:0x0
14401:0x1
14410x123456789123456789:0x123456789123456789
1442+inf:inf
1443-inf:-inf
1444NaNas_hex:NaN
1445&as_bin
1446128:0b10000000
1447-128:-0b10000000
14480:0b0
1449-0:0b0
14501:0b1
14510b1010111101010101010110110110110110101:0b1010111101010101010110110110110110101
1452+inf:inf
1453-inf:-inf
1454NaNas_bin:NaN