summaryrefslogtreecommitdiff
path: root/lib/Math/BigInt/t/bigintc.t
diff options
context:
space:
mode:
authorTels <nospam-abuse@bloodgate.com>2002-08-21 21:12:59 +0200
committerhv <hv@crypt.org>2002-08-22 19:42:58 +0000
commit56d9de6816913bcb7b0068c30f7fa91039be4429 (patch)
tree31e0a05a2c61ad2046db94890888c91f2ef40ab5 /lib/Math/BigInt/t/bigintc.t
parent11f72409a81d362ab963d688ed5b84835e953fd8 (diff)
downloadperl-56d9de6816913bcb7b0068c30f7fa91039be4429.tar.gz
ANNOUNCE: Math-BigInt v1.62
Message-Id: <200208211513.g7LFDUs02512@crypt.org> p4raw-id: //depot/perl@17754
Diffstat (limited to 'lib/Math/BigInt/t/bigintc.t')
-rw-r--r--lib/Math/BigInt/t/bigintc.t169
1 files changed, 149 insertions, 20 deletions
diff --git a/lib/Math/BigInt/t/bigintc.t b/lib/Math/BigInt/t/bigintc.t
index 26530ca6e1..22e64c5f2c 100644
--- a/lib/Math/BigInt/t/bigintc.t
+++ b/lib/Math/BigInt/t/bigintc.t
@@ -7,22 +7,19 @@ BEGIN
{
$| = 1;
chdir 't' if -d 't';
- unshift @INC, '../lib'; # for running manually
+ unshift @INC, '../lib'; # for running manually
}
use Math::BigInt::Calc;
BEGIN
{
- my $additional = 0;
- $additional = 27 if $Math::BigInt::Calc::VERSION > 0.18;
- plan tests => 80 + $additional;
+ plan tests => 276;
}
-# testing of Math::BigInt::Calc, primarily for interface/api and not for the
-# math functionality
+# testing of Math::BigInt::Calc
-my $C = 'Math::BigInt::Calc'; # pass classname to sub's
+my $C = 'Math::BigInt::Calc'; # pass classname to sub's
# _new and _str
my $x = $C->_new(\"123"); my $y = $C->_new(\"321");
@@ -61,15 +58,54 @@ my ($re,$rr) = $C->_div($x,$y);
ok (${$C->_str($re)},123); ok (${$C->_str($rr)},2);
# is_zero, _is_one, _one, _zero
-ok ($C->_is_zero($x),0);
-ok ($C->_is_one($x),0);
+ok ($C->_is_zero($x)||0,0);
+ok ($C->_is_one($x)||0,0);
-ok ($C->_is_one($C->_one()),1); ok ($C->_is_one($C->_zero()),0);
-ok ($C->_is_zero($C->_zero()),1); ok ($C->_is_zero($C->_one()),0);
+ok (${$C->_str($C->_zero())},"0");
+ok (${$C->_str($C->_one())},"1");
+
+# _two() (only used internally)
+ok (${$C->_str($C->_two())},"2");
+
+ok ($C->_is_one($C->_one()),1);
+
+ok ($C->_is_one($C->_zero()) || 0,0);
+
+ok ($C->_is_zero($C->_zero()),1);
+
+ok ($C->_is_zero($C->_one()) || 0,0);
# is_odd, is_even
-ok ($C->_is_odd($C->_one()),1); ok ($C->_is_odd($C->_zero()),0);
-ok ($C->_is_even($C->_one()),0); ok ($C->_is_even($C->_zero()),1);
+ok ($C->_is_odd($C->_one()),1); ok ($C->_is_odd($C->_zero())||0,0);
+ok ($C->_is_even($C->_one()) || 0,0); ok ($C->_is_even($C->_zero()),1);
+
+# _len
+$x = $C->_new(\"1"); ok ($C->_len($x),1);
+$x = $C->_new(\"12"); ok ($C->_len($x),2);
+$x = $C->_new(\"123"); ok ($C->_len($x),3);
+$x = $C->_new(\"1234"); ok ($C->_len($x),4);
+$x = $C->_new(\"12345"); ok ($C->_len($x),5);
+$x = $C->_new(\"123456"); ok ($C->_len($x),6);
+$x = $C->_new(\"1234567"); ok ($C->_len($x),7);
+$x = $C->_new(\"12345678"); ok ($C->_len($x),8);
+$x = $C->_new(\"123456789"); ok ($C->_len($x),9);
+
+$x = $C->_new(\"8"); ok ($C->_len($x),1);
+$x = $C->_new(\"21"); ok ($C->_len($x),2);
+$x = $C->_new(\"321"); ok ($C->_len($x),3);
+$x = $C->_new(\"4321"); ok ($C->_len($x),4);
+$x = $C->_new(\"54321"); ok ($C->_len($x),5);
+$x = $C->_new(\"654321"); ok ($C->_len($x),6);
+$x = $C->_new(\"7654321"); ok ($C->_len($x),7);
+$x = $C->_new(\"87654321"); ok ($C->_len($x),8);
+$x = $C->_new(\"987654321"); ok ($C->_len($x),9);
+
+for (my $i = 1; $i < 9; $i++)
+ {
+ my $a = "$i" . '0' x ($i-1);
+ $x = $C->_new(\$a);
+ print "# Tried len '$a'\n" unless ok ($C->_len($x),$i);
+ }
# _digit
$x = $C->_new(\"123456789");
@@ -81,8 +117,12 @@ ok ($C->_digit($x,-2),2);
ok ($C->_digit($x,-3),3);
# _copy
-$x = $C->_new(\"12356");
-ok (${$C->_str($C->_copy($x))},12356);
+foreach (qw/ 1 12 123 1234 12345 123456 1234567 12345678 123456789/)
+ {
+ $x = $C->_new(\"$_");
+ ok (${$C->_str($C->_copy($x))},"$_");
+ ok (${$C->_str($x)},"$_"); # did _copy destroy original x?
+ }
# _zeros
$x = $C->_new(\"1256000000"); ok ($C->_zeros($x),6);
@@ -105,6 +145,10 @@ ok (${$C->_str($C->_rsft($x,$y,10))},20);
$x = $C->_new(\"256"); $y = $C->_new(\"4");
ok (${$C->_str($C->_rsft($x,$y,2))},256 >> 4);
+$x = $C->_new(\"6411906467305339182857313397200584952398");
+$y = $C->_new(\"45");
+ok (${$C->_str($C->_rsft($x,$y,10))},0);
+
# _acmp
$x = $C->_new(\"123456789");
$y = $C->_new(\"987654321");
@@ -113,6 +157,27 @@ ok ($C->_acmp($y,$x),1);
ok ($C->_acmp($x,$x),0);
ok ($C->_acmp($y,$y),0);
+$x = $C->_new(\"1234567890123456789");
+$y = $C->_new(\"987654321012345678");
+ok ($C->_acmp($x,$y),1);
+ok ($C->_acmp($y,$x),-1);
+ok ($C->_acmp($x,$x),0);
+ok ($C->_acmp($y,$y),0);
+
+$x = $C->_new(\"1234");
+$y = $C->_new(\"987654321012345678");
+ok ($C->_acmp($x,$y),-1);
+ok ($C->_acmp($y,$x),1);
+ok ($C->_acmp($x,$x),0);
+ok ($C->_acmp($y,$y),0);
+
+# _modinv
+$x = $C->_new(\"8");
+$y = $C->_new(\"5033");
+my ($xmod,$sign) = $C->_modinv($x,$y);
+ok (${$C->_str($xmod)},'629'); # -629 % 5033 == 4404
+ok ($sign, '-');
+
# _div
$x = $C->_new(\"3333"); $y = $C->_new(\"1111");
ok (${$C->_str(scalar $C->_div($x,$y))},3);
@@ -122,7 +187,12 @@ $x = $C->_new(\"123"); $y = $C->_new(\"1111");
($x,$y) = $C->_div($x,$y); ok (${$C->_str($x)},0); ok (${$C->_str($y)},123);
# _num
-$x = $C->_new(\"12345"); $x = $C->_num($x); ok (ref($x)||'',''); ok ($x,12345);
+foreach (qw/1 12 123 1234 12345 1234567 12345678 123456789 1234567890/)
+ {
+ $x = $C->_new(\"$_");
+ ok (ref($x)||'','ARRAY'); ok (${$C->_str($x)},"$_");
+ $x = $C->_num($x); ok (ref($x)||'',''); ok ($x,$_);
+ }
# _sqrt
$x = $C->_new(\"144"); ok (${$C->_str($C->_sqrt($x))},'12');
@@ -136,12 +206,41 @@ $x = $C->_new(\"4"); ok (${$C->_str($C->_fac($x))},'24');
$x = $C->_new(\"5"); ok (${$C->_str($C->_fac($x))},'120');
$x = $C->_new(\"10"); ok (${$C->_str($C->_fac($x))},'3628800');
$x = $C->_new(\"11"); ok (${$C->_str($C->_fac($x))},'39916800');
+$x = $C->_new(\"12"); ok (${$C->_str($C->_fac($x))},'479001600');
+
+##############################################################################
+# _inc and _dec
+foreach (qw/1 11 121 1231 12341 1234561 12345671 123456781 1234567891/)
+ {
+ $x = $C->_new(\"$_"); $C->_inc($x);
+ print "# \$x = ",${$C->_str($x)},"\n"
+ unless ok (${$C->_str($x)},substr($_,0,length($_)-1) . '2');
+ $C->_dec($x); ok (${$C->_str($x)},$_);
+ }
+foreach (qw/19 119 1219 12319 1234519 12345619 123456719 1234567819/)
+ {
+ $x = $C->_new(\"$_"); $C->_inc($x);
+ print "# \$x = ",${$C->_str($x)},"\n"
+ unless ok (${$C->_str($x)},substr($_,0,length($_)-2) . '20');
+ $C->_dec($x); ok (${$C->_str($x)},$_);
+ }
+foreach (qw/999 9999 99999 9999999 99999999 999999999 9999999999 99999999999/)
+ {
+ $x = $C->_new(\"$_"); $C->_inc($x);
+ print "# \$x = ",${$C->_str($x)},"\n"
+ unless ok (${$C->_str($x)}, '1' . '0' x (length($_)));
+ $C->_dec($x); ok (${$C->_str($x)},$_);
+ }
-# _inc
$x = $C->_new(\"1000"); $C->_inc($x); ok (${$C->_str($x)},'1001');
$C->_dec($x); ok (${$C->_str($x)},'1000');
-my $BL = Math::BigInt::Calc::_base_len();
+my $BL;
+{
+ no strict 'refs';
+ $BL = &{"$C"."::_base_len"}();
+}
+
$x = '1' . '0' x $BL;
$z = '1' . '0' x ($BL-1); $z .= '1';
$x = $C->_new(\$x); $C->_inc($x); ok (${$C->_str($x)},$z);
@@ -152,6 +251,7 @@ $x = $C->_new(\$x); $C->_dec($x); ok (${$C->_str($x)},$z);
# should not happen:
# $x = $C->_new(\"-2"); $y = $C->_new(\"4"); ok ($C->_acmp($x,$y),-1);
+###############################################################################
# _mod
$x = $C->_new(\"1000"); $y = $C->_new(\"3");
ok (${$C->_str(scalar $C->_mod($x,$y))},1);
@@ -180,9 +280,38 @@ ok ($C->_check($x),0);
ok ($C->_check(123),'123 is not a reference');
###############################################################################
-# _to_large and _to_small (last since they toy with BASE_LEN etc)
+# __strip_zeros
+
+{
+ no strict 'refs';
+ # correct empty arrays
+ $x = &{$C."::__strip_zeros"}([]); ok (@$x,1); ok ($x->[0],0);
+ # don't strip single elements
+ $x = &{$C."::__strip_zeros"}([0]); ok (@$x,1); ok ($x->[0],0);
+ $x = &{$C."::__strip_zeros"}([1]); ok (@$x,1); ok ($x->[0],1);
+ # don't strip non-zero elements
+ $x = &{$C."::__strip_zeros"}([0,1]);
+ ok (@$x,2); ok ($x->[0],0); ok ($x->[1],1);
+ $x = &{$C."::__strip_zeros"}([0,1,2]);
+ ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2);
+
+ # but strip leading zeros
+ $x = &{$C."::__strip_zeros"}([0,1,2,0]);
+ ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2);
+
+ $x = &{$C."::__strip_zeros"}([0,1,2,0,0]);
+ ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2);
+
+ $x = &{$C."::__strip_zeros"}([0,1,2,0,0,0]);
+ ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2);
+
+ # collapse multiple zeros
+ $x = &{$C."::__strip_zeros"}([0,0,0,0]);
+ ok (@$x,1); ok ($x->[0],0);
+}
-exit if $Math::BigInt::Calc::VERSION < 0.19;
+###############################################################################
+# _to_large and _to_small (last since they toy with BASE_LEN etc)
$C->_base_len(5,7); $x = [ qw/67890 12345 67890 12345/ ]; $C->_to_large($x);
ok (@$x,3);