Debian Codegraph @

t/11_pc_expo.t

raw
1 # copied over from JSON::PC and modified to use JSON
2 # copied over from JSON::XS and modified to use JSON
3
4 use Test::More;
5 use strict;
6 use warnings;
7 BEGIN { plan tests => 8 + 2 };
8 BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; }
9
10 use JSON;
11
12 #########################
13 my ($js,$obj);
14 my $pc = JSON->new;
15
16 $js = q|[-12.34]|;
17 $obj = $pc->decode($js);
18 is($obj->[0], -12.34, 'digit -12.34');
19 $js = $pc->encode($obj);
20 is($js,'[-12.34]', 'digit -12.34');
21
22 $js = q|[-1.234e5]|;
23 $obj = $pc->decode($js);
24 is($obj->[0], -123400, 'digit -1.234e5');
25 SKIP: { skip "not for $JSON::BackendModule", 1 if $JSON::BackendModule eq 'Cpanel::JSON::XS';
26 $js = $pc->encode($obj);
27 is($js,'[-123400]', 'digit -1.234e5');
28 }
29
30 $js = q|[1.23E-4]|;
31 $obj = $pc->decode($js);
32 is($obj->[0], 0.000123, 'digit 1.23E-4');
33 $js = $pc->encode($obj);
34 is($js,'[0.000123]', 'digit 1.23E-4');
35
36
37 $js = q|[1.01e+30]|;
38 $obj = $pc->decode($js);
39 is($obj->[0], 1.01e+30, 'digit 1.01e+30');
40 $js = $pc->encode($obj);
41 like($js,qr/\[(?:1.01[Ee]\+0?30|1010000000000000000000000000000)]/, 'digit 1.01e+30'); # RT-128589 (-Duselongdouble or -Dquadmath)
42
43 my $vax_float = (pack("d",1) =~ /^[\x80\x10]\x40/);
44
45 if ($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 }