CBOR-Free

 view release on metacpan or  search on metacpan

t/float.t  view on Meta::CPAN

    cmp_deeply(
        CBOR::Free::decode($encoded),
        $Config{'uselongdouble'} || $Config{'usequadmath'} ? num($i, 0.0001) : $i,
        "… and it round-trips",
    );
}

{
    my @ints = map { int( rand() * (2**17) - (2**16) ) } 1 .. 20;
    for my $int (@ints) {
        my $cbor = pack('C f>', 0xfa, $int);

        is( CBOR::Free::decode($cbor), $int, "decode int as float: $int" );

        $cbor = pack('C d>', 0xfb, $int);

        is( CBOR::Free::decode($cbor), $int, "decode int as double: $int" );
    }
}

{
    my @ints = map { int( rand() * (2**33) - (2**32) ) } 1 .. 20;
    for my $int (@ints) {
        my $cbor = pack('C d>', 0xfb, $int);

        is( CBOR::Free::decode($cbor), $int, "decode int as double: $int" );
    }
}

my $inf = unpack("f>", "\x7f\x80\x00\x00");
my $nan = unpack("f>", "\x7f\xc0\x00\x00");
my $neginf = unpack("f>", "\xff\x80\x00\x00");

is(
    sprintf('%v.02x', CBOR::Free::encode($inf)),
    'f9.7c.00',
    'Inf encodes to half-precision as expected',
);

TODO: {
    my $reason;
    if ($^O eq 'MSWin32' && $^V lt v5.24) {
        $reason = 'Apparently broken on MSWin32 prior to 5.24?';
    }
    elsif ($^O eq 'linux' && $^V lt v5.22) {
        $reason = 'Linux 5.4.12 appears to need inf detection that was added in Perl 5.22.';
    }

    local $TODO = $reason if $reason;

    is(
        sprintf('%v.02x', CBOR::Free::encode($neginf)),
        'f9.fc.00',
        '-Inf encodes to half-precision as expected',
    );
}

is(
    sprintf('%v.02x', CBOR::Free::encode($nan)),
    'f9.7e.00',
    'NaN encodes to half-precision as expected',
);

sub _cmpbin {
    my ($got, $expect, $label) = @_;

    $_ = sprintf('%v.02x', $_) for ($got, $expect);

    return is( $got, $expect, $label );
}

#----------------------------------------------------------------------

done_testing;



( run in 0.491 second using v1.01-cache-2.11-cpan-ceb78f64989 )