10 unshift @INC, '../lib'; # for running manually
13 my $version = '1.36'; # for $VERSION tests, match current release (by hand!)
15 ##############################################################################
16 # for testing inheritance of _swap
22 @ISA = (qw/Math::BigInt/);
25 # customized overload for sub, since original does not use swap there
26 '-' => sub { my @a = ref($_[0])->_swap(@_);
31 # a fake _swap, which reverses the params
32 my $self = shift; # for override in subclass
35 my $c = ref ($_[0] ) || 'Math::Foo';
36 return ( $_[0]->copy(), $_[1] );
40 return ( Math::Foo->new($_[1]), $_[0] );
44 ##############################################################################
48 #use Math::BigInt lib => 'BitVect'; # for testing
49 #use Math::BigInt lib => 'Small'; # for testing
51 my $CALC = Math::BigInt::_core_lib();
53 my (@args,$f,$try,$x,$y,$z,$a,$exp,$ans,$ans1,@a,$m,$e,$round_mode);
58 next if /^#/; # skip comments
66 $round_mode =~ s/^\$/Math::BigInt->/;
67 # print "$round_mode\n";
71 @args = split(/:/,$_,99);
73 $try = "\$x = Math::BigInt->new(\"$args[0]\");";
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;';
84 } elsif ($f eq "is_inf") {
85 $try .= "\$x->is_inf('$args[1]')+0;";
86 } elsif ($f eq "binf") {
87 $try .= "\$x->binf('$args[1]');";
88 } elsif ($f eq "bfloor") {
89 $try .= '$x->bfloor();';
90 } elsif ($f eq "bceil") {
91 $try .= '$x->bceil();';
92 } elsif ($f eq "bsstr") {
93 $try .= '$x->bsstr();';
94 } elsif ($f eq "bneg") {
96 } elsif ($f eq "babs") {
98 } elsif ($f eq "binc") {
100 } elsif ($f eq "bdec") {
102 }elsif ($f eq "bnot") {
104 }elsif ($f eq "bsqrt") {
105 $try .= '$x->bsqrt();';
106 }elsif ($f eq "length") {
107 $try .= "\$x->length();";
108 }elsif ($f eq "exponent"){
109 $try .= '$x = $x->exponent()->bstr();';
110 }elsif ($f eq "mantissa"){
111 $try .= '$x = $x->mantissa()->bstr();';
112 }elsif ($f eq "parts"){
113 $try .= "(\$m,\$e) = \$x->parts();";
114 $try .= '$m = $m->bstr(); $m = "NaN" if !defined $m;';
115 $try .= '$e = $e->bstr(); $e = "NaN" if !defined $e;';
118 $try .= "\$y = new Math::BigInt ('$args[1]');";
120 $try .= '$x <=> $y;';
121 }elsif ($f eq "bround") {
122 $try .= "$round_mode; \$x->bround(\$y);";
123 }elsif ($f eq "bacmp"){
124 $try .= "\$x->bacmp(\$y);";
125 }elsif ($f eq "badd"){
126 $try .= "\$x + \$y;";
127 }elsif ($f eq "bsub"){
128 $try .= "\$x - \$y;";
129 }elsif ($f eq "bmul"){
130 $try .= "\$x * \$y;";
131 }elsif ($f eq "bdiv"){
132 $try .= "\$x / \$y;";
133 }elsif ($f eq "bmod"){
134 $try .= "\$x % \$y;";
135 }elsif ($f eq "bgcd")
137 if (defined $args[2])
139 $try .= " \$z = new Math::BigInt \"$args[2]\"; ";
141 $try .= "Math::BigInt::bgcd(\$x, \$y";
142 $try .= ", \$z" if (defined $args[2]);
147 if (defined $args[2])
149 $try .= " \$z = new Math::BigInt \"$args[2]\"; ";
151 $try .= "Math::BigInt::blcm(\$x, \$y";
152 $try .= ", \$z" if (defined $args[2]);
154 }elsif ($f eq "blsft"){
155 if (defined $args[2])
157 $try .= "\$x->blsft(\$y,$args[2]);";
161 $try .= "\$x << \$y;";
163 }elsif ($f eq "brsft"){
164 if (defined $args[2])
166 $try .= "\$x->brsft(\$y,$args[2]);";
170 $try .= "\$x >> \$y;";
172 }elsif ($f eq "band"){
173 $try .= "\$x & \$y;";
174 }elsif ($f eq "bior"){
175 $try .= "\$x | \$y;";
176 }elsif ($f eq "bxor"){
177 $try .= "\$x ^ \$y;";
178 }elsif ($f eq "bpow"){
179 $try .= "\$x ** \$y;";
180 }elsif ($f eq "digit"){
181 $try = "\$x = Math::BigInt->new(\"$args[0]\"); \$x->digit($args[1]);";
182 } else { warn "Unknown op '$f'"; }
184 # print "trying $try\n";
186 $ans =~ s/^[+]([0-9])/$1/; # remove leading '+'
193 #print "try: $try ans: $ans1 $ans\n";
194 print "# Tried: '$try'\n" if !ok ($ans1, $ans);
196 # check internal state of number objects
197 is_valid($ans1,$f) if ref $ans1;
199 } # endwhile data tests
202 # XXX Tels 06/29/2001 following tests never fail or do not work :(
204 # test whether use Math::BigInt qw/version/ works
205 $try = "use Math::BigInt ($version.'1');";
206 $try .= ' $x = Math::BigInt->new(123); $x = "$x";';
208 ok_undef ( $_ ); # should result in error!
210 # test whether constant works or not, also test for qw($version)
211 $try = "use Math::BigInt ($version,'babs',':constant');";
212 $try .= ' $x = 2**150; babs($x); $x = "$x";';
214 ok ( $ans1, "1427247692705959881058285969449495136382746624");
216 # test wether Math::BigInt::Small via use works (w/ dff. spellings of calc)
217 #$try = "use Math::BigInt ($version,'CALC','Small');";
218 #$try .= ' $x = 2**10; $x = "$x";';
220 #ok ( $ans1, "1024");
221 #$try = "use Math::BigInt ($version,'cAlC','Math::BigInt::Small');";
222 #$try .= ' $x = 2**10; $x = "$x";';
224 #ok ( $ans1, "1024");
225 # test wether calc => undef (array element not existing) works
226 #$try = "use Math::BigInt ($version,'CALC');";
227 #$try = "require Math::BigInt; Math::BigInt::import($version,'CALC');";
228 #$try .= ' $x = Math::BigInt->new(2)**10; $x = "$x";';
234 for (my $i = 1; $i < 10; $i++)
238 ok "@a", "1 2 3 4 5 6 7 8 9";
240 # test whether selfmultiplication works correctly (result is 2**64)
241 $try = '$x = new Math::BigInt "+4294967296";';
242 $try .= '$a = $x->bmul($x);';
244 print "# Tried: '$try'\n" if !ok ($ans1, Math::BigInt->new(2) ** 64);
246 # test whether op destroys args or not (should better not)
248 $x = new Math::BigInt (3);
249 $y = new Math::BigInt (4);
258 $x = new Math::BigInt (1);
259 $y = new Math::BigInt (2);
265 $x = new Math::BigInt (5);
266 $y = new Math::BigInt (4);
272 $x = new Math::BigInt (-5); $y = -$x;
275 $x = new Math::BigInt (-5); $y = abs($x);
278 # check whether overloading cmp works
279 $try = "\$x = Math::BigInt->new(0);";
281 $try .= "'false' if \$x ne \$y;";
283 print "# For '$try'\n" if (!ok "$ans" , "false" );
285 # we cant test for working cmpt with other objects here, we would need a dummy
286 # object with stringify overload for this. see Math::String tests
288 ###############################################################################
290 $try = "\$x = Math::BigInt->new(1); \$x += 9;";
291 $try .= "'ok' if \$x == 10;";
293 print "# For '$try'\n" if (!ok "$ans" , "ok" );
295 $try = "\$x = Math::BigInt->new(1); \$x -= 9;";
296 $try .= "'ok' if \$x == -8;";
298 print "# For '$try'\n" if (!ok "$ans" , "ok" );
300 $try = "\$x = Math::BigInt->new(1); \$x *= 9;";
301 $try .= "'ok' if \$x == 9;";
303 print "# For '$try'\n" if (!ok "$ans" , "ok" );
305 $try = "\$x = Math::BigInt->new(10); \$x /= 2;";
306 $try .= "'ok' if \$x == 5;";
308 print "# For '$try'\n" if (!ok "$ans" , "ok" );
310 ###############################################################################
311 # check reversed order of arguments
312 $try = "\$x = Math::BigInt->new(10); \$x = 2 ** \$x;";
313 $try .= "'ok' if \$x == 1024;"; $ans = eval $try;
314 print "# For '$try'\n" if (!ok "$ans" , "ok" );
316 $try = "\$x = Math::BigInt->new(10); \$x = 2 * \$x;";
317 $try .= "'ok' if \$x == 20;"; $ans = eval $try;
318 print "# For '$try'\n" if (!ok "$ans" , "ok" );
320 $try = "\$x = Math::BigInt->new(10); \$x = 2 + \$x;";
321 $try .= "'ok' if \$x == 12;"; $ans = eval $try;
322 print "# For '$try'\n" if (!ok "$ans" , "ok" );
324 $try = "\$x = Math::BigInt->new(10); \$x = 2 - \$x;";
325 $try .= "'ok' if \$x == -8;"; $ans = eval $try;
326 print "# For '$try'\n" if (!ok "$ans" , "ok" );
328 $try = "\$x = Math::BigInt->new(10); \$x = 20 / \$x;";
329 $try .= "'ok' if \$x == 2;"; $ans = eval $try;
330 print "# For '$try'\n" if (!ok "$ans" , "ok" );
332 ###############################################################################
333 # check badd(4,5) form
335 $try = "\$x = Math::BigInt::badd(4,5);";
336 $try .= "'ok' if \$x == 9;";
338 print "# For '$try'\n" if (!ok "$ans" , "ok" );
340 $try = "\$x = Math::BigInt->badd(4,5);";
341 $try .= "'ok' if \$x == 9;";
343 print "# For '$try'\n" if (!ok "$ans" , "ok" );
345 ###############################################################################
346 # check proper length of internal arrays
348 $x = Math::BigInt->new(99999); is_valid($x);
349 $x += 1; ok ($x,100000); is_valid($x);
350 $x -= 1; ok ($x,99999); is_valid($x);
352 ###############################################################################
353 # check numify, these tests only make sense with Math::BigInt::Calc, since
354 # only this uses $BASE
356 my $BASE = int(1e5); # should access Math::BigInt::Calc::BASE
357 $x = Math::BigInt->new($BASE-1); ok ($x->numify(),$BASE-1);
358 $x = Math::BigInt->new(-($BASE-1)); ok ($x->numify(),-($BASE-1));
359 $x = Math::BigInt->new($BASE); ok ($x->numify(),$BASE);
360 $x = Math::BigInt->new(-$BASE); ok ($x->numify(),-$BASE);
361 $x = Math::BigInt->new( -($BASE*$BASE*1+$BASE*1+1) );
362 ok($x->numify(),-($BASE*$BASE*1+$BASE*1+1));
364 ###############################################################################
365 # test bug in _digits with length($c[-1]) where $c[-1] was "00001" instead of 1
367 $x = Math::BigInt->new(99998); $x++; $x++; $x++; $x++;
368 if ($x > 100000) { ok (1,1) } else { ok ("$x < 100000","$x > 100000"); }
370 $x = Math::BigInt->new(100003); $x++;
371 $y = Math::BigInt->new(1000000);
372 if ($x < 1000000) { ok (1,1) } else { ok ("$x > 1000000","$x < 1000000"); }
374 ###############################################################################
375 # bug in sub where number with at least 6 trailing zeros after any op failed
377 $x = Math::BigInt->new(123456); $z = Math::BigInt->new(10000); $z *= 10;
382 ###############################################################################
383 # bug with rest "-0" in div, causing further div()s to fail
385 $x = Math::BigInt->new('-322056000'); ($x,$y) = $x->bdiv('-12882240');
387 ok ($y,'0','not -0'); # not '-0'
390 ###############################################################################
391 # check undefs: NOT DONE YET
393 ###############################################################################
396 $x = Math::BigInt->new(1); if ($x) { ok (1,1); } else { ok($x,'to be true') }
397 $x = Math::BigInt->new(0); if (!$x) { ok (1,1); } else { ok($x,'to be false') }
399 ###############################################################################
402 @args = Math::BigInt::objectify(2,4,5);
403 ok (scalar @args,3); # 'Math::BigInt', 4, 5
404 ok ($args[0],'Math::BigInt');
408 @args = Math::BigInt::objectify(0,4,5);
409 ok (scalar @args,3); # 'Math::BigInt', 4, 5
410 ok ($args[0],'Math::BigInt');
414 @args = Math::BigInt::objectify(2,4,5);
415 ok (scalar @args,3); # 'Math::BigInt', 4, 5
416 ok ($args[0],'Math::BigInt');
420 @args = Math::BigInt::objectify(2,4,5,6,7);
421 ok (scalar @args,5); # 'Math::BigInt', 4, 5, 6, 7
422 ok ($args[0],'Math::BigInt');
423 ok ($args[1],4); ok (ref($args[1]),$args[0]);
424 ok ($args[2],5); ok (ref($args[2]),$args[0]);
425 ok ($args[3],6); ok (ref($args[3]),'');
426 ok ($args[4],7); ok (ref($args[4]),'');
428 @args = Math::BigInt::objectify(2,'Math::BigInt',4,5,6,7);
429 ok (scalar @args,5); # 'Math::BigInt', 4, 5, 6, 7
430 ok ($args[0],'Math::BigInt');
431 ok ($args[1],4); ok (ref($args[1]),$args[0]);
432 ok ($args[2],5); ok (ref($args[2]),$args[0]);
433 ok ($args[3],6); ok (ref($args[3]),'');
434 ok ($args[4],7); ok (ref($args[4]),'');
436 ###############################################################################
437 # test for floating-point input (other tests in bnorm() below)
439 $z = 1050000000000000; # may be int on systems with 64bit?
440 $x = Math::BigInt->new($z); ok ($x->bsstr(),'105e+13'); # not 1.03e+15
441 $z = 1e+129; # definitely a float (may fail on UTS)
442 $x = Math::BigInt->new($z); ok ($x->bsstr(),$z);
444 ###############################################################################
445 # prime number tests, also test for **= and length()
446 # found on: http://www.utm.edu/research/primes/notes/by_year.html
449 $x = Math::BigInt->new(2); $x **= 148; $x++; $x = $x / 17;
450 ok ($x,"20988936657440586486151264256610222593863921");
451 ok ($x->length(),length "20988936657440586486151264256610222593863921");
454 $x = Math::BigInt->new(2); $x **= 127; $x--;
455 ok ($x,"170141183460469231731687303715884105727");
457 # I am afraid the following is not yet possible due to slowness
458 # Also, testing for 2 meg output is a bit hard ;)
459 #$x = new Math::BigInt(2); $x **= 6972593; $x--;
461 # 593573509*2^332162+1 has exactly 1,000,000 digits
462 # takes about 24 mins on 300 Mhz, so cannot be done yet ;)
463 #$x = Math::BigInt->new(2); $x **= 332162; $x *= "593573509"; $x++;
464 #ok ($x->length(),1_000_000);
466 ###############################################################################
467 # inheritance and overriding of _swap
469 $x = Math::Foo->new(5);
470 $x = $x - 8; # 8 - 5 instead of 5-8
472 ok (ref($x),'Math::Foo');
474 $x = Math::Foo->new(5);
475 $x = 8 - $x; # 5 - 8 instead of 8 - 5
477 ok (ref($x),'Math::Foo');
479 ###############################################################################
482 ###############################################################################
483 # Perl 5.005 does not like ok ($x,undef)
489 ok (1,1) and return if !defined $x;
493 ###############################################################################
494 # sub to check validity of a BigInt internally, to ensure that no op leaves a
495 # number object in an invalid state (f.i. "-0")
503 $e = 'Not a reference to Math::BigInt' if !ref($x);
506 $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'"
507 if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/;
509 $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0;
510 $e = $CALC->_check($x->{value}) if $e eq '0';
512 # test done, see if error did crop up
513 ok (1,1), return if ($e eq '0');
515 ok (1,$e." op '$f'");
546 -123456789:+987654321:-1
547 +123456789:-987654321:-1
548 +987654321:+123456789:1
549 -987654321:+123456789:1
563 0b1000000000000000000000000000000:1073741824
599 -123456789:-123456789
616 # floating point input
639 # it must be exactly /^[+-]inf$/
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
760 +9999999:+1:+10000000
761 +99999999:+1:+100000000
762 +999999999:+1:+1000000000
763 +9999999999:+1:+10000000000
764 +99999999999:+1:+100000000000
771 +10000000:-1:+9999999
772 +100000000:-1:+99999999
773 +1000000000:-1:+999999999
774 +10000000000:-1:+9999999999
775 +123456789:+987654321:+1111111110
776 -123456789:+987654321:+864197532
777 -123456789:-987654321:-1111111110
778 +123456789:-987654321:-864197532
799 +99999999:+1:+99999998
800 +999999999:+1:+999999998
801 +9999999999:+1:+9999999998
802 +99999999999:+1:+99999999998
809 +10000000:-1:+10000001
810 +100000000:-1:+100000001
811 +1000000000:-1:+1000000001
812 +10000000000:-1:+10000000001
813 +123456789:+987654321:-864197532
814 -123456789:+987654321:-1111111110
815 -123456789:-987654321:+864197532
816 +123456789:-987654321:+1111111110
826 +123456789123456789:+0:+0
827 +0:+123456789123456789:+0
837 +10101:+10101:+102030201
838 +1001001:+1001001:+1002003002001
839 +100010001:+100010001:+10002000300020001
840 +10000100001:+10000100001:+100002000030000200001
841 +11111111111:+9:+99999999999
842 +22222222222:+9:+199999999998
843 +33333333333:+9:+299999999997
844 +44444444444:+9:+399999999996
845 +55555555555:+9:+499999999995
846 +66666666666:+9:+599999999994
847 +77777777777:+9:+699999999993
848 +88888888888:+9:+799999999992
849 +99999999999:+9:+899999999991
851 +12345:+12345:+152399025
852 +99999:+11111:+1111088889
872 +1000000000:+9:+111111111
873 +2000000000:+9:+222222222
874 +3000000000:+9:+333333333
875 +4000000000:+9:+444444444
876 +5000000000:+9:+555555555
877 +6000000000:+9:+666666666
878 +7000000000:+9:+777777777
879 +8000000000:+9:+888888888
880 +9000000000:+9:+1000000000
881 +35500000:+113:+314159
882 +71000000:+226:+314159
883 +106500000:+339:+314159
884 +1000000000:+3:+333333333
889 +999999999999:+9:+111111111111
890 +999999999999:+99:+10101010101
891 +999999999999:+999:+1001001001
892 +999999999999:+9999:+100010001
893 +999999999999999:+99999:+10000100001
894 +1111088889:+99999:+11111
937 +999999999999:+999:+0
938 +999999999999:+9999:+0
939 +999999999999999:+99999:+0
983 +281474976710656:+0:+0
984 +281474976710656:+1:+0
985 +281474976710656:+281474976710656:+281474976710656
992 +281474976710656:+0:+281474976710656
993 +281474976710656:+1:+281474976710657
994 +281474976710656:+281474976710656:+281474976710656
1001 +281474976710656:+0:+281474976710656
1002 +281474976710656:+1:+281474976710657
1003 +281474976710656:+281474976710656:+0
1008 +281474976710656:-281474976710657
1078 +inf:1234500012:+inf
1079 -inf:1234500012:-inf
1080 +inf:-12345000123:+inf
1081 -inf:-12345000123:-inf
1082 # 1 ** -x => 1 / (1 ** x)
1102 10:20:100000000000000000000
1103 123456:2:15241383936
1110 10000000000000000:17
1120 4000000000000:2000000
1126 $round_mode('trunc')
1132 +10123456789:5:+10123000000
1133 -10123456789:5:-10123000000
1134 +10123456789:9:+10123456700
1135 -10123456789:9:-10123456700
1136 +101234500:6:+101234000
1137 -101234500:6:-101234000
1138 #+101234500:-4:+101234000
1139 #-101234500:-4:-101234000
1141 +20123456789:5:+20123000000
1142 -20123456789:5:-20123000000
1143 +20123456789:9:+20123456800
1144 -20123456789:9:-20123456800
1145 +201234500:6:+201234000
1146 -201234500:6:-201234000
1147 #+201234500:-4:+201234000
1148 #-201234500:-4:-201234000
1149 +12345000:4:12340000
1150 -12345000:4:-12340000
1152 +30123456789:5:+30123000000
1153 -30123456789:5:-30123000000
1154 +30123456789:9:+30123456800
1155 -30123456789:9:-30123456800
1156 +301234500:6:+301235000
1157 -301234500:6:-301234000
1158 #+301234500:-4:+301235000
1159 #-301234500:-4:-301234000
1160 +12345000:4:12350000
1161 -12345000:4:-12340000
1163 +40123456789:5:+40123000000
1164 -40123456789:5:-40123000000
1165 +40123456789:9:+40123456800
1166 -40123456789:9:-40123456800
1167 +401234500:6:+401234000
1168 +401234500:6:+401234000
1169 #-401234500:-4:-401235000
1170 #-401234500:-4:-401235000
1171 +12345000:4:12340000
1172 -12345000:4:-12350000
1174 +50123456789:5:+50123000000
1175 -50123456789:5:-50123000000
1176 +50123456789:9:+50123456800
1177 -50123456789:9:-50123456800
1178 +501234500:6:+501235000
1179 -501234500:6:-501235000
1180 #+501234500:-4:+501235000
1181 #-501234500:-4:-501235000
1182 +12345000:4:12350000
1183 -12345000:4:-12350000
1185 +60123456789:5:+60123000000
1186 -60123456789:5:-60123000000
1187 +60123456789:9:+60123456800
1188 -60123456789:9:-60123456800
1189 +601234500:6:+601234000
1190 -601234500:6:-601234000
1191 #+601234500:-4:+601234000
1192 #-601234500:-4:-601234000
1199 +12345000:4:12340000
1200 -12345000:4:-12340000
1213 # floor and ceil tests are pretty pointless in integer space...but play safe