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