Add Devel::PPPort originally from Kenneth Albanowski,
[p5sagit/p5-mst-13.2.git] / t / op / pack.t
CommitLineData
d50dd4e4 1#!./perl -w
a687059c 2
bf80f5a2 3print "1..613\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;
7eed2ccc 52 print "not ok $test # $err\n";
fa8ec7c1 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;
800e6488 381 if (ref $total) {
382 $calc_sum = &$total($len);
fa8ec7c1 383 } else {
384 $calc_sum = $total;
385 # Shift into range by some multiple of the total
386 my $mult = int ($total / $max_p1);
387 # Need this to make sure that -1 + (~0+1) is ~0 (ie still integer)
388 $calc_sum = $total - $mult;
389 $calc_sum -= $mult * $max;
390 if ($calc_sum < 0) {
391 $calc_sum += 1;
392 $calc_sum += $max;
393 }
394 }
395 if ($calc_sum == $calc_sum - 1 && $calc_sum == $max_p1) {
396 # we're into floating point (either by getting out of the range of
397 # UV arithmetic, or because we're doing a floating point checksum) and
398 # our calculation of the checksum has become rounded up to
399 # max_checksum + 1
400 $calc_sum = 0;
401 }
2e821511 402
fa8ec7c1 403 if ($calc_sum == $sum) {
404 print "ok $test # unpack '%$_$format' gave $sum\n";
2e821511 405 } else {
fa8ec7c1 406 my $delta = 1.000001;
407 if ($format =~ tr /dDfF//
408 && ($calc_sum <= $sum * $delta && $calc_sum >= $sum / $delta)) {
409 print "ok $test # unpack '%$_$format' gave $sum,"
410 . " expected $calc_sum\n";
411 } else {
811a02aa 412 my $text = ref $total ? &$total($len) : $total;
413 print "not ok $test # For list (" . join (", ", @_) . ") (total $text)"
fa8ec7c1 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);
800e6488 449# This total is icky, but the true total is 2**65-1, and need a way to generate
450# the epxected checksum on any system including those where NVs can preserve
451# 65 bits. (long double is 128 bits on sparc, so they certainly can)
452# or where rounding is down not up on binary conversion (crays)
453numbers_with_total ('Q', sub {
454 my $len = shift;
455 $len = 65 if $len > 65; # unmasked total is 2**65-1 here
456 my $total = 1 + 2 * (int (2**($len - 1)) - 1);
457 return 0 if $total == $total - 1; # Overflowed integers
458 return $total; # NVs still accurate to nearest integer
459 },
fa8ec7c1 460 0, 1,9223372036854775807, 9223372036854775808,
461 18446744073709551615);
462
463# pack nvNV byteorders
464
465ok (pack("n", 0xdead) eq "\xde\xad");
466ok (pack("v", 0xdead) eq "\xad\xde");
467ok (pack("N", 0xdeadbeef) eq "\xde\xad\xbe\xef");
468ok (pack("V", 0xdeadbeef) eq "\xef\xbe\xad\xde");
43192e07 469
fa8ec7c1 470{
471 # /
472
473 my ($x, $y, $z);
474 eval { ($x) = unpack '/a*','hello' };
475 ok ($@ =~ m!/ must follow a numeric type!, undef, $@);
476 eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
477 ok ($z eq 'ok');
478 ok ($x eq 'yes');
479 ok ($y eq 'z');
480 ok ($@ eq '', undef, $@);
481
482 eval { ($x) = pack '/a*','hello' };
483 ok ($@ =~ m!Invalid type in pack: '/'!, undef, $@);
484
485 $z = pack 'n/a* N/Z* w/A*','string','hi there ','etc';
486 my $expect = "\000\006string\0\0\0\012hi there \000\003etc";
487 unless (ok ($z eq $expect)) {
488 printf "# got '%s'\n", encode $z;
489 }
4b5b2118 490
b85d93de 491 $expect = 'hello world';
492 eval { ($x) = unpack ("w/a", chr (11) . "hello world!")};
493 ok ($x eq $expect);
494 ok ($@ eq '', undef, $@);
495 # Doing this in scalar context used to fail.
496 eval { $x = unpack ("w/a", chr (11) . "hello world!")};
497 unless (ok ($x eq $expect, undef, $@)) {
498 printf "# expected '$expect' got '%s'\n", encode $x;
499 }
500 ok ($@ eq '', undef, $@);
501
fa8ec7c1 502 foreach (
503['a/a*/a*', '212ab345678901234567','ab3456789012'],
504['a/a*/a*', '3012ab345678901234567', 'ab3456789012'],
505['a/a*/b*', '212ab', $Is_EBCDIC ? '100000010100' : '100001100100'],
506) {
507 my ($pat, $in, $expect) = @$_;
508 eval { ($x) = unpack $pat, $in };
b85d93de 509 ok ($@ eq '' && $x eq $expect, undef, $@)
510 or printf "# list unpack ('$pat', '$in') gave %s, expected '$expect'\n",
511 encode_list ($x);
512 eval { $x = unpack $pat, $in };
513 ok ($@ eq '' && $x eq $expect, undef, $@)
514 or printf "# scalar unpack ('$pat', '$in') gave %s, expected '$expect'\n",
515 encode_list ($x);
fa8ec7c1 516 }
4b5b2118 517
fa8ec7c1 518# / with #
17f4a12d 519
520eval { ($z,$x,$y) = unpack <<EOU, "003ok \003yes\004z\000abc" };
521 a3/A # Count in ASCII
522 C/a* # Count in a C char
523 C/Z # Count in a C char but skip after \0
524EOU
fa8ec7c1 525 ok ($z eq 'ok');
526 ok ($x eq 'yes');
527 ok ($y eq 'z');
528 ok ($@ eq '', undef, $@);
17f4a12d 529
530$z = pack <<EOP,'string','etc';
531 n/a* # Count as network short
532 w/A* # Count a BER integer
533EOP
fa8ec7c1 534 $expect = "\000\006string\003etc";
535unless (ok ($z eq $expect)) {
536 $z = encode $z;
537 print "# got '$z', expected '$expect'\n";
538}
539}
540
541ok ("1.20.300.4000" eq sprintf "%vd", pack("U*",1,20,300,4000));
542ok ("1.20.300.4000" eq sprintf "%vd", pack(" U*",1,20,300,4000));
543ok (v1.20.300.4000 ne sprintf "%vd", pack("C0U*",1,20,300,4000));
544
545ok (join(" ", unpack("C*", chr(0x1e2))) eq ((ord("A") == 193) ? "156 67"
546 : "199 162"));
547
548# does pack U create Unicode?
549ok (ord(pack('U', 300)) == 300);
550
551# does unpack U deref Unicode?
552ok ((unpack('U', chr(300)))[0] == 300);
553
554# is unpack U the reverse of pack U for Unicode string?
555ok ("@{[unpack('U*', pack('U*', 100, 200, 300))]}" eq "100 200 300");
556
557# is unpack U the reverse of pack U for byte string?
558ok ("@{[unpack('U*', pack('U*', 100, 200))]}" eq "100 200");
559
2dcec3fe 560if (ord('A') == 193) {
561 ok(1, undef, "skipped") for 1..4; # EBCDIC
562} else {
7eed2ccc 563 # does unpack C unravel pack U?
564 ok ("@{[unpack('C*', pack('U*', 100, 200))]}" eq "100 195 136");
fa8ec7c1 565
7eed2ccc 566 # does pack U0C create Unicode?
567 ok ("@{[pack('U0C*', 100, 195, 136)]}" eq v100.v200);
fa8ec7c1 568
7eed2ccc 569 # does pack C0U create characters?
570 ok ("@{[pack('C0U*', 100, 200)]}" eq pack("C*", 100, 195, 136));
fa8ec7c1 571
2dcec3fe 572 # does unpack U0U on byte data warn?
573 {
574 local $SIG{__WARN__} = sub { $@ = "@_" };
575 my @null = unpack('U0U', chr(255));
576 ok ($@ =~ /^Malformed UTF-8 character /, undef, $@);
577 }
35bcd338 578}
579
fa8ec7c1 580{
581 my $p = pack 'i*', -2147483648, ~0, 0, 1, 2147483647;
582 my (@a);
583 # bug - % had to be at the start of the pattern, no leading whitespace or
584 # comments. %i! didn't work at all.
585 foreach my $pat ('%32i*', ' %32i*', "# Muhahahaha\n%32i*", '%32i* ',
586 '%32i!*', ' %32i!*', "\n#\n#\n\r \t\f%32i!*", '%32i!*#') {
587 @a = unpack $pat, $p;
588 ok ($a[0] == 0xFFFFFFFF, "$pat failed");
589 @a = scalar unpack $pat, $p;
590 ok ($a[0] == 0xFFFFFFFF, "$pat failed in scalar context");
591 }
592
593
594 $p = pack 'I*', 42, 12;
595 # Multiline patterns in scalar context failed.
596 foreach my $pat ('I', <<EOPOEMSNIPPET, 'I#I', 'I # I', 'I # !!!') {
597# On the Ning Nang Nong
598# Where the Cows go Bong!
599# And the Monkeys all say Boo!
600I
601EOPOEMSNIPPET
602 @a = unpack $pat, $p;
603 ok (@a == 1 && $a[0] == 42);
604 @a = scalar unpack $pat, $p;
605 ok (@a == 1 && $a[0] == 42);
606}
607
608 # shorts (of all flavours) didn't calculate checksums > 32 bits with floating
609 # point, so a pathologically long pattern would wrap at 32 bits.
610 my $pat = "\xff\xff"x65538; # Start with it long, to save any copying.
611 foreach (4,3,2,1,0) {
612 my $len = 65534 + $_;
613 ok (unpack ("%33n$len", $pat) == 65535 * $len);
614 }
615}
b85d93de 616
617
618# pack x X @
619foreach (
620['x', "N", "\0"],
621['x4', "N", "\0"x4],
622['xX', "N", ""],
623['xXa*', "Nick", "Nick"],
624['a5Xa5', "cameL", "llama", "camellama"],
625['@4', 'N', "\0"x4],
626['a*@8a*', 'Camel', 'Dromedary', "Camel\0\0\0Dromedary"],
627['a*@4a', 'Perl rules', '!', 'Perl!'],
628) {
629 my ($template, @in) = @$_;
630 my $out = pop @in;
631 my $got = eval {pack $template, @in};
632 ok ($@ eq '' and $out eq $got, '', $@)
633 or printf "# pack ('$template', %s) gave %s expected %s\n",
634 encode_list (@in), encode_list ($got), encode_list ($out);
635}
636
637# unpack x X @
638foreach (
639['x', "N"],
640['xX', "N"],
641['xXa*', "Nick", "Nick"],
642['a5Xa5', "camellama", "camel", "llama"],
643['@3', "ice"],
644['@2a2', "water", "te"],
645['a*@1a3', "steam", "steam", "tea"],
646) {
647 my ($template, $in, @out) = @$_;
648 my @got = eval {unpack $template, $in};
649 ok (($@ eq '' and list_eq (\@got, \@out)), undef, $@)
650 or printf "# list unpack ('$template', \"%s\") gave %s expected %s\n",
651 encode ($in), encode_list (@got), encode_list (@out);
652
653 my $got = eval {unpack $template, $in};
654 ok (($@ eq '' and @out ? $got eq $out[0] # 1 or more items; should get first
655 : !defined $got) # 0 items; should get undef
656 , "", $@)
657 or printf "# scalar unpack ('$template', \"%s\") gave %s expected %s\n",
658 encode ($in), encode_list ($got), encode_list ($out[0]);
659}
d50dd4e4 660
661{
662 # 611
663 my $t = 'Z*Z*';
664 my ($u, $v) = qw(foo xyzzy);
665 my $p = pack($t, $u, $v);
666 my @u = unpack($t, $p);
01af4f20 667 ok(@u == 2 && $u[0] eq $u && $u[1] eq $v);
d50dd4e4 668}
bf80f5a2 669
670{
671 # 612
672
673 ok((unpack("w/a*", "\x02abc"))[0] eq "ab");
674
675 # 613: "w/a*" should be seen as one unit
676
677 ok(scalar unpack("w/a*", "\x02abc") eq "ab");
678}