14 my $Is_EBCDIC = (defined $Config{ebcdic} && $Config{ebcdic} eq 'define');
17 # Using Test considered bad plan in op/*.t ?
22 s/([[:cntrl:]\177 ])/sprintf "\\%03o", ord $1/ge if defined;
31 s/([[:cntrl:]\177])/sprintf "\\%03o", ord $1/ge;
40 return '(' . join (', ', @result) . ')';
44 my ($pass, $wrong, $err) = @_;
52 print "not ok $test # \$\@ = $err\n";
55 $wrong = ", got $wrong";
59 printf "not ok $test # line %d$wrong\n", (caller)[2];
68 return unless @$l == @$r;
70 if (defined $l->[$i]) {
71 return unless defined ($r->[$i]) && $l->[$i] eq $r->[$i];
73 return if defined $r->[$i]
79 ##############################################################################
81 # Here starteth the tests
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);
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:/;
103 # How about counting bits?
107 ok( ($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")) == 16 );
109 ok( ($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")) == 12 );
111 ok( ($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")) == 9 );
115 my $sum = 129; # ASCII
116 $sum = 103 if $Is_EBCDIC;
119 ok( ($x = unpack("%32B*", "Now is the time for all good blurfl")) == $sum );
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;
127 $sum = unpack("%32b*", $foo);
128 my $longway = unpack("b*", $foo);
129 ok( $sum == $longway =~ tr/1/1/ );
134 ok( ($x = unpack("I",pack("I", 0xFFFFFFFF))) == 0xFFFFFFFF );
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';
144 ok ($x eq $y, unpack 'H*', $x);
145 my @y = unpack('w*', $y);
147 while ($a = pop @x) {
149 ok ($a eq $b, "\$a='$a' \$b='$b'");
152 @y = unpack('w2', $x);
154 ok (scalar(@y) == 2);
155 ok ($y[1] == 130, $y[1]);
161 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
162 ok ($@ =~ /^Unterminated compressed integer/, undef, $@);
164 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
165 ok ($@ =~ /^Unterminated compressed integer/, undef, $@);
167 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
168 ok ($@ =~ /^Unterminated compressed integer/, undef, $@);
172 # test the "p" template
175 ok(unpack("p",pack("p","foo")) eq "foo");
178 ok(unpack("p",pack("p",$test)) == $test);
181 sub foo { my $a = "a"; return $a . $a++ . $a++ }
185 local $SIG{__WARN__} = sub {
186 print "ok ",$test++,"\n" if $_[0] =~ /temporary val/
188 my $junk = pack("p", &foo);
189 print "not ok ", $test++, "\n" if $last == $test;
192 # undef should give null pointer
193 ok (pack("p", undef) =~ /^\0+/);
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
199 ok((unpack("i",pack("i",-1))) == -1, "__osf__ like bug seems to exist");
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));
207 ok ($expect == $len, "format '$format' has length $len, expected $expect");
211 "format '$format' has length $len, expected >= $expect");
215 # 41..56: test unpack-pack lengths
217 my @templates = qw(c C i I s S l L n N v V f d);
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);
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");
231 # 57..60: uuencode/decode
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
238 my $in = pack 'C*', 0 .. 255;
240 # just to be anal, we do some random tr/`/ /
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 `
253 ok (pack('u', $in) eq $_);
255 ok (unpack('u', $uu) eq $in);
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";
259 M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
263 ok unless unpack('u', $uu);
265 # 60 identical to 59 except that backquotes have been changed to spaces
268 M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
273 ok (unpack('u', $uu) eq $in);
277 # 61..73: test the ascii template types (A, a, Z)
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"],
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";
303 # 74..79: packing native shorts/ints/longs
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)));
314 return numbers_with_total ($format, undef, @_);
317 sub numbers_with_total {
320 if (!defined $total) {
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 == $_) {
332 print "not ok $test # unpack '$format', pack '$format', $_ gives $out\n";
333 print "# \$\@='$@'\n" if $@;
338 my $skip_if_longer_than = ~0; # "Infinity"
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};
347 foreach ('', 1, 2, 3, 15, 16, 17, 31, 32, 33, 53, 54, 63, 64, 65) {
348 my $sum = eval {unpack "%$_$format*", pack "$format*", @_};
350 if ($@ =~ /Invalid type in pack: '$format'/) {
351 print "ok $test # skip cannot pack '$format' on this perl\n";
353 print "not ok $test # \$\@='$@'\n" if $@;
357 my $len = $_; # Copy, so that we can reassign ''
358 $len = 16 unless length $len;
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";
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;
381 if ($total =~ /^0b[01]*?([01]{1,$len})/) {
382 no warnings qw(overflow portable);
383 $calc_sum = oct "0b$1";
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;
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
404 if ($calc_sum == $sum) {
405 print "ok $test # unpack '%$_$format' gave $sum\n";
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";
413 print "not ok $test # For list (" . join (", ", @_) . ") (total $total)"
414 . " packed with $format unpack '%$_$format' gave $sum,"
415 . " expected $calc_sum\n";
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));
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);
456 # pack nvNV byteorders
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");
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" };
473 ok ($@ eq '', undef, $@);
475 eval { ($x) = pack '/a*','hello' };
476 ok ($@ =~ m!Invalid type in pack: '/'!, undef, $@);
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;
484 $expect = 'hello world';
485 eval { ($x) = unpack ("w/a", chr (11) . "hello world!")};
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;
493 ok ($@ eq '', undef, $@);
496 ['a/a*/a*', '212ab345678901234567','ab3456789012'],
497 ['a/a*/a*', '3012ab345678901234567', 'ab3456789012'],
498 ['a/a*/b*', '212ab', $Is_EBCDIC ? '100000010100' : '100001100100'],
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",
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",
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
521 ok ($@ eq '', undef, $@);
523 $z = pack <<EOP,'string','etc';
524 n/a* # Count as network short
525 w/A* # Count a BER integer
527 $expect = "\000\006string\003etc";
528 unless (ok ($z eq $expect)) {
530 print "# got '$z', expected '$expect'\n";
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));
538 ok (join(" ", unpack("C*", chr(0x1e2))) eq ((ord("A") == 193) ? "156 67"
541 # does pack U create Unicode?
542 ok (ord(pack('U', 300)) == 300);
544 # does unpack U deref Unicode?
545 ok ((unpack('U', chr(300)))[0] == 300);
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");
550 # is unpack U the reverse of pack U for byte string?
551 ok ("@{[unpack('U*', pack('U*', 100, 200))]}" eq "100 200");
553 # does unpack C unravel pack U?
554 ok ("@{[unpack('C*', pack('U*', 100, 200))]}" eq "100 195 136");
556 # does pack U0C create Unicode?
557 ok ("@{[pack('U0C*', 100, 195, 136)]}" eq v100.v200);
559 # does pack C0U create characters?
560 ok ("@{[pack('C0U*', 100, 200)]}" eq pack("C*", 100, 195, 136));
562 # does unpack U0U on byte data warn?
564 local $SIG{__WARN__} = sub { $@ = "@_" };
565 my @null = unpack('U0U', chr(255));
566 ok ($@ =~ /^Malformed UTF-8 character /, undef, $@);
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 ok ($a[0] == 0xFFFFFFFF, "$pat failed");
578 @a = scalar unpack $pat, $p;
579 ok ($a[0] == 0xFFFFFFFF, "$pat failed in scalar context");
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;
592 ok (@a == 1 && $a[0] == 42);
593 @a = scalar unpack $pat, $p;
594 ok (@a == 1 && $a[0] == 42);
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);
612 ['xXa*', "Nick", "Nick"],
613 ['a5Xa5', "cameL", "llama", "camellama"],
615 ['a*@8a*', 'Camel', 'Dromedary', "Camel\0\0\0Dromedary"],
616 ['a*@4a', 'Perl rules', '!', 'Perl!'],
618 my ($template, @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);
630 ['xXa*', "Nick", "Nick"],
631 ['a5Xa5', "camellama", "camel", "llama"],
633 ['@2a2', "water", "te"],
634 ['a*@1a3', "steam", "steam", "tea"],
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);
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
646 or printf "# scalar unpack ('$template', \"%s\") gave %s expected %s\n",
647 encode ($in), encode_list ($got), encode_list ($out[0]);
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";