Debian Codegraph @

t/110_bignum.t

raw
1
2 use strict;
3 use warnings;
4 use Test::More;
5 BEGIN { plan tests => 9 };
6
7 BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; }
8
9 use JSON -support_by_pp;
10
11 eval q| require Math::BigInt |;
12
13 SKIP: {
14 skip "Can't load Math::BigInt.", 9 if ($@);
15
16 my $v = Math::BigInt->VERSION;
17 $v =~ s/_.+$// if $v;
18
19 my $fix = !$v ? '+'
20 : $v < 1.6 ? '+'
21 : '';
22
23
24 my $json = JSON->new;
25
26 $json->allow_nonref->allow_bignum(1);
27 $json->convert_blessed->allow_blessed;
28
29 my $num = $json->decode(q|100000000000000000000000000000000000000|);
30
31 ok($num->isa('Math::BigInt'));
32 is("$num", $fix . '100000000000000000000000000000000000000');
33 is($json->encode($num), $fix . '100000000000000000000000000000000000000');
34
35 SKIP: { skip "requires $JSON::BackendModule 2.91_03 or newer", 2 if $JSON::BackendModulePP and eval $JSON::BackendModulePP->VERSION < 2.91_03;
36 $num = $json->decode(q|10|);
37
38 ok(!(ref $num and $num->isa('Math::BigInt')), 'small integer is not a BigInt');
39 ok(!(ref $num and $num->isa('Math::BigFloat')), 'small integer is not a BigFloat');
40 }
41
42 $num = $json->decode(q|2.0000000000000000001|);
43
44 ok($num->isa('Math::BigFloat'));
45 is("$num", '2.0000000000000000001');
46 is($json->encode($num), '2.0000000000000000001');
47
48 SKIP: { skip "requires $JSON::BackendModule 2.90 or newer", 1 if $JSON::BackendModulePP and eval $JSON::BackendModulePP->VERSION < 2.90;
49 is($json->encode([Math::BigInt->new("0")]), "[${fix}0]", "zero bigint is 0 (the number), not '0' (the string)" );
50 }
51 }