Skip to content

Commit

Permalink
[t/spec/S32-str] added pack.t/unpack.t for the new Rakudo &pack/&unpack
Browse files Browse the repository at this point in the history
Actually, there was an old pack.t there, but it was all wrong,
so bulldozed it.


git-svn-id: http://svn.pugscode.org/pugs@32003 c213334d-75ef-0310-aa23-eaa082d1ae64
  • Loading branch information
masak committed Aug 15, 2010
1 parent d2b95d7 commit 6c00c9b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
42 changes: 22 additions & 20 deletions S32-str/pack.t
Expand Up @@ -3,28 +3,30 @@ use Test;

# L<S32::Str/Str/"=item pack">

plan 12;

is(unpack("%32B*", "\o001\o002\o004\o010\o020\o040\o100\o200\o377"), 16 );
is(unpack("%32b69", "\o001\o002\o004\o010\o020\o040\o100\o200\o017"), 12 );
is(unpack("%32B69", "\o001\o002\o004\o010\o020\o040\o100\o200\o017"), 9 );
is(unpack("I",pack("I", 0xFFFFFFFF)), 0xFFFFFFFF );
plan 3;

{
# Testing @!
is(pack('a* @3', "abcde"), "abc", 'Test basic @');
is(pack('a* @!3', "abcde"), "abc", 'Test basic @!');
is(pack('a* @2', "\x[301,302,303,304,305]"), "\x[301,302]",
'Test basic utf8 @');
is(pack('a* @!2', "\x[301,302,303,304,305]"), "\x[301]",
'Test basic utf8 @!');
my $buf = pack('H*', "414243");
is_deeply $buf.contents, [:16<41>, :16<42>, :16<43>], 'H* works';
}

is(unpack('@4 a*', "abcde"), "e", 'Test basic @');
is(unpack('@!4 a*', "abcde"), "e", 'Test basic @!');
is(unpack('@4 a*', "\x[301,302,303,304,305]"), "\x[305]",
'Test basic utf8 @');
is(unpack('@!4 a*', "\x[301,302,303,304,305]"),
"\x[303,304,305]", 'Test basic utf8 @!');
{
my $buf = pack("A11 A28 A8 A*",
"03/23/2001", "Totals", "1235.00", " 1172.98");
is_deeply $buf.contents,
"03/23/2001 Totals 1235.00 1172.98"\
.encode.contents,
"A works";
}

# vim: ft=perl6
{
my $buf = pack("C S L n N v V",
0x130, 0x10030, 0x100000030,
0x1234, 0x12345678,
0x1234, 0x12345678);
is_deeply $buf.contents,
[0x30, 0x30, 0x00, 0x30, 0x00, 0x00, 0x00,
0x12, 0x34, 0x12, 0x34, 0x56, 0x78,
0x34, 0x12, 0x78, 0x56, 0x34, 0x12],
"C S L n N v V work";
}
40 changes: 40 additions & 0 deletions S32-str/unpack.t
@@ -0,0 +1,40 @@
use v6;
use Test;

# L<S32::Str/Str/"=item pack">

plan 12;

{
my $hexstring = Buf.new(:16<41>, :16<42>, :16<43>).unpack("H*");
is $hexstring, "414243", 'H* works';
}

{
my $buf
= "03/23/2001 Totals 1235.00 1172.98".encode;
my ($date, $totals, $tot_income, $tot_expend)
= $buf.unpack("A10 x A6 x19 A10 x A*");

is $date, "03/23/2001", 'field 1 (A11) works';
is $totals, "Totals", 'field 2 (A28) works';
is $tot_income, " 1235.00", 'field 3 (A8) works';
is $tot_expend, " 1172.98", 'field 4 (A*) works';
}

{
my $buf = Buf.new(0x30, 0x30, 0x00, 0x30, 0x00, 0x00, 0x00,
0x12, 0x34, 0x12, 0x34, 0x56, 0x78,
0x34, 0x12, 0x78, 0x56, 0x34, 0x12);
my ($char, $short, $long,
$bigend_short, $bigend_long, $lilend_short, $lilend_long)
= $buf.unpack("C S L n N v V");

is $char, 0x30, 'C works';
is $short, 0x30, 'S works';
is $long, 0x30, 'L works';
is $bigend_short, 0x1234, 'n works';
is $bigend_long, 0x12345678, 'N works';
is $lilend_short, 0x1234, 'v works';
is $lilend_long, 0x12345678, 'V works';
}

0 comments on commit 6c00c9b

Please sign in to comment.