msgpack/perl/t/06_stream_unpack2.t

50 lines
1.1 KiB
Perl
Raw Normal View History

use strict;
use warnings;
use Data::MessagePack;
2010-05-17 05:52:32 +09:00
use Test::More tests => 6;
2010-09-15 15:25:48 +09:00
my $input = [42, "foo", { x => [ (undef)x16 ] }, 3.14 ];
my $packed = Data::MessagePack->pack($input);
is_deeply(Data::MessagePack->unpack($packed), $input);
{
my $up = Data::MessagePack::Unpacker->new();
$up->execute($packed, 0);
ok $up->is_finished;
is_deeply $up->data, $input;
}
{
my $up = Data::MessagePack::Unpacker->new();
is $up->execute(substr($packed, 0, 3), 0), 3;
2010-09-15 15:25:48 +09:00
ok !$up->is_finished;
$up->execute($packed, 3);
ok $up->is_finished;
is_deeply $up->data, $input;
}
2010-09-15 15:25:48 +09:00
{
my $up = Data::MessagePack::Unpacker->new();
my $offset = 0;
my $size = 5;
note "length: ", length($packed);
while(not $up->is_finished) {
note "offset: ", $offset;
my $bytes = substr($packed, $offset, $size);
note join " ", map { unpack 'H2', $_ } split //, $bytes;
my $x = $up->execute($bytes, 0);
if($x <= 0) {
diag "Something's wrong: $x";
last;
}
else {
$offset += $x;
}
}
ok $up->is_finished;
is_deeply $up->data, $input;
}