diff options
author | Ricardo Signes <rjbs@cpan.org> | 2015-09-05 10:20:42 -0400 |
---|---|---|
committer | Ricardo Signes <rjbs@cpan.org> | 2015-09-06 15:31:21 -0400 |
commit | 6b0f9b46aaa4198a1e8ed620a940f4f2bd304859 (patch) | |
tree | 5aa61fa3f398429b6c615b38c91a4232a6139d29 /cpan/Math-BigInt-FastCalc | |
parent | 361334bbfb74d04f2a6294a43be24dbe29ba16c0 (diff) | |
download | perl-6b0f9b46aaa4198a1e8ed620a940f4f2bd304859.tar.gz |
move Math-Big* from ./dist to ./cpan
Diffstat (limited to 'cpan/Math-BigInt-FastCalc')
-rw-r--r-- | cpan/Math-BigInt-FastCalc/FastCalc.xs | 410 | ||||
-rw-r--r-- | cpan/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm | 112 | ||||
-rw-r--r-- | cpan/Math-BigInt-FastCalc/t/bigintfc.t | 430 | ||||
-rw-r--r-- | cpan/Math-BigInt-FastCalc/t/bootstrap.t | 8 | ||||
-rw-r--r-- | cpan/Math-BigInt-FastCalc/t/leak.t | 79 | ||||
-rw-r--r-- | cpan/Math-BigInt-FastCalc/t/mbi_rand.t | 55 |
6 files changed, 1094 insertions, 0 deletions
diff --git a/cpan/Math-BigInt-FastCalc/FastCalc.xs b/cpan/Math-BigInt-FastCalc/FastCalc.xs new file mode 100644 index 0000000000..a045c7172e --- /dev/null +++ b/cpan/Math-BigInt-FastCalc/FastCalc.xs @@ -0,0 +1,410 @@ +#define PERL_NO_GET_CONTEXT + +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" + +/* for Perl prior to v5.7.1 */ +#ifndef SvUOK +# define SvUOK(sv) SvIOK_UV(sv) +#endif + +/* for Perl v5.6 (RT #63859) */ +#ifndef croak_xs_usage +# define croak_xs_usage croak +#endif + +double XS_BASE = 0; +double XS_BASE_LEN = 0; + +MODULE = Math::BigInt::FastCalc PACKAGE = Math::BigInt::FastCalc + +PROTOTYPES: DISABLE + + ############################################################################# + # 2002-08-12 0.03 Tels unreleased + # * is_zero/is_one/is_odd/is_even/len work now (pass v1.61 tests) + # 2002-08-13 0.04 Tels unreleased + # * returns no/yes for is_foo() methods to be faster + # 2002-08-18 0.06alpha + # * added _num(), _inc() and _dec() + # 2002-08-25 0.06 Tels + # * added __strip_zeros(), _copy() + # 2004-08-13 0.07 Tels + # * added _is_two(), _is_ten(), _ten() + # 2007-04-02 0.08 Tels + # * plug leaks by creating mortals + # 2007-05-27 0.09 Tels + # * add _new() + +#define RETURN_MORTAL_INT(value) \ + ST(0) = sv_2mortal(newSViv(value)); \ + XSRETURN(1); + +BOOT: +{ + if (items < 4) + croak("Usage: Math::BigInt::FastCalc::BOOT(package, version, base_len, base)"); + XS_BASE_LEN = SvIV(ST(2)); + XS_BASE = SvNV(ST(3)); +} + +############################################################################## +# _new + +SV * +_new(class, x) + SV* x + INIT: + STRLEN len; + char* cur; + STRLEN part_len; + AV *av = newAV(); + + CODE: + if (SvUOK(x) && SvUV(x) < XS_BASE) + { + /* shortcut for integer arguments */ + av_push (av, newSVuv( SvUV(x) )); + } + else + { + /* split the input (as string) into XS_BASE_LEN long parts */ + /* in perl: + [ reverse(unpack("a" . ($il % $BASE_LEN+1) + . ("a$BASE_LEN" x ($il / $BASE_LEN)), $_[1])) ]; + */ + cur = SvPV(x, len); /* convert to string & store length */ + cur += len; /* doing "cur = SvEND(x)" does not work! */ + # process the string from the back + while (len > 0) + { + /* use either BASE_LEN or the amount of remaining digits */ + part_len = (STRLEN) XS_BASE_LEN; + if (part_len > len) + { + part_len = len; + } + /* processed so many digits */ + cur -= part_len; + len -= part_len; + /* printf ("part '%s' (part_len: %i, len: %i, BASE_LEN: %i)\n", cur, part_len, len, XS_BASE_LEN); */ + if (part_len > 0) + { + av_push (av, newSVpvn(cur, part_len) ); + } + } + } + RETVAL = newRV_noinc((SV *)av); + OUTPUT: + RETVAL + +############################################################################## +# _copy + +void +_copy(class, x) + SV* x + INIT: + AV* a; + AV* a2; + SSize_t elems; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + elems = av_len(a); /* number of elems in array */ + a2 = (AV*)sv_2mortal((SV*)newAV()); + av_extend (a2, elems); /* pre-padd */ + while (elems >= 0) + { + /* av_store( a2, elems, newSVsv( (SV*)*av_fetch(a, elems, 0) ) ); */ + + /* looking and trying to preserve IV is actually slower when copying */ + /* temp = (SV*)*av_fetch(a, elems, 0); + if (SvIOK(temp)) + { + av_store( a2, elems, newSViv( SvIV( (SV*)*av_fetch(a, elems, 0) ))); + } + else + { + av_store( a2, elems, newSVnv( SvNV( (SV*)*av_fetch(a, elems, 0) ))); + } + */ + av_store( a2, elems, newSVnv( SvNV( (SV*)*av_fetch(a, elems, 0) ))); + elems--; + } + ST(0) = sv_2mortal( newRV_inc((SV*) a2) ); + +############################################################################## +# __strip_zeros (also check for empty arrays from div) + +void +__strip_zeros(x) + SV* x + INIT: + AV* a; + SV* temp; + SSize_t elems; + SSize_t index; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + elems = av_len(a); /* number of elems in array */ + ST(0) = x; /* we return x */ + if (elems == -1) + { + av_push (a, newSViv(0)); /* correct empty arrays */ + XSRETURN(1); + } + if (elems == 0) + { + XSRETURN(1); /* nothing to do since only one elem */ + } + index = elems; + while (index > 0) + { + temp = *av_fetch(a, index, 0); /* fetch ptr to current element */ + if (SvNV(temp) != 0) + { + break; + } + index--; + } + if (index < elems) + { + index = elems - index; + while (index-- > 0) + { + av_pop (a); + } + } + XSRETURN(1); + +############################################################################## +# decrement (subtract one) + +void +_dec(class,x) + SV* x + INIT: + AV* a; + SV* temp; + SSize_t elems; + SSize_t index; + NV MAX; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + elems = av_len(a); /* number of elems in array */ + ST(0) = x; /* we return x */ + + MAX = XS_BASE - 1; + index = 0; + while (index <= elems) + { + temp = *av_fetch(a, index, 0); /* fetch ptr to current element */ + sv_setnv (temp, SvNV(temp)-1); /* decrement */ + if (SvNV(temp) >= 0) + { + break; /* early out */ + } + sv_setnv (temp, MAX); /* overflow, so set this to $MAX */ + index++; + } + /* do have more than one element? */ + /* (more than one because [0] should be kept as single-element) */ + if (elems > 0) + { + temp = *av_fetch(a, elems, 0); /* fetch last element */ + if (SvIV(temp) == 0) /* did last elem overflow? */ + { + av_pop(a); /* yes, so shrink array */ + /* aka remove leading zeros */ + } + } + XSRETURN(1); /* return x */ + +############################################################################## +# increment (add one) + +void +_inc(class,x) + SV* x + INIT: + AV* a; + SV* temp; + SSize_t elems; + SSize_t index; + NV BASE; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + elems = av_len(a); /* number of elems in array */ + ST(0) = x; /* we return x */ + + BASE = XS_BASE; + index = 0; + while (index <= elems) + { + temp = *av_fetch(a, index, 0); /* fetch ptr to current element */ + sv_setnv (temp, SvNV(temp)+1); + if (SvNV(temp) < BASE) + { + XSRETURN(1); /* return (early out) */ + } + sv_setiv (temp, 0); /* overflow, so set this elem to 0 */ + index++; + } + temp = *av_fetch(a, elems, 0); /* fetch last element */ + if (SvIV(temp) == 0) /* did last elem overflow? */ + { + av_push(a, newSViv(1)); /* yes, so extend array by 1 */ + } + XSRETURN(1); /* return x */ + +############################################################################## + +SV * +_zero(class) + ALIAS: + _one = 1 + _two = 2 + _ten = 10 + PREINIT: + AV *av = newAV(); + CODE: + av_push (av, newSViv( ix )); + RETVAL = newRV_noinc((SV *)av); + OUTPUT: + RETVAL + +############################################################################## + +void +_is_even(class, x) + SV* x + ALIAS: + _is_odd = 1 + INIT: + AV* a; + SV* temp; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + temp = *av_fetch(a, 0, 0); /* fetch first element */ + ST(0) = sv_2mortal(boolSV((SvIV(temp) & 1) == ix)); + +############################################################################## + +void +_is_zero(class, x) + SV* x + ALIAS: + _is_one = 1 + _is_two = 2 + _is_ten = 10 + INIT: + AV* a; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + if ( av_len(a) != 0) + { + ST(0) = &PL_sv_no; /* len != 1, can't be '0' */ + } + else + { + SV *const temp = *av_fetch(a, 0, 0); /* fetch first element */ + ST(0) = boolSV(SvIV(temp) == ix); + } + XSRETURN(1); + +############################################################################## + +void +_len(class,x) + SV* x + INIT: + AV* a; + SV* temp; + IV elems; + STRLEN len; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + elems = av_len(a); /* number of elems in array */ + temp = *av_fetch(a, elems, 0); /* fetch last element */ + SvPV(temp, len); /* convert to string & store length */ + len += (IV) XS_BASE_LEN * elems; + ST(0) = sv_2mortal(newSViv(len)); + +############################################################################## + +void +_acmp(class, cx, cy); + SV* cx + SV* cy + INIT: + AV* array_x; + AV* array_y; + SSize_t elemsx, elemsy, diff; + SV* tempx; + SV* tempy; + STRLEN lenx; + STRLEN leny; + NV diff_nv; + SSize_t diff_str; + + CODE: + array_x = (AV*)SvRV(cx); /* ref to aray, don't check ref */ + array_y = (AV*)SvRV(cy); /* ref to aray, don't check ref */ + elemsx = av_len(array_x); + elemsy = av_len(array_y); + diff = elemsx - elemsy; /* difference */ + + if (diff > 0) + { + RETURN_MORTAL_INT(1); /* len differs: X > Y */ + } + else if (diff < 0) + { + RETURN_MORTAL_INT(-1); /* len differs: X < Y */ + } + /* both have same number of elements, so check length of last element + and see if it differs */ + tempx = *av_fetch(array_x, elemsx, 0); /* fetch last element */ + tempy = *av_fetch(array_y, elemsx, 0); /* fetch last element */ + SvPV(tempx, lenx); /* convert to string & store length */ + SvPV(tempy, leny); /* convert to string & store length */ + diff_str = (SSize_t)lenx - (SSize_t)leny; + if (diff_str > 0) + { + RETURN_MORTAL_INT(1); /* same len, but first elems differs in len */ + } + if (diff_str < 0) + { + RETURN_MORTAL_INT(-1); /* same len, but first elems differs in len */ + } + /* same number of digits, so need to make a full compare */ + diff_nv = 0; + while (elemsx >= 0) + { + tempx = *av_fetch(array_x, elemsx, 0); /* fetch curr x element */ + tempy = *av_fetch(array_y, elemsx, 0); /* fetch curr y element */ + diff_nv = SvNV(tempx) - SvNV(tempy); + if (diff_nv != 0) + { + break; + } + elemsx--; + } + if (diff_nv > 0) + { + RETURN_MORTAL_INT(1); + } + if (diff_nv < 0) + { + RETURN_MORTAL_INT(-1); + } + ST(0) = sv_2mortal(newSViv(0)); /* X and Y are equal */ + diff --git a/cpan/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm b/cpan/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm new file mode 100644 index 0000000000..9bf5a60839 --- /dev/null +++ b/cpan/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm @@ -0,0 +1,112 @@ +package Math::BigInt::FastCalc; + +use 5.006; +use strict; +use warnings; + +use Math::BigInt::Calc 1.997; + +use vars '$VERSION'; + +$VERSION = '0.31'; + +############################################################################## +# global constants, flags and accessory + +# announce that we are compatible with MBI v1.83 and up +sub api_version () { 2; } + +# use Calc to override the methods that we do not provide in XS + +for my $method (qw/ + str num + add sub mul div + rsft lsft + mod modpow modinv + gcd + pow root sqrt log_int fac nok + digit check + from_hex from_bin from_oct as_hex as_bin as_oct + zeros base_len + xor or and + alen 1ex + /) + { + no strict 'refs'; + *{'Math::BigInt::FastCalc::_' . $method} = \&{'Math::BigInt::Calc::_' . $method}; + } + +require XSLoader; +XSLoader::load(__PACKAGE__, $VERSION, Math::BigInt::Calc::_base_len()); + +############################################################################## +############################################################################## + +1; +__END__ +=pod + +=head1 NAME + +Math::BigInt::FastCalc - Math::BigInt::Calc with some XS for more speed + +=head1 SYNOPSIS + +Provides support for big integer calculations. Not intended to be used by +other modules. Other modules which sport the same functions can also be used +to support Math::BigInt, like L<Math::BigInt::GMP> or L<Math::BigInt::Pari>. + +=head1 DESCRIPTION + +In order to allow for multiple big integer libraries, Math::BigInt was +rewritten to use library modules for core math routines. Any module which +follows the same API as this can be used instead by using the following: + + use Math::BigInt lib => 'libname'; + +'libname' is either the long name ('Math::BigInt::Pari'), or only the short +version like 'Pari'. To use this library: + + use Math::BigInt lib => 'FastCalc'; + +Note that from L<Math::BigInt> v1.76 onwards, FastCalc will be loaded +automatically, if possible. + +=head1 STORAGE + +FastCalc works exactly like Calc, in stores the numbers in decimal form, +chopped into parts. + +=head1 METHODS + +The following functions are now implemented in FastCalc.xs: + + _is_odd _is_even _is_one _is_zero + _is_two _is_ten + _zero _one _two _ten + _acmp _len + _inc _dec + __strip_zeros _copy + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 AUTHORS + +Original math code by Mark Biggar, rewritten by Tels L<http://bloodgate.com/> +in late 2000. +Separated from BigInt and shaped API with the help of John Peacock. + +Fixed, sped-up and enhanced by Tels http://bloodgate.com 2001-2003. +Further streamlining (api_version 1 etc.) by Tels 2004-2007. + +Bug-fixing by Peter John Acklam E<lt>pjacklam@online.noE<gt> 2010-2011. + +=head1 SEE ALSO + +L<Math::BigInt>, L<Math::BigFloat>, +L<Math::BigInt::GMP>, L<Math::BigInt::FastCalc> and L<Math::BigInt::Pari>. + +=cut diff --git a/cpan/Math-BigInt-FastCalc/t/bigintfc.t b/cpan/Math-BigInt-FastCalc/t/bigintfc.t new file mode 100644 index 0000000000..c8751ad81b --- /dev/null +++ b/cpan/Math-BigInt-FastCalc/t/bigintfc.t @@ -0,0 +1,430 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 359; + +use Math::BigInt::FastCalc; + +my ($BASE_LEN, undef, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN_SMALL, $MAX_VAL) = + Math::BigInt::FastCalc->_base_len(); + +print "# BASE_LEN = $BASE_LEN\n"; +print "# MAX_VAL = $MAX_VAL\n"; +print "# AND_BITS = $AND_BITS\n"; +print "# XOR_BITS = $XOR_BITS\n"; +print "# IOR_BITS = $OR_BITS\n"; + +# testing of Math::BigInt::FastCalc + +my $C = 'Math::BigInt::FastCalc'; # pass classname to sub's + +# _new and _str +my $x = $C->_new("123"); my $y = $C->_new("321"); +is (ref($x),'ARRAY'); is ($C->_str($x),123); is ($C->_str($y),321); + +############################################################################### +# _add, _sub, _mul, _div +is ($C->_str($C->_add($x,$y)),444); +is ($C->_str($C->_sub($x,$y)),123); +is ($C->_str($C->_mul($x,$y)),39483); +is ($C->_str($C->_div($x,$y)),123); + +############################################################################### +# check that mul/div doesn't change $y +# and returns the same reference, not something new +is ($C->_str($C->_mul($x,$y)),39483); +is ($C->_str($x),39483); is ($C->_str($y),321); + +is ($C->_str($C->_div($x,$y)),123); +is ($C->_str($x),123); is ($C->_str($y),321); + +$x = $C->_new("39483"); +my ($x1,$r1) = $C->_div($x,$y); +is ("$x1","$x"); +$C->_inc($x1); +is ("$x1","$x"); +is ($C->_str($r1),'0'); + +$x = $C->_new("39483"); # reset + +############################################################################### +my $z = $C->_new("2"); +is ($C->_str($C->_add($x,$z)),39485); +my ($re,$rr) = $C->_div($x,$y); + +is ($C->_str($re),123); is ($C->_str($rr),2); + +# is_zero, _is_one, _one, _zero +is ($C->_is_zero($x),''); +is ($C->_is_one($x),''); + +is ($C->_str($C->_zero()),"0"); +is ($C->_str($C->_one()),"1"); + +# _two() and _ten() +is ($C->_str($C->_two()),"2"); +is ($C->_str($C->_ten()),"10"); +is ($C->_is_ten($C->_two()),''); +is ($C->_is_two($C->_two()),1); +is ($C->_is_ten($C->_ten()),1); +is ($C->_is_two($C->_ten()),''); + +is ($C->_is_one($C->_one()),1); +is ($C->_is_one($C->_two()), ''); +is ($C->_is_one($C->_ten()), ''); + +is ($C->_is_one($C->_zero()), ''); + +is ($C->_is_zero($C->_zero()),1); + +is ($C->_is_zero($C->_one()), ''); + +# is_odd, is_even +is ($C->_is_odd($C->_one()),1); is ($C->_is_odd($C->_zero()),''); +is ($C->_is_even($C->_one()), ''); is ($C->_is_even($C->_zero()),1); + +# _len +for my $method (qw/_alen _len/) + { + $x = $C->_new("1"); is ($C->$method($x),1); + $x = $C->_new("12"); is ($C->$method($x),2); + $x = $C->_new("123"); is ($C->$method($x),3); + $x = $C->_new("1234"); is ($C->$method($x),4); + $x = $C->_new("12345"); is ($C->$method($x),5); + $x = $C->_new("123456"); is ($C->$method($x),6); + $x = $C->_new("1234567"); is ($C->$method($x),7); + $x = $C->_new("12345678"); is ($C->$method($x),8); + $x = $C->_new("123456789"); is ($C->$method($x),9); + + $x = $C->_new("8"); is ($C->$method($x),1); + $x = $C->_new("21"); is ($C->$method($x),2); + $x = $C->_new("321"); is ($C->$method($x),3); + $x = $C->_new("4321"); is ($C->$method($x),4); + $x = $C->_new("54321"); is ($C->$method($x),5); + $x = $C->_new("654321"); is ($C->$method($x),6); + $x = $C->_new("7654321"); is ($C->$method($x),7); + $x = $C->_new("87654321"); is ($C->$method($x),8); + $x = $C->_new("987654321"); is ($C->$method($x),9); + + $x = $C->_new("0"); is ($C->$method($x),1); + $x = $C->_new("20"); is ($C->$method($x),2); + $x = $C->_new("320"); is ($C->$method($x),3); + $x = $C->_new("4320"); is ($C->$method($x),4); + $x = $C->_new("54320"); is ($C->$method($x),5); + $x = $C->_new("654320"); is ($C->$method($x),6); + $x = $C->_new("7654320"); is ($C->$method($x),7); + $x = $C->_new("87654320"); is ($C->$method($x),8); + $x = $C->_new("987654320"); is ($C->$method($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 is ($C->_len($x),$i); + } + } + +# _digit +$x = $C->_new("123456789"); +is ($C->_digit($x,0),9); +is ($C->_digit($x,1),8); +is ($C->_digit($x,2),7); +is ($C->_digit($x,-1),1); +is ($C->_digit($x,-2),2); +is ($C->_digit($x,-3),3); + +# _copy +foreach (qw/ 1 12 123 1234 12345 123456 1234567 12345678 123456789/) + { + $x = $C->_new("$_"); + is ($C->_str($C->_copy($x)),"$_"); + is ($C->_str($x),"$_"); # did _copy destroy original x? + } + +# _zeros +$x = $C->_new("1256000000"); is ($C->_zeros($x),6); +$x = $C->_new("152"); is ($C->_zeros($x),0); +$x = $C->_new("123000"); is ($C->_zeros($x),3); +$x = $C->_new("0"); is ($C->_zeros($x),0); + +# _lsft, _rsft +$x = $C->_new("10"); $y = $C->_new("3"); +is ($C->_str($C->_lsft($x,$y,10)),10000); +$x = $C->_new("20"); $y = $C->_new("3"); +is ($C->_str($C->_lsft($x,$y,10)),20000); + +$x = $C->_new("128"); $y = $C->_new("4"); +is ($C->_str($C->_lsft($x,$y,2)), 128 << 4); + +$x = $C->_new("1000"); $y = $C->_new("3"); +is ($C->_str($C->_rsft($x,$y,10)),1); +$x = $C->_new("20000"); $y = $C->_new("3"); +is ($C->_str($C->_rsft($x,$y,10)),20); +$x = $C->_new("256"); $y = $C->_new("4"); +is ($C->_str($C->_rsft($x,$y,2)),256 >> 4); + +$x = $C->_new("6411906467305339182857313397200584952398"); +$y = $C->_new("45"); +is ($C->_str($C->_rsft($x,$y,10)),0); + +# _acmp +$x = $C->_new("123456789"); +$y = $C->_new("987654321"); +is ($C->_acmp($x,$y),-1); +is ($C->_acmp($y,$x),1); +is ($C->_acmp($x,$x),0); +is ($C->_acmp($y,$y),0); +$x = $C->_new("12"); +$y = $C->_new("12"); +is ($C->_acmp($x,$y),0); +$x = $C->_new("21"); +is ($C->_acmp($x,$y),1); +is ($C->_acmp($y,$x),-1); +$x = $C->_new("123456789"); +$y = $C->_new("1987654321"); +is ($C->_acmp($x,$y),-1); +is ($C->_acmp($y,$x),+1); + +$x = $C->_new("1234567890123456789"); +$y = $C->_new("987654321012345678"); +is ($C->_acmp($x,$y),1); +is ($C->_acmp($y,$x),-1); +is ($C->_acmp($x,$x),0); +is ($C->_acmp($y,$y),0); + +$x = $C->_new("1234"); +$y = $C->_new("987654321012345678"); +is ($C->_acmp($x,$y),-1); +is ($C->_acmp($y,$x),1); +is ($C->_acmp($x,$x),0); +is ($C->_acmp($y,$y),0); + +# _modinv +$x = $C->_new("8"); +$y = $C->_new("5033"); +my ($xmod,$sign) = $C->_modinv($x,$y); +is ($C->_str($xmod),'629'); # -629 % 5033 == 4404 +is ($sign, '-'); + +# _div +$x = $C->_new("3333"); $y = $C->_new("1111"); +is ($C->_str(scalar $C->_div($x,$y)),3); +$x = $C->_new("33333"); $y = $C->_new("1111"); ($x,$y) = $C->_div($x,$y); +is ($C->_str($x),30); is ($C->_str($y),3); +$x = $C->_new("123"); $y = $C->_new("1111"); +($x,$y) = $C->_div($x,$y); is ($C->_str($x),0); is ($C->_str($y),123); + +# _num +foreach (qw/1 12 123 1234 12345 1234567 12345678 123456789 1234567890/) + { + $x = $C->_new("$_"); + is (ref($x),'ARRAY'); is ($C->_str($x),"$_"); + $x = $C->_num($x); is (ref($x),''); is ($x,$_); + } + +# _sqrt +$x = $C->_new("144"); is ($C->_str($C->_sqrt($x)),'12'); +$x = $C->_new("144000000000000"); is ($C->_str($C->_sqrt($x)),'12000000'); + +# _root +$x = $C->_new("81"); my $n = $C->_new("3"); # 4*4*4 = 64, 5*5*5 = 125 +is ($C->_str($C->_root($x,$n)),'4'); # 4.xx => 4.0 +$x = $C->_new("81"); $n = $C->_new("4"); # 3*3*3*3 == 81 +is ($C->_str($C->_root($x,$n)),'3'); + +# _pow (and _root) +$x = $C->_new("0"); $n = $C->_new("3"); # 0 ** y => 0 +is ($C->_str($C->_pow($x,$n)), 0); +$x = $C->_new("3"); $n = $C->_new("0"); # x ** 0 => 1 +is ($C->_str($C->_pow($x,$n)), 1); +$x = $C->_new("1"); $n = $C->_new("3"); # 1 ** y => 1 +is ($C->_str($C->_pow($x,$n)), 1); +$x = $C->_new("5"); $n = $C->_new("1"); # x ** 1 => x +is ($C->_str($C->_pow($x,$n)), 5); + +$x = $C->_new("81"); $n = $C->_new("3"); # 81 ** 3 == 531441 +is ($C->_str($C->_pow($x,$n)),81 ** 3); + +is ($C->_str($C->_root($x,$n)),81); + +$x = $C->_new("81"); +is ($C->_str($C->_pow($x,$n)),81 ** 3); +is ($C->_str($C->_pow($x,$n)),'150094635296999121'); # 531441 ** 3 == + +is ($C->_str($C->_root($x,$n)),'531441'); +is ($C->_str($C->_root($x,$n)),'81'); + +$x = $C->_new("81"); $n = $C->_new("14"); +is ($C->_str($C->_pow($x,$n)),'523347633027360537213511521'); +is ($C->_str($C->_root($x,$n)),'81'); + +$x = $C->_new("523347633027360537213511520"); +is ($C->_str($C->_root($x,$n)),'80'); + +$x = $C->_new("523347633027360537213511522"); +is ($C->_str($C->_root($x,$n)),'81'); + +my $res = [ qw/ 9 31 99 316 999 3162 9999/ ]; + +# 99 ** 2 = 9801, 999 ** 2 = 998001 etc +for my $i (2 .. 9) + { + $x = '9' x $i; $x = $C->_new($x); + $n = $C->_new("2"); + my $rc = '9' x ($i-1). '8' . '0' x ($i-1) . '1'; + print "# _pow( ", '9' x $i, ", 2) \n" unless + is ($C->_str($C->_pow($x,$n)),$rc); + + if ($i <= 7) + { + $x = '9' x $i; $x = $C->_new($x); + $n = '9' x $i; $n = $C->_new($n); + print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless + is ($C->_str($C->_root($x,$n)),'1'); + + $x = '9' x $i; $x = $C->_new($x); + $n = $C->_new("2"); + print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless + is ($C->_str($C->_root($x,$n)), $res->[$i-2]); + } + } + +############################################################################## +# _fac +$x = $C->_new("0"); is ($C->_str($C->_fac($x)),'1'); +$x = $C->_new("1"); is ($C->_str($C->_fac($x)),'1'); +$x = $C->_new("2"); is ($C->_str($C->_fac($x)),'2'); +$x = $C->_new("3"); is ($C->_str($C->_fac($x)),'6'); +$x = $C->_new("4"); is ($C->_str($C->_fac($x)),'24'); +$x = $C->_new("5"); is ($C->_str($C->_fac($x)),'120'); +$x = $C->_new("10"); is ($C->_str($C->_fac($x)),'3628800'); +$x = $C->_new("11"); is ($C->_str($C->_fac($x)),'39916800'); +$x = $C->_new("12"); is ($C->_str($C->_fac($x)),'479001600'); +$x = $C->_new("13"); is ($C->_str($C->_fac($x)),'6227020800'); + +# test that _fac modifies $x in place for small arguments +$x = $C->_new("3"); $C->_fac($x); is ($C->_str($x),'6'); +$x = $C->_new("13"); $C->_fac($x); is ($C->_str($x),'6227020800'); + +############################################################################## +# _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 is ($C->_str($x),substr($_,0,length($_)-1) . '2'); + $C->_dec($x); is ($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 is ($C->_str($x),substr($_,0,length($_)-2) . '20'); + $C->_dec($x); is ($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 is ($C->_str($x), '1' . '0' x (length($_))); + $C->_dec($x); is ($C->_str($x),$_); + } + +$x = $C->_new("1000"); $C->_inc($x); is ($C->_str($x),'1001'); +$C->_dec($x); is ($C->_str($x),'1000'); + +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); is ($C->_str($x),$z); + +$x = '1' . '0' x $BL; $z = '9' x $BL; +$x = $C->_new($x); $C->_dec($x); is ($C->_str($x),$z); + +# should not happen: +# $x = $C->_new("-2"); $y = $C->_new("4"); is ($C->_acmp($x,$y),-1); + +############################################################################### +# _mod +$x = $C->_new("1000"); $y = $C->_new("3"); +is ($C->_str(scalar $C->_mod($x,$y)),1); +$x = $C->_new("1000"); $y = $C->_new("2"); +is ($C->_str(scalar $C->_mod($x,$y)),0); + +# _and, _or, _xor +$x = $C->_new("5"); $y = $C->_new("2"); +is ($C->_str(scalar $C->_xor($x,$y)),7); +$x = $C->_new("5"); $y = $C->_new("2"); +is ($C->_str(scalar $C->_or($x,$y)),7); +$x = $C->_new("5"); $y = $C->_new("3"); +is ($C->_str(scalar $C->_and($x,$y)),1); + +# _from_hex, _from_bin, _from_oct +is ($C->_str( $C->_from_hex("0xFf")),255); +is ($C->_str( $C->_from_bin("0b10101011")),160+11); +is ($C->_str( $C->_from_oct("0100")), 8*8); +is ($C->_str( $C->_from_oct("01000")), 8*8*8); +is ($C->_str( $C->_from_oct("010001")), 8*8*8*8+1); +is ($C->_str( $C->_from_oct("010007")), 8*8*8*8+7); + +# _as_hex, _as_bin, as_oct +is ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new("128")))), 128); +is ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new("128")))), 128); +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("128")))), 128); + +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("123456")))), 123456); +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("123456789")))), "123456789"); +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("1234567890123")))), "1234567890123"); + +# _1ex +is ($C->_str($C->_1ex(0)), "1"); +is ($C->_str($C->_1ex(1)), "10"); +is ($C->_str($C->_1ex(2)), "100"); +is ($C->_str($C->_1ex(12)), "1000000000000"); +is ($C->_str($C->_1ex(16)), "10000000000000000"); + +# _check +$x = $C->_new("123456789"); +is ($C->_check($x),0); +is ($C->_check(123),'123 is not a reference'); + +############################################################################### +# __strip_zeros + +{ + no strict 'refs'; + # correct empty arrays + $x = &{$C."::__strip_zeros"}([]); is (@$x,1); is ($x->[0],0); + # don't strip single elements + $x = &{$C."::__strip_zeros"}([0]); is (@$x,1); is ($x->[0],0); + $x = &{$C."::__strip_zeros"}([1]); is (@$x,1); is ($x->[0],1); + # don't strip non-zero elements + $x = &{$C."::__strip_zeros"}([0,1]); + is (@$x,2); is ($x->[0],0); is ($x->[1],1); + $x = &{$C."::__strip_zeros"}([0,1,2]); + is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); + + # but strip leading zeros + $x = &{$C."::__strip_zeros"}([0,1,2,0]); + is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); + + $x = &{$C."::__strip_zeros"}([0,1,2,0,0]); + is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); + + $x = &{$C."::__strip_zeros"}([0,1,2,0,0,0]); + is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); + + # collapse multiple zeros + $x = &{$C."::__strip_zeros"}([0,0,0,0]); + is (@$x,1); is ($x->[0],0); +} + +# done + +1; + diff --git a/cpan/Math-BigInt-FastCalc/t/bootstrap.t b/cpan/Math-BigInt-FastCalc/t/bootstrap.t new file mode 100644 index 0000000000..d73afcb47c --- /dev/null +++ b/cpan/Math-BigInt-FastCalc/t/bootstrap.t @@ -0,0 +1,8 @@ +#!/usr/bin/perl -w + +use Test::More tests => 1; + +BEGIN { + use_ok('Math::BigInt::FastCalc'); +} + diff --git a/cpan/Math-BigInt-FastCalc/t/leak.t b/cpan/Math-BigInt-FastCalc/t/leak.t new file mode 100644 index 0000000000..5db38e1dbd --- /dev/null +++ b/cpan/Math-BigInt-FastCalc/t/leak.t @@ -0,0 +1,79 @@ +#!/usr/bin/perl -w + +# Test for memory leaks. + +# XXX TODO: This test file doesn't actually seem to work! If you remove +# the sv_2mortal() in the XS file, it still happily passes all tests... + +use strict; +use Test::More tests => 22; + +use Math::BigInt::FastCalc; + +############################################################################# +package Math::BigInt::FastCalc::LeakCheck; +use parent qw(Math::BigInt::FastCalc); + +my $destroyed = 0; +sub DESTROY { $destroyed++; } + +############################################################################# +package main; + +for my $method (qw(_zero _one _two _ten)) + { + $destroyed = 0; + { + my $num = Math::BigInt::FastCalc::LeakCheck->$method(); + bless $num, "Math::BigInt::FastCalc::LeakCheck"; + } + is ($destroyed, 1, "$method does not leak memory"); + } + +my $num = Math::BigInt::FastCalc->_zero(); +for my $method (qw(_is_zero _is_one _is_two _is_ten _num)) + { + $destroyed = 0; + { + my $rc = Math::BigInt::FastCalc->$method($num); + bless \$rc, "Math::BigInt::FastCalc::LeakCheck"; + } + is ($destroyed, 1, "$method does not leak memory"); + } + +my $num_10 = Math::BigInt::FastCalc->_ten(); +my $num_2 = Math::BigInt::FastCalc->_two(); + +my $num_long = Math::BigInt::FastCalc->_new("1234567890"); +my $num_long_2 = Math::BigInt::FastCalc->_new("12345678900987654321"); + +is (Math::BigInt::FastCalc->_str($num_long), "1234567890"); +is (Math::BigInt::FastCalc->_str($num_long_2), "12345678900987654321"); + +# to hit all possible code branches +_test_acmp($num, $num); +_test_acmp($num_10, $num_10); +_test_acmp($num, $num_10); +_test_acmp($num_10, $num); +_test_acmp($num, $num_2); +_test_acmp($num_2, $num); +_test_acmp($num_long, $num); +_test_acmp($num, $num_long); +_test_acmp($num_long, $num_long); +_test_acmp($num_long, $num_long_2); +_test_acmp($num_long_2, $num_long); + +sub _test_acmp + { + my ($n1,$n2) = @_; + + $destroyed = 0; + { + my $rc = Math::BigInt::FastCalc->_acmp($n1,$n2); + bless \$rc, "Math::BigInt::FastCalc::LeakCheck"; + } + my $n_1 = Math::BigInt::FastCalc->_str($n1); + my $n_2 = Math::BigInt::FastCalc->_str($n2); + is ($destroyed, 1, "_acmp($n_1,$n_2) does not leak memory"); + } + diff --git a/cpan/Math-BigInt-FastCalc/t/mbi_rand.t b/cpan/Math-BigInt-FastCalc/t/mbi_rand.t new file mode 100644 index 0000000000..4ad473d882 --- /dev/null +++ b/cpan/Math-BigInt-FastCalc/t/mbi_rand.t @@ -0,0 +1,55 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More; + +my $count = 128; + +plan(($^O eq 'os390') + ? (skip_all => 'takes too long on os390') : (tests => $count*2)); + +use Math::BigInt lib => 'FastCalc'; +my $c = 'Math::BigInt'; + +my $length = 128; + +# If you get a failure here, please re-run the test with the printed seed +# value as input: perl t/mbi_rand.t seed + +my $seed = ($#ARGV == 0) ? $ARGV[0] : int(rand(65537)); +print "# seed: $seed\n"; srand($seed); + +my ($A,$B,$As,$Bs,$ADB,$AMB,$la,$lb); +my $two = Math::BigInt->new(2); +for (my $i = 0; $i < $count; $i++) + { + # length of A and B + $la = int(rand($length)+1); $lb = int(rand($length)+1); + $As = ''; $Bs = ''; + # we create the numbers from "patterns", e.g. get a random number and a + # random count and string them together. This means things like + # "100000999999999999911122222222" are much more likely. If we just strung + # together digits, we would end up with "1272398823211223" etc. + while (length($As) < $la) { $As .= int(rand(100)) x int(rand(16)); } + while (length($Bs) < $lb) { $Bs .= int(rand(100)) x int(rand(16)); } + $As =~ s/^0+//; $Bs =~ s/^0+//; + $As = $As || '0'; $Bs = $Bs || '0'; + # print "# As $As\n# Bs $Bs\n"; + $A = $c->new($As); $B = $c->new($Bs); + # print "# A $A\n# B $B\n"; + if ($A->is_zero() || $B->is_zero()) + { + is (1,1); is (1,1); next; + } + # check that int(A/B)*B + A % B == A holds for all inputs + # $X = ($A/$B)*$B + 2 * ($A % $B) - ($A % $B); + ($ADB,$AMB) = $A->copy()->bdiv($B); + print "# ". join(' ',Math::BigInt::Calc->_base_len()),"\n" + unless is ($ADB*$B+$two*$AMB-$AMB,$As); + # swap 'em and try this, too + # $X = ($B/$A)*$A + $B % $A; + ($ADB,$AMB) = $B->copy()->bdiv($A); + print "# ". join(' ',Math::BigInt::Calc->_base_len()),"\n" + unless is ($ADB*$A+$two*$AMB-$AMB,$Bs); + } + |