6b9ceeb947fadc240b048d514cfc4d93e2e3604d
[p5sagit/p5-mst-13.2.git] / t / op / pack.t
1 #!./perl -w
2
3 print "1..611\n";
4
5 BEGIN {
6     chdir 't' if -d 't';
7     @INC = '../lib';
8 }
9
10 use strict;
11 use warnings;
12 use Config;
13
14 my $Is_EBCDIC = (defined $Config{ebcdic} && $Config{ebcdic} eq 'define');
15
16 my $test = 1;
17 # Using Test considered bad plan in op/*.t ?
18
19 sub encode {
20   my @result = @_;
21   foreach (@result) {
22     s/([[:cntrl:]\177 ])/sprintf "\\%03o", ord $1/ge if defined;
23   }
24   @result;
25 }
26
27 sub 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
43 sub ok {
44   my ($pass, $wrong, $err) = @_;
45   if ($pass) {
46     print "ok $test\n";
47     $test++;
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;
64 }
65
66 sub 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
84 {
85 my $format = "c2 x5 C C x s d i l a6";
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.
88 my @ary = (1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,"abcdef");
89 my $foo = pack($format,@ary);
90 my @ary2 = unpack($format,$foo);
91
92 ok($#ary == $#ary2);
93
94 my $out1=join(':',@ary);
95 my $out2=join(':',@ary2);
96 # Using long double NVs may introduce greater accuracy than wanted.
97 $out1 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
98 $out2 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
99 ok($out1 eq $out2);
100
101 ok($foo =~ /def/);
102 }
103 # How about counting bits?
104
105 {
106 my $x;
107 ok( ($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")) == 16 );
108
109 ok( ($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")) == 12 );
110
111 ok( ($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")) == 9 );
112 }
113
114 {
115 my $sum = 129; # ASCII
116 $sum = 103 if $Is_EBCDIC;
117
118 my $x;
119 ok( ($x = unpack("%32B*", "Now is the time for all good blurfl")) == $sum );
120
121 my $foo;
122 open(BIN, "./perl") || open(BIN, "./perl.exe") || open(BIN, $^X)
123     || die "Can't open ../perl or ../perl.exe: $!\n";
124 sysread BIN, $foo, 8192;
125 close BIN;
126
127 $sum = unpack("%32b*", $foo);
128 my $longway = unpack("b*", $foo);
129 ok( $sum == $longway =~ tr/1/1/ );
130 }
131
132 {
133   my $x;
134   ok( ($x = unpack("I",pack("I", 0xFFFFFFFF))) == 0xFFFFFFFF );
135 }
136
137 {
138 # check 'w'
139 my @x = (5,130,256,560,32000,3097152,268435455,1073741844, 2**33,
140          '4503599627365785','23728385234614992549757750638446');
141 my $x = pack('w*', @x);
142 my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f8480808014A08080800087ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e';
143
144 ok ($x eq $y, unpack 'H*', $x);
145 my @y = unpack('w*', $y);
146 my $a;
147 while ($a = pop @x) {
148   my $b = pop @y;
149   ok ($a eq $b, "\$a='$a' \$b='$b'");
150 }
151
152 @y = unpack('w2', $x);
153
154 ok (scalar(@y) == 2);
155 ok ($y[1] == 130, $y[1]);
156 }
157
158 {
159   # test exeptions
160   my $x;
161   eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
162   ok ($@ =~ /^Unterminated compressed integer/, undef, $@);
163
164   eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
165   ok ($@ =~ /^Unterminated compressed integer/, undef, $@);
166
167   eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
168   ok ($@ =~ /^Unterminated compressed integer/, undef, $@);
169 }
170
171 #
172 # test the "p" template
173
174 # literals
175 ok(unpack("p",pack("p","foo")) eq "foo");
176
177 # scalars
178 ok(unpack("p",pack("p",$test)) == $test);
179
180 # temps
181 sub foo { my $a = "a"; return $a . $a++ . $a++ }
182 {
183   use warnings;
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
193 ok (pack("p", undef) =~ /^\0+/);
194
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:
199 ok((unpack("i",pack("i",-1))) == -1, "__osf__ like bug seems to exist");
200
201 # 31..36: test the pack lengths of s S i I l L
202 # 37..40: test the pack lengths of n N v V
203 my @lengths = qw(s 2 S 2 i -4 I -4 l 4 L 4 n 2 N 4 v 2 V 4);
204 while (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 }
214
215 # 41..56: test unpack-pack lengths
216
217 my @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...
221 eval { my $q = pack("q",0) };
222 push @templates, $@ !~ /Invalid type in pack/ ? qw(q Q) : qw(f d);
223
224 foreach my $t (@templates) {
225     my @t = unpack("$t*", pack("$t*", 12, 34));
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");
228 }
229
230 {
231 # 57..60: uuencode/decode
232
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.
237
238 my $in = pack 'C*', 0 .. 255;
239
240 # just to be anal, we do some random tr/`/ /
241 my $uu = <<'EOUU';
242 M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL
243 M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9
244 M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6&
245 MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S
246 MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@
247 ?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P `
248 EOUU
249
250 $_ = $uu;
251 tr/ /`/;
252
253 ok (pack('u', $in) eq $_);
254
255 ok (unpack('u', $uu) eq $in);
256
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';
259 M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
260 &8%P:````
261 EOUU
262
263 ok unless unpack('u', $uu);
264
265 # 60 identical to 59 except that backquotes have been changed to spaces
266
267 $uu = <<'EOUU';
268 M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
269 &8%P:
270 EOUU
271
272 # ' # Grr
273 ok (unpack('u', $uu) eq $in);
274
275 }
276
277 # 61..73: test the ascii template types (A, a, Z)
278
279 foreach (
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   }
301 }
302
303 # 74..79: packing native shorts/ints/longs
304
305 ok (length(pack("s!", 0)) == $Config{shortsize});
306 ok (length(pack("i!", 0)) == $Config{intsize});
307 ok (length(pack("l!", 0)) == $Config{longsize});
308 ok (length(pack("s!", 0)) <= length(pack("i!", 0)));
309 ok (length(pack("i!", 0)) <= length(pack("l!", 0)));
310 ok (length(pack("i!", 0)) == length(pack("i", 0)));
311
312 sub numbers {
313   my $format = shift;
314   return numbers_with_total ($format, undef, @_);
315 }
316
317 sub 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   }
337
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   }
346
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;
359
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     }
365
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     }
403
404     if ($calc_sum == $sum) {
405       print "ok $test # unpack '%$_$format' gave $sum\n";
406     } else {
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       }
417     }
418   } continue {
419     $test++;
420   }
421 }
422
423 numbers ('c', -128, -1, 0, 1, 127);
424 numbers ('C', 0, 1, 127, 128, 255);
425 numbers ('s', -32768, -1, 0, 1, 32767);
426 numbers ('S', 0, 1, 32767, 32768, 65535);
427 numbers ('i', -2147483648, -1, 0, 1, 2147483647);
428 numbers ('I', 0, 1, 2147483647, 2147483648, 4294967295);
429 numbers ('l', -2147483648, -1, 0, 1, 2147483647);
430 numbers ('L', 0, 1, 2147483647, 2147483648, 4294967295);
431 numbers ('s!', -32768, -1, 0, 1, 32767);
432 numbers ('S!', 0, 1, 32767, 32768, 65535);
433 numbers ('i!', -2147483648, -1, 0, 1, 2147483647);
434 numbers ('I!', 0, 1, 2147483647, 2147483648, 4294967295);
435 numbers ('l!', -2147483648, -1, 0, 1, 2147483647);
436 numbers ('L!', 0, 1, 2147483647, 2147483648, 4294967295);
437 numbers ('n', 0, 1, 32767, 32768, 65535);
438 numbers ('v', 0, 1, 32767, 32768, 65535);
439 numbers ('N', 0, 1, 2147483647, 2147483648, 4294967295);
440 numbers ('V', 0, 1, 2147483647, 2147483648, 4294967295);
441 # All these should have exact binary representations:
442 numbers ('f', -1, 0, 0.5, 42, 2**34);
443 numbers ('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));
446
447 numbers_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)
452 numbers_with_total ('Q', "0b" . "1" x 65,
453                     0, 1,9223372036854775807, 9223372036854775808,
454                     18446744073709551615);
455
456 # pack nvNV byteorders
457
458 ok (pack("n", 0xdead) eq "\xde\xad");
459 ok (pack("v", 0xdead) eq "\xad\xde");
460 ok (pack("N", 0xdeadbeef) eq "\xde\xad\xbe\xef");
461 ok (pack("V", 0xdeadbeef) eq "\xef\xbe\xad\xde");
462
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   }
483
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
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 };
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);
509   }
510
511 # / with #
512
513 eval { ($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
517 EOU
518   ok ($z eq 'ok');
519   ok ($x eq 'yes');
520   ok ($y eq 'z');
521   ok ($@ eq '', undef, $@);
522
523 $z = pack <<EOP,'string','etc';
524   n/a*                  # Count as network short
525   w/A*                  # Count a  BER integer
526 EOP
527   $expect = "\000\006string\003etc";
528 unless (ok ($z eq $expect)) {
529   $z = encode $z;
530   print "# got '$z', expected '$expect'\n";
531 }
532 }
533
534 ok ("1.20.300.4000" eq sprintf "%vd", pack("U*",1,20,300,4000));
535 ok ("1.20.300.4000" eq sprintf "%vd", pack("  U*",1,20,300,4000));
536 ok (v1.20.300.4000 ne sprintf "%vd", pack("C0U*",1,20,300,4000));
537
538 ok (join(" ", unpack("C*", chr(0x1e2))) eq ((ord("A") == 193) ? "156 67"
539                                             : "199 162"));
540
541 # does pack U create Unicode?
542 ok (ord(pack('U', 300)) == 300);
543
544 # does unpack U deref Unicode?
545 ok ((unpack('U', chr(300)))[0] == 300);
546
547 # is unpack U the reverse of pack U for Unicode string?
548 ok ("@{[unpack('U*', pack('U*', 100, 200, 300))]}" eq "100 200 300");
549
550 # is unpack U the reverse of pack U for byte string?
551 ok ("@{[unpack('U*', pack('U*', 100, 200))]}" eq "100 200");
552
553 # does unpack C unravel pack U?
554 ok ("@{[unpack('C*', pack('U*', 100, 200))]}" eq "100 195 136");
555
556 # does pack U0C create Unicode?
557 ok ("@{[pack('U0C*', 100, 195, 136)]}" eq v100.v200);
558
559 # does pack C0U create characters?
560 ok ("@{[pack('C0U*', 100, 200)]}" eq pack("C*", 100, 195, 136));
561
562 # does unpack U0U on byte data warn?
563 {
564     local $SIG{__WARN__} = sub { $@ = "@_" };
565     my @null = unpack('U0U', chr(255));
566     ok ($@ =~ /^Malformed UTF-8 character /, undef, $@);
567 }
568
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!
589 I
590 EOPOEMSNIPPET
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 }
605
606
607 # pack x X @
608 foreach (
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 @
627 foreach (
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 }
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 }