Codec-CBOR

 view release on metacpan or  search on metacpan

t/02_fixtures.t  view on Meta::CPAN

my @basic_fixtures = (
    { name => 'array-empty',            data => [],                 hex => '80' },
    { name => 'array-2',                data => [2],                hex => '8102' },
    { name => 'array-255',              data => [255],              hex => '8118ff' },
    { name => 'array-3,4,5,6',          data => [ 3, 4, 5, 6 ],     hex => '8403040506' },
    { name => 'array-500',              data => [500],              hex => '811901f4' },
    { name => 'array-65536',            data => [65536],            hex => '811a00010000' },
    { name => 'array-9007199254740991', data => [9007199254740991], hex => '811b001fffffffffffff' },
    { name => 'array-6433713753386423', data => [6433713753386423], hex => '811b0016db6db6db6db7' },
    {   name => 'array-5-nested',
        data => [ 'array', [ 'of', [ 5, [ 'nested', [ 'arrays', '!' ] ] ] ] ],
        hex  => '8265617272617982626f66820582666e657374656482666172726179736121'
    },
    { name => 'bytes-empty',             data => \'',                                                             hex => '40' },
    { name => 'bytes-a1',                data => \"\xa1",                                                         hex => '41a1' },
    { name => 'false',                   data => Codec::CBOR->false_obj,                                          hex => 'f4' },
    { name => 'true',                    data => Codec::CBOR->true_obj,                                           hex => 'f5' },
    { name => 'null',                    data => undef,                                                           hex => 'f6' },
    { name => 'float-array_of_specials', data => [ 1, Codec::CBOR->true_obj, Codec::CBOR->false_obj, undef, -1 ], hex => '8501f5f4f620' },
    {   name => 'array-mixed',
        data => [ 6433713753386423, 65536, 500, 2, 0, -1, -3, -256, -2784428724, -6433713753386424, \"a1", 'Čaues ßvěte!' ],
        hex  => '8c1b0016db6db6db6db71a000100001901f40200202238ff3aa5f702b33b0016db6db6db6db74261316fc48c6175657320c39f76c49b746521'
    }
);
my @float_fixtures = (
    { name => 'float-0.5',  data =>  0.5, hex => 'fb3fe0000000000000' },
    { name => 'float-1.1',  data =>  1.1, hex => 'fb3ff199999999999a' },
    { name => 'float--0.5', data => -0.5, hex => 'fbbfe0000000000000' },
    { name => 'float--1.1', data => -1.1, hex => 'fbbff199999999999a' }
);
my @cid_fixtures = (
    {   name => 'cid-QmQg1v4o9xdT3Q14wh4S7dxZkDjyZ9ssFzFzyep1YrVJBY',
        hex  => 'd82a582300122022ad631c69ee983095b5b8acd029ff94aff1dc6c48837878589a92b90dfea317'
    },
    {   name => 'cid-QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL',
        hex  => 'd82a582300122031c3d57080d8463a3c63b2923df5a1d40ad7a73eae5a14af584213e5f504ac33'
    },
    {   name => 'cid-QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V',
        hex  => 'd82a58230012208ab7a6c5e74737878ac73863cb76739d15d4666de44e5756bf55a2f9e9ab5f44'
    },
    { name => 'cid-bafkqabiaaebagba', hex => 'd82a4a00015500050001020304' },
    {   name => 'cid-bafkreiebzrnroamgos2adnbpgw5apo3z4iishhbdx77gldnbk57d4zdio4',
        hex  => 'd82a5825000155122081cc5b17018674b401b42f35ba07bb79e211239c23bffe658da1577e3e646877'
    },
    { name => 'cid-z8mWaJ1dZ9fH5EetPuRsj8jj26pXsgpsr', hex => 'd82a58190001781114c876ceeaa8b30123bc5fc99359e682a737308659' }
);
my @dagpb_fixtures = (
    { name => 'dagpb_empty',     hex => 'a1654c696e6b7380',                       data => { Links => [] } },
    { name => 'dagpb_Data_some', hex => 'a26444617461450001020304654c696e6b7380', data => { Data  => "\x00\x01\x02\x03\x04", Links => [] } },
    { name => 'dagpb_Data_zero', hex => 'a2644461746140654c696e6b7380',           data => { Data  => '',                     Links => [] } },
    { name => 'dagpb_1link',     hex => 'a1654c696e6b7381a16448617368d82a58230012207521fe19c374a97759226dc5c0c8e674e73950e81b211f7dd3b6b30883a08a51' }
);
my $garbage_00
    = '8679075f593b44722664215c2961322b62222ee298ba0955235125776c6c7d21402622493834676426343b2c33425f2e674274356d6c0a6d6f6d642c292e4a3b386b45425d4e627b5935744834452278787a6b793c747b7c243a553b656b5a74704a6f2d6f6c4d606a79494a35574e6e7d4c3d4022464b4c772...

# Garbage fixtures (Round-trip check)
subtest 'Garbage Round-trip' => sub {
    my @garbage = ( { name => 'garbage-00', hex => $garbage_00 } );
    for my $f (@garbage) {
        my $bin     = pack( 'H*', $f->{hex} );
        my $decoded = $codec->decode($bin);
        ok $decoded, 'Decoded ' . $f->{name};
    }
};

# Basic & Floats
subtest 'Basic and Float Fixtures' => sub {
    for my $f ( @basic_fixtures, @float_fixtures ) {
        my $encoded = $codec->encode( $f->{data} );
        is( unpack( 'H*', $encoded ), $f->{hex}, 'Encoding matches for ' . $f->{name} );
        my $decoded = $codec->decode( pack( 'H*', $f->{hex} ) );
        if ( builtin::blessed( $f->{data} ) && $f->{data}->isa('Codec::CBOR::Boolean') ) {
            ok $decoded->isa('Codec::CBOR::Boolean'), 'Decoded is a Boolean object';
            is $decoded, $f->{data}, 'Boolean value matches';
        }
        else {
            is normalize($decoded), ( $f->{name} =~ /float-[-\d]/ ? float( normalize( $f->{data} ), tolerance => 0.01 ) : normalize( $f->{data} ) ),
                'Decoding matches for ' . $f->{name};
        }
    }
};

# CIDs
subtest 'CID Fixtures' => sub {
    for my $f (@cid_fixtures) {
        my $bin     = pack( 'H*', $f->{hex} );
        my $decoded = $codec->decode($bin);
        is ref $decoded, 'HASH', "Decoded $f->{name} into hash";
        ok exists $decoded->{cid_raw}, 'Has cid_raw key';
    }
};

# DAG-PB
subtest 'DAG-PB Fixtures' => sub {
    for my $f (@dagpb_fixtures) {
        my $bin     = pack( 'H*', $f->{hex} );
        my $decoded = $codec->decode($bin);
        ok $decoded, 'Decoded ' . $f->{name};
        is normalize($decoded), normalize( $f->{data} ), 'Decoded data matches expected for ' . $f->{name} if defined $f->{data};
    }
};
#
done_testing;



( run in 0.465 second using v1.01-cache-2.11-cpan-ecdf5575e8d )