Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add tests for List as template for pack
  • Loading branch information
lizmat committed Dec 4, 2015
1 parent d8db71f commit 25424a4
Showing 1 changed file with 39 additions and 26 deletions.
65 changes: 39 additions & 26 deletions S32-str/pack.t
Expand Up @@ -3,52 +3,65 @@ use Test;

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

plan 7;
plan 14;

{
my $buf = pack('H*', "414243");
is-deeply $buf.contents, (:16<41>, :16<42>, :16<43>), 'H* works';
for 'H*', ('H*',) -> $t {
my $buf = pack($t, "414243");
is-deeply $buf.contents, (:16<41>, :16<42>, :16<43>), "$t works";
}
}

{
my $buf = pack('H', 'a');
is-deeply $buf.contents, ( 0xA0, ), 'H works on odd-length strings';
for 'H', ('H',) -> $t {
my $buf = pack($t, 'a');
is-deeply $buf.contents, ( 0xA0, ), "$t works on odd-length strings";
}
}

{
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";
for "A11 A28 A8 A*", <A11 A28 A8 A*> -> $t {
my $buf = pack($t, "03/23/2001", "Totals", "1235.00", " 1172.98");
is-deeply $buf.contents,
"03/23/2001 Totals 1235.00 1172.98"
.encode.contents,
"$t works";
}
}

{
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";
for "C S L n N v V", <C S L n N v V> -> $t {
my $buf = pack($t,
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),
"$t work";
}
}

{
my $buf = pack('x');
is-deeply $buf.contents, (0x00,), 'x by itself works';
for 'x', ('x',) -> $t {
my $buf = pack($t);
is-deeply $buf.contents, (0x00,), "$t by itself works";
}
}

{
my $buf = pack('x4');
is-deeply $buf.contents, (0x00, 0x00, 0x00, 0x00), 'x with amount works';
for 'x4', ('x4',) -> $t {
my $buf = pack($t);
is-deeply $buf.contents, (0x00, 0x00, 0x00, 0x00), "$t & amount works";
}
}

{
my $buf = pack('x*');
is-deeply $buf.contents, (), 'x* works (as in it does nothing.)';
for 'x*', ('x*',) -> $t {
my $buf = pack($t);
is-deeply $buf.contents, (), "$t & amount works: does nothing";
}
}

# vim: ft=perl6

0 comments on commit 25424a4

Please sign in to comment.