Skip to content

Commit fd7d309

Browse files
committed
Add tests for Buf.unpack(<t em plate*>)
1 parent db02e90 commit fd7d309

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

S32-str/unpack.t

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,44 @@ use Test;
33

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

6-
plan 12;
6+
plan 2 * 12;
77

88
{
9-
my $hexstring = Buf.new(:16<41>, :16<42>, :16<43>).unpack("H*");
10-
is $hexstring, "414243", 'H* works';
9+
for "H*", <H*> -> $template {
10+
my $hexstring = Buf.new(:16<41>, :16<42>, :16<43>).unpack($template);
11+
is $hexstring, "414243", 'H* works';
12+
}
1113
}
1214

1315
{
14-
my $buf
15-
= "03/23/2001 Totals 1235.00 1172.98".encode;
16-
my ($date, $totals, $tot_income, $tot_expend)
17-
= $buf.unpack("A10 x A6 x19 A10 x A*");
18-
19-
is $date, "03/23/2001", 'field 1 (A11) works';
20-
is $totals, "Totals", 'field 2 (A28) works';
21-
is $tot_income, " 1235.00", 'field 3 (A8) works';
22-
is $tot_expend, " 1172.98", 'field 4 (A*) works';
16+
my $buf =
17+
"03/23/2001 Totals 1235.00 1172.98".encode;
18+
for "A10 x A6 x19 A10 x A*", <A10 x A6 x19 A10 x A*> -> $template {
19+
my ($date,$totals,$tot_income,$tot_expend) = $buf.unpack($template);
20+
21+
is $date, "03/23/2001", 'field 1 (A11) works';
22+
is $totals, "Totals", 'field 2 (A28) works';
23+
is $tot_income, " 1235.00", 'field 3 (A8) works';
24+
is $tot_expend, " 1172.98", 'field 4 (A*) works';
25+
}
2326
}
2427

2528
{
2629
my $buf = Buf.new(0x30, 0x30, 0x00, 0x30, 0x00, 0x00, 0x00,
2730
0x12, 0x34, 0x12, 0x34, 0x56, 0x78,
2831
0x34, 0x12, 0x78, 0x56, 0x34, 0x12);
29-
my ($char, $short, $long,
30-
$bigend_short, $bigend_long, $lilend_short, $lilend_long)
31-
= $buf.unpack("C S L n N v V");
32-
33-
is $char, 0x30, 'C works';
34-
is $short, 0x30, 'S works';
35-
is $long, 0x30, 'L works';
36-
is $bigend_short, 0x1234, 'n works';
37-
is $bigend_long, 0x12345678, 'N works';
38-
is $lilend_short, 0x1234, 'v works';
39-
is $lilend_long, 0x12345678, 'V works';
32+
for "C S L n N v V", <C S L n N v V> -> $template {
33+
my ($char, $short, $long, $bigend_short, $bigend_long,
34+
$lilend_short, $lilend_long) = $buf.unpack($template);
35+
36+
is $char, 0x30, 'C works';
37+
is $short, 0x30, 'S works';
38+
is $long, 0x30, 'L works';
39+
is $bigend_short, 0x1234, 'n works';
40+
is $bigend_long, 0x12345678, 'N works';
41+
is $lilend_short, 0x1234, 'v works';
42+
is $lilend_long, 0x12345678, 'V works';
43+
}
4044
}
4145

4246
# vim: ft=perl6

0 commit comments

Comments
 (0)