Fix saving 'V' magic in scan_vstring()
[p5sagit/p5-mst-13.2.git] / t / op / pack.t
CommitLineData
d50dd4e4 1#!./perl -w
a687059c 2
a1a0e61e 3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
0b568b5f 6 require './test.pl';
b23b8711 7}
8
db37a92e 9plan tests => 5852;
0b568b5f 10
fa8ec7c1 11use strict;
12use warnings;
b23b8711 13use Config;
14
fa8ec7c1 15my $Is_EBCDIC = (defined $Config{ebcdic} && $Config{ebcdic} eq 'define');
0b568b5f 16my $Perl = which_perl();
c274e827 17
b85d93de 18sub encode_list {
9e17a6b8 19 my @result = map {_qq($_)} @_;
b85d93de 20 if (@result == 1) {
21 return @result;
22 }
23 return '(' . join (', ', @result) . ')';
24}
25
a1a0e61e 26
b85d93de 27sub list_eq ($$) {
28 my ($l, $r) = @_;
c8f824eb 29 return 0 unless @$l == @$r;
b85d93de 30 for my $i (0..$#$l) {
31 if (defined $l->[$i]) {
c8f824eb 32 return 0 unless defined ($r->[$i]) && $l->[$i] eq $r->[$i];
b85d93de 33 } else {
c8f824eb 34 return 0 if defined $r->[$i]
b85d93de 35 }
36 }
37 return 1;
38}
39
40##############################################################################
41#
42# Here starteth the tests
43#
44
fa8ec7c1 45{
0b568b5f 46 my $format = "c2 x5 C C x s d i l a6";
47 # Need the expression in here to force ary[5] to be numeric. This avoids
48 # test2 failing because ary2 goes str->numeric->str and ary doesn't.
49 my @ary = (1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,
50 "abcdef");
51 my $foo = pack($format,@ary);
52 my @ary2 = unpack($format,$foo);
53
54 is($#ary, $#ary2);
55
56 my $out1=join(':',@ary);
57 my $out2=join(':',@ary2);
58 # Using long double NVs may introduce greater accuracy than wanted.
59 $out1 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
60 $out2 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
61 is($out1, $out2);
62
63 like($foo, qr/def/);
fa8ec7c1 64}
79072805 65# How about counting bits?
66
fa8ec7c1 67{
0b568b5f 68 my $x;
69 is( ($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")), 16 );
79072805 70
0b568b5f 71 is( ($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")), 12 );
79072805 72
0b568b5f 73 is( ($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")), 9 );
fa8ec7c1 74}
79072805 75
fa8ec7c1 76{
0b568b5f 77 my $sum = 129; # ASCII
78 $sum = 103 if $Is_EBCDIC;
9d116dd7 79
0b568b5f 80 my $x;
81 is( ($x = unpack("%32B*", "Now is the time for all good blurfl")), $sum );
79072805 82
0b568b5f 83 my $foo;
84 open(BIN, $Perl) || die "Can't open $Perl: $!\n";
85 sysread BIN, $foo, 8192;
86 close BIN;
79072805 87
0b568b5f 88 $sum = unpack("%32b*", $foo);
89 my $longway = unpack("b*", $foo);
90 is( $sum, $longway =~ tr/1/1/ );
fa8ec7c1 91}
73a1c01a 92
fa8ec7c1 93{
94 my $x;
0b568b5f 95 is( ($x = unpack("I",pack("I", 0xFFFFFFFF))), 0xFFFFFFFF );
fa8ec7c1 96}
def98dd4 97
fa8ec7c1 98{
0b568b5f 99 # check 'w'
100 my @x = (5,130,256,560,32000,3097152,268435455,1073741844, 2**33,
101 '4503599627365785','23728385234614992549757750638446');
102 my $x = pack('w*', @x);
103 my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f8480808014A0808'.
104 '0800087ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e';
105
106 is($x, $y);
107
108 my @y = unpack('w*', $y);
109 my $a;
110 while ($a = pop @x) {
111 my $b = pop @y;
112 is($a, $b);
113 }
def98dd4 114
0b568b5f 115 @y = unpack('w2', $x);
def98dd4 116
0b568b5f 117 is(scalar(@y), 2);
118 is($y[1], 130);
a6c48a57 119 $x = pack('w*', 5000000000); $y = '';
120 eval {
121 use Math::BigInt;
122 $y = pack('w*', Math::BigInt::->new(5000000000));
123 };
124 is($x, $y);
196b62db 125
126 $x = pack 'w', ~0;
127 $y = pack 'w', (~0).'';
128 is($x, $y);
129 is(unpack ('w',$x), ~0);
130 is(unpack ('w',$y), ~0);
131
132 $x = pack 'w', ~0 - 1;
133 $y = pack 'w', (~0) - 2;
134
135 if (~0 - 1 == (~0) - 2) {
136 is($x, $y, "NV arithmetic");
137 } else {
138 isnt($x, $y, "IV/NV arithmetic");
139 }
140 cmp_ok(unpack ('w',$x), '==', ~0 - 1);
141 cmp_ok(unpack ('w',$y), '==', ~0 - 2);
203d3f29 142
143 # These should spot that pack 'w' is using NV, not double, on platforms
144 # where IVs are smaller than doubles, and harmlessly pass elsewhere.
145 # (tests for change 16861)
146 my $x0 = 2**54+3;
147 my $y0 = 2**54-2;
148
149 $x = pack 'w', $x0;
150 $y = pack 'w', $y0;
151
152 if ($x0 == $y0) {
153 is($x, $y, "NV arithmetic");
154 } else {
155 isnt($x, $y, "IV/NV arithmetic");
156 }
157 cmp_ok(unpack ('w',$x), '==', $x0);
158 cmp_ok(unpack ('w',$y), '==', $y0);
fa8ec7c1 159}
def98dd4 160
0b568b5f 161
fa8ec7c1 162{
196b62db 163 # test exceptions
fa8ec7c1 164 my $x;
165 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
0b568b5f 166 like($@, qr/^Unterminated compressed integer/);
def98dd4 167
fa8ec7c1 168 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
0b568b5f 169 like($@, qr/^Unterminated compressed integer/);
def98dd4 170
fa8ec7c1 171 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
0b568b5f 172 like($@, qr/^Unterminated compressed integer/);
0258719b 173
174 eval { $x = pack 'w', -1 };
175 like ($@, qr/^Cannot compress negative numbers/);
176
177 eval { $x = pack 'w', '1'x(1 + length ~0) . 'e0' };
178 like ($@, qr/^Can only compress unsigned integers/);
179
180 SKIP: {
181 # Is this a stupid thing to do on VMS, VOS and other unusual platforms?
71f93cb9 182
183 skip "-- the IEEE infinity model is unavailable in this configuration."
184 if (($^O eq 'VMS') && !defined($Config{useieee}));
185
8b571b32 186 skip "-- MPE/iX has serious fp indigestionf on w-packed infinities"
187 if (($^O eq 'mpeix'));
188
0258719b 189 my $inf = eval '2**10000';
190
191 skip "Couldn't generate infinity - got error '$@'"
71b080b8 192 unless defined $inf and $inf == $inf / 2 and $inf + 1 == $inf;
0258719b 193
46ff39aa 194 local our $TODO;
195 $TODO = "VOS needs a fix for posix-1022 to pass this test."
196 if ($^O eq 'vos');
197
0258719b 198 eval { $x = pack 'w', $inf };
46ff39aa 199 like ($@, qr/^Cannot compress integer/, "Cannot compress integer");
0258719b 200 }
201
202 SKIP: {
71f93cb9 203
204 skip "-- the full range of an IEEE double may not be available in this configuration."
205 if (($^O eq 'VMS') && !defined($Config{useieee}));
206
0258719b 207 # This should be about the biggest thing possible on an IEEE double
208 my $big = eval '2**1023';
209
92b5f8d1 210 skip "Couldn't generate 2**1023 - got error '$@'", 3
0258719b 211 unless defined $big and $big != $big / 2;
212
213 eval { $x = pack 'w', $big };
214 is ($@, '', "Should be able to pack 'w', $big # 2**1023");
215
216 my $y = eval {unpack 'w', $x};
217 is ($@, '',
218 "Should be able to unpack 'w' the result of pack 'w', $big # 2**1023");
219
220 # I'm getting about 1e-16 on FreeBSD
221 my $quotient = int (100 * ($y - $big) / $big);
222 ok($quotient < 2 && $quotient > -2,
223 "Round trip pack, unpack 'w' of $big is withing 1% ($quotient%)");
224 }
225
fa8ec7c1 226}
def98dd4 227
84902520 228#
229# test the "p" template
230
231# literals
0b568b5f 232is(unpack("p",pack("p","foo")), "foo");
84902520 233
234# scalars
0b568b5f 235is(unpack("p",pack("p",239)), 239);
84902520 236
237# temps
238sub foo { my $a = "a"; return $a . $a++ . $a++ }
239{
9f1b1f2d 240 use warnings;
0b568b5f 241 my $warning;
84902520 242 local $SIG{__WARN__} = sub {
0b568b5f 243 $warning = $_[0];
84902520 244 };
245 my $junk = pack("p", &foo);
0b568b5f 246
247 like($warning, qr/temporary val/);
84902520 248}
249
250# undef should give null pointer
0b568b5f 251like(pack("p", undef), qr/^\0+/);
84902520 252
20408e3c 253# Check for optimizer bug (e.g. Digital Unix GEM cc with -O4 on DU V4.0B gives
254# 4294967295 instead of -1)
255# see #ifdef __osf__ in pp.c pp_unpack
0b568b5f 256is((unpack("i",pack("i",-1))), -1);
20408e3c 257
0b568b5f 258# test the pack lengths of s S i I l L
259# test the pack lengths of n N v V
fa8ec7c1 260my @lengths = qw(s 2 S 2 i -4 I -4 l 4 L 4 n 2 N 4 v 2 V 4);
261while (my ($format, $expect) = splice @lengths, 0, 2) {
262 my $len = length(pack($format, 0));
263 if ($expect > 0) {
0b568b5f 264 is($expect, $len, "format '$format'");
fa8ec7c1 265 } else {
266 $expect = -$expect;
0b568b5f 267 ok ($len >= $expect, "format '$format'") ||
268 print "# format '$format' has length $len, expected >= $expect\n";
fa8ec7c1 269 }
270}
d4217c7e 271
d4217c7e 272
0b568b5f 273# test unpack-pack lengths
274my @templates = qw(c C i I s S l L n N v V f d q Q);
d4217c7e 275
276foreach my $t (@templates) {
0b568b5f 277 SKIP: {
278 my @t = eval { unpack("$t*", pack("$t*", 12, 34)) };
279
280 # quads not supported everywhere
49704364 281 skip "Quads not supported", 4 if $@ =~ /Invalid type/;
0b568b5f 282 is( $@, '' );
283
284 is(scalar @t, 2);
3020ec7a 285
286 SKIP: {
287 skip "$t not expected to work for some reason", 2 if $t =~ /[nv]/i;
288
0b568b5f 289 is($t[0], 12);
290 is($t[1], 34);
291 }
0b568b5f 292 }
d4217c7e 293}
9d116dd7 294
fa8ec7c1 295{
0b568b5f 296 # uuencode/decode
9d116dd7 297
0b568b5f 298 # Note that first uuencoding known 'text' data and then checking the
299 # binary values of the uuencoded version would not be portable between
300 # character sets. Uuencoding is meant for encoding binary data, not
301 # text data.
c4d5f83a 302
0b568b5f 303 my $in = pack 'C*', 0 .. 255;
ba1ac976 304
0b568b5f 305 # just to be anal, we do some random tr/`/ /
306 my $uu = <<'EOUU';
ba1ac976 307M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL
9d116dd7 308M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9
309M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6&
310MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S
311MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@
ba1ac976 312?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P `
9d116dd7 313EOUU
314
0b568b5f 315 $_ = $uu;
316 tr/ /`/;
9d116dd7 317
0b568b5f 318 is(pack('u', $in), $_);
fa8ec7c1 319
0b568b5f 320 is(unpack('u', $uu), $in);
9d116dd7 321
0b568b5f 322 $in = "\x1f\x8b\x08\x08\x58\xdc\xc4\x35\x02\x03\x4a\x41\x50\x55\x00\xf3\x2a\x2d\x2e\x51\x48\xcc\xcb\x2f\xc9\x48\x2d\x52\x08\x48\x2d\xca\x51\x28\x2d\x4d\xce\x4f\x49\x2d\xe2\x02\x00\x64\x66\x60\x5c\x1a\x00\x00\x00";
323 $uu = <<'EOUU';
eddc390b 324M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
325&8%P:````
326EOUU
327
0b568b5f 328 is(unpack('u', $uu), $in);
eddc390b 329
0b568b5f 330# This is identical to the above except that backquotes have been
331# changed to spaces
eddc390b 332
0b568b5f 333 $uu = <<'EOUU';
eddc390b 334M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
c4d5f83a 335&8%P:
eddc390b 336EOUU
337
0b568b5f 338 # ' # Grr
339 is(unpack('u', $uu), $in);
ef54e1a4 340
d99ad34e 341}
726ea183 342
0b568b5f 343# test the ascii template types (A, a, Z)
726ea183 344
fa8ec7c1 345foreach (
0b568b5f 346['p', 'A*', "foo\0bar\0 ", "foo\0bar\0 "],
fa8ec7c1 347['p', 'A11', "foo\0bar\0 ", "foo\0bar\0 "],
0b568b5f 348['u', 'A*', "foo\0bar \0", "foo\0bar"],
349['u', 'A8', "foo\0bar \0", "foo\0bar"],
350['p', 'a*', "foo\0bar\0 ", "foo\0bar\0 "],
fa8ec7c1 351['p', 'a11', "foo\0bar\0 ", "foo\0bar\0 \0\0"],
0b568b5f 352['u', 'a*', "foo\0bar \0", "foo\0bar \0"],
353['u', 'a8', "foo\0bar \0", "foo\0bar "],
354['p', 'Z*', "foo\0bar\0 ", "foo\0bar\0 \0"],
fa8ec7c1 355['p', 'Z11', "foo\0bar\0 ", "foo\0bar\0 \0\0"],
0b568b5f 356['p', 'Z3', "foo", "fo\0"],
357['u', 'Z*', "foo\0bar \0", "foo"],
358['u', 'Z8', "foo\0bar \0", "foo"],
359)
360{
361 my ($what, $template, $in, $out) = @$_;
362 my $got = $what eq 'u' ? (unpack $template, $in) : (pack $template, $in);
363 unless (is($got, $out)) {
0b568b5f 364 my $un = $what eq 'u' ? 'un' : '';
9e17a6b8 365 print "# ${un}pack ('$template', "._qq($in).') gave '._qq($out).
366 ' not '._qq($got)."\n";
0b568b5f 367 }
d99ad34e 368}
726ea183 369
0b568b5f 370# packing native shorts/ints/longs
726ea183 371
0b568b5f 372is(length(pack("s!", 0)), $Config{shortsize});
373is(length(pack("i!", 0)), $Config{intsize});
374is(length(pack("l!", 0)), $Config{longsize});
375ok(length(pack("s!", 0)) <= length(pack("i!", 0)));
376ok(length(pack("i!", 0)) <= length(pack("l!", 0)));
377is(length(pack("i!", 0)), length(pack("i", 0)));
726ea183 378
fa8ec7c1 379sub numbers {
380 my $format = shift;
381 return numbers_with_total ($format, undef, @_);
d99ad34e 382}
726ea183 383
fa8ec7c1 384sub numbers_with_total {
385 my $format = shift;
386 my $total = shift;
387 if (!defined $total) {
388 foreach (@_) {
389 $total += $_;
390 }
391 }
392 foreach (@_) {
0b568b5f 393 SKIP: {
394 my $out = eval {unpack($format, pack($format, $_))};
395 skip "cannot pack '$format' on this perl", 2 if
49704364 396 $@ =~ /Invalid type '$format'/;
0b568b5f 397
398 is($@, '');
399 is($out, $_);
fa8ec7c1 400 }
fa8ec7c1 401 }
726ea183 402
fa8ec7c1 403 my $skip_if_longer_than = ~0; # "Infinity"
404 if (~0 - 1 == ~0) {
405 # If we're running with -DNO_PERLPRESERVE_IVUV and NVs don't preserve all
406 # UVs (in which case ~0 is NV, ~0-1 will be the same NV) then we can't
407 # correctly in perl calculate UV totals for long checksums, as pp_unpack
408 # is using UV maths, and we've only got NVs.
53133ed1 409 $skip_if_longer_than = $Config{nv_preserves_uv_bits};
fa8ec7c1 410 }
726ea183 411
fa8ec7c1 412 foreach ('', 1, 2, 3, 15, 16, 17, 31, 32, 33, 53, 54, 63, 64, 65) {
0b568b5f 413 SKIP: {
414 my $sum = eval {unpack "%$_$format*", pack "$format*", @_};
415 skip "cannot pack '$format' on this perl", 3
49704364 416 if $@ =~ /Invalid type '$format'/;
0b568b5f 417
418 is($@, '');
419 ok(defined $sum);
420
421 my $len = $_; # Copy, so that we can reassign ''
422 $len = 16 unless length $len;
423
424 SKIP: {
425 skip "cannot test checksums over $skip_if_longer_than bits", 1
426 if $len > $skip_if_longer_than;
427
428 # Our problem with testing this portably is that the checksum code in
429 # pp_unpack is able to cast signed to unsigned, and do modulo 2**n
430 # arithmetic in unsigned ints, which perl has no operators to do.
431 # (use integer; does signed ints, which won't wrap on UTS, which is just
432 # fine with ANSI, but not with most people's assumptions.
433 # This is why we need to supply the totals for 'Q' as there's no way in
434 # perl to calculate them, short of unpack '%0Q' (is that documented?)
435 # ** returns NVs; make sure it's IV.
436 my $max = 1 + 2 * (int (2 ** ($len-1))-1); # The max possible checksum
437 my $max_p1 = $max + 1;
438 my ($max_is_integer, $max_p1_is_integer);
439 $max_p1_is_integer = 1 unless $max_p1 + 1 == $max_p1;
440 $max_is_integer = 1 if $max - 1 < ~0;
441
442 my $calc_sum;
443 if (ref $total) {
444 $calc_sum = &$total($len);
445 } else {
446 $calc_sum = $total;
447 # Shift into range by some multiple of the total
448 my $mult = int ($total / $max_p1);
449 # Need this to make sure that -1 + (~0+1) is ~0 (ie still integer)
450 $calc_sum = $total - $mult;
451 $calc_sum -= $mult * $max;
452 if ($calc_sum < 0) {
453 $calc_sum += 1;
454 $calc_sum += $max;
455 }
456 }
457 if ($calc_sum == $calc_sum - 1 && $calc_sum == $max_p1) {
458 # we're into floating point (either by getting out of the range of
459 # UV arithmetic, or because we're doing a floating point checksum)
460 # and our calculation of the checksum has become rounded up to
461 # max_checksum + 1
462 $calc_sum = 0;
463 }
464
0dccb3d1 465 if ($calc_sum == $sum) { # HAS to be ==, not eq (so no is()).
466 ok ("unpack '%$_$format' gave $sum");
0b568b5f 467 } else {
468 my $delta = 1.000001;
469 if ($format =~ tr /dDfF//
470 && ($calc_sum <= $sum * $delta && $calc_sum >= $sum / $delta)) {
0dccb3d1 471 pass ("unpack '%$_$format' gave $sum, expected $calc_sum");
0b568b5f 472 } else {
473 my $text = ref $total ? &$total($len) : $total;
474 fail;
475 print "# For list (" . join (", ", @_) . ") (total $text)"
476 . " packed with $format unpack '%$_$format' gave $sum,"
477 . " expected $calc_sum\n";
478 }
479 }
fa8ec7c1 480 }
0b568b5f 481 }
2e821511 482 }
483}
484
fa8ec7c1 485numbers ('c', -128, -1, 0, 1, 127);
486numbers ('C', 0, 1, 127, 128, 255);
487numbers ('s', -32768, -1, 0, 1, 32767);
488numbers ('S', 0, 1, 32767, 32768, 65535);
489numbers ('i', -2147483648, -1, 0, 1, 2147483647);
490numbers ('I', 0, 1, 2147483647, 2147483648, 4294967295);
491numbers ('l', -2147483648, -1, 0, 1, 2147483647);
492numbers ('L', 0, 1, 2147483647, 2147483648, 4294967295);
493numbers ('s!', -32768, -1, 0, 1, 32767);
494numbers ('S!', 0, 1, 32767, 32768, 65535);
495numbers ('i!', -2147483648, -1, 0, 1, 2147483647);
496numbers ('I!', 0, 1, 2147483647, 2147483648, 4294967295);
497numbers ('l!', -2147483648, -1, 0, 1, 2147483647);
498numbers ('L!', 0, 1, 2147483647, 2147483648, 4294967295);
499numbers ('n', 0, 1, 32767, 32768, 65535);
500numbers ('v', 0, 1, 32767, 32768, 65535);
501numbers ('N', 0, 1, 2147483647, 2147483648, 4294967295);
502numbers ('V', 0, 1, 2147483647, 2147483648, 4294967295);
503# All these should have exact binary representations:
504numbers ('f', -1, 0, 0.5, 42, 2**34);
b85d93de 505numbers ('d', -(2**34), -1, 0, 1, 2**34);
506## These don't, but 'd' is NV. XXX wrong, it's double
507#numbers ('d', -1, 0, 1, 1-exp(-1), -exp(1));
fa8ec7c1 508
509numbers_with_total ('q', -1,
510 -9223372036854775808, -1, 0, 1,9223372036854775807);
800e6488 511# This total is icky, but the true total is 2**65-1, and need a way to generate
512# the epxected checksum on any system including those where NVs can preserve
513# 65 bits. (long double is 128 bits on sparc, so they certainly can)
514# or where rounding is down not up on binary conversion (crays)
515numbers_with_total ('Q', sub {
516 my $len = shift;
517 $len = 65 if $len > 65; # unmasked total is 2**65-1 here
518 my $total = 1 + 2 * (int (2**($len - 1)) - 1);
519 return 0 if $total == $total - 1; # Overflowed integers
520 return $total; # NVs still accurate to nearest integer
521 },
fa8ec7c1 522 0, 1,9223372036854775807, 9223372036854775808,
523 18446744073709551615);
524
525# pack nvNV byteorders
526
0b568b5f 527is(pack("n", 0xdead), "\xde\xad");
528is(pack("v", 0xdead), "\xad\xde");
529is(pack("N", 0xdeadbeef), "\xde\xad\xbe\xef");
530is(pack("V", 0xdeadbeef), "\xef\xbe\xad\xde");
43192e07 531
fa8ec7c1 532{
533 # /
534
535 my ($x, $y, $z);
536 eval { ($x) = unpack '/a*','hello' };
49704364 537 like($@, qr!'/' must follow a numeric type!);
72b034c3 538 undef $x;
539 eval { $x = unpack '/a*','hello' };
49704364 540 like($@, qr!'/' must follow a numeric type!);
0b568b5f 541
72b034c3 542 undef $x;
fa8ec7c1 543 eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
0b568b5f 544 is($@, '');
545 is($z, 'ok');
546 is($x, 'yes');
547 is($y, 'z');
72b034c3 548 undef $z;
549 eval { $z = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
550 is($@, '');
551 is($z, 'ok');
552
fa8ec7c1 553
72b034c3 554 undef $x;
fa8ec7c1 555 eval { ($x) = pack '/a*','hello' };
49704364 556 like($@, qr!Invalid type '/'!);
72b034c3 557 undef $x;
558 eval { $x = pack '/a*','hello' };
49704364 559 like($@, qr!Invalid type '/'!);
fa8ec7c1 560
561 $z = pack 'n/a* N/Z* w/A*','string','hi there ','etc';
562 my $expect = "\000\006string\0\0\0\012hi there \000\003etc";
0b568b5f 563 is($z, $expect);
4b5b2118 564
72b034c3 565 undef $x;
b85d93de 566 $expect = 'hello world';
567 eval { ($x) = unpack ("w/a", chr (11) . "hello world!")};
0b568b5f 568 is($x, $expect);
569 is($@, '');
570
72b034c3 571 undef $x;
b85d93de 572 # Doing this in scalar context used to fail.
573 eval { $x = unpack ("w/a", chr (11) . "hello world!")};
0b568b5f 574 is($@, '');
575 is($x, $expect);
b85d93de 576
fa8ec7c1 577 foreach (
0b568b5f 578 ['a/a*/a*', '212ab345678901234567','ab3456789012'],
579 ['a/a*/a*', '3012ab345678901234567', 'ab3456789012'],
580 ['a/a*/b*', '212ab', $Is_EBCDIC ? '100000010100' : '100001100100'],
581 )
582 {
fa8ec7c1 583 my ($pat, $in, $expect) = @$_;
72b034c3 584 undef $x;
fa8ec7c1 585 eval { ($x) = unpack $pat, $in };
0b568b5f 586 is($@, '');
587 is($x, $expect) ||
588 printf "# list unpack ('$pat', '$in') gave %s, expected '$expect'\n",
589 encode_list ($x);
590
72b034c3 591 undef $x;
b85d93de 592 eval { $x = unpack $pat, $in };
0b568b5f 593 is($@, '');
594 is($x, $expect) ||
595 printf "# scalar unpack ('$pat', '$in') gave %s, expected '$expect'\n",
596 encode_list ($x);
fa8ec7c1 597 }
4b5b2118 598
0b568b5f 599 # / with #
17f4a12d 600
72b034c3 601 my $pattern = <<'EOU';
17f4a12d 602 a3/A # Count in ASCII
603 C/a* # Count in a C char
604 C/Z # Count in a C char but skip after \0
605EOU
17f4a12d 606
72b034c3 607 $x = $y = $z =undef;
608 eval { ($z,$x,$y) = unpack $pattern, "003ok \003yes\004z\000abc" };
0b568b5f 609 is($@, '');
610 is($z, 'ok');
611 is($x, 'yes');
612 is($y, 'z');
72b034c3 613 undef $x;
614 eval { $z = unpack $pattern, "003ok \003yes\004z\000abc" };
615 is($@, '');
616 is($z, 'ok');
0b568b5f 617
72b034c3 618 $pattern = <<'EOP';
17f4a12d 619 n/a* # Count as network short
620 w/A* # Count a BER integer
621EOP
fa8ec7c1 622 $expect = "\000\006string\003etc";
72b034c3 623 $z = pack $pattern,'string','etc';
9e17a6b8 624 is($z, $expect);
fa8ec7c1 625}
626
60a99fa7 627
628SKIP: {
629 skip("(EBCDIC and) version strings are bad idea", 2) if $Is_EBCDIC;
630
631 is("1.20.300.4000", sprintf "%vd", pack("U*",1,20,300,4000));
632 is("1.20.300.4000", sprintf "%vd", pack(" U*",1,20,300,4000));
633}
0b568b5f 634isnt(v1.20.300.4000, sprintf "%vd", pack("C0U*",1,20,300,4000));
fa8ec7c1 635
0b568b5f 636my $rslt = $Is_EBCDIC ? "156 67" : "199 162";
637is(join(" ", unpack("C*", chr(0x1e2))), $rslt);
fa8ec7c1 638
639# does pack U create Unicode?
0b568b5f 640is(ord(pack('U', 300)), 300);
fa8ec7c1 641
642# does unpack U deref Unicode?
0b568b5f 643is((unpack('U', chr(300)))[0], 300);
fa8ec7c1 644
645# is unpack U the reverse of pack U for Unicode string?
0b568b5f 646is("@{[unpack('U*', pack('U*', 100, 200, 300))]}", "100 200 300");
fa8ec7c1 647
648# is unpack U the reverse of pack U for byte string?
0b568b5f 649is("@{[unpack('U*', pack('U*', 100, 200))]}", "100 200");
650
651
652SKIP: {
653 skip "Not for EBCDIC", 4 if $Is_EBCDIC;
fa8ec7c1 654
7eed2ccc 655 # does unpack C unravel pack U?
0b568b5f 656 is("@{[unpack('C*', pack('U*', 100, 200))]}", "100 195 136");
fa8ec7c1 657
7eed2ccc 658 # does pack U0C create Unicode?
0b568b5f 659 is("@{[pack('U0C*', 100, 195, 136)]}", v100.v200);
fa8ec7c1 660
7eed2ccc 661 # does pack C0U create characters?
0b568b5f 662 is("@{[pack('C0U*', 100, 200)]}", pack("C*", 100, 195, 136));
fa8ec7c1 663
2dcec3fe 664 # does unpack U0U on byte data warn?
665 {
666 local $SIG{__WARN__} = sub { $@ = "@_" };
667 my @null = unpack('U0U', chr(255));
0b568b5f 668 like($@, /^Malformed UTF-8 character /);
2dcec3fe 669 }
35bcd338 670}
671
fa8ec7c1 672{
673 my $p = pack 'i*', -2147483648, ~0, 0, 1, 2147483647;
674 my (@a);
675 # bug - % had to be at the start of the pattern, no leading whitespace or
676 # comments. %i! didn't work at all.
677 foreach my $pat ('%32i*', ' %32i*', "# Muhahahaha\n%32i*", '%32i* ',
678 '%32i!*', ' %32i!*', "\n#\n#\n\r \t\f%32i!*", '%32i!*#') {
679 @a = unpack $pat, $p;
0b568b5f 680 is($a[0], 0xFFFFFFFF) || print "# $pat\n";
fa8ec7c1 681 @a = scalar unpack $pat, $p;
0b568b5f 682 is($a[0], 0xFFFFFFFF) || print "# $pat\n";
fa8ec7c1 683 }
684
685
686 $p = pack 'I*', 42, 12;
687 # Multiline patterns in scalar context failed.
688 foreach my $pat ('I', <<EOPOEMSNIPPET, 'I#I', 'I # I', 'I # !!!') {
689# On the Ning Nang Nong
690# Where the Cows go Bong!
691# And the Monkeys all say Boo!
692I
693EOPOEMSNIPPET
0b568b5f 694 @a = unpack $pat, $p;
695 is(scalar @a, 1);
696 is($a[0], 42);
697 @a = scalar unpack $pat, $p;
698 is(scalar @a, 1);
699 is($a[0], 42);
700 }
fa8ec7c1 701
702 # shorts (of all flavours) didn't calculate checksums > 32 bits with floating
703 # point, so a pathologically long pattern would wrap at 32 bits.
704 my $pat = "\xff\xff"x65538; # Start with it long, to save any copying.
705 foreach (4,3,2,1,0) {
706 my $len = 65534 + $_;
0b568b5f 707 is(unpack ("%33n$len", $pat), 65535 * $len);
fa8ec7c1 708 }
709}
b85d93de 710
711
712# pack x X @
713foreach (
0b568b5f 714 ['x', "N", "\0"],
715 ['x4', "N", "\0"x4],
716 ['xX', "N", ""],
717 ['xXa*', "Nick", "Nick"],
718 ['a5Xa5', "cameL", "llama", "camellama"],
719 ['@4', 'N', "\0"x4],
720 ['a*@8a*', 'Camel', 'Dromedary', "Camel\0\0\0Dromedary"],
721 ['a*@4a', 'Perl rules', '!', 'Perl!'],
722)
723{
b85d93de 724 my ($template, @in) = @$_;
725 my $out = pop @in;
726 my $got = eval {pack $template, @in};
0b568b5f 727 is($@, '');
728 is($out, $got) ||
729 printf "# pack ('$template', %s) gave %s expected %s\n",
730 encode_list (@in), encode_list ($got), encode_list ($out);
b85d93de 731}
732
733# unpack x X @
734foreach (
0b568b5f 735 ['x', "N"],
736 ['xX', "N"],
737 ['xXa*', "Nick", "Nick"],
738 ['a5Xa5', "camellama", "camel", "llama"],
739 ['@3', "ice"],
740 ['@2a2', "water", "te"],
741 ['a*@1a3', "steam", "steam", "tea"],
742)
743{
b85d93de 744 my ($template, $in, @out) = @$_;
745 my @got = eval {unpack $template, $in};
0b568b5f 746 is($@, '');
c8f824eb 747 ok (list_eq (\@got, \@out)) ||
9e17a6b8 748 printf "# list unpack ('$template', %s) gave %s expected %s\n",
749 _qq($in), encode_list (@got), encode_list (@out);
b85d93de 750
751 my $got = eval {unpack $template, $in};
0b568b5f 752 is($@, '');
753 @out ? is( $got, $out[0] ) # 1 or more items; should get first
754 : ok( !defined $got ) # 0 items; should get undef
9e17a6b8 755 or printf "# scalar unpack ('$template', %s) gave %s expected %s\n",
756 _qq($in), encode_list ($got), encode_list ($out[0]);
b85d93de 757}
d50dd4e4 758
759{
d50dd4e4 760 my $t = 'Z*Z*';
761 my ($u, $v) = qw(foo xyzzy);
762 my $p = pack($t, $u, $v);
763 my @u = unpack($t, $p);
0b568b5f 764 is(scalar @u, 2);
765 is($u[0], $u);
766 is($u[1], $v);
d50dd4e4 767}
bf80f5a2 768
769{
0b568b5f 770 is((unpack("w/a*", "\x02abc"))[0], "ab");
bf80f5a2 771
0b568b5f 772 # "w/a*" should be seen as one unit
bf80f5a2 773
0b568b5f 774 is(scalar unpack("w/a*", "\x02abc"), "ab");
bf80f5a2 775}
b81060d6 776
777{
b81060d6 778 # from Wolfgang Laun: fix in change #13163
779
780 my $s = 'ABC' x 10;
644d52eb 781 my $t = '*';
782 my $x = ord($t);
b81060d6 783 my $buf = pack( 'Z*/A* C', $s, $x );
784 my $y;
785
786 my $h = $buf;
787 $h =~ s/[^[:print:]]/./g;
788 ( $s, $y ) = unpack( "Z*/A* C", $buf );
644d52eb 789 is($h, "30.ABCABCABCABCABCABCABCABCABCABC$t");
0b568b5f 790 is(length $buf, 34);
791 is($s, "ABCABCABCABCABCABCABCABCABCABC");
644d52eb 792 is($y, $x);
b81060d6 793}
3bf38418 794
795{
b223308b 796 # from Wolfgang Laun: fix in change #13288
3bf38418 797
b223308b 798 eval { my $t=unpack("P*", "abc") };
49704364 799 like($@, qr/'P' must have an explicit size/);
3bf38418 800}
18529408 801
802{ # Grouping constructs
803 my (@a, @b);
804 @a = unpack '(SL)', pack 'SLSLSL', 67..90;
805 is("@a", "67 68");
806 @a = unpack '(SL)3', pack 'SLSLSL', 67..90;
807 @b = (67..72);
808 is("@a", "@b");
809 @a = unpack '(SL)3', pack 'SLSLSLSL', 67..90;
810 is("@a", "@b");
811 @a = unpack '(SL)[3]', pack 'SLSLSLSL', 67..90;
812 is("@a", "@b");
813 @a = unpack '(SL)[2] SL', pack 'SLSLSLSL', 67..90;
814 is("@a", "@b");
815 @a = unpack 'A/(SL)', pack 'ASLSLSLSL', 3, 67..90;
816 is("@a", "@b");
817 @a = unpack 'A/(SL)SL', pack 'ASLSLSLSL', 2, 67..90;
818 is("@a", "@b");
819 @a = unpack '(SL)*', pack 'SLSLSLSL', 67..90;
820 @b = (67..74);
821 is("@a", "@b");
822 @a = unpack '(SL)*SL', pack 'SLSLSLSL', 67..90;
823 is("@a", "@b");
824 eval { @a = unpack '(*SL)', '' };
825 like($@, qr/\(\)-group starts with a count/);
826 eval { @a = unpack '(3SL)', '' };
827 like($@, qr/\(\)-group starts with a count/);
828 eval { @a = unpack '([3]SL)', '' };
829 like($@, qr/\(\)-group starts with a count/);
830 eval { @a = pack '(*SL)' };
831 like($@, qr/\(\)-group starts with a count/);
832 @a = unpack '(SL)3 SL', pack '(SL)4', 67..74;
833 is("@a", "@b");
834 @a = unpack '(SL)3 SL', pack '(SL)[4]', 67..74;
835 is("@a", "@b");
836 @a = unpack '(SL)3 SL', pack '(SL)*', 67..74;
837 is("@a", "@b");
838}
206947d2 839
49704364 840{ # more on grouping (W.Laun)
841 use warnings;
842 my $warning;
843 local $SIG{__WARN__} = sub {
844 $warning = $_[0];
845 };
846 # @ absolute within ()-group
847 my $badc = pack( '(a)*', unpack( '(@1a @0a @2)*', 'abcd' ) );
848 is( $badc, 'badc' );
849 my @b = ( 1, 2, 3 );
850 my $buf = pack( '(@1c)((@2C)@3c)', @b );
851 is( $buf, "\0\1\0\0\2\3" );
852 my @a = unpack( '(@1c)((@2c)@3c)', $buf );
853 is( "@a", "@b" );
854
855 # various unpack count/code scenarios
856 my @Env = ( a => 'AAA', b => 'BBB' );
857 my $env = pack( 'S(S/A*S/A*)*', @Env/2, @Env );
858
859 # unpack full length - ok
860 my @pup = unpack( 'S/(S/A* S/A*)', $env );
861 is( "@pup", "@Env" );
862
863 # warn when count/code goes beyond end of string
864 # \0002 \0001 a \0003 AAA \0001 b \0003 BBB
865 # 2 4 5 7 10 1213
866 eval { @pup = unpack( 'S/(S/A* S/A*)', substr( $env, 0, 13 ) ) };
867 like( $@, qr{length/code after end of string} );
868
869 # postfix repeat count
870 $env = pack( '(S/A* S/A*)' . @Env/2, @Env );
871
872 # warn when count/code goes beyond end of string
873 # \0001 a \0003 AAA \0001 b \0003 BBB
874 # 2 3c 5 8 10 11 13 16
875 eval { @pup = unpack( '(S/A* S/A*)' . @Env/2, substr( $env, 0, 11 ) ) };
876 like( $@, qr{length/code after end of string} );
877
878 # catch stack overflow/segfault
879 eval { $_ = pack( ('(' x 105) . 'A' . (')' x 105) ); };
880 like( $@, qr{Too deeply nested \(\)-groups} );
881}
882
883{ # syntax checks (W.Laun)
884 use warnings;
885 my @warning;
886 local $SIG{__WARN__} = sub {
887 push( @warning, $_[0] );
888 };
889 eval { my $s = pack( 'Ax![4c]A', 1..5 ); };
890 like( $@, qr{Malformed integer in \[\]} );
891
892 eval { my $buf = pack( '(c/*a*)', 'AAA', 'BB' ); };
893 like( $@, qr{'/' does not take a repeat count} );
894
895 eval { my @inf = unpack( 'c/1a', "\x03AAA\x02BB" ); };
896 like( $@, qr{'/' does not take a repeat count} );
897
898 eval { my @inf = unpack( 'c/*a', "\x03AAA\x02BB" ); };
899 like( $@, qr{'/' does not take a repeat count} );
900
901 # white space where possible
902 my @Env = ( a => 'AAA', b => 'BBB' );
903 my $env = pack( ' S ( S / A* S / A* )* ', @Env/2, @Env );
904 my @pup = unpack( ' S / ( S / A* S / A* ) ', $env );
905 is( "@pup", "@Env" );
906
907 # white space in 4 wrong places
908 for my $temp ( 'A ![4]', 'A [4]', 'A *', 'A 4' ){
909 eval { my $s = pack( $temp, 'B' ); };
910 like( $@, qr{Invalid type } );
911 }
912
913 # warning for commas
914 @warning = ();
915 my $x = pack( 'I,A', 4, 'X' );
916 like( $warning[0], qr{Invalid type ','} );
917
918 # comma warning only once
919 @warning = ();
920 $x = pack( 'C(C,C)C,C', 65..71 );
921 like( scalar @warning, 1 );
922
923 # forbidden code in []
924 eval { my $x = pack( 'A[@4]', 'XXXX' ); };
925 like( $@, qr{Within \[\]-length '\@' not allowed} );
926
927 # @ repeat default 1
928 my $s = pack( 'AA@A', 'A', 'B', 'C' );
929 my @c = unpack( 'AA@A', $s );
930 is( $s, 'AC' );
931 is( "@c", "A C C" );
932
933 # no unpack code after /
934 eval { my @a = unpack( "C/", "\3" ); };
935 like( $@, qr{Code missing after '/'} );
936
937}
938
206947d2 939{ # Repeat count [SUBEXPR]
92d41999 940 my @codes = qw( x A Z a c C B b H h s v n S i I l V N L p P f F d
941 s! S! i! I! l! L! j J);
942 my $G;
206947d2 943 if (eval { pack 'q', 1 } ) {
944 push @codes, qw(q Q);
945 } else {
946 push @codes, qw(c C); # Keep the count the same
947 }
92d41999 948 if (eval { pack 'D', 1 } ) {
949 push @codes, 'D';
950 } else {
951 push @codes, 'd'; # Keep the count the same
952 }
206947d2 953
954 my %val;
955 @val{@codes} = map { / [Xx] (?{ undef })
956 | [AZa] (?{ 'something' })
957 | C (?{ 214 })
958 | c (?{ 114 })
959 | [Bb] (?{ '101' })
960 | [Hh] (?{ 'b8' })
92d41999 961 | [svnSiIlVNLqQjJ] (?{ 10111 })
206947d2 962 | [FfDd] (?{ 1.36514538e67 })
963 | [pP] (?{ "try this buffer" })
964 /x; $^R } @codes;
965 my @end = (0x12345678, 0x23456781, 0x35465768, 0x15263748);
966 my $end = "N4";
967
968 for my $type (@codes) {
969 my @list = $val{$type};
970 @list = () unless defined $list[0];
971 for my $count ('', '3', '[11]') {
972 my $c = 1;
973 $c = $1 if $count =~ /(\d+)/;
974 my @list1 = @list;
975 @list1 = (@list1) x $c unless $type =~ /[XxAaZBbHhP]/;
976 for my $groupend ('', ')2', ')[8]') {
977 my $groupbegin = ($groupend ? '(' : '');
978 $c = 1;
979 $c = $1 if $groupend =~ /(\d+)/;
980 my @list2 = (@list1) x $c;
981
982 my $junk1 = "$groupbegin $type$count $groupend";
983 # print "# junk1=$junk1\n";
984 my $p = pack $junk1, @list2;
985 my $half = int( (length $p)/2 );
62f95557 986 for my $move ('', "X$half", "X!$half", 'x1', 'x!8', "x$half") {
206947d2 987 my $junk = "$junk1 $move";
62f95557 988 # print "# junk='$junk', list=(@list2)\n";
206947d2 989 $p = pack "$junk $end", @list2, @end;
990 my @l = unpack "x[$junk] $end", $p;
991 is(scalar @l, scalar @end);
992 is("@l", "@end", "skipping x[$junk]");
993 }
994 }
995 }
996 }
997}
998
999# / is recognized after spaces in scalar context
1000# XXXX no spaces are allowed in pack... In pack only before the slash...
1001is(scalar unpack('A /A Z20', pack 'A/A* Z20', 'bcde', 'xxxxx'), 'bcde');
1002is(scalar unpack('A /A /A Z20', '3004bcde'), 'bcde');
62f95557 1003
1004{ # X! and x!
1005 my $t = 'C[3] x!8 C[2]';
1006 my @a = (0x73..0x77);
1007 my $p = pack($t, @a);
1008 is($p, "\x73\x74\x75\0\0\0\0\0\x76\x77");
1009 my @b = unpack $t, $p;
1010 is(scalar @b, scalar @a);
1011 is("@b", "@a", 'x!8');
1012 $t = 'x[5] C[6] X!8 C[2]';
1013 @a = (0x73..0x7a);
1014 $p = pack($t, @a);
1015 is($p, "\0\0\0\0\0\x73\x74\x75\x79\x7a");
1016 @b = unpack $t, $p;
1017 @a = (0x73..0x75, 0x79, 0x7a, 0x79, 0x7a);
1018 is(scalar @b, scalar @a);
1019 is("@b", "@a");
1020}
1021
1022{ # struct {char c1; double d; char cc[2];}
1023 my $t = 'C x![d] d C[2]';
1024 my @a = (173, 1.283476517e-45, 42, 215);
1025 my $p = pack $t, @a;
1026 ok( length $p);
1027 my @b = unpack "$t X[$t] $t", $p; # Extract, step back, extract again
1028 is(scalar @b, 2 * scalar @a);
3f7e3417 1029 $b = "@b";
1030 $b =~ s/(?:17000+|16999+)\d+(e-45) /17$1 /gi; # stringification is gamble
1031 is($b, "@a @a");
62f95557 1032
1033 my $warning;
1034 local $SIG{__WARN__} = sub {
1035 $warning = $_[0];
1036 };
1037 @b = unpack "x[C] x[$t] X[$t] X[C] $t", "$p\0";
1038
1039 is($warning, undef);
1040 is(scalar @b, scalar @a);
3f7e3417 1041 $b = "@b";
1042 $b =~ s/(?:17000+|16999+)\d+(e-45) /17$1 /gi; # stringification is gamble
1043 is($b, "@a");
62f95557 1044}
92d41999 1045
1046is(length(pack("j", 0)), $Config{ivsize});
1047is(length(pack("J", 0)), $Config{uvsize});
1048is(length(pack("F", 0)), $Config{nvsize});
1049
1050numbers ('j', -2147483648, -1, 0, 1, 2147483647);
1051numbers ('J', 0, 1, 2147483647, 2147483648, 4294967295);
1052numbers ('F', -(2**34), -1, 0, 1, 2**34);
1053SKIP: {
1054 my $t = eval { unpack("D*", pack("D", 12.34)) };
1055
49704364 1056 skip "Long doubles not in use", 56 if $@ =~ /Invalid type/;
92d41999 1057
1058 is(length(pack("D", 0)), $Config{longdblsize});
1059 numbers ('D', -(2**34), -1, 0, 1, 2**34);
1060}
1061
c8f824eb 1062# Maybe this knowledge needs to be "global" for all of pack.t
1063# Or a "can checksum" which would effectively be all the number types"
1064my %cant_checksum = map {$_=> 1} qw(A Z u w);
1065# not a b B h H
1066foreach my $template (qw(A Z c C s S i I l L n N v V q Q j J f d F D u U w)) {
1067 SKIP: {
1068 my $packed = eval {pack "${template}4", 1, 4, 9, 16};
1069 if ($@) {
49704364 1070 die unless $@ =~ /Invalid type '$template'/;
c8f824eb 1071 skip ("$template not supported on this perl",
1072 $cant_checksum{$template} ? 4 : 8);
1073 }
1074 my @unpack4 = unpack "${template}4", $packed;
1075 my @unpack = unpack "${template}*", $packed;
1076 my @unpack1 = unpack "${template}", $packed;
1077 my @unpack1s = scalar unpack "${template}", $packed;
1078 my @unpack4s = scalar unpack "${template}4", $packed;
1079 my @unpacks = scalar unpack "${template}*", $packed;
1080
1081 my @tests = ( ["${template}4 vs ${template}*", \@unpack4, \@unpack],
1082 ["scalar ${template} ${template}", \@unpack1s, \@unpack1],
1083 ["scalar ${template}4 vs ${template}", \@unpack4s, \@unpack1],
1084 ["scalar ${template}* vs ${template}", \@unpacks, \@unpack1],
1085 );
1086
1087 unless ($cant_checksum{$template}) {
1088 my @unpack4_c = unpack "\%${template}4", $packed;
1089 my @unpack_c = unpack "\%${template}*", $packed;
1090 my @unpack1_c = unpack "\%${template}", $packed;
1091 my @unpack1s_c = scalar unpack "\%${template}", $packed;
1092 my @unpack4s_c = scalar unpack "\%${template}4", $packed;
1093 my @unpacks_c = scalar unpack "\%${template}*", $packed;
1094
1095 push @tests,
1096 ( ["% ${template}4 vs ${template}*", \@unpack4_c, \@unpack_c],
1097 ["% scalar ${template} ${template}", \@unpack1s_c, \@unpack1_c],
1098 ["% scalar ${template}4 vs ${template}*", \@unpack4s_c, \@unpack_c],
1099 ["% scalar ${template}* vs ${template}*", \@unpacks_c, \@unpack_c],
1100 );
1101 }
1102 foreach my $test (@tests) {
1103 ok (list_eq ($test->[1], $test->[2]), $test->[0]) ||
1104 printf "# unpack gave %s expected %s\n",
1105 encode_list (@{$test->[1]}), encode_list (@{$test->[2]});
1106 }
1107 }
1108}
9c3dc587 1109
1110ok(pack('u2', 'AA'), "[perl #8026]"); # used to hang and eat RAM in perl 5.7.2
1111
fe581ec7 1112$_ = pack('c', 65); # 'A' would not be EBCDIC-friendly
1113is(unpack('c'), 65, "one-arg unpack (change #18751)"); # defaulting to $_
db37a92e 1114
1115{
1116 my $a = "X\t01234567\n" x 100;
1117 my @a = unpack("(a1 c/a)*", $a);
1118 is(scalar @a, 200, "[perl #15288]");
1119 is($a[-1], "01234567\n", "[perl #15288]");
1120 is($a[-2], "X", "[perl #15288]");
1121}