Blame t/110_bignum.t

Packit a557cb
Packit a557cb
use strict;
Packit a557cb
use Test::More;
Packit a557cb
BEGIN { plan tests => 9 };
Packit a557cb
Packit a557cb
BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; }
Packit a557cb
Packit a557cb
use JSON -support_by_pp;
Packit a557cb
Packit a557cb
eval q| require Math::BigInt |;
Packit a557cb
Packit a557cb
SKIP: {
Packit a557cb
    skip "Can't load Math::BigInt.", 9 if ($@);
Packit a557cb
Packit a557cb
    my $v = Math::BigInt->VERSION;
Packit a557cb
    $v =~ s/_.+$// if $v;
Packit a557cb
Packit a557cb
my $fix =  !$v       ? '+'
Packit a557cb
          : $v < 1.6 ? '+'
Packit a557cb
          : '';
Packit a557cb
Packit a557cb
Packit a557cb
my $json = new JSON;
Packit a557cb
Packit a557cb
$json->allow_nonref->allow_bignum(1);
Packit a557cb
$json->convert_blessed->allow_blessed;
Packit a557cb
Packit a557cb
my $num  = $json->decode(q|100000000000000000000000000000000000000|);
Packit a557cb
Packit a557cb
ok($num->isa('Math::BigInt'));
Packit a557cb
is("$num", $fix . '100000000000000000000000000000000000000');
Packit a557cb
is($json->encode($num), $fix . '100000000000000000000000000000000000000');
Packit a557cb
Packit a557cb
SKIP: { skip "requires $JSON::BackendModule 2.91_03 or newer", 2 if $JSON::BackendModulePP and eval $JSON::BackendModulePP->VERSION < 2.91_03;
Packit a557cb
$num  = $json->decode(q|10|);
Packit a557cb
Packit a557cb
ok(!(ref $num and $num->isa('Math::BigInt')), 'small integer is not a BigInt');
Packit a557cb
ok(!(ref $num and $num->isa('Math::BigFloat')), 'small integer is not a BigFloat');
Packit a557cb
}
Packit a557cb
Packit a557cb
$num  = $json->decode(q|2.0000000000000000001|);
Packit a557cb
Packit a557cb
ok($num->isa('Math::BigFloat'));
Packit a557cb
is("$num", '2.0000000000000000001');
Packit a557cb
is($json->encode($num), '2.0000000000000000001');
Packit a557cb
Packit a557cb
SKIP: { skip "requires $JSON::BackendModule 2.90 or newer", 1 if $JSON::BackendModulePP and eval $JSON::BackendModulePP->VERSION < 2.90;
Packit a557cb
is($json->encode([Math::BigInt->new("0")]), "[${fix}0]", "zero bigint is 0 (the number), not '0' (the string)" );
Packit a557cb
}
Packit a557cb
}