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