unpack("Z*Z*", pack("Z*Z*", ..)) bug, patch and test from
[p5sagit/p5-mst-13.2.git] / t / op / pack.t
CommitLineData
d50dd4e4 1#!./perl -w
a687059c 2
d50dd4e4 3print "1..611\n";
fa8ec7c1 4
a1a0e61e 5BEGIN {
6 chdir 't' if -d 't';
20822f61 7 @INC = '../lib';
b23b8711 8}
9
fa8ec7c1 10use strict;
11use warnings;
b23b8711 12use Config;
13
fa8ec7c1 14my $Is_EBCDIC = (defined $Config{ebcdic} && $Config{ebcdic} eq 'define');
b23b8711 15
16my $test = 1;
fa8ec7c1 17# Using Test considered bad plan in op/*.t ?
c274e827 18
fa8ec7c1 19sub encode {
20 my @result = @_;
b85d93de 21 foreach (@result) {
22 s/([[:cntrl:]\177 ])/sprintf "\\%03o", ord $1/ge if defined;
23 }
fa8ec7c1 24 @result;
25}
17639bde 26
b85d93de 27sub encode_list {
28 my @result = @_;
29 foreach (@result) {
30 if (defined) {
31 s/([[:cntrl:]\177])/sprintf "\\%03o", ord $1/ge;
32 $_ = qq("$_");
33 } else {
34 $_ = 'undef';
35 }
36 }
37 if (@result == 1) {
38 return @result;
39 }
40 return '(' . join (', ', @result) . ')';
41}
42
fa8ec7c1 43sub ok {
44 my ($pass, $wrong, $err) = @_;
45 if ($pass) {
46 print "ok $test\n";
b23b8711 47 $test++;
fa8ec7c1 48 return 1;
49 } else {
50 if ($err) {
51 chomp $err;
52 print "not ok $test # \$\@ = $err\n";
53 } else {
54 if (defined $wrong) {
55 $wrong = ", got $wrong";
56 } else {
57 $wrong = '';
58 }
59 printf "not ok $test # line %d$wrong\n", (caller)[2];
60 }
61 }
62 $test++;
63 return;
a1a0e61e 64}
65
b85d93de 66sub list_eq ($$) {
67 my ($l, $r) = @_;
68 return unless @$l == @$r;
69 for my $i (0..$#$l) {
70 if (defined $l->[$i]) {
71 return unless defined ($r->[$i]) && $l->[$i] eq $r->[$i];
72 } else {
73 return if defined $r->[$i]
74 }
75 }
76 return 1;
77}
78
79##############################################################################
80#
81# Here starteth the tests
82#
83
fa8ec7c1 84{
85my $format = "c2 x5 C C x s d i l a6";
450a55e4 86# Need the expression in here to force ary[5] to be numeric. This avoids
87# test2 failing because ary2 goes str->numeric->str and ary doesn't.
fa8ec7c1 88my @ary = (1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,"abcdef");
89my $foo = pack($format,@ary);
90my @ary2 = unpack($format,$foo);
a687059c 91
b23b8711 92ok($#ary == $#ary2);
a687059c 93
fa8ec7c1 94my $out1=join(':',@ary);
95my $out2=join(':',@ary2);
ae165b0c 96# Using long double NVs may introduce greater accuracy than wanted.
658bbd66 97$out1 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
c4ab3e64 98$out2 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
b23b8711 99ok($out1 eq $out2);
a687059c 100
b23b8711 101ok($foo =~ /def/);
fa8ec7c1 102}
79072805 103# How about counting bits?
104
fa8ec7c1 105{
106my $x;
b23b8711 107ok( ($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")) == 16 );
79072805 108
b23b8711 109ok( ($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")) == 12 );
79072805 110
b23b8711 111ok( ($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")) == 9 );
fa8ec7c1 112}
79072805 113
fa8ec7c1 114{
9d116dd7 115my $sum = 129; # ASCII
b23b8711 116$sum = 103 if $Is_EBCDIC;
9d116dd7 117
fa8ec7c1 118my $x;
b23b8711 119ok( ($x = unpack("%32B*", "Now is the time for all good blurfl")) == $sum );
79072805 120
fa8ec7c1 121my $foo;
95e8664e 122open(BIN, "./perl") || open(BIN, "./perl.exe") || open(BIN, $^X)
b8440792 123 || die "Can't open ../perl or ../perl.exe: $!\n";
79072805 124sysread BIN, $foo, 8192;
125close BIN;
126
127$sum = unpack("%32b*", $foo);
fa8ec7c1 128my $longway = unpack("b*", $foo);
b23b8711 129ok( $sum == $longway =~ tr/1/1/ );
fa8ec7c1 130}
73a1c01a 131
fa8ec7c1 132{
133 my $x;
134 ok( ($x = unpack("I",pack("I", 0xFFFFFFFF))) == 0xFFFFFFFF );
135}
def98dd4 136
fa8ec7c1 137{
def98dd4 138# check 'w'
2e821511 139my @x = (5,130,256,560,32000,3097152,268435455,1073741844, 2**33,
55497cff 140 '4503599627365785','23728385234614992549757750638446');
def98dd4 141my $x = pack('w*', @x);
2e821511 142my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f8480808014A08080800087ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e';
def98dd4 143
fa8ec7c1 144ok ($x eq $y, unpack 'H*', $x);
145my @y = unpack('w*', $y);
55497cff 146my $a;
147while ($a = pop @x) {
148 my $b = pop @y;
fa8ec7c1 149 ok ($a eq $b, "\$a='$a' \$b='$b'");
55497cff 150}
def98dd4 151
152@y = unpack('w2', $x);
153
fa8ec7c1 154ok (scalar(@y) == 2);
155ok ($y[1] == 130, $y[1]);
156}
def98dd4 157
fa8ec7c1 158{
159 # test exeptions
160 my $x;
161 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
162 ok ($@ =~ /^Unterminated compressed integer/, undef, $@);
def98dd4 163
fa8ec7c1 164 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
165 ok ($@ =~ /^Unterminated compressed integer/, undef, $@);
def98dd4 166
fa8ec7c1 167 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
168 ok ($@ =~ /^Unterminated compressed integer/, undef, $@);
169}
def98dd4 170
84902520 171#
172# test the "p" template
173
174# literals
fa8ec7c1 175ok(unpack("p",pack("p","foo")) eq "foo");
84902520 176
177# scalars
fa8ec7c1 178ok(unpack("p",pack("p",$test)) == $test);
84902520 179
180# temps
181sub foo { my $a = "a"; return $a . $a++ . $a++ }
182{
9f1b1f2d 183 use warnings;
84902520 184 my $last = $test;
185 local $SIG{__WARN__} = sub {
186 print "ok ",$test++,"\n" if $_[0] =~ /temporary val/
187 };
188 my $junk = pack("p", &foo);
189 print "not ok ", $test++, "\n" if $last == $test;
190}
191
192# undef should give null pointer
fa8ec7c1 193ok (pack("p", undef) =~ /^\0+/);
84902520 194
20408e3c 195# Check for optimizer bug (e.g. Digital Unix GEM cc with -O4 on DU V4.0B gives
196# 4294967295 instead of -1)
197# see #ifdef __osf__ in pp.c pp_unpack
198# Test 30:
fa8ec7c1 199ok((unpack("i",pack("i",-1))) == -1, "__osf__ like bug seems to exist");
20408e3c 200
d4217c7e 201# 31..36: test the pack lengths of s S i I l L
d4217c7e 202# 37..40: test the pack lengths of n N v V
fa8ec7c1 203my @lengths = qw(s 2 S 2 i -4 I -4 l 4 L 4 n 2 N 4 v 2 V 4);
204while (my ($format, $expect) = splice @lengths, 0, 2) {
205 my $len = length(pack($format, 0));
206 if ($expect > 0) {
207 ok ($expect == $len, "format '$format' has length $len, expected $expect");
208 } else {
209 $expect = -$expect;
210 ok ($len >= $expect,
211 "format '$format' has length $len, expected >= $expect");
212 }
213}
d4217c7e 214
215# 41..56: test unpack-pack lengths
216
217my @templates = qw(c C i I s S l L n N v V f d);
218
219# quads not supported everywhere: if not, retest floats/doubles
220# to preserve the test count...
221eval { my $q = pack("q",0) };
222push @templates, $@ !~ /Invalid type in pack/ ? qw(q Q) : qw(f d);
223
224foreach my $t (@templates) {
225 my @t = unpack("$t*", pack("$t*", 12, 34));
fa8ec7c1 226 ok ((@t == 2 and (($t[0] == 12 and $t[1] == 34) or ($t =~ /[nv]/i))),
227 "unpack-pack length for '$t' failed; \@t=@t");
d4217c7e 228}
9d116dd7 229
fa8ec7c1 230{
eddc390b 231# 57..60: uuencode/decode
9d116dd7 232
ef54e1a4 233# Note that first uuencoding known 'text' data and then checking the
234# binary values of the uuencoded version would not be portable between
235# character sets. Uuencoding is meant for encoding binary data, not
236# text data.
c4d5f83a 237
fa8ec7c1 238my $in = pack 'C*', 0 .. 255;
ba1ac976 239
240# just to be anal, we do some random tr/`/ /
fa8ec7c1 241my $uu = <<'EOUU';
ba1ac976 242M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL
9d116dd7 243M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9
244M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6&
245MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S
246MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@
ba1ac976 247?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P `
9d116dd7 248EOUU
249
ba1ac976 250$_ = $uu;
251tr/ /`/;
9d116dd7 252
fa8ec7c1 253ok (pack('u', $in) eq $_);
254
255ok (unpack('u', $uu) eq $in);
9d116dd7 256
eddc390b 257$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";
258$uu = <<'EOUU';
259M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
260&8%P:````
261EOUU
262
fa8ec7c1 263ok unless unpack('u', $uu);
eddc390b 264
265# 60 identical to 59 except that backquotes have been changed to spaces
266
267$uu = <<'EOUU';
268M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
c4d5f83a 269&8%P:
eddc390b 270EOUU
271
fa8ec7c1 272# ' # Grr
273ok (unpack('u', $uu) eq $in);
ef54e1a4 274
d99ad34e 275}
726ea183 276
fa8ec7c1 277# 61..73: test the ascii template types (A, a, Z)
726ea183 278
fa8ec7c1 279foreach (
280['p', 'A*', "foo\0bar\0 ", "foo\0bar\0 "],
281['p', 'A11', "foo\0bar\0 ", "foo\0bar\0 "],
282['u', 'A*', "foo\0bar \0", "foo\0bar"],
283['u', 'A8', "foo\0bar \0", "foo\0bar"],
284['p', 'a*', "foo\0bar\0 ", "foo\0bar\0 "],
285['p', 'a11', "foo\0bar\0 ", "foo\0bar\0 \0\0"],
286['u', 'a*', "foo\0bar \0", "foo\0bar \0"],
287['u', 'a8', "foo\0bar \0", "foo\0bar "],
288['p', 'Z*', "foo\0bar\0 ", "foo\0bar\0 \0"],
289['p', 'Z11', "foo\0bar\0 ", "foo\0bar\0 \0\0"],
290['p', 'Z3', "foo", "fo\0"],
291['u', 'Z*', "foo\0bar \0", "foo"],
292['u', 'Z8', "foo\0bar \0", "foo"],
293) {
294 my ($what, $template, $in, $out) = @$_;
295 my $got = $what eq 'u' ? (unpack $template, $in) : (pack $template, $in);
296 unless (ok ($got eq $out)) {
297 ($in, $out, $got) = encode ($in, $out, $got);
298 my $un = $what eq 'u' ? 'un' : '';
299 print "# ${un}pack ('$template', \"$in\") gave $out not $got\n";
300 }
d99ad34e 301}
726ea183 302
fa8ec7c1 303# 74..79: packing native shorts/ints/longs
726ea183 304
fa8ec7c1 305ok (length(pack("s!", 0)) == $Config{shortsize});
306ok (length(pack("i!", 0)) == $Config{intsize});
307ok (length(pack("l!", 0)) == $Config{longsize});
308ok (length(pack("s!", 0)) <= length(pack("i!", 0)));
309ok (length(pack("i!", 0)) <= length(pack("l!", 0)));
310ok (length(pack("i!", 0)) == length(pack("i", 0)));
726ea183 311
fa8ec7c1 312sub numbers {
313 my $format = shift;
314 return numbers_with_total ($format, undef, @_);
d99ad34e 315}
726ea183 316
fa8ec7c1 317sub numbers_with_total {
318 my $format = shift;
319 my $total = shift;
320 if (!defined $total) {
321 foreach (@_) {
322 $total += $_;
323 }
324 }
325 foreach (@_) {
326 my $out = eval {unpack($format, pack($format, $_))};
327 if ($@ =~ /Invalid type in pack: '$format'/) {
328 print "ok $test # skip cannot pack '$format' on this perl\n";
329 } elsif ($out == $_) {
330 print "ok $test\n";
331 } else {
332 print "not ok $test # unpack '$format', pack '$format', $_ gives $out\n";
333 print "# \$\@='$@'\n" if $@;
334 }
335 $test++;
336 }
726ea183 337
fa8ec7c1 338 my $skip_if_longer_than = ~0; # "Infinity"
339 if (~0 - 1 == ~0) {
340 # If we're running with -DNO_PERLPRESERVE_IVUV and NVs don't preserve all
341 # UVs (in which case ~0 is NV, ~0-1 will be the same NV) then we can't
342 # correctly in perl calculate UV totals for long checksums, as pp_unpack
343 # is using UV maths, and we've only got NVs.
344 $skip_if_longer_than = $Config{d_nv_preserves_uv_bits};
345 }
726ea183 346
fa8ec7c1 347 foreach ('', 1, 2, 3, 15, 16, 17, 31, 32, 33, 53, 54, 63, 64, 65) {
348 my $sum = eval {unpack "%$_$format*", pack "$format*", @_};
349 if (!defined $sum) {
350 if ($@ =~ /Invalid type in pack: '$format'/) {
351 print "ok $test # skip cannot pack '$format' on this perl\n";
352 } else {
353 print "not ok $test # \$\@='$@'\n" if $@;
354 }
355 next;
356 }
357 my $len = $_; # Copy, so that we can reassign ''
358 $len = 16 unless length $len;
43192e07 359
fa8ec7c1 360 if ($len > $skip_if_longer_than) {
361 print "ok $test # skip cannot test checksums over $skip_if_longer_than "
362 ."bits for this perl (compiled with -DNO_PERLPRESERVE_IVUV)\n";
363 next;
364 }
43192e07 365
fa8ec7c1 366 # Our problem with testing this portably is that the checksum code in
367 # pp_unpack is able to cast signed to unsigned, and do modulo 2**n
368 # arithmetic in unsigned ints, which perl has no operators to do.
369 # (use integer; does signed ints, which won't wrap on UTS, which is just
370 # fine with ANSI, but not with most people's assumptions.
371 # This is why we need to supply the totals for 'Q' as there's no way in
372 # perl to calculate them, short of unpack '%0Q' (is that documented?)
373 # ** returns NVs; make sure it's IV.
374 my $max = 1 + 2 * (int (2 ** ($len-1))-1); # The maximum possible checksum
375 my $max_p1 = $max + 1;
376 my ($max_is_integer, $max_p1_is_integer);
377 $max_p1_is_integer = 1 unless $max_p1 + 1 == $max_p1;
378 $max_is_integer = 1 if $max - 1 < ~0;
379
380 my $calc_sum;
381 if ($total =~ /^0b[01]*?([01]{1,$len})/) {
382 no warnings qw(overflow portable);
383 $calc_sum = oct "0b$1";
384 } else {
385 $calc_sum = $total;
386 # Shift into range by some multiple of the total
387 my $mult = int ($total / $max_p1);
388 # Need this to make sure that -1 + (~0+1) is ~0 (ie still integer)
389 $calc_sum = $total - $mult;
390 $calc_sum -= $mult * $max;
391 if ($calc_sum < 0) {
392 $calc_sum += 1;
393 $calc_sum += $max;
394 }
395 }
396 if ($calc_sum == $calc_sum - 1 && $calc_sum == $max_p1) {
397 # we're into floating point (either by getting out of the range of
398 # UV arithmetic, or because we're doing a floating point checksum) and
399 # our calculation of the checksum has become rounded up to
400 # max_checksum + 1
401 $calc_sum = 0;
402 }
2e821511 403
fa8ec7c1 404 if ($calc_sum == $sum) {
405 print "ok $test # unpack '%$_$format' gave $sum\n";
2e821511 406 } else {
fa8ec7c1 407 my $delta = 1.000001;
408 if ($format =~ tr /dDfF//
409 && ($calc_sum <= $sum * $delta && $calc_sum >= $sum / $delta)) {
410 print "ok $test # unpack '%$_$format' gave $sum,"
411 . " expected $calc_sum\n";
412 } else {
413 print "not ok $test # For list (" . join (", ", @_) . ") (total $total)"
414 . " packed with $format unpack '%$_$format' gave $sum,"
415 . " expected $calc_sum\n";
416 }
2e821511 417 }
fa8ec7c1 418 } continue {
419 $test++;
2e821511 420 }
421}
422
fa8ec7c1 423numbers ('c', -128, -1, 0, 1, 127);
424numbers ('C', 0, 1, 127, 128, 255);
425numbers ('s', -32768, -1, 0, 1, 32767);
426numbers ('S', 0, 1, 32767, 32768, 65535);
427numbers ('i', -2147483648, -1, 0, 1, 2147483647);
428numbers ('I', 0, 1, 2147483647, 2147483648, 4294967295);
429numbers ('l', -2147483648, -1, 0, 1, 2147483647);
430numbers ('L', 0, 1, 2147483647, 2147483648, 4294967295);
431numbers ('s!', -32768, -1, 0, 1, 32767);
432numbers ('S!', 0, 1, 32767, 32768, 65535);
433numbers ('i!', -2147483648, -1, 0, 1, 2147483647);
434numbers ('I!', 0, 1, 2147483647, 2147483648, 4294967295);
435numbers ('l!', -2147483648, -1, 0, 1, 2147483647);
436numbers ('L!', 0, 1, 2147483647, 2147483648, 4294967295);
437numbers ('n', 0, 1, 32767, 32768, 65535);
438numbers ('v', 0, 1, 32767, 32768, 65535);
439numbers ('N', 0, 1, 2147483647, 2147483648, 4294967295);
440numbers ('V', 0, 1, 2147483647, 2147483648, 4294967295);
441# All these should have exact binary representations:
442numbers ('f', -1, 0, 0.5, 42, 2**34);
b85d93de 443numbers ('d', -(2**34), -1, 0, 1, 2**34);
444## These don't, but 'd' is NV. XXX wrong, it's double
445#numbers ('d', -1, 0, 1, 1-exp(-1), -exp(1));
fa8ec7c1 446
447numbers_with_total ('q', -1,
448 -9223372036854775808, -1, 0, 1,9223372036854775807);
449# This total is icky, but need a way to express 2**65-1 that is going to
450# work independant of whether NVs can preserve 65 bits.
451# (long double is 128 bits on sparc, so they certianly can)
452numbers_with_total ('Q', "0b" . "1" x 65,
453 0, 1,9223372036854775807, 9223372036854775808,
454 18446744073709551615);
455
456# pack nvNV byteorders
457
458ok (pack("n", 0xdead) eq "\xde\xad");
459ok (pack("v", 0xdead) eq "\xad\xde");
460ok (pack("N", 0xdeadbeef) eq "\xde\xad\xbe\xef");
461ok (pack("V", 0xdeadbeef) eq "\xef\xbe\xad\xde");
43192e07 462
fa8ec7c1 463{
464 # /
465
466 my ($x, $y, $z);
467 eval { ($x) = unpack '/a*','hello' };
468 ok ($@ =~ m!/ must follow a numeric type!, undef, $@);
469 eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
470 ok ($z eq 'ok');
471 ok ($x eq 'yes');
472 ok ($y eq 'z');
473 ok ($@ eq '', undef, $@);
474
475 eval { ($x) = pack '/a*','hello' };
476 ok ($@ =~ m!Invalid type in pack: '/'!, undef, $@);
477
478 $z = pack 'n/a* N/Z* w/A*','string','hi there ','etc';
479 my $expect = "\000\006string\0\0\0\012hi there \000\003etc";
480 unless (ok ($z eq $expect)) {
481 printf "# got '%s'\n", encode $z;
482 }
4b5b2118 483
b85d93de 484 $expect = 'hello world';
485 eval { ($x) = unpack ("w/a", chr (11) . "hello world!")};
486 ok ($x eq $expect);
487 ok ($@ eq '', undef, $@);
488 # Doing this in scalar context used to fail.
489 eval { $x = unpack ("w/a", chr (11) . "hello world!")};
490 unless (ok ($x eq $expect, undef, $@)) {
491 printf "# expected '$expect' got '%s'\n", encode $x;
492 }
493 ok ($@ eq '', undef, $@);
494
fa8ec7c1 495 foreach (
496['a/a*/a*', '212ab345678901234567','ab3456789012'],
497['a/a*/a*', '3012ab345678901234567', 'ab3456789012'],
498['a/a*/b*', '212ab', $Is_EBCDIC ? '100000010100' : '100001100100'],
499) {
500 my ($pat, $in, $expect) = @$_;
501 eval { ($x) = unpack $pat, $in };
b85d93de 502 ok ($@ eq '' && $x eq $expect, undef, $@)
503 or printf "# list unpack ('$pat', '$in') gave %s, expected '$expect'\n",
504 encode_list ($x);
505 eval { $x = unpack $pat, $in };
506 ok ($@ eq '' && $x eq $expect, undef, $@)
507 or printf "# scalar unpack ('$pat', '$in') gave %s, expected '$expect'\n",
508 encode_list ($x);
fa8ec7c1 509 }
4b5b2118 510
fa8ec7c1 511# / with #
17f4a12d 512
513eval { ($z,$x,$y) = unpack <<EOU, "003ok \003yes\004z\000abc" };
514 a3/A # Count in ASCII
515 C/a* # Count in a C char
516 C/Z # Count in a C char but skip after \0
517EOU
fa8ec7c1 518 ok ($z eq 'ok');
519 ok ($x eq 'yes');
520 ok ($y eq 'z');
521 ok ($@ eq '', undef, $@);
17f4a12d 522
523$z = pack <<EOP,'string','etc';
524 n/a* # Count as network short
525 w/A* # Count a BER integer
526EOP
fa8ec7c1 527 $expect = "\000\006string\003etc";
528unless (ok ($z eq $expect)) {
529 $z = encode $z;
530 print "# got '$z', expected '$expect'\n";
531}
532}
533
534ok ("1.20.300.4000" eq sprintf "%vd", pack("U*",1,20,300,4000));
535ok ("1.20.300.4000" eq sprintf "%vd", pack(" U*",1,20,300,4000));
536ok (v1.20.300.4000 ne sprintf "%vd", pack("C0U*",1,20,300,4000));
537
538ok (join(" ", unpack("C*", chr(0x1e2))) eq ((ord("A") == 193) ? "156 67"
539 : "199 162"));
540
541# does pack U create Unicode?
542ok (ord(pack('U', 300)) == 300);
543
544# does unpack U deref Unicode?
545ok ((unpack('U', chr(300)))[0] == 300);
546
547# is unpack U the reverse of pack U for Unicode string?
548ok ("@{[unpack('U*', pack('U*', 100, 200, 300))]}" eq "100 200 300");
549
550# is unpack U the reverse of pack U for byte string?
551ok ("@{[unpack('U*', pack('U*', 100, 200))]}" eq "100 200");
552
553# does unpack C unravel pack U?
554ok ("@{[unpack('C*', pack('U*', 100, 200))]}" eq "100 195 136");
555
556# does pack U0C create Unicode?
557ok ("@{[pack('U0C*', 100, 195, 136)]}" eq v100.v200);
558
559# does pack C0U create characters?
560ok ("@{[pack('C0U*', 100, 200)]}" eq pack("C*", 100, 195, 136));
561
562# does unpack U0U on byte data warn?
35bcd338 563{
564 local $SIG{__WARN__} = sub { $@ = "@_" };
565 my @null = unpack('U0U', chr(255));
fa8ec7c1 566 ok ($@ =~ /^Malformed UTF-8 character /, undef, $@);
35bcd338 567}
568
fa8ec7c1 569{
570 my $p = pack 'i*', -2147483648, ~0, 0, 1, 2147483647;
571 my (@a);
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 ok ($a[0] == 0xFFFFFFFF, "$pat failed");
578 @a = scalar unpack $pat, $p;
579 ok ($a[0] == 0xFFFFFFFF, "$pat failed in scalar context");
580 }
581
582
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!
589I
590EOPOEMSNIPPET
591 @a = unpack $pat, $p;
592 ok (@a == 1 && $a[0] == 42);
593 @a = scalar unpack $pat, $p;
594 ok (@a == 1 && $a[0] == 42);
595}
596
597 # shorts (of all flavours) didn't calculate checksums > 32 bits with floating
598 # point, so a pathologically long pattern would wrap at 32 bits.
599 my $pat = "\xff\xff"x65538; # Start with it long, to save any copying.
600 foreach (4,3,2,1,0) {
601 my $len = 65534 + $_;
602 ok (unpack ("%33n$len", $pat) == 65535 * $len);
603 }
604}
b85d93de 605
606
607# pack x X @
608foreach (
609['x', "N", "\0"],
610['x4', "N", "\0"x4],
611['xX', "N", ""],
612['xXa*', "Nick", "Nick"],
613['a5Xa5', "cameL", "llama", "camellama"],
614['@4', 'N', "\0"x4],
615['a*@8a*', 'Camel', 'Dromedary', "Camel\0\0\0Dromedary"],
616['a*@4a', 'Perl rules', '!', 'Perl!'],
617) {
618 my ($template, @in) = @$_;
619 my $out = pop @in;
620 my $got = eval {pack $template, @in};
621 ok ($@ eq '' and $out eq $got, '', $@)
622 or printf "# pack ('$template', %s) gave %s expected %s\n",
623 encode_list (@in), encode_list ($got), encode_list ($out);
624}
625
626# unpack x X @
627foreach (
628['x', "N"],
629['xX', "N"],
630['xXa*', "Nick", "Nick"],
631['a5Xa5', "camellama", "camel", "llama"],
632['@3', "ice"],
633['@2a2', "water", "te"],
634['a*@1a3', "steam", "steam", "tea"],
635) {
636 my ($template, $in, @out) = @$_;
637 my @got = eval {unpack $template, $in};
638 ok (($@ eq '' and list_eq (\@got, \@out)), undef, $@)
639 or printf "# list unpack ('$template', \"%s\") gave %s expected %s\n",
640 encode ($in), encode_list (@got), encode_list (@out);
641
642 my $got = eval {unpack $template, $in};
643 ok (($@ eq '' and @out ? $got eq $out[0] # 1 or more items; should get first
644 : !defined $got) # 0 items; should get undef
645 , "", $@)
646 or printf "# scalar unpack ('$template', \"%s\") gave %s expected %s\n",
647 encode ($in), encode_list ($got), encode_list ($out[0]);
648}
d50dd4e4 649
650{
651 # 611
652 my $t = 'Z*Z*';
653 my ($u, $v) = qw(foo xyzzy);
654 my $p = pack($t, $u, $v);
655 my @u = unpack($t, $p);
656 print @u == 2 && $u[0] eq $u && $u[1] eq $v ? "ok 611\n" : "not ok 611\n";
657}