📄 upgrade.inc
字号:
# include this file into another for subclass testing# This file is nearly identical to bigintpm.t, except that certain results# are _requird_ to be different due to "upgrading" or "promoting" to BigFloat.# The reverse is not true, any unmarked results can be either BigInt or# BigFloat, depending on how good the internal optimization is (e.g. it# is usually desirable to have 2 ** 2 return a BigInt, not a BigFloat).# Results that are required to be BigFloat are marked with C<^> at the end.# Please note that the testcount goes up by two for each extra result marked# with ^, since then we test whether it has the proper class and that it left# the upgrade variable alone.my $version = ${"$class\::VERSION"};############################################################################### for testing inheritance of _swappackage Math::Foo;use Math::BigInt lib => $main::CL;use vars qw/@ISA/;@ISA = (qw/Math::BigInt/);use overload# customized overload for sub, since original does not use swap there'-' => sub { my @a = ref($_[0])->_swap(@_); $a[0]->bsub($a[1])};sub _swap { # a fake _swap, which reverses the params my $self = shift; # for override in subclass if ($_[2]) { my $c = ref ($_[0] ) || 'Math::Foo'; return ( $_[0]->copy(), $_[1] ); } else { return ( Math::Foo->new($_[1]), $_[0] ); } }##############################################################################package main;my $CALC = $class->config()->{lib}; ok ($CALC,$CL);my ($f,$z,$a,$exp,@a,$m,$e,$round_mode,$expected_class);while (<DATA>) { $_ =~ s/[\n\r]//g; # remove newlines next if /^#/; # skip comments if (s/^&//) { $f = $_; next; } elsif (/^\$/) { $round_mode = $_; $round_mode =~ s/^\$/$class\->/; next; } @args = split(/:/,$_,99); $ans = pop(@args); $expected_class = $class; if ($ans =~ /\^$/) { $expected_class = $ECL; $ans =~ s/\^$//; } $try = "\$x = $class->new(\"$args[0]\");"; if ($f eq "bnorm") { $try = "\$x = $class->bnorm(\"$args[0]\");"; # some is_xxx tests } elsif ($f =~ /^is_(zero|one|odd|even|negative|positive|nan|int)$/) { $try .= "\$x->$f();"; } elsif ($f eq "as_hex") { $try .= '$x->as_hex();'; } elsif ($f eq "as_bin") { $try .= '$x->as_bin();'; } elsif ($f eq "is_inf") { $try .= "\$x->is_inf('$args[1]');"; } elsif ($f eq "binf") { $try .= "\$x->binf('$args[1]');"; } elsif ($f eq "bone") { $try .= "\$x->bone('$args[1]');"; # some unary ops } elsif ($f =~ /^b(nan|floor|ceil|sstr|neg|abs|inc|dec|not|sqrt|fac)$/) { $try .= "\$x->$f();"; } elsif ($f eq "length") { $try .= '$x->length();'; } elsif ($f eq "exponent"){ # ->bstr() to see if an object is returned $try .= '$x = $x->exponent()->bstr();'; } elsif ($f eq "mantissa"){ # ->bstr() to see if an object is returned $try .= '$x = $x->mantissa()->bstr();'; } elsif ($f eq "parts"){ $try .= '($m,$e) = $x->parts();'; # ->bstr() to see if an object is returned $try .= '$m = $m->bstr(); $m = "NaN" if !defined $m;'; $try .= '$e = $e->bstr(); $e = "NaN" if !defined $e;'; $try .= '"$m,$e";'; } else { if ($args[1] !~ /\./) { $try .= "\$y = $class->new(\"$args[1]\");"; # BigInt } else { $try .= "\$y = $ECL->new(\"$args[1]\");"; # BigFloat } if ($f eq "bcmp") { $try .= '$x->bcmp($y);'; } elsif ($f eq "bround") { $try .= "$round_mode; \$x->bround(\$y);"; } elsif ($f eq "broot") { $try .= "\$x->broot(\$y);"; } elsif ($f eq "bacmp"){ $try .= '$x->bacmp($y);'; } elsif ($f eq "badd"){ $try .= '$x + $y;'; } elsif ($f eq "bsub"){ $try .= '$x - $y;'; } elsif ($f eq "bmul"){ $try .= '$x * $y;'; } elsif ($f eq "bdiv"){ $try .= '$x / $y;'; } elsif ($f eq "bdiv-list"){ $try .= 'join (",",$x->bdiv($y));'; # overload via x= } elsif ($f =~ /^.=$/){ $try .= "\$x $f \$y;"; # overload via x } elsif ($f =~ /^.$/){ $try .= "\$x $f \$y;"; } elsif ($f eq "bmod"){ $try .= '$x % $y;'; } elsif ($f eq "bgcd") { if (defined $args[2]) { $try .= " \$z = $class->new('$args[2]'); "; } $try .= "$class\::bgcd(\$x, \$y"; $try .= ", \$z" if (defined $args[2]); $try .= " );"; } elsif ($f eq "blcm") { if (defined $args[2]) { $try .= " \$z = $class->new('$args[2]'); "; } $try .= "$class\::blcm(\$x, \$y"; $try .= ", \$z" if (defined $args[2]); $try .= " );"; }elsif ($f eq "blsft"){ if (defined $args[2]) { $try .= "\$x->blsft(\$y,$args[2]);"; } else { $try .= "\$x << \$y;"; } }elsif ($f eq "brsft"){ if (defined $args[2]) { $try .= "\$x->brsft(\$y,$args[2]);"; } else { $try .= "\$x >> \$y;"; } }elsif ($f eq "band"){ $try .= "\$x & \$y;"; }elsif ($f eq "bior"){ $try .= "\$x | \$y;"; }elsif ($f eq "bxor"){ $try .= "\$x ^ \$y;"; }elsif ($f eq "bpow"){ $try .= "\$x ** \$y;"; }elsif ($f eq "digit"){ $try = "\$x = $class->new('$args[0]'); \$x->digit($args[1]);"; } else { warn "Unknown op '$f'"; } } # end else all other ops $ans1 = eval $try; # convert hex/binary targets to decimal if ($ans =~ /^(0x0x|0b0b)/) { $ans =~ s/^0[xb]//; $ans = Math::BigInt->new($ans)->bstr(); } if ($ans eq "") { ok_undef ($ans1); } else { # print "try: $try ans: $ans1 $ans\n"; print "# Tried: '$try'\n" if !ok ($ans1, $ans); if ($expected_class ne $class) { ok (ref($ans1),$expected_class); # also checks that it really is ref! ok ($Math::BigInt::upgrade,'Math::BigFloat'); # still okay? } } # check internal state of number objects is_valid($ans1,$f) if ref $ans1; } # endwhile data testsclose DATA;my $warn = ''; $SIG{__WARN__} = sub { $warn = shift; };# these should not warn$warn = ''; eval "\$z = 3.17 <= \$y"; ok ($z, 1); ok ($warn, '');$warn = ''; eval "\$z = \$y >= 3.17"; ok ($z, 1); ok ($warn, '');# all tests done1;############################################################################################################################################################### Perl 5.005 does not like ok ($x,undef)sub ok_undef { my $x = shift; ok (1,1) and return if !defined $x; ok ($x,'undef'); }################################################################################ sub to check validity of a BigInt internally, to ensure that no op leaves a# number object in an invalid state (f.i. "-0")sub is_valid { my ($x,$f,$c) = @_; # The checks here are loosened a bit to allow BigInt or BigFloats to pass my $e = 0; # error? # ok as reference? # $e = "Not a reference to $c" if (ref($x) || '') ne $c; # has ok sign? $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'" if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/; $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0; #$e = $CALC->_check($x->{value}) if $e eq '0'; # test done, see if error did crop up ok (1,1), return if ($e eq '0'); ok (1,$e." after op '$f'"); }__DATA__&.=1234:-345:1234-345&+=1:2:3-1:-2:-3&-=1:2:-1-1:-2:1&*=2:3:6-1:5:-5&%=100:3:18:9:8&/=100:3:33.33333333333333333333333333333333333333-8:2:-4&|=2:1:3&&=5:7:5&^=5:7:2&is_negative0:0-1:11:0+inf:0-inf:1NaNneg:0&is_positive0:0-1:01:1+inf:1-inf:0NaNneg:0&is_oddabc:00:01:13:1-1:1-3:110000001:110000002:02:0120:0121:1&is_intNaN:0inf:0-inf:01:112:1123e12:1&is_evenabc:00:11:03:0-1:0-3:010000001:010000002:12:1120:1121:0&bacmp+0:-0:0+0:+1:-1-1:+1:0+1:-1:0-1:+2:-1+2:-1:1-123456789:+987654321:-1+123456789:-987654321:-1+987654321:+123456789:1-987654321:+123456789:1-123:+4567889:-1# NaNsacmpNaN:123:123:acmpNaN:acmpNaN:acmpNaN:# infinity+inf:+inf:0-inf:-inf:0+inf:-inf:0-inf:+inf:0+inf:123:1-inf:123:1+inf:-123:1-inf:-123:1# return undef+inf:NaN:NaN:inf:-inf:NaN:NaN:-inf:&bnorm123:12312.3:12.3^# binary input0babc:NaN0b123:NaN0b0:0-0b0:0-0b1:-10b0001:10b001:10b011:30b101:50b1001:90b10001:170b100001:330b1000001:650b10000001:1290b100000001:2570b1000000001:5130b10000000001:10250b100000000001:20490b1000000000001:40970b10000000000001:81930b100000000000001:163850b1000000000000001:327690b10000000000000001:655370b100000000000000001:1310730b1000000000000000001:2621450b10000000000000000001:5242890b100000000000000000001:10485770b1000000000000000000001:20971530b10000000000000000000001:41943050b100000000000000000000001:83886090b1000000000000000000000001:167772170b10000000000000000000000001:335544330b100000000000000000000000001:671088650b1000000000000000000000000001:1342177290b10000000000000000000000000001:2684354570b100000000000000000000000000001:5368709130b1000000000000000000000000000001:10737418250b10000000000000000000000000000001:21474836490b100000000000000000000000000000001:42949672970b1000000000000000000000000000000001:85899345930b10000000000000000000000000000000001:171798691850b_101:NaN0b1_0_1:50b0_0_0_1:1# hex input-0x0:00xabcdefgh:NaN0x1234:46600xabcdef:11259375-0xABCDEF:-11259375-0x1234:-46600x12345678:3054198960x1_2_3_4_56_78:3054198960xa_b_c_d_e_f:112593750x_123:NaN0x9:90x11:170x21:330x41:650x81:1290x101:2570x201:5130x401:10250x801:20490x1001:40970x2001:81930x4001:163850x8001:327690x10001:655370x20001:1310730x40001:2621450x80001:5242890x100001:10485770x200001:20971530x400001:41943050x800001:83886090x1000001:167772170x2000001:335544330x4000001:671088650x8000001:1342177290x10000001:2684354570x20000001:5368709130x40000001:10737418250x80000001:21474836490x100000001:42949672970x200000001:85899345930x400000001:171798691850x800000001:34359738369# inf inputinf:inf+inf:inf-inf:-inf0inf:NaN# abnormal input:NaNabc:NaN 1 a:NaN1bcd2:NaN11111b:NaN+1z:NaN-1z:NaN# only one underscore between two digits_123:NaN_123_:NaN123_:NaN1__23:NaN1E1__2:NaN1_E12:NaN1E_12:NaN1_E_12:NaN+_1E12:NaN+0_1E2:100+0_0_1E2:100-0_0_1E2:-100-0_0_1E+0_0_2:-100E1:NaNE23:NaN1.23E1:12.3^1.23E-1:0.123^# bug with two E's in number being valid1e2e3:NaN1e2r:NaN1e2.0:NaN# leading zeros012:120123:12301234:1234012345:123450123456:12345601234567:1234567012345678:123456780123456789:12345678901234567891:1234567891012345678912:123456789120123456789123:12345678912301234567891234:1234567891234# normal input0:0+0:0+00:0+000:0000000000000000000:0-0:0-0000:0+1:1+01:1+001:1+00000100000:100000123456789:123456789-1:-1-01:-1-001:-1-123456789:-123456789-00000100000:-1000001_2_3:12310000000000E-1_0:11E2:1001E1:101E0:11.23E2:123100E-1:10# floating point input# .2e2:201.E3:10001.01E2:1011010E-1:101-1010E0:-1010-1010E1:-101001234.00:1234# non-integer numbers-1010E-2:-10.1^-1.01E+1:-10.1^-1.01E-1:-0.101^&bnan1:NaN2:NaNabc:NaN&bone2:+:12:-:-1boneNaN:-:-1boneNaN:+:12:abc:13::1&binf1:+:inf2:-:-inf3:abc:inf&is_nan123:0abc:1NaN:1-123:0&is_inf+inf::1-inf::1abc::01::0NaN::0-1::0+inf:-:0+inf:+:1-inf:-:1-inf:+:0# it must be exactly /^[+-]inf$/+infinity::0-infinity::0&blsftabc:abc:NaN+2:+2:8+1:+32:4294967296+1:+48:281474976710656+8:-2:NaN# excercise base 10+12345:4:10:123450000-1234:0:10:-1234+1234:0:10:1234+2:2:10:200+12:2:10:1200+1234:-3:10:NaN1234567890123:12:10:1234567890123000000000000&brsftabc:abc:NaN+8:+2:2+4294967296:+32:1+281474976710656:+48:1+2:-2:NaN# excercise base 10-1234:0:10:-1234+1234:0:10:1234+200:2:10:2+1234:3:10:1+1234:2:10:12+1234:-3:10:NaN310000:4:10:3112300000:5:10:1231230000000000:10:10:12309876123456789067890:12:10:98761231234561234567890123:13:10:123456&bsstr1e+34:1e+34123.456E3:123456e+0100:1e+2abc:NaN&bnegbnegNaN:NaN+inf:-inf-inf:infabd:NaN0:01:-1-1:1+123456789:-123456789-123456789:123456789&babsbabsNaN:NaN+inf:inf-inf:inf0:01:1-1:1+123456789:123456789-123456789:123456789&bcmpbcmpNaN:bcmpNaN:bcmpNaN:0:0:bcmpNaN:0:0:0-1:0:-10:-1:11:0:10:1:-1-1:1:-11:-1:1-1:-1:01:1:0123:123:0123:12:112:123:-1-123:-123:0-123:-12:-1-12:-123:1123:124:-1124:123:1-123:-124:1-124:-123:-1100:5:1-123456789:987654321:-1+123456789:-987654321:1-987654321:123456789:-1-inf:5432112345:-1+inf:5432112345:1-inf:-5432112345:-1+inf:-5432112345:1+inf:+inf:0-inf:-inf:0+inf:-inf:1-inf:+inf:-15:inf:-15:inf:-1-5:-inf:1-5:-inf:1# return undef+inf:NaN:NaN:inf:-inf:NaN:NaN:-inf:&bincabc:NaN+inf:inf-inf:-inf+0:1+1:2-1:0&bdecabc:NaN+inf:inf-inf:-inf+0:-1+1:0-1:-2&baddabc:abc:NaNabc:0:NaN+0:abc:NaN+inf:-inf:NaN-inf:+inf:NaN+inf:+inf:inf-inf:-inf:-infbaddNaN:+inf:NaNbaddNaN:+inf:NaN+inf:baddNaN:NaN-inf:baddNaN:NaN0:0:01:0:10:1:11:1:2-1:0:-10:-1:-1-1:-1:-2-1:+1:0+1:-1:0+9:+1:10+99:+1:100+999:+1:1000+9999:+1:10000+99999:+1:100000+999999:+1:1000000+9999999:+1:10000000+99999999:+1:100000000+999999999:+1:1000000000+9999999999:+1:10000000000+99999999999:+1:100000000000+10:-1:9+100:-1:99+1000:-1:999+10000:-1:9999+100000:-1:99999+1000000:-1:999999+10000000:-1:9999999+100000000:-1:99999999+1000000000:-1:999999999+10000000000:-1:9999999999+123456789:987654321:1111111110-123456789:987654321:864197532-123456789:-987654321:-1111111110+123456789:-987654321:-8641975322:2.5:4.5^-123:-1.5:-124.5^-1.2:1:-0.2^&bsubabc:abc:NaNabc:+0:NaN+0:abc:NaN+inf:-inf:inf-inf:+inf:-inf+inf:+inf:NaN-inf:-inf:NaN+0:+0:0+1:+0:1+0:+1:-1+1:+1:0-1:+0:-1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -