xref: /openbsd-src/gnu/usr.bin/perl/cpan/JSON-PP/t/011_pc_expo.t (revision 256a93a44f36679bee503f12e49566c2183f6181)
1# copied over from JSON::PC and modified to use JSON::PP
2# copied over from JSON::XS and modified to use JSON::PP
3
4use Test::More;
5use strict;
6use warnings;
7BEGIN { plan tests => 8 + 2 };
8BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
9
10use JSON::PP;
11
12#########################
13my ($js,$obj);
14my $pc = JSON::PP->new;
15
16$js  = q|[-12.34]|;
17$obj = $pc->decode($js);
18is($obj->[0], -12.34, 'digit -12.34');
19$js = $pc->encode($obj);
20is($js,'[-12.34]', 'digit -12.34');
21
22$js  = q|[-1.234e5]|;
23$obj = $pc->decode($js);
24is($obj->[0], -123400, 'digit -1.234e5');
25{ #SKIP_IF_CPANEL
26$js = $pc->encode($obj);
27is($js,'[-123400]', 'digit -1.234e5');
28}
29
30$js  = q|[1.23E-4]|;
31$obj = $pc->decode($js);
32is($obj->[0], 0.000123, 'digit 1.23E-4');
33$js = $pc->encode($obj);
34is($js,'[0.000123]', 'digit 1.23E-4');
35
36
37$js  = q|[1.01e+30]|;
38$obj = $pc->decode($js);
39is($obj->[0], 1.01e+30, 'digit 1.01e+30');
40$js = $pc->encode($obj);
41like($js,qr/\[(?:1.01[Ee]\+0?30|1010000000000000000000000000000)]/, 'digit 1.01e+30'); # RT-128589 (-Duselongdouble or -Dquadmath)
42
43my $vax_float = (pack("d",1) =~ /^[\x80\x10]\x40/);
44
45if ($vax_float) {
46    # VAX has smaller float range.
47    $js  = q|[1.01e+37]|;
48    $obj = $pc->decode($js);
49    is($obj->[0], eval '1.01e+37', 'digit 1.01e+37');
50    $js = $pc->encode($obj);
51    like($js,qr/\[1.01[Ee]\+0?37\]/, 'digit 1.01e+37');
52} else {
53    $js  = q|[1.01e+67]|; # 30 -> 67 ... patched by H.Merijn Brand
54    $obj = $pc->decode($js);
55    is($obj->[0], eval '1.01e+67', 'digit 1.01e+67');
56    $js = $pc->encode($obj);
57    like($js,qr/\[1.01[Ee]\+0?67\]/, 'digit 1.01e+67');
58}
59