10 unshift @INC, '../lib'; # for running manually
14 ##############################################################################
15 # for testing inheritance of _swap
21 @ISA = (qw/Math::BigInt/);
24 # customized overload for sub, since original does not use swap there
25 '-' => sub { my @a = ref($_[0])->_swap(@_);
30 # a fake _swap, which reverses the params
31 my $self = shift; # for override in subclass
34 my $c = ref ($_[0] ) || 'Math::Foo';
35 return ( $_[0]->copy(), $_[1] );
39 return ( Math::Foo->new($_[1]), $_[0] );
43 ##############################################################################
48 my (@args,$f,$try,$x,$y,$z,$a,$exp,$ans,$ans1,@a,$m,$e,$round_mode);
53 next if /^#/; # skip comments
61 $round_mode =~ s/^\$/Math::BigInt->/;
62 # print "$round_mode\n";
66 @args = split(/:/,$_,99);
68 $try = "\$x = Math::BigInt->new(\"$args[0]\");";
71 } elsif ($f eq "_set") {
72 $try .= '$x->_set($args[1]); "$x";';
73 } elsif ($f eq "is_zero") {
74 $try .= '$x->is_zero()+0;';
75 } elsif ($f eq "is_one") {
76 $try .= '$x->is_one()+0;';
77 } elsif ($f eq "is_odd") {
78 $try .= '$x->is_odd()+0;';
79 } elsif ($f eq "is_even") {
80 $try .= '$x->is_even()+0;';
81 } elsif ($f eq "binf") {
82 $try .= "\$x->binf('$args[1]');";
83 } elsif ($f eq "bfloor") {
84 $try .= '$x->bfloor();';
85 } elsif ($f eq "bceil") {
86 $try .= '$x->bceil();';
87 } elsif ($f eq "is_inf") {
88 $try .= "\$x->is_inf('$args[1]')+0;";
89 } elsif ($f eq "bsstr") {
90 $try .= '$x->bsstr();';
91 } elsif ($f eq "bneg") {
93 } elsif ($f eq "babs") {
95 } elsif ($f eq "binc") {
97 } elsif ($f eq "bdec") {
99 }elsif ($f eq "bnot") {
101 }elsif ($f eq "bsqrt") {
102 $try .= '$x->bsqrt();';
103 }elsif ($f eq "length") {
104 $try .= "\$x->length();";
105 }elsif ($f eq "bround") {
106 $try .= "$round_mode; \$x->bround($args[1]);";
107 }elsif ($f eq "exponent"){
108 $try .= '$x = $x->exponent()->bstr();';
109 }elsif ($f eq "mantissa"){
110 $try .= '$x = $x->mantissa()->bstr();';
111 }elsif ($f eq "parts"){
112 $try .= "(\$m,\$e) = \$x->parts();";
113 $try .= '$m = $m->bstr(); $m = "NaN" if !defined $m;';
114 $try .= '$e = $e->bstr(); $e = "NaN" if !defined $e;';
117 $try .= "\$y = new Math::BigInt \"$args[1]\";";
119 $try .= '$x <=> $y;';
120 }elsif ($f eq "bacmp"){
121 $try .= '$x->bacmp($y);';
122 }elsif ($f eq "badd"){
123 $try .= "\$x + \$y;";
124 }elsif ($f eq "bsub"){
125 $try .= "\$x - \$y;";
126 }elsif ($f eq "bmul"){
127 $try .= "\$x * \$y;";
128 }elsif ($f eq "bdiv"){
129 $try .= "\$x / \$y;";
130 }elsif ($f eq "bmod"){
131 $try .= "\$x % \$y;";
132 }elsif ($f eq "bgcd")
134 if (defined $args[2])
136 $try .= " \$z = new Math::BigInt \"$args[2]\"; ";
138 $try .= "Math::BigInt::bgcd(\$x, \$y";
139 $try .= ", \$z" if (defined $args[2]);
144 if (defined $args[2])
146 $try .= " \$z = new Math::BigInt \"$args[2]\"; ";
148 $try .= "Math::BigInt::blcm(\$x, \$y";
149 $try .= ", \$z" if (defined $args[2]);
151 }elsif ($f eq "blsft"){
152 if (defined $args[2])
154 $try .= "\$x->blsft(\$y,$args[2]);";
158 $try .= "\$x << \$y;";
160 }elsif ($f eq "brsft"){
161 if (defined $args[2])
163 $try .= "\$x->brsft(\$y,$args[2]);";
167 $try .= "\$x >> \$y;";
169 }elsif ($f eq "band"){
170 $try .= "\$x & \$y;";
171 }elsif ($f eq "bior"){
172 $try .= "\$x | \$y;";
173 }elsif ($f eq "bxor"){
174 $try .= "\$x ^ \$y;";
175 }elsif ($f eq "bpow"){
176 $try .= "\$x ** \$y;";
177 }elsif ($f eq "digit"){
178 $try = "\$x = Math::BigInt->new(\"$args[0]\"); \$x->digit($args[1]);";
179 } else { warn "Unknown op '$f'"; }
181 # print "trying $try\n";
183 $ans =~ s/^[+]([0-9])/$1/; # remove leading '+'
190 #print "try: $try ans: $ans1 $ans\n";
191 print "# Tried: '$try'\n" if !ok ($ans1, $ans);
193 # check internal state of number objects
194 is_valid($ans1) if ref $ans1;
196 } # endwhile data tests
199 # test whether constant works or not
200 $try = "use Math::BigInt (1.31,'babs',':constant');";
201 $try .= ' $x = 2**150; babs($x); $x = "$x";';
204 ok ( $ans1, "1427247692705959881058285969449495136382746624");
208 for (my $i = 1; $i < 10; $i++)
212 ok "@a", "1 2 3 4 5 6 7 8 9";
214 # test whether selfmultiplication works correctly (result is 2**64)
215 $try = '$x = new Math::BigInt "+4294967296";';
216 $try .= '$a = $x->bmul($x);';
218 print "# Tried: '$try'\n" if !ok ($ans1, Math::BigInt->new(2) ** 64);
220 # test whether op detroys args or not (should better not)
222 $x = new Math::BigInt (3);
223 $y = new Math::BigInt (4);
232 $x = new Math::BigInt (1);
233 $y = new Math::BigInt (2);
239 $x = new Math::BigInt (5);
240 $y = new Math::BigInt (4);
246 $x = new Math::BigInt (-5); $y = -$x;
249 $x = new Math::BigInt (-5); $y = abs($x);
252 # check whether overloading cmp works
253 $try = "\$x = Math::BigInt->new(0);";
255 $try .= "'false' if \$x ne \$y;";
257 print "# For '$try'\n" if (!ok "$ans" , "false" );
259 # we cant test for working cmpt with other objects here, we would need a dummy
260 # object with stringify overload for this. see Math::String tests
262 ###############################################################################
264 $try = "\$x = Math::BigInt->new(1); \$x += 9;";
265 $try .= "'ok' if \$x == 10;";
267 print "# For '$try'\n" if (!ok "$ans" , "ok" );
269 $try = "\$x = Math::BigInt->new(1); \$x -= 9;";
270 $try .= "'ok' if \$x == -8;";
272 print "# For '$try'\n" if (!ok "$ans" , "ok" );
274 $try = "\$x = Math::BigInt->new(1); \$x *= 9;";
275 $try .= "'ok' if \$x == 9;";
277 print "# For '$try'\n" if (!ok "$ans" , "ok" );
279 $try = "\$x = Math::BigInt->new(10); \$x /= 2;";
280 $try .= "'ok' if \$x == 5;";
282 print "# For '$try'\n" if (!ok "$ans" , "ok" );
284 ###############################################################################
285 # check reversed order of arguments
286 $try = "\$x = Math::BigInt->new(10); \$x = 2 ** \$x;";
287 $try .= "'ok' if \$x == 1024;"; $ans = eval $try;
288 print "# For '$try'\n" if (!ok "$ans" , "ok" );
290 $try = "\$x = Math::BigInt->new(10); \$x = 2 * \$x;";
291 $try .= "'ok' if \$x == 20;"; $ans = eval $try;
292 print "# For '$try'\n" if (!ok "$ans" , "ok" );
294 $try = "\$x = Math::BigInt->new(10); \$x = 2 + \$x;";
295 $try .= "'ok' if \$x == 12;"; $ans = eval $try;
296 print "# For '$try'\n" if (!ok "$ans" , "ok" );
298 $try = "\$x = Math::BigInt->new(10); \$x = 2 - \$x;";
299 $try .= "'ok' if \$x == -8;"; $ans = eval $try;
300 print "# For '$try'\n" if (!ok "$ans" , "ok" );
302 $try = "\$x = Math::BigInt->new(10); \$x = 20 / \$x;";
303 $try .= "'ok' if \$x == 2;"; $ans = eval $try;
304 print "# For '$try'\n" if (!ok "$ans" , "ok" );
306 ###############################################################################
307 # check badd(4,5) form
309 $try = "\$x = Math::BigInt::badd(4,5);";
310 $try .= "'ok' if \$x == 9;";
312 print "# For '$try'\n" if (!ok "$ans" , "ok" );
314 $try = "\$x = Math::BigInt->badd(4,5);";
315 $try .= "'ok' if \$x == 9;";
317 print "# For '$try'\n" if (!ok "$ans" , "ok" );
319 ###############################################################################
320 # check proper length of internal arrays
322 $x = Math::BigInt->new(99999);
324 ok (scalar @{$x->{value}}, 1);
327 ok (scalar @{$x->{value}}, 2);
330 ok (scalar @{$x->{value}}, 1);
332 ###############################################################################
336 $x = Math::BigInt->new($BASE-1); ok ($x->numify(),$BASE-1);
337 $x = Math::BigInt->new(-($BASE-1)); ok ($x->numify(),-($BASE-1));
338 $x = Math::BigInt->new($BASE); ok ($x->numify(),$BASE);
339 $x = Math::BigInt->new(-$BASE); ok ($x->numify(),-$BASE);
340 $x = Math::BigInt->new( -($BASE*$BASE*1+$BASE*1+1) );
341 ok($x->numify(),-($BASE*$BASE*1+$BASE*1+1));
343 ###############################################################################
344 # test bug in _digits with length($c[-1]) where $c[-1] was "00001" instead of 1
346 $x = Math::BigInt->new(99998); $x++; $x++; $x++; $x++;
347 if ($x > 100000) { ok (1,1) } else { ok ("$x < 100000","$x > 100000"); }
349 $x = Math::BigInt->new(100003); $x++;
350 $y = Math::BigInt->new(1000000);
351 if ($x < 1000000) { ok (1,1) } else { ok ("$x > 1000000","$x < 1000000"); }
353 ###############################################################################
354 # bug in sub where number with at least 6 trailing zeros after any op failed
356 $x = Math::BigInt->new(123456); $z = Math::BigInt->new(10000); $z *= 10;
361 ###############################################################################
362 # bug with rest "-0" in div, causing further div()s to fail
364 $x = Math::BigInt->new(-322056000); ($x,$y) = $x->bdiv('-12882240');
366 ok ($y,'0'); # not '-0'
369 ###############################################################################
370 # check undefs: NOT DONE YET
372 ###############################################################################
375 $x = Math::BigInt->new(1); if ($x) { ok (1,1); } else { ok($x,'to be true') }
376 $x = Math::BigInt->new(0); if (!$x) { ok (1,1); } else { ok($x,'to be false') }
378 ###############################################################################
381 @args = Math::BigInt::objectify(2,4,5);
382 ok (scalar @args,3); # 'Math::BigInt', 4, 5
383 ok ($args[0],'Math::BigInt');
387 @args = Math::BigInt::objectify(0,4,5);
388 ok (scalar @args,3); # 'Math::BigInt', 4, 5
389 ok ($args[0],'Math::BigInt');
393 @args = Math::BigInt::objectify(2,4,5);
394 ok (scalar @args,3); # 'Math::BigInt', 4, 5
395 ok ($args[0],'Math::BigInt');
399 @args = Math::BigInt::objectify(2,4,5,6,7);
400 ok (scalar @args,5); # 'Math::BigInt', 4, 5, 6, 7
401 ok ($args[0],'Math::BigInt');
402 ok ($args[1],4); ok (ref($args[1]),$args[0]);
403 ok ($args[2],5); ok (ref($args[2]),$args[0]);
404 ok ($args[3],6); ok (ref($args[3]),'');
405 ok ($args[4],7); ok (ref($args[4]),'');
407 @args = Math::BigInt::objectify(2,'Math::BigInt',4,5,6,7);
408 ok (scalar @args,5); # 'Math::BigInt', 4, 5, 6, 7
409 ok ($args[0],'Math::BigInt');
410 ok ($args[1],4); ok (ref($args[1]),$args[0]);
411 ok ($args[2],5); ok (ref($args[2]),$args[0]);
412 ok ($args[3],6); ok (ref($args[3]),'');
413 ok ($args[4],7); ok (ref($args[4]),'');
415 ###############################################################################
416 # test for flaoting-point input (other tests in bnorm() below)
418 $z = 1050000000000000; # may be int on systems with 64bit?
419 $x = Math::BigInt->new($z); ok ($x->bsstr(),'105e+13'); # not 1.03e+15?
420 $z = 1e+129; # definitely a float
421 $x = Math::BigInt->new($z); ok ($x->bsstr(),$z);
423 ###############################################################################
424 # prime number tests, also test for **= and length()
425 # found on: http://www.utm.edu/research/primes/notes/by_year.html
428 $x = Math::BigInt->new(2); $x **= 148; $x++; $x = $x / 17;
429 ok ($x,"20988936657440586486151264256610222593863921");
430 ok ($x->length(),length "20988936657440586486151264256610222593863921");
433 $x = Math::BigInt->new(2); $x **= 127; $x--;
434 ok ($x,"170141183460469231731687303715884105727");
436 # I am afraid the following is not yet possible due to slowness
437 # Also, testing for 2 meg output is a bit hard ;)
438 #$x = new Math::BigInt(2); $x **= 6972593; $x--;
440 # 593573509*2^332162+1 has exactly 100.000 digits
441 # takes over 16 mins and still not complete, so can not be done yet ;)
442 #$x = Math::BigInt->new(2); $x **= 332162; $x *= "593573509"; $x++;
443 #ok ($x->digits(),100000);
445 ###############################################################################
446 # inheritance and overriding of _swap
448 $x = Math::Foo->new(5);
449 $x = $x - 8; # 8 - 5 instead of 5-8
451 ok (ref($x),'Math::Foo');
453 $x = Math::Foo->new(5);
454 $x = 8 - $x; # 5 - 8 instead of 8 - 5
456 ok (ref($x),'Math::Foo');
458 ###############################################################################
461 # devel test, see whether valid catches errors
462 #$x = Math::BigInt->new(0);
469 #$x->{value}->[0] = undef;
472 #$x->{value}->[0] = 1e6;
475 #$x->{value}->[0] = -2;
481 ###############################################################################
482 # Perl 5.005 does not like ok ($x,undef)
488 ok (1,1) and return if !defined $x;
492 ###############################################################################
493 # sub to check validity of a BigInt internally, to ensure that no op leaves a
494 # number object in an invalid state (f.i. "-0")
503 is_okay('ref($x)','Math::BigInt',ref($x),$error);
506 is_okay('$x->{sign}',"'+', '-', '-inf', '+inf' or 'NaN'",$x->{sign},$error)
507 if $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/;
510 if (($x->{sign} eq '-') && (@{$x->{value}} == 1) && ($x->{value}->[0] == 0))
512 is_okay("\$x ne '-0'","0",$x,$error);
514 # all parts are valid?
515 my $i = 0; my $j = scalar @{$x->{value}}; my $e; my $try;
518 $e = $x->{value}->[$i]; $e = 'undef' unless defined $e;
519 $try = '=~ /^[\+]?[0-9]+\$/; '."($f, $x, $e)";
520 last if $e !~ /^[+]?[0-9]+$/;
521 $try = ' < 0 || >= 1e5; '."($f, $x, $e)";
522 last if $e <0 || $e >= 1e5;
523 # this test is disabled, since new/bnorm and certain ops (like early out
524 # in add/sub) are allowed/expected to leave '00000' in some elements
525 #$try = '=~ /^00+/; '."($f, $x, $e)";
526 #last if $e =~ /^00+/;
529 is_okay("\$x->{value}->[$i] $try","not $e",$e,$error)
530 if $i < $j; # trough all?
532 # see whether errors crop up
533 $error->[1] = 'undef' unless defined $error->[1];
534 if ($error->[0] ne "")
536 ok ($error->[1],$error->[2]);
537 print "# Tried: $error->[0]\n";
547 my ($tried,$expected,$try,$error) = @_;
549 return if $error->[0] ne ""; # error, no further testing
551 @$error = ( $tried, $try, $expected ) if $try ne $expected;
566 0b1000000000000000000000000000000:1073741824
602 -123456789:-123456789
619 # floating point input
646 +1:+48:+281474976710656
649 +12345:4:10:123450000
655 1234567890123:12:10:1234567890123000000000000
660 +281474976710656:+48:+1
671 1230000000000:10:10:123
672 09876123456789067890:12:10:9876123
673 1234561234567890123:13:10:123456
684 +123456789:-123456789
685 -123456789:+123456789
691 +123456789:+123456789
692 -123456789:+123456789
717 -123456789:+987654321:-1
718 +123456789:-987654321:1
719 -987654321:+123456789:-1
727 -123456789:+987654321:-1
728 +123456789:-987654321:-1
729 -987654321:+123456789:1
759 +9999999:+1:+10000000
760 +99999999:+1:+100000000
761 +999999999:+1:+1000000000
762 +9999999999:+1:+10000000000
763 +99999999999:+1:+100000000000
770 +10000000:-1:+9999999
771 +100000000:-1:+99999999
772 +1000000000:-1:+999999999
773 +10000000000:-1:+9999999999
774 +123456789:+987654321:+1111111110
775 -123456789:+987654321:+864197532
776 -123456789:-987654321:-1111111110
777 +123456789:-987654321:-864197532
798 +99999999:+1:+99999998
799 +999999999:+1:+999999998
800 +9999999999:+1:+9999999998
801 +99999999999:+1:+99999999998
808 +10000000:-1:+10000001
809 +100000000:-1:+100000001
810 +1000000000:-1:+1000000001
811 +10000000000:-1:+10000000001
812 +123456789:+987654321:-864197532
813 -123456789:+987654321:-1111111110
814 -123456789:-987654321:+864197532
815 +123456789:-987654321:+1111111110
825 +123456789123456789:+0:+0
826 +0:+123456789123456789:+0
836 +10101:+10101:+102030201
837 +1001001:+1001001:+1002003002001
838 +100010001:+100010001:+10002000300020001
839 +10000100001:+10000100001:+100002000030000200001
840 +11111111111:+9:+99999999999
841 +22222222222:+9:+199999999998
842 +33333333333:+9:+299999999997
843 +44444444444:+9:+399999999996
844 +55555555555:+9:+499999999995
845 +66666666666:+9:+599999999994
846 +77777777777:+9:+699999999993
847 +88888888888:+9:+799999999992
848 +99999999999:+9:+899999999991
850 +12345:+12345:+152399025
851 +99999:+11111:+1111088889
868 +1000000000:+9:+111111111
869 +2000000000:+9:+222222222
870 +3000000000:+9:+333333333
871 +4000000000:+9:+444444444
872 +5000000000:+9:+555555555
873 +6000000000:+9:+666666666
874 +7000000000:+9:+777777777
875 +8000000000:+9:+888888888
876 +9000000000:+9:+1000000000
877 +35500000:+113:+314159
878 +71000000:+226:+314159
879 +106500000:+339:+314159
880 +1000000000:+3:+333333333
885 +999999999999:+9:+111111111111
886 +999999999999:+99:+10101010101
887 +999999999999:+999:+1001001001
888 +999999999999:+9999:+100010001
889 +999999999999999:+99999:+10000100001
890 +1111088889:+99999:+11111
933 +999999999999:+999:+0
934 +999999999999:+9999:+0
935 +999999999999999:+99999:+0
977 +281474976710656:+0:+0
978 +281474976710656:+1:+0
979 +281474976710656:+281474976710656:+281474976710656
985 +281474976710656:+0:+281474976710656
986 +281474976710656:+1:+281474976710657
987 +281474976710656:+281474976710656:+281474976710656
993 +281474976710656:+0:+281474976710656
994 +281474976710656:+1:+281474976710657
995 +281474976710656:+281474976710656:+0
1000 +281474976710656:-281474976710657
1068 # 1 ** -x => 1 / (1 ** x)
1088 10:20:100000000000000000000
1089 123456:2:15241383936
1096 10000000000000000:17
1106 4000000000000:2000000
1112 $round_mode('trunc')
1118 +10123456789:5:+10123000000
1119 -10123456789:5:-10123000000
1120 +10123456789:9:+10123456700
1121 -10123456789:9:-10123456700
1122 +101234500:6:+101234000
1123 -101234500:6:-101234000
1124 #+101234500:-4:+101234000
1125 #-101234500:-4:-101234000
1127 +20123456789:5:+20123000000
1128 -20123456789:5:-20123000000
1129 +20123456789:9:+20123456800
1130 -20123456789:9:-20123456800
1131 +201234500:6:+201234000
1132 -201234500:6:-201234000
1133 #+201234500:-4:+201234000
1134 #-201234500:-4:-201234000
1135 +12345000:4:12340000
1136 -12345000:4:-12340000
1138 +30123456789:5:+30123000000
1139 -30123456789:5:-30123000000
1140 +30123456789:9:+30123456800
1141 -30123456789:9:-30123456800
1142 +301234500:6:+301235000
1143 -301234500:6:-301234000
1144 #+301234500:-4:+301235000
1145 #-301234500:-4:-301234000
1146 +12345000:4:12350000
1147 -12345000:4:-12340000
1149 +40123456789:5:+40123000000
1150 -40123456789:5:-40123000000
1151 +40123456789:9:+40123456800
1152 -40123456789:9:-40123456800
1153 +401234500:6:+401234000
1154 +401234500:6:+401234000
1155 #-401234500:-4:-401235000
1156 #-401234500:-4:-401235000
1157 +12345000:4:12340000
1158 -12345000:4:-12350000
1160 +50123456789:5:+50123000000
1161 -50123456789:5:-50123000000
1162 +50123456789:9:+50123456800
1163 -50123456789:9:-50123456800
1164 +501234500:6:+501235000
1165 -501234500:6:-501235000
1166 #+501234500:-4:+501235000
1167 #-501234500:-4:-501235000
1168 +12345000:4:12350000
1169 -12345000:4:-12350000
1171 +60123456789:5:+60123000000
1172 -60123456789:5:-60123000000
1173 +60123456789:9:+60123456800
1174 -60123456789:9:-60123456800
1175 +601234500:6:+601234000
1176 -601234500:6:-601234000
1177 #+601234500:-4:+601234000
1178 #-601234500:-4:-601234000
1185 +12345000:4:12340000
1186 -12345000:4:-12340000
1224 # floor and ceil tests are pretty pointless in integer space...but play safe