Integrate mainline - all tests pass - some noise from threads
[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
92d41999 9plan tests => 5619;
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) = @_;
29 return unless @$l == @$r;
30 for my $i (0..$#$l) {
31 if (defined $l->[$i]) {
32 return unless defined ($r->[$i]) && $l->[$i] eq $r->[$i];
33 } else {
34 return if defined $r->[$i]
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);
fa8ec7c1 125}
def98dd4 126
0b568b5f 127
fa8ec7c1 128{
129 # test exeptions
130 my $x;
131 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
0b568b5f 132 like($@, qr/^Unterminated compressed integer/);
def98dd4 133
fa8ec7c1 134 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
0b568b5f 135 like($@, qr/^Unterminated compressed integer/);
def98dd4 136
fa8ec7c1 137 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
0b568b5f 138 like($@, qr/^Unterminated compressed integer/);
fa8ec7c1 139}
def98dd4 140
84902520 141#
142# test the "p" template
143
144# literals
0b568b5f 145is(unpack("p",pack("p","foo")), "foo");
84902520 146
147# scalars
0b568b5f 148is(unpack("p",pack("p",239)), 239);
84902520 149
150# temps
151sub foo { my $a = "a"; return $a . $a++ . $a++ }
152{
9f1b1f2d 153 use warnings;
0b568b5f 154 my $warning;
84902520 155 local $SIG{__WARN__} = sub {
0b568b5f 156 $warning = $_[0];
84902520 157 };
158 my $junk = pack("p", &foo);
0b568b5f 159
160 like($warning, qr/temporary val/);
84902520 161}
162
163# undef should give null pointer
0b568b5f 164like(pack("p", undef), qr/^\0+/);
84902520 165
20408e3c 166# Check for optimizer bug (e.g. Digital Unix GEM cc with -O4 on DU V4.0B gives
167# 4294967295 instead of -1)
168# see #ifdef __osf__ in pp.c pp_unpack
0b568b5f 169is((unpack("i",pack("i",-1))), -1);
20408e3c 170
0b568b5f 171# test the pack lengths of s S i I l L
172# test the pack lengths of n N v V
fa8ec7c1 173my @lengths = qw(s 2 S 2 i -4 I -4 l 4 L 4 n 2 N 4 v 2 V 4);
174while (my ($format, $expect) = splice @lengths, 0, 2) {
175 my $len = length(pack($format, 0));
176 if ($expect > 0) {
0b568b5f 177 is($expect, $len, "format '$format'");
fa8ec7c1 178 } else {
179 $expect = -$expect;
0b568b5f 180 ok ($len >= $expect, "format '$format'") ||
181 print "# format '$format' has length $len, expected >= $expect\n";
fa8ec7c1 182 }
183}
d4217c7e 184
d4217c7e 185
0b568b5f 186# test unpack-pack lengths
187my @templates = qw(c C i I s S l L n N v V f d q Q);
d4217c7e 188
189foreach my $t (@templates) {
0b568b5f 190 SKIP: {
191 my @t = eval { unpack("$t*", pack("$t*", 12, 34)) };
192
193 # quads not supported everywhere
194 skip "Quads not supported", 4 if $@ =~ /Invalid type in pack/;
195 is( $@, '' );
196
197 is(scalar @t, 2);
3020ec7a 198
199 SKIP: {
200 skip "$t not expected to work for some reason", 2 if $t =~ /[nv]/i;
201
0b568b5f 202 is($t[0], 12);
203 is($t[1], 34);
204 }
0b568b5f 205 }
d4217c7e 206}
9d116dd7 207
fa8ec7c1 208{
0b568b5f 209 # uuencode/decode
9d116dd7 210
0b568b5f 211 # Note that first uuencoding known 'text' data and then checking the
212 # binary values of the uuencoded version would not be portable between
213 # character sets. Uuencoding is meant for encoding binary data, not
214 # text data.
c4d5f83a 215
0b568b5f 216 my $in = pack 'C*', 0 .. 255;
ba1ac976 217
0b568b5f 218 # just to be anal, we do some random tr/`/ /
219 my $uu = <<'EOUU';
ba1ac976 220M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL
9d116dd7 221M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9
222M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6&
223MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S
224MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@
ba1ac976 225?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P `
9d116dd7 226EOUU
227
0b568b5f 228 $_ = $uu;
229 tr/ /`/;
9d116dd7 230
0b568b5f 231 is(pack('u', $in), $_);
fa8ec7c1 232
0b568b5f 233 is(unpack('u', $uu), $in);
9d116dd7 234
0b568b5f 235 $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";
236 $uu = <<'EOUU';
eddc390b 237M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
238&8%P:````
239EOUU
240
0b568b5f 241 is(unpack('u', $uu), $in);
eddc390b 242
0b568b5f 243# This is identical to the above except that backquotes have been
244# changed to spaces
eddc390b 245
0b568b5f 246 $uu = <<'EOUU';
eddc390b 247M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
c4d5f83a 248&8%P:
eddc390b 249EOUU
250
0b568b5f 251 # ' # Grr
252 is(unpack('u', $uu), $in);
ef54e1a4 253
d99ad34e 254}
726ea183 255
0b568b5f 256# test the ascii template types (A, a, Z)
726ea183 257
fa8ec7c1 258foreach (
0b568b5f 259['p', 'A*', "foo\0bar\0 ", "foo\0bar\0 "],
fa8ec7c1 260['p', 'A11', "foo\0bar\0 ", "foo\0bar\0 "],
0b568b5f 261['u', 'A*', "foo\0bar \0", "foo\0bar"],
262['u', 'A8', "foo\0bar \0", "foo\0bar"],
263['p', 'a*', "foo\0bar\0 ", "foo\0bar\0 "],
fa8ec7c1 264['p', 'a11', "foo\0bar\0 ", "foo\0bar\0 \0\0"],
0b568b5f 265['u', 'a*', "foo\0bar \0", "foo\0bar \0"],
266['u', 'a8', "foo\0bar \0", "foo\0bar "],
267['p', 'Z*', "foo\0bar\0 ", "foo\0bar\0 \0"],
fa8ec7c1 268['p', 'Z11', "foo\0bar\0 ", "foo\0bar\0 \0\0"],
0b568b5f 269['p', 'Z3', "foo", "fo\0"],
270['u', 'Z*', "foo\0bar \0", "foo"],
271['u', 'Z8', "foo\0bar \0", "foo"],
272)
273{
274 my ($what, $template, $in, $out) = @$_;
275 my $got = $what eq 'u' ? (unpack $template, $in) : (pack $template, $in);
276 unless (is($got, $out)) {
0b568b5f 277 my $un = $what eq 'u' ? 'un' : '';
9e17a6b8 278 print "# ${un}pack ('$template', "._qq($in).') gave '._qq($out).
279 ' not '._qq($got)."\n";
0b568b5f 280 }
d99ad34e 281}
726ea183 282
0b568b5f 283# packing native shorts/ints/longs
726ea183 284
0b568b5f 285is(length(pack("s!", 0)), $Config{shortsize});
286is(length(pack("i!", 0)), $Config{intsize});
287is(length(pack("l!", 0)), $Config{longsize});
288ok(length(pack("s!", 0)) <= length(pack("i!", 0)));
289ok(length(pack("i!", 0)) <= length(pack("l!", 0)));
290is(length(pack("i!", 0)), length(pack("i", 0)));
726ea183 291
fa8ec7c1 292sub numbers {
293 my $format = shift;
294 return numbers_with_total ($format, undef, @_);
d99ad34e 295}
726ea183 296
fa8ec7c1 297sub numbers_with_total {
298 my $format = shift;
299 my $total = shift;
300 if (!defined $total) {
301 foreach (@_) {
302 $total += $_;
303 }
304 }
305 foreach (@_) {
0b568b5f 306 SKIP: {
307 my $out = eval {unpack($format, pack($format, $_))};
308 skip "cannot pack '$format' on this perl", 2 if
309 $@ =~ /Invalid type in pack: '$format'/;
310
311 is($@, '');
312 is($out, $_);
fa8ec7c1 313 }
fa8ec7c1 314 }
726ea183 315
fa8ec7c1 316 my $skip_if_longer_than = ~0; # "Infinity"
317 if (~0 - 1 == ~0) {
318 # If we're running with -DNO_PERLPRESERVE_IVUV and NVs don't preserve all
319 # UVs (in which case ~0 is NV, ~0-1 will be the same NV) then we can't
320 # correctly in perl calculate UV totals for long checksums, as pp_unpack
321 # is using UV maths, and we've only got NVs.
322 $skip_if_longer_than = $Config{d_nv_preserves_uv_bits};
323 }
726ea183 324
fa8ec7c1 325 foreach ('', 1, 2, 3, 15, 16, 17, 31, 32, 33, 53, 54, 63, 64, 65) {
0b568b5f 326 SKIP: {
327 my $sum = eval {unpack "%$_$format*", pack "$format*", @_};
328 skip "cannot pack '$format' on this perl", 3
329 if $@ =~ /Invalid type in pack: '$format'/;
330
331 is($@, '');
332 ok(defined $sum);
333
334 my $len = $_; # Copy, so that we can reassign ''
335 $len = 16 unless length $len;
336
337 SKIP: {
338 skip "cannot test checksums over $skip_if_longer_than bits", 1
339 if $len > $skip_if_longer_than;
340
341 # Our problem with testing this portably is that the checksum code in
342 # pp_unpack is able to cast signed to unsigned, and do modulo 2**n
343 # arithmetic in unsigned ints, which perl has no operators to do.
344 # (use integer; does signed ints, which won't wrap on UTS, which is just
345 # fine with ANSI, but not with most people's assumptions.
346 # This is why we need to supply the totals for 'Q' as there's no way in
347 # perl to calculate them, short of unpack '%0Q' (is that documented?)
348 # ** returns NVs; make sure it's IV.
349 my $max = 1 + 2 * (int (2 ** ($len-1))-1); # The max possible checksum
350 my $max_p1 = $max + 1;
351 my ($max_is_integer, $max_p1_is_integer);
352 $max_p1_is_integer = 1 unless $max_p1 + 1 == $max_p1;
353 $max_is_integer = 1 if $max - 1 < ~0;
354
355 my $calc_sum;
356 if (ref $total) {
357 $calc_sum = &$total($len);
358 } else {
359 $calc_sum = $total;
360 # Shift into range by some multiple of the total
361 my $mult = int ($total / $max_p1);
362 # Need this to make sure that -1 + (~0+1) is ~0 (ie still integer)
363 $calc_sum = $total - $mult;
364 $calc_sum -= $mult * $max;
365 if ($calc_sum < 0) {
366 $calc_sum += 1;
367 $calc_sum += $max;
368 }
369 }
370 if ($calc_sum == $calc_sum - 1 && $calc_sum == $max_p1) {
371 # we're into floating point (either by getting out of the range of
372 # UV arithmetic, or because we're doing a floating point checksum)
373 # and our calculation of the checksum has become rounded up to
374 # max_checksum + 1
375 $calc_sum = 0;
376 }
377
0dccb3d1 378 if ($calc_sum == $sum) { # HAS to be ==, not eq (so no is()).
379 ok ("unpack '%$_$format' gave $sum");
0b568b5f 380 } else {
381 my $delta = 1.000001;
382 if ($format =~ tr /dDfF//
383 && ($calc_sum <= $sum * $delta && $calc_sum >= $sum / $delta)) {
0dccb3d1 384 pass ("unpack '%$_$format' gave $sum, expected $calc_sum");
0b568b5f 385 } else {
386 my $text = ref $total ? &$total($len) : $total;
387 fail;
388 print "# For list (" . join (", ", @_) . ") (total $text)"
389 . " packed with $format unpack '%$_$format' gave $sum,"
390 . " expected $calc_sum\n";
391 }
392 }
fa8ec7c1 393 }
0b568b5f 394 }
2e821511 395 }
396}
397
fa8ec7c1 398numbers ('c', -128, -1, 0, 1, 127);
399numbers ('C', 0, 1, 127, 128, 255);
400numbers ('s', -32768, -1, 0, 1, 32767);
401numbers ('S', 0, 1, 32767, 32768, 65535);
402numbers ('i', -2147483648, -1, 0, 1, 2147483647);
403numbers ('I', 0, 1, 2147483647, 2147483648, 4294967295);
404numbers ('l', -2147483648, -1, 0, 1, 2147483647);
405numbers ('L', 0, 1, 2147483647, 2147483648, 4294967295);
406numbers ('s!', -32768, -1, 0, 1, 32767);
407numbers ('S!', 0, 1, 32767, 32768, 65535);
408numbers ('i!', -2147483648, -1, 0, 1, 2147483647);
409numbers ('I!', 0, 1, 2147483647, 2147483648, 4294967295);
410numbers ('l!', -2147483648, -1, 0, 1, 2147483647);
411numbers ('L!', 0, 1, 2147483647, 2147483648, 4294967295);
412numbers ('n', 0, 1, 32767, 32768, 65535);
413numbers ('v', 0, 1, 32767, 32768, 65535);
414numbers ('N', 0, 1, 2147483647, 2147483648, 4294967295);
415numbers ('V', 0, 1, 2147483647, 2147483648, 4294967295);
416# All these should have exact binary representations:
417numbers ('f', -1, 0, 0.5, 42, 2**34);
b85d93de 418numbers ('d', -(2**34), -1, 0, 1, 2**34);
419## These don't, but 'd' is NV. XXX wrong, it's double
420#numbers ('d', -1, 0, 1, 1-exp(-1), -exp(1));
fa8ec7c1 421
422numbers_with_total ('q', -1,
423 -9223372036854775808, -1, 0, 1,9223372036854775807);
800e6488 424# This total is icky, but the true total is 2**65-1, and need a way to generate
425# the epxected checksum on any system including those where NVs can preserve
426# 65 bits. (long double is 128 bits on sparc, so they certainly can)
427# or where rounding is down not up on binary conversion (crays)
428numbers_with_total ('Q', sub {
429 my $len = shift;
430 $len = 65 if $len > 65; # unmasked total is 2**65-1 here
431 my $total = 1 + 2 * (int (2**($len - 1)) - 1);
432 return 0 if $total == $total - 1; # Overflowed integers
433 return $total; # NVs still accurate to nearest integer
434 },
fa8ec7c1 435 0, 1,9223372036854775807, 9223372036854775808,
436 18446744073709551615);
437
438# pack nvNV byteorders
439
0b568b5f 440is(pack("n", 0xdead), "\xde\xad");
441is(pack("v", 0xdead), "\xad\xde");
442is(pack("N", 0xdeadbeef), "\xde\xad\xbe\xef");
443is(pack("V", 0xdeadbeef), "\xef\xbe\xad\xde");
43192e07 444
fa8ec7c1 445{
446 # /
447
448 my ($x, $y, $z);
449 eval { ($x) = unpack '/a*','hello' };
0b568b5f 450 like($@, qr!/ must follow a numeric type!);
72b034c3 451 undef $x;
452 eval { $x = unpack '/a*','hello' };
453 like($@, qr!/ must follow a numeric type!);
0b568b5f 454
72b034c3 455 undef $x;
fa8ec7c1 456 eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
0b568b5f 457 is($@, '');
458 is($z, 'ok');
459 is($x, 'yes');
460 is($y, 'z');
72b034c3 461 undef $z;
462 eval { $z = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
463 is($@, '');
464 is($z, 'ok');
465
fa8ec7c1 466
72b034c3 467 undef $x;
fa8ec7c1 468 eval { ($x) = pack '/a*','hello' };
0b568b5f 469 like($@, qr!Invalid type in pack: '/'!);
72b034c3 470 undef $x;
471 eval { $x = pack '/a*','hello' };
472 like($@, qr!Invalid type in pack: '/'!);
fa8ec7c1 473
474 $z = pack 'n/a* N/Z* w/A*','string','hi there ','etc';
475 my $expect = "\000\006string\0\0\0\012hi there \000\003etc";
0b568b5f 476 is($z, $expect);
4b5b2118 477
72b034c3 478 undef $x;
b85d93de 479 $expect = 'hello world';
480 eval { ($x) = unpack ("w/a", chr (11) . "hello world!")};
0b568b5f 481 is($x, $expect);
482 is($@, '');
483
72b034c3 484 undef $x;
b85d93de 485 # Doing this in scalar context used to fail.
486 eval { $x = unpack ("w/a", chr (11) . "hello world!")};
0b568b5f 487 is($@, '');
488 is($x, $expect);
b85d93de 489
fa8ec7c1 490 foreach (
0b568b5f 491 ['a/a*/a*', '212ab345678901234567','ab3456789012'],
492 ['a/a*/a*', '3012ab345678901234567', 'ab3456789012'],
493 ['a/a*/b*', '212ab', $Is_EBCDIC ? '100000010100' : '100001100100'],
494 )
495 {
fa8ec7c1 496 my ($pat, $in, $expect) = @$_;
72b034c3 497 undef $x;
fa8ec7c1 498 eval { ($x) = unpack $pat, $in };
0b568b5f 499 is($@, '');
500 is($x, $expect) ||
501 printf "# list unpack ('$pat', '$in') gave %s, expected '$expect'\n",
502 encode_list ($x);
503
72b034c3 504 undef $x;
b85d93de 505 eval { $x = unpack $pat, $in };
0b568b5f 506 is($@, '');
507 is($x, $expect) ||
508 printf "# scalar unpack ('$pat', '$in') gave %s, expected '$expect'\n",
509 encode_list ($x);
fa8ec7c1 510 }
4b5b2118 511
0b568b5f 512 # / with #
17f4a12d 513
72b034c3 514 my $pattern = <<'EOU';
17f4a12d 515 a3/A # Count in ASCII
516 C/a* # Count in a C char
517 C/Z # Count in a C char but skip after \0
518EOU
17f4a12d 519
72b034c3 520 $x = $y = $z =undef;
521 eval { ($z,$x,$y) = unpack $pattern, "003ok \003yes\004z\000abc" };
0b568b5f 522 is($@, '');
523 is($z, 'ok');
524 is($x, 'yes');
525 is($y, 'z');
72b034c3 526 undef $x;
527 eval { $z = unpack $pattern, "003ok \003yes\004z\000abc" };
528 is($@, '');
529 is($z, 'ok');
0b568b5f 530
72b034c3 531 $pattern = <<'EOP';
17f4a12d 532 n/a* # Count as network short
533 w/A* # Count a BER integer
534EOP
fa8ec7c1 535 $expect = "\000\006string\003etc";
72b034c3 536 $z = pack $pattern,'string','etc';
9e17a6b8 537 is($z, $expect);
fa8ec7c1 538}
539
60a99fa7 540
541SKIP: {
542 skip("(EBCDIC and) version strings are bad idea", 2) if $Is_EBCDIC;
543
544 is("1.20.300.4000", sprintf "%vd", pack("U*",1,20,300,4000));
545 is("1.20.300.4000", sprintf "%vd", pack(" U*",1,20,300,4000));
546}
0b568b5f 547isnt(v1.20.300.4000, sprintf "%vd", pack("C0U*",1,20,300,4000));
fa8ec7c1 548
0b568b5f 549my $rslt = $Is_EBCDIC ? "156 67" : "199 162";
550is(join(" ", unpack("C*", chr(0x1e2))), $rslt);
fa8ec7c1 551
552# does pack U create Unicode?
0b568b5f 553is(ord(pack('U', 300)), 300);
fa8ec7c1 554
555# does unpack U deref Unicode?
0b568b5f 556is((unpack('U', chr(300)))[0], 300);
fa8ec7c1 557
558# is unpack U the reverse of pack U for Unicode string?
0b568b5f 559is("@{[unpack('U*', pack('U*', 100, 200, 300))]}", "100 200 300");
fa8ec7c1 560
561# is unpack U the reverse of pack U for byte string?
0b568b5f 562is("@{[unpack('U*', pack('U*', 100, 200))]}", "100 200");
563
564
565SKIP: {
566 skip "Not for EBCDIC", 4 if $Is_EBCDIC;
fa8ec7c1 567
7eed2ccc 568 # does unpack C unravel pack U?
0b568b5f 569 is("@{[unpack('C*', pack('U*', 100, 200))]}", "100 195 136");
fa8ec7c1 570
7eed2ccc 571 # does pack U0C create Unicode?
0b568b5f 572 is("@{[pack('U0C*', 100, 195, 136)]}", v100.v200);
fa8ec7c1 573
7eed2ccc 574 # does pack C0U create characters?
0b568b5f 575 is("@{[pack('C0U*', 100, 200)]}", pack("C*", 100, 195, 136));
fa8ec7c1 576
2dcec3fe 577 # does unpack U0U on byte data warn?
578 {
579 local $SIG{__WARN__} = sub { $@ = "@_" };
580 my @null = unpack('U0U', chr(255));
0b568b5f 581 like($@, /^Malformed UTF-8 character /);
2dcec3fe 582 }
35bcd338 583}
584
fa8ec7c1 585{
586 my $p = pack 'i*', -2147483648, ~0, 0, 1, 2147483647;
587 my (@a);
588 # bug - % had to be at the start of the pattern, no leading whitespace or
589 # comments. %i! didn't work at all.
590 foreach my $pat ('%32i*', ' %32i*', "# Muhahahaha\n%32i*", '%32i* ',
591 '%32i!*', ' %32i!*', "\n#\n#\n\r \t\f%32i!*", '%32i!*#') {
592 @a = unpack $pat, $p;
0b568b5f 593 is($a[0], 0xFFFFFFFF) || print "# $pat\n";
fa8ec7c1 594 @a = scalar unpack $pat, $p;
0b568b5f 595 is($a[0], 0xFFFFFFFF) || print "# $pat\n";
fa8ec7c1 596 }
597
598
599 $p = pack 'I*', 42, 12;
600 # Multiline patterns in scalar context failed.
601 foreach my $pat ('I', <<EOPOEMSNIPPET, 'I#I', 'I # I', 'I # !!!') {
602# On the Ning Nang Nong
603# Where the Cows go Bong!
604# And the Monkeys all say Boo!
605I
606EOPOEMSNIPPET
0b568b5f 607 @a = unpack $pat, $p;
608 is(scalar @a, 1);
609 is($a[0], 42);
610 @a = scalar unpack $pat, $p;
611 is(scalar @a, 1);
612 is($a[0], 42);
613 }
fa8ec7c1 614
615 # shorts (of all flavours) didn't calculate checksums > 32 bits with floating
616 # point, so a pathologically long pattern would wrap at 32 bits.
617 my $pat = "\xff\xff"x65538; # Start with it long, to save any copying.
618 foreach (4,3,2,1,0) {
619 my $len = 65534 + $_;
0b568b5f 620 is(unpack ("%33n$len", $pat), 65535 * $len);
fa8ec7c1 621 }
622}
b85d93de 623
624
625# pack x X @
626foreach (
0b568b5f 627 ['x', "N", "\0"],
628 ['x4', "N", "\0"x4],
629 ['xX', "N", ""],
630 ['xXa*', "Nick", "Nick"],
631 ['a5Xa5', "cameL", "llama", "camellama"],
632 ['@4', 'N', "\0"x4],
633 ['a*@8a*', 'Camel', 'Dromedary', "Camel\0\0\0Dromedary"],
634 ['a*@4a', 'Perl rules', '!', 'Perl!'],
635)
636{
b85d93de 637 my ($template, @in) = @$_;
638 my $out = pop @in;
639 my $got = eval {pack $template, @in};
0b568b5f 640 is($@, '');
641 is($out, $got) ||
642 printf "# pack ('$template', %s) gave %s expected %s\n",
643 encode_list (@in), encode_list ($got), encode_list ($out);
b85d93de 644}
645
646# unpack x X @
647foreach (
0b568b5f 648 ['x', "N"],
649 ['xX', "N"],
650 ['xXa*', "Nick", "Nick"],
651 ['a5Xa5', "camellama", "camel", "llama"],
652 ['@3', "ice"],
653 ['@2a2', "water", "te"],
654 ['a*@1a3', "steam", "steam", "tea"],
655)
656{
b85d93de 657 my ($template, $in, @out) = @$_;
658 my @got = eval {unpack $template, $in};
0b568b5f 659 is($@, '');
660 list_eq (\@got, \@out) ||
9e17a6b8 661 printf "# list unpack ('$template', %s) gave %s expected %s\n",
662 _qq($in), encode_list (@got), encode_list (@out);
b85d93de 663
664 my $got = eval {unpack $template, $in};
0b568b5f 665 is($@, '');
666 @out ? is( $got, $out[0] ) # 1 or more items; should get first
667 : ok( !defined $got ) # 0 items; should get undef
9e17a6b8 668 or printf "# scalar unpack ('$template', %s) gave %s expected %s\n",
669 _qq($in), encode_list ($got), encode_list ($out[0]);
b85d93de 670}
d50dd4e4 671
672{
d50dd4e4 673 my $t = 'Z*Z*';
674 my ($u, $v) = qw(foo xyzzy);
675 my $p = pack($t, $u, $v);
676 my @u = unpack($t, $p);
0b568b5f 677 is(scalar @u, 2);
678 is($u[0], $u);
679 is($u[1], $v);
d50dd4e4 680}
bf80f5a2 681
682{
0b568b5f 683 is((unpack("w/a*", "\x02abc"))[0], "ab");
bf80f5a2 684
0b568b5f 685 # "w/a*" should be seen as one unit
bf80f5a2 686
0b568b5f 687 is(scalar unpack("w/a*", "\x02abc"), "ab");
bf80f5a2 688}
b81060d6 689
690{
b81060d6 691 # from Wolfgang Laun: fix in change #13163
692
693 my $s = 'ABC' x 10;
644d52eb 694 my $t = '*';
695 my $x = ord($t);
b81060d6 696 my $buf = pack( 'Z*/A* C', $s, $x );
697 my $y;
698
699 my $h = $buf;
700 $h =~ s/[^[:print:]]/./g;
701 ( $s, $y ) = unpack( "Z*/A* C", $buf );
644d52eb 702 is($h, "30.ABCABCABCABCABCABCABCABCABCABC$t");
0b568b5f 703 is(length $buf, 34);
704 is($s, "ABCABCABCABCABCABCABCABCABCABC");
644d52eb 705 is($y, $x);
b81060d6 706}
3bf38418 707
708{
b223308b 709 # from Wolfgang Laun: fix in change #13288
3bf38418 710
b223308b 711 eval { my $t=unpack("P*", "abc") };
0b568b5f 712 like($@, qr/P must have an explicit size/);
3bf38418 713}
18529408 714
715{ # Grouping constructs
716 my (@a, @b);
717 @a = unpack '(SL)', pack 'SLSLSL', 67..90;
718 is("@a", "67 68");
719 @a = unpack '(SL)3', pack 'SLSLSL', 67..90;
720 @b = (67..72);
721 is("@a", "@b");
722 @a = unpack '(SL)3', pack 'SLSLSLSL', 67..90;
723 is("@a", "@b");
724 @a = unpack '(SL)[3]', pack 'SLSLSLSL', 67..90;
725 is("@a", "@b");
726 @a = unpack '(SL)[2] SL', pack 'SLSLSLSL', 67..90;
727 is("@a", "@b");
728 @a = unpack 'A/(SL)', pack 'ASLSLSLSL', 3, 67..90;
729 is("@a", "@b");
730 @a = unpack 'A/(SL)SL', pack 'ASLSLSLSL', 2, 67..90;
731 is("@a", "@b");
732 @a = unpack '(SL)*', pack 'SLSLSLSL', 67..90;
733 @b = (67..74);
734 is("@a", "@b");
735 @a = unpack '(SL)*SL', pack 'SLSLSLSL', 67..90;
736 is("@a", "@b");
737 eval { @a = unpack '(*SL)', '' };
738 like($@, qr/\(\)-group starts with a count/);
739 eval { @a = unpack '(3SL)', '' };
740 like($@, qr/\(\)-group starts with a count/);
741 eval { @a = unpack '([3]SL)', '' };
742 like($@, qr/\(\)-group starts with a count/);
743 eval { @a = pack '(*SL)' };
744 like($@, qr/\(\)-group starts with a count/);
745 @a = unpack '(SL)3 SL', pack '(SL)4', 67..74;
746 is("@a", "@b");
747 @a = unpack '(SL)3 SL', pack '(SL)[4]', 67..74;
748 is("@a", "@b");
749 @a = unpack '(SL)3 SL', pack '(SL)*', 67..74;
750 is("@a", "@b");
751}
206947d2 752
753{ # Repeat count [SUBEXPR]
92d41999 754 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
755 s! S! i! I! l! L! j J);
756 my $G;
206947d2 757 if (eval { pack 'q', 1 } ) {
758 push @codes, qw(q Q);
759 } else {
760 push @codes, qw(c C); # Keep the count the same
761 }
92d41999 762 if (eval { pack 'D', 1 } ) {
763 push @codes, 'D';
764 } else {
765 push @codes, 'd'; # Keep the count the same
766 }
206947d2 767
768 my %val;
769 @val{@codes} = map { / [Xx] (?{ undef })
770 | [AZa] (?{ 'something' })
771 | C (?{ 214 })
772 | c (?{ 114 })
773 | [Bb] (?{ '101' })
774 | [Hh] (?{ 'b8' })
92d41999 775 | [svnSiIlVNLqQjJ] (?{ 10111 })
206947d2 776 | [FfDd] (?{ 1.36514538e67 })
777 | [pP] (?{ "try this buffer" })
778 /x; $^R } @codes;
779 my @end = (0x12345678, 0x23456781, 0x35465768, 0x15263748);
780 my $end = "N4";
781
782 for my $type (@codes) {
783 my @list = $val{$type};
784 @list = () unless defined $list[0];
785 for my $count ('', '3', '[11]') {
786 my $c = 1;
787 $c = $1 if $count =~ /(\d+)/;
788 my @list1 = @list;
789 @list1 = (@list1) x $c unless $type =~ /[XxAaZBbHhP]/;
790 for my $groupend ('', ')2', ')[8]') {
791 my $groupbegin = ($groupend ? '(' : '');
792 $c = 1;
793 $c = $1 if $groupend =~ /(\d+)/;
794 my @list2 = (@list1) x $c;
795
796 my $junk1 = "$groupbegin $type$count $groupend";
797 # print "# junk1=$junk1\n";
798 my $p = pack $junk1, @list2;
799 my $half = int( (length $p)/2 );
62f95557 800 for my $move ('', "X$half", "X!$half", 'x1', 'x!8', "x$half") {
206947d2 801 my $junk = "$junk1 $move";
62f95557 802 # print "# junk='$junk', list=(@list2)\n";
206947d2 803 $p = pack "$junk $end", @list2, @end;
804 my @l = unpack "x[$junk] $end", $p;
805 is(scalar @l, scalar @end);
806 is("@l", "@end", "skipping x[$junk]");
807 }
808 }
809 }
810 }
811}
812
813# / is recognized after spaces in scalar context
814# XXXX no spaces are allowed in pack... In pack only before the slash...
815is(scalar unpack('A /A Z20', pack 'A/A* Z20', 'bcde', 'xxxxx'), 'bcde');
816is(scalar unpack('A /A /A Z20', '3004bcde'), 'bcde');
62f95557 817
818{ # X! and x!
819 my $t = 'C[3] x!8 C[2]';
820 my @a = (0x73..0x77);
821 my $p = pack($t, @a);
822 is($p, "\x73\x74\x75\0\0\0\0\0\x76\x77");
823 my @b = unpack $t, $p;
824 is(scalar @b, scalar @a);
825 is("@b", "@a", 'x!8');
826 $t = 'x[5] C[6] X!8 C[2]';
827 @a = (0x73..0x7a);
828 $p = pack($t, @a);
829 is($p, "\0\0\0\0\0\x73\x74\x75\x79\x7a");
830 @b = unpack $t, $p;
831 @a = (0x73..0x75, 0x79, 0x7a, 0x79, 0x7a);
832 is(scalar @b, scalar @a);
833 is("@b", "@a");
834}
835
836{ # struct {char c1; double d; char cc[2];}
837 my $t = 'C x![d] d C[2]';
838 my @a = (173, 1.283476517e-45, 42, 215);
839 my $p = pack $t, @a;
840 ok( length $p);
841 my @b = unpack "$t X[$t] $t", $p; # Extract, step back, extract again
842 is(scalar @b, 2 * scalar @a);
843 is("@b", "@a @a");
844
845 my $warning;
846 local $SIG{__WARN__} = sub {
847 $warning = $_[0];
848 };
849 @b = unpack "x[C] x[$t] X[$t] X[C] $t", "$p\0";
850
851 is($warning, undef);
852 is(scalar @b, scalar @a);
853 is("@b", "@a");
854}
92d41999 855
856is(length(pack("j", 0)), $Config{ivsize});
857is(length(pack("J", 0)), $Config{uvsize});
858is(length(pack("F", 0)), $Config{nvsize});
859
860numbers ('j', -2147483648, -1, 0, 1, 2147483647);
861numbers ('J', 0, 1, 2147483647, 2147483648, 4294967295);
862numbers ('F', -(2**34), -1, 0, 1, 2**34);
863SKIP: {
864 my $t = eval { unpack("D*", pack("D", 12.34)) };
865
866 skip "Long doubles not in use", 56 if $@ =~ /Invalid type in pack/;
867
868 is(length(pack("D", 0)), $Config{longdblsize});
869 numbers ('D', -(2**34), -1, 0, 1, 2**34);
870}
871