15 my $Is_EBCDIC = (defined $Config{ebcdic} && $Config{ebcdic} eq 'define');
16 my $Perl = which_perl();
21 s/([[:cntrl:]\177 ])/sprintf "\\%03o", ord $1/ge if defined;
30 s/([[:cntrl:]\177])/sprintf "\\%03o", ord $1/ge;
39 return '(' . join (', ', @result) . ')';
45 return unless @$l == @$r;
47 if (defined $l->[$i]) {
48 return unless defined ($r->[$i]) && $l->[$i] eq $r->[$i];
50 return if defined $r->[$i]
56 ##############################################################################
58 # Here starteth the tests
62 my $format = "c2 x5 C C x s d i l a6";
63 # Need the expression in here to force ary[5] to be numeric. This avoids
64 # test2 failing because ary2 goes str->numeric->str and ary doesn't.
65 my @ary = (1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,
67 my $foo = pack($format,@ary);
68 my @ary2 = unpack($format,$foo);
72 my $out1=join(':',@ary);
73 my $out2=join(':',@ary2);
74 # Using long double NVs may introduce greater accuracy than wanted.
75 $out1 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
76 $out2 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
81 # How about counting bits?
85 is( ($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")), 16 );
87 is( ($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")), 12 );
89 is( ($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")), 9 );
93 my $sum = 129; # ASCII
94 $sum = 103 if $Is_EBCDIC;
97 is( ($x = unpack("%32B*", "Now is the time for all good blurfl")), $sum );
100 open(BIN, $Perl) || die "Can't open $Perl: $!\n";
101 sysread BIN, $foo, 8192;
104 $sum = unpack("%32b*", $foo);
105 my $longway = unpack("b*", $foo);
106 is( $sum, $longway =~ tr/1/1/ );
111 is( ($x = unpack("I",pack("I", 0xFFFFFFFF))), 0xFFFFFFFF );
116 my @x = (5,130,256,560,32000,3097152,268435455,1073741844, 2**33,
117 '4503599627365785','23728385234614992549757750638446');
118 my $x = pack('w*', @x);
119 my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f8480808014A0808'.
120 '0800087ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e';
124 my @y = unpack('w*', $y);
126 while ($a = pop @x) {
131 @y = unpack('w2', $x);
141 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
142 like($@, qr/^Unterminated compressed integer/);
144 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
145 like($@, qr/^Unterminated compressed integer/);
147 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
148 like($@, qr/^Unterminated compressed integer/);
152 # test the "p" template
155 is(unpack("p",pack("p","foo")), "foo");
158 is(unpack("p",pack("p",239)), 239);
161 sub foo { my $a = "a"; return $a . $a++ . $a++ }
165 local $SIG{__WARN__} = sub {
168 my $junk = pack("p", &foo);
170 like($warning, qr/temporary val/);
173 # undef should give null pointer
174 like(pack("p", undef), qr/^\0+/);
176 # Check for optimizer bug (e.g. Digital Unix GEM cc with -O4 on DU V4.0B gives
177 # 4294967295 instead of -1)
178 # see #ifdef __osf__ in pp.c pp_unpack
179 is((unpack("i",pack("i",-1))), -1);
181 # test the pack lengths of s S i I l L
182 # test the pack lengths of n N v V
183 my @lengths = qw(s 2 S 2 i -4 I -4 l 4 L 4 n 2 N 4 v 2 V 4);
184 while (my ($format, $expect) = splice @lengths, 0, 2) {
185 my $len = length(pack($format, 0));
187 is($expect, $len, "format '$format'");
190 ok ($len >= $expect, "format '$format'") ||
191 print "# format '$format' has length $len, expected >= $expect\n";
196 # test unpack-pack lengths
197 my @templates = qw(c C i I s S l L n N v V f d q Q);
199 foreach my $t (@templates) {
201 my @t = eval { unpack("$t*", pack("$t*", 12, 34)) };
203 # quads not supported everywhere
204 skip "Quads not supported", 4 if $@ =~ /Invalid type in pack/;
210 skip "$t not expected to work for some reason", 2 if $t =~ /[nv]/i;
221 # Note that first uuencoding known 'text' data and then checking the
222 # binary values of the uuencoded version would not be portable between
223 # character sets. Uuencoding is meant for encoding binary data, not
226 my $in = pack 'C*', 0 .. 255;
228 # just to be anal, we do some random tr/`/ /
230 M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL
231 M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9
232 M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6&
233 MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S
234 MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@
235 ?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P `
241 is(pack('u', $in), $_);
243 is(unpack('u', $uu), $in);
245 $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";
247 M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
251 is(unpack('u', $uu), $in);
253 # This is identical to the above except that backquotes have been
257 M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
262 is(unpack('u', $uu), $in);
266 # test the ascii template types (A, a, Z)
269 ['p', 'A*', "foo\0bar\0 ", "foo\0bar\0 "],
270 ['p', 'A11', "foo\0bar\0 ", "foo\0bar\0 "],
271 ['u', 'A*', "foo\0bar \0", "foo\0bar"],
272 ['u', 'A8', "foo\0bar \0", "foo\0bar"],
273 ['p', 'a*', "foo\0bar\0 ", "foo\0bar\0 "],
274 ['p', 'a11', "foo\0bar\0 ", "foo\0bar\0 \0\0"],
275 ['u', 'a*', "foo\0bar \0", "foo\0bar \0"],
276 ['u', 'a8', "foo\0bar \0", "foo\0bar "],
277 ['p', 'Z*', "foo\0bar\0 ", "foo\0bar\0 \0"],
278 ['p', 'Z11', "foo\0bar\0 ", "foo\0bar\0 \0\0"],
279 ['p', 'Z3', "foo", "fo\0"],
280 ['u', 'Z*', "foo\0bar \0", "foo"],
281 ['u', 'Z8', "foo\0bar \0", "foo"],
284 my ($what, $template, $in, $out) = @$_;
285 my $got = $what eq 'u' ? (unpack $template, $in) : (pack $template, $in);
286 unless (is($got, $out)) {
287 ($in, $out, $got) = encode ($in, $out, $got);
288 my $un = $what eq 'u' ? 'un' : '';
289 print "# ${un}pack ('$template', \"$in\") gave $out not $got\n";
293 # packing native shorts/ints/longs
295 is(length(pack("s!", 0)), $Config{shortsize});
296 is(length(pack("i!", 0)), $Config{intsize});
297 is(length(pack("l!", 0)), $Config{longsize});
298 ok(length(pack("s!", 0)) <= length(pack("i!", 0)));
299 ok(length(pack("i!", 0)) <= length(pack("l!", 0)));
300 is(length(pack("i!", 0)), length(pack("i", 0)));
304 return numbers_with_total ($format, undef, @_);
307 sub numbers_with_total {
310 if (!defined $total) {
317 my $out = eval {unpack($format, pack($format, $_))};
318 skip "cannot pack '$format' on this perl", 2 if
319 $@ =~ /Invalid type in pack: '$format'/;
326 my $skip_if_longer_than = ~0; # "Infinity"
328 # If we're running with -DNO_PERLPRESERVE_IVUV and NVs don't preserve all
329 # UVs (in which case ~0 is NV, ~0-1 will be the same NV) then we can't
330 # correctly in perl calculate UV totals for long checksums, as pp_unpack
331 # is using UV maths, and we've only got NVs.
332 $skip_if_longer_than = $Config{d_nv_preserves_uv_bits};
335 foreach ('', 1, 2, 3, 15, 16, 17, 31, 32, 33, 53, 54, 63, 64, 65) {
337 my $sum = eval {unpack "%$_$format*", pack "$format*", @_};
338 skip "cannot pack '$format' on this perl", 3
339 if $@ =~ /Invalid type in pack: '$format'/;
344 my $len = $_; # Copy, so that we can reassign ''
345 $len = 16 unless length $len;
348 skip "cannot test checksums over $skip_if_longer_than bits", 1
349 if $len > $skip_if_longer_than;
351 # Our problem with testing this portably is that the checksum code in
352 # pp_unpack is able to cast signed to unsigned, and do modulo 2**n
353 # arithmetic in unsigned ints, which perl has no operators to do.
354 # (use integer; does signed ints, which won't wrap on UTS, which is just
355 # fine with ANSI, but not with most people's assumptions.
356 # This is why we need to supply the totals for 'Q' as there's no way in
357 # perl to calculate them, short of unpack '%0Q' (is that documented?)
358 # ** returns NVs; make sure it's IV.
359 my $max = 1 + 2 * (int (2 ** ($len-1))-1); # The max possible checksum
360 my $max_p1 = $max + 1;
361 my ($max_is_integer, $max_p1_is_integer);
362 $max_p1_is_integer = 1 unless $max_p1 + 1 == $max_p1;
363 $max_is_integer = 1 if $max - 1 < ~0;
367 $calc_sum = &$total($len);
370 # Shift into range by some multiple of the total
371 my $mult = int ($total / $max_p1);
372 # Need this to make sure that -1 + (~0+1) is ~0 (ie still integer)
373 $calc_sum = $total - $mult;
374 $calc_sum -= $mult * $max;
380 if ($calc_sum == $calc_sum - 1 && $calc_sum == $max_p1) {
381 # we're into floating point (either by getting out of the range of
382 # UV arithmetic, or because we're doing a floating point checksum)
383 # and our calculation of the checksum has become rounded up to
388 if (is( $calc_sum, $sum)) {
389 print "# unpack '%$_$format' gave $sum\n";
391 my $delta = 1.000001;
392 if ($format =~ tr /dDfF//
393 && ($calc_sum <= $sum * $delta && $calc_sum >= $sum / $delta)) {
395 print "# unpack '%$_$format' gave $sum, expected $calc_sum\n";
397 my $text = ref $total ? &$total($len) : $total;
399 print "# For list (" . join (", ", @_) . ") (total $text)"
400 . " packed with $format unpack '%$_$format' gave $sum,"
401 . " expected $calc_sum\n";
409 numbers ('c', -128, -1, 0, 1, 127);
410 numbers ('C', 0, 1, 127, 128, 255);
411 numbers ('s', -32768, -1, 0, 1, 32767);
412 numbers ('S', 0, 1, 32767, 32768, 65535);
413 numbers ('i', -2147483648, -1, 0, 1, 2147483647);
414 numbers ('I', 0, 1, 2147483647, 2147483648, 4294967295);
415 numbers ('l', -2147483648, -1, 0, 1, 2147483647);
416 numbers ('L', 0, 1, 2147483647, 2147483648, 4294967295);
417 numbers ('s!', -32768, -1, 0, 1, 32767);
418 numbers ('S!', 0, 1, 32767, 32768, 65535);
419 numbers ('i!', -2147483648, -1, 0, 1, 2147483647);
420 numbers ('I!', 0, 1, 2147483647, 2147483648, 4294967295);
421 numbers ('l!', -2147483648, -1, 0, 1, 2147483647);
422 numbers ('L!', 0, 1, 2147483647, 2147483648, 4294967295);
423 numbers ('n', 0, 1, 32767, 32768, 65535);
424 numbers ('v', 0, 1, 32767, 32768, 65535);
425 numbers ('N', 0, 1, 2147483647, 2147483648, 4294967295);
426 numbers ('V', 0, 1, 2147483647, 2147483648, 4294967295);
427 # All these should have exact binary representations:
428 numbers ('f', -1, 0, 0.5, 42, 2**34);
429 numbers ('d', -(2**34), -1, 0, 1, 2**34);
430 ## These don't, but 'd' is NV. XXX wrong, it's double
431 #numbers ('d', -1, 0, 1, 1-exp(-1), -exp(1));
433 numbers_with_total ('q', -1,
434 -9223372036854775808, -1, 0, 1,9223372036854775807);
435 # This total is icky, but the true total is 2**65-1, and need a way to generate
436 # the epxected checksum on any system including those where NVs can preserve
437 # 65 bits. (long double is 128 bits on sparc, so they certainly can)
438 # or where rounding is down not up on binary conversion (crays)
439 numbers_with_total ('Q', sub {
441 $len = 65 if $len > 65; # unmasked total is 2**65-1 here
442 my $total = 1 + 2 * (int (2**($len - 1)) - 1);
443 return 0 if $total == $total - 1; # Overflowed integers
444 return $total; # NVs still accurate to nearest integer
446 0, 1,9223372036854775807, 9223372036854775808,
447 18446744073709551615);
449 # pack nvNV byteorders
451 is(pack("n", 0xdead), "\xde\xad");
452 is(pack("v", 0xdead), "\xad\xde");
453 is(pack("N", 0xdeadbeef), "\xde\xad\xbe\xef");
454 is(pack("V", 0xdeadbeef), "\xef\xbe\xad\xde");
460 eval { ($x) = unpack '/a*','hello' };
461 like($@, qr!/ must follow a numeric type!);
463 eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
469 eval { ($x) = pack '/a*','hello' };
470 like($@, qr!Invalid type in pack: '/'!);
472 $z = pack 'n/a* N/Z* w/A*','string','hi there ','etc';
473 my $expect = "\000\006string\0\0\0\012hi there \000\003etc";
476 $expect = 'hello world';
477 eval { ($x) = unpack ("w/a", chr (11) . "hello world!")};
481 # Doing this in scalar context used to fail.
482 eval { $x = unpack ("w/a", chr (11) . "hello world!")};
487 ['a/a*/a*', '212ab345678901234567','ab3456789012'],
488 ['a/a*/a*', '3012ab345678901234567', 'ab3456789012'],
489 ['a/a*/b*', '212ab', $Is_EBCDIC ? '100000010100' : '100001100100'],
492 my ($pat, $in, $expect) = @$_;
493 eval { ($x) = unpack $pat, $in };
496 printf "# list unpack ('$pat', '$in') gave %s, expected '$expect'\n",
499 eval { $x = unpack $pat, $in };
502 printf "# scalar unpack ('$pat', '$in') gave %s, expected '$expect'\n",
508 eval { ($z,$x,$y) = unpack <<EOU, "003ok \003yes\004z\000abc" };
509 a3/A # Count in ASCII
510 C/a* # Count in a C char
511 C/Z # Count in a C char but skip after \0
519 $z = pack <<EOP,'string','etc';
520 n/a* # Count as network short
521 w/A* # Count a BER integer
523 $expect = "\000\006string\003etc";
526 printf "# got '%s', expected '$expect'\n", encode $z;
529 is("1.20.300.4000", sprintf "%vd", pack("U*",1,20,300,4000));
530 is("1.20.300.4000", sprintf "%vd", pack(" U*",1,20,300,4000));
531 isnt(v1.20.300.4000, sprintf "%vd", pack("C0U*",1,20,300,4000));
533 my $rslt = $Is_EBCDIC ? "156 67" : "199 162";
534 is(join(" ", unpack("C*", chr(0x1e2))), $rslt);
536 # does pack U create Unicode?
537 is(ord(pack('U', 300)), 300);
539 # does unpack U deref Unicode?
540 is((unpack('U', chr(300)))[0], 300);
542 # is unpack U the reverse of pack U for Unicode string?
543 is("@{[unpack('U*', pack('U*', 100, 200, 300))]}", "100 200 300");
545 # is unpack U the reverse of pack U for byte string?
546 is("@{[unpack('U*', pack('U*', 100, 200))]}", "100 200");
550 skip "Not for EBCDIC", 4 if $Is_EBCDIC;
552 # does unpack C unravel pack U?
553 is("@{[unpack('C*', pack('U*', 100, 200))]}", "100 195 136");
555 # does pack U0C create Unicode?
556 is("@{[pack('U0C*', 100, 195, 136)]}", v100.v200);
558 # does pack C0U create characters?
559 is("@{[pack('C0U*', 100, 200)]}", pack("C*", 100, 195, 136));
561 # does unpack U0U on byte data warn?
563 local $SIG{__WARN__} = sub { $@ = "@_" };
564 my @null = unpack('U0U', chr(255));
565 like($@, /^Malformed UTF-8 character /);
570 my $p = pack 'i*', -2147483648, ~0, 0, 1, 2147483647;
572 # bug - % had to be at the start of the pattern, no leading whitespace or
573 # comments. %i! didn't work at all.
574 foreach my $pat ('%32i*', ' %32i*', "# Muhahahaha\n%32i*", '%32i* ',
575 '%32i!*', ' %32i!*', "\n#\n#\n\r \t\f%32i!*", '%32i!*#') {
576 @a = unpack $pat, $p;
577 is($a[0], 0xFFFFFFFF) || print "# $pat\n";
578 @a = scalar unpack $pat, $p;
579 is($a[0], 0xFFFFFFFF) || print "# $pat\n";
583 $p = pack 'I*', 42, 12;
584 # Multiline patterns in scalar context failed.
585 foreach my $pat ('I', <<EOPOEMSNIPPET, 'I#I', 'I # I', 'I # !!!') {
586 # On the Ning Nang Nong
587 # Where the Cows go Bong!
588 # And the Monkeys all say Boo!
591 @a = unpack $pat, $p;
594 @a = scalar unpack $pat, $p;
599 # shorts (of all flavours) didn't calculate checksums > 32 bits with floating
600 # point, so a pathologically long pattern would wrap at 32 bits.
601 my $pat = "\xff\xff"x65538; # Start with it long, to save any copying.
602 foreach (4,3,2,1,0) {
603 my $len = 65534 + $_;
604 is(unpack ("%33n$len", $pat), 65535 * $len);
614 ['xXa*', "Nick", "Nick"],
615 ['a5Xa5', "cameL", "llama", "camellama"],
617 ['a*@8a*', 'Camel', 'Dromedary', "Camel\0\0\0Dromedary"],
618 ['a*@4a', 'Perl rules', '!', 'Perl!'],
621 my ($template, @in) = @$_;
623 my $got = eval {pack $template, @in};
626 printf "# pack ('$template', %s) gave %s expected %s\n",
627 encode_list (@in), encode_list ($got), encode_list ($out);
634 ['xXa*', "Nick", "Nick"],
635 ['a5Xa5', "camellama", "camel", "llama"],
637 ['@2a2', "water", "te"],
638 ['a*@1a3', "steam", "steam", "tea"],
641 my ($template, $in, @out) = @$_;
642 my @got = eval {unpack $template, $in};
644 list_eq (\@got, \@out) ||
645 printf "# list unpack ('$template', \"%s\") gave %s expected %s\n",
646 encode ($in), encode_list (@got), encode_list (@out);
648 my $got = eval {unpack $template, $in};
650 @out ? is( $got, $out[0] ) # 1 or more items; should get first
651 : ok( !defined $got ) # 0 items; should get undef
652 or printf "# scalar unpack ('$template', \"%s\") gave %s expected %s\n",
653 encode ($in), encode_list ($got), encode_list ($out[0]);
658 my ($u, $v) = qw(foo xyzzy);
659 my $p = pack($t, $u, $v);
660 my @u = unpack($t, $p);
667 is((unpack("w/a*", "\x02abc"))[0], "ab");
669 # "w/a*" should be seen as one unit
671 is(scalar unpack("w/a*", "\x02abc"), "ab");
675 # from Wolfgang Laun: fix in change #13163
679 my $buf = pack( 'Z*/A* C', $s, $x );
683 $h =~ s/[^[:print:]]/./g;
684 ( $s, $y ) = unpack( "Z*/A* C", $buf );
685 is($h, "30.ABCABCABCABCABCABCABCABCABCABC*");
687 is($s, "ABCABCABCABCABCABCABCABCABCABC");
692 # from Wolfgang Laun: fix in change #13288
694 eval { my $t=unpack("P*", "abc") };
695 like($@, qr/P must have an explicit size/);