10 $Is_EBCDIC = (defined $Config{ebcdic} && $Config{ebcdic} eq 'define');
16 # You have to do it this way or VMS will get confused.
18 $out = "not " unless $ok;
22 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
31 # Note: All test numbers in comments are off by 1 after the comment below..
33 $format = "c2 x5 C C x s d i l a6";
34 # Need the expression in here to force ary[5] to be numeric. This avoids
35 # test2 failing because ary2 goes str->numeric->str and ary doesn't.
36 @ary = (1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,"abcdef");
37 $foo = pack($format,@ary);
38 @ary2 = unpack($format,$foo);
43 $out2=join(':',@ary2);
44 # Using long double NVs may introduce greater accuracy than wanted.
45 $out1 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
46 $out2 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
51 # How about counting bits?
53 ok( ($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")) == 16 );
55 ok( ($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")) == 12 );
57 ok( ($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")) == 9 );
59 my $sum = 129; # ASCII
60 $sum = 103 if $Is_EBCDIC;
62 ok( ($x = unpack("%32B*", "Now is the time for all good blurfl")) == $sum );
64 open(BIN, "./perl") || open(BIN, "./perl.exe") || open(BIN, $^X)
65 || die "Can't open ../perl or ../perl.exe: $!\n";
66 sysread BIN, $foo, 8192;
69 $sum = unpack("%32b*", $foo);
70 $longway = unpack("b*", $foo);
71 ok( $sum == $longway =~ tr/1/1/ );
73 ok( ($x = unpack("I",pack("I", 0xFFFFFFFF))) == 0xFFFFFFFF );
76 my @x = (5,130,256,560,32000,3097152,268435455,1073741844, 2**33,
77 '4503599627365785','23728385234614992549757750638446');
78 my $x = pack('w*', @x);
79 my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f8480808014A08080800087ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e';
84 printf "not ok $test # %s\n", unpack 'H*', $x;
88 @y = unpack('w*', $y);
92 print $a eq $b ? "ok $test\n" : "not ok $test\n$a\n$b\n"; $test++;
95 # XXX All test numbers in comments are off by 1 after this point.
97 @y = unpack('w2', $x);
99 print scalar(@y) == 2 ? "ok $test\n" : "not ok $test\n"; $test++;
100 print $y[1] == 130 ? "ok $test\n" : "not ok $test # $y[1]\n"; $test++;
103 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
104 print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
106 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
107 print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
109 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
110 print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
113 # test the "p" template
116 print((unpack("p",pack("p","foo")) eq "foo" ? "ok " : "not ok "),$test++,"\n");
119 print((unpack("p",pack("p",$test)) == $test ? "ok " : "not ok "),$test++,"\n");
122 sub foo { my $a = "a"; return $a . $a++ . $a++ }
126 local $SIG{__WARN__} = sub {
127 print "ok ",$test++,"\n" if $_[0] =~ /temporary val/
129 my $junk = pack("p", &foo);
130 print "not ok ", $test++, "\n" if $last == $test;
133 # undef should give null pointer
134 print((pack("p", undef) =~ /^\0+/ ? "ok " : "not ok "),$test++,"\n");
136 # Check for optimizer bug (e.g. Digital Unix GEM cc with -O4 on DU V4.0B gives
137 # 4294967295 instead of -1)
138 # see #ifdef __osf__ in pp.c pp_unpack
140 print( ((unpack("i",pack("i",-1))) == -1 ? "ok " : "not ok "),$test++,"\n");
142 # 31..36: test the pack lengths of s S i I l L
143 print "not " unless length(pack("s", 0)) == 2;
144 print "ok ", $test++, "\n";
146 print "not " unless length(pack("S", 0)) == 2;
147 print "ok ", $test++, "\n";
149 print "not " unless length(pack("i", 0)) >= 4;
150 print "ok ", $test++, "\n";
152 print "not " unless length(pack("I", 0)) >= 4;
153 print "ok ", $test++, "\n";
155 print "not " unless length(pack("l", 0)) == 4;
156 print "ok ", $test++, "\n";
158 print "not " unless length(pack("L", 0)) == 4;
159 print "ok ", $test++, "\n";
161 # 37..40: test the pack lengths of n N v V
163 print "not " unless length(pack("n", 0)) == 2;
164 print "ok ", $test++, "\n";
166 print "not " unless length(pack("N", 0)) == 4;
167 print "ok ", $test++, "\n";
169 print "not " unless length(pack("v", 0)) == 2;
170 print "ok ", $test++, "\n";
172 print "not " unless length(pack("V", 0)) == 4;
173 print "ok ", $test++, "\n";
175 # 41..56: test unpack-pack lengths
177 my @templates = qw(c C i I s S l L n N v V f d);
179 # quads not supported everywhere: if not, retest floats/doubles
180 # to preserve the test count...
181 eval { my $q = pack("q",0) };
182 push @templates, $@ !~ /Invalid type in pack/ ? qw(q Q) : qw(f d);
184 foreach my $t (@templates) {
185 my @t = unpack("$t*", pack("$t*", 12, 34));
187 unless @t == 2 and (($t[0] == 12 and $t[1] == 34) or ($t =~ /[nv]/i));
188 print "ok ", $test++, "\n";
191 # 57..60: uuencode/decode
193 # Note that first uuencoding known 'text' data and then checking the
194 # binary values of the uuencoded version would not be portable between
195 # character sets. Uuencoding is meant for encoding binary data, not
198 $in = pack 'C*', 0 .. 255;
200 # just to be anal, we do some random tr/`/ /
202 M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL
203 M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9
204 M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6&
205 MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S
206 MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@
207 ?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P `
212 print "not " unless pack('u', $in) eq $_;
213 print "ok ", $test++, "\n";
215 print "not " unless unpack('u', $uu) eq $in;
216 print "ok ", $test++, "\n";
218 $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";
220 M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
224 print "not " unless unpack('u', $uu) eq $in;
225 print "ok ", $test++, "\n";
227 # 60 identical to 59 except that backquotes have been changed to spaces
230 M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
234 print "not " unless unpack('u', $uu) eq $in;
235 print "ok ", $test++, "\n";
237 # 61..73: test the ascii template types (A, a, Z)
239 print "not " unless pack('A*', "foo\0bar\0 ") eq "foo\0bar\0 ";
240 print "ok ", $test++, "\n";
242 print "not " unless pack('A11', "foo\0bar\0 ") eq "foo\0bar\0 ";
243 print "ok ", $test++, "\n";
245 print "not " unless unpack('A*', "foo\0bar \0") eq "foo\0bar";
246 print "ok ", $test++, "\n";
248 print "not " unless unpack('A8', "foo\0bar \0") eq "foo\0bar";
249 print "ok ", $test++, "\n";
251 print "not " unless pack('a*', "foo\0bar\0 ") eq "foo\0bar\0 ";
252 print "ok ", $test++, "\n";
254 print "not " unless pack('a11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0";
255 print "ok ", $test++, "\n";
257 print "not " unless unpack('a*', "foo\0bar \0") eq "foo\0bar \0";
258 print "ok ", $test++, "\n";
260 print "not " unless unpack('a8', "foo\0bar \0") eq "foo\0bar ";
261 print "ok ", $test++, "\n";
263 print "not " unless pack('Z*', "foo\0bar\0 ") eq "foo\0bar\0 \0";
264 print "ok ", $test++, "\n";
266 print "not " unless pack('Z11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0";
267 print "ok ", $test++, "\n";
269 print "not " unless pack('Z3', "foo") eq "fo\0";
270 print "ok ", $test++, "\n";
272 print "not " unless unpack('Z*', "foo\0bar \0") eq "foo";
273 print "ok ", $test++, "\n";
275 print "not " unless unpack('Z8', "foo\0bar \0") eq "foo";
276 print "ok ", $test++, "\n";
278 # 74..79: packing native shorts/ints/longs
280 print "not " unless length(pack("s!", 0)) == $Config{shortsize};
281 print "ok ", $test++, "\n";
283 print "not " unless length(pack("i!", 0)) == $Config{intsize};
284 print "ok ", $test++, "\n";
286 print "not " unless length(pack("l!", 0)) == $Config{longsize};
287 print "ok ", $test++, "\n";
289 print "not " unless length(pack("s!", 0)) <= length(pack("i!", 0));
290 print "ok ", $test++, "\n";
292 print "not " unless length(pack("i!", 0)) <= length(pack("l!", 0));
293 print "ok ", $test++, "\n";
295 print "not " unless length(pack("i!", 0)) == length(pack("i", 0));
296 print "ok ", $test++, "\n";
298 # 80..139: pack <-> unpack bijectionism
301 foreach my $c (-128, -1, 0, 1, 127) {
302 print "not " unless unpack("c", pack("c", $c)) == $c;
303 print "ok ", $test++, "\n";
307 foreach my $C (0, 1, 127, 128, 255) {
308 print "not " unless unpack("C", pack("C", $C)) == $C;
309 print "ok ", $test++, "\n";
313 foreach my $s (-32768, -1, 0, 1, 32767) {
314 print "not " unless unpack("s", pack("s", $s)) == $s;
315 print "ok ", $test++, "\n";
319 foreach my $S (0, 1, 32767, 32768, 65535) {
320 print "not " unless unpack("S", pack("S", $S)) == $S;
321 print "ok ", $test++, "\n";
325 foreach my $i (-2147483648, -1, 0, 1, 2147483647) {
326 print "not " unless unpack("i", pack("i", $i)) == $i;
327 print "ok ", $test++, "\n";
331 foreach my $I (0, 1, 2147483647, 2147483648, 4294967295) {
332 print "not " unless unpack("I", pack("I", $I)) == $I;
333 print "ok ", $test++, "\n";
337 foreach my $l (-2147483648, -1, 0, 1, 2147483647) {
338 print "not " unless unpack("l", pack("l", $l)) == $l;
339 print "ok ", $test++, "\n";
343 foreach my $L (0, 1, 2147483647, 2147483648, 4294967295) {
344 print "not " unless unpack("L", pack("L", $L)) == $L;
345 print "ok ", $test++, "\n";
349 foreach my $n (0, 1, 32767, 32768, 65535) {
350 print "not " unless unpack("n", pack("n", $n)) == $n;
351 print "ok ", $test++, "\n";
355 foreach my $v (0, 1, 32767, 32768, 65535) {
356 print "not " unless unpack("v", pack("v", $v)) == $v;
357 print "ok ", $test++, "\n";
361 foreach my $N (0, 1, 2147483647, 2147483648, 4294967295) {
362 print "not " unless unpack("N", pack("N", $N)) == $N;
363 print "ok ", $test++, "\n";
367 foreach my $V (0, 1, 2147483647, 2147483648, 4294967295) {
368 print "not " unless unpack("V", pack("V", $V)) == $V;
369 print "ok ", $test++, "\n";
372 # 140..143: pack nvNV byteorders
374 print "not " unless pack("n", 0xdead) eq "\xde\xad";
375 print "ok ", $test++, "\n";
377 print "not " unless pack("v", 0xdead) eq "\xad\xde";
378 print "ok ", $test++, "\n";
380 print "not " unless pack("N", 0xdeadbeef) eq "\xde\xad\xbe\xef";
381 print "ok ", $test++, "\n";
383 print "not " unless pack("V", 0xdeadbeef) eq "\xef\xbe\xad\xde";
384 print "ok ", $test++, "\n";
388 # Using Test considered bad plan in op/*.t ?
391 my ($pass, $test, $err, $wrong) = @_;
397 print "not ok $test # \$\@ = $err\n";
399 $wrong =~ s/([[:cntrl:]\177 ])/sprintf "\\%03o", ord $1/ge;
400 print "not ok $test # got $wrong\n";
406 eval { ($x) = unpack '/a*','hello' };
407 print 'not ' unless $@; print "ok $test\n"; $test++;
408 eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
409 print $@ eq '' && $z eq 'ok' ? "ok $test\n" : "not ok $test\n"; $test++;
410 print $@ eq '' && $x eq 'yes' ? "ok $test\n" : "not ok $test\n"; $test++;
411 print $@ eq '' && $y eq 'z' ? "ok $test\n" : "not ok $test\n"; $test++;
413 eval { ($x) = pack '/a*','hello' };
414 print 'not ' unless $@; print "ok $test\n"; $test++;
415 $z = pack 'n/a* N/Z* w/A*','string','hi there ','etc';
416 my $expect = "\000\006string\0\0\0\012hi there \000\003etc";
417 report ($z eq $expect, $test++, '', $z);
419 eval { ($x) = unpack 'a/a*/a*', '212ab345678901234567' };
420 print $@ eq '' && $x eq 'ab3456789012' ? "ok $test\n" : "#$x,$@\nnot ok $test\n";
423 eval { ($x) = unpack 'a/a*/a*', '3012ab345678901234567' };
424 print $@ eq '' && $x eq 'ab3456789012' ? "ok $test\n" : "not ok $test\n";
427 eval { ($x) = unpack 'a/a*/b*', '212ab' };
428 my $expected_x = '100001100100';
429 if ($Is_EBCDIC) { $expected_x = '100000010100'; }
430 print $@ eq '' && $x eq $expected_x ? "ok $test\n" : "#$x,$@\nnot ok $test\n";
435 eval { ($z,$x,$y) = unpack <<EOU, "003ok \003yes\004z\000abc" };
436 a3/A # Count in ASCII
437 C/a* # Count in a C char
438 C/Z # Count in a C char but skip after \0
440 print $@ eq '' && $z eq 'ok' ? "ok $test\n" : "not ok $test\n"; $test++;
441 print $@ eq '' && $x eq 'yes' ? "ok $test\n" : "not ok $test\n"; $test++;
442 print $@ eq '' && $y eq 'z' ? "ok $test\n" : "not ok $test\n"; $test++;
444 $z = pack <<EOP,'string','etc';
445 n/a* # Count as network short
446 w/A* # Count a BER integer
448 $expect = "\000\006string\003etc";
449 report ($z eq $expect, $test++, '', $z);
451 print 'not ' unless "1.20.300.4000" eq sprintf "%vd", pack("U*",1,20,300,4000);
452 print "ok $test\n"; $test++;
453 print 'not ' unless "1.20.300.4000" eq
454 sprintf "%vd", pack(" U*",1,20,300,4000);
455 print "ok $test\n"; $test++;
456 print 'not ' unless v1.20.300.4000 ne
457 sprintf "%vd", pack("C0U*",1,20,300,4000);
458 print "ok $test\n"; $test++;
461 print "not " unless join(" ", unpack("C*", chr(0x1e2)))
462 eq ((ord(A) == 193) ? "156 67" : "199 162");
463 print "ok $test\n"; $test++;
465 # 162: does pack U create Unicode?
466 print "not " unless ord(pack('U', 300)) == 300;
467 print "ok $test\n"; $test++;
469 # 163: does unpack U deref Unicode?
470 print "not " unless (unpack('U', chr(300)))[0] == 300;
471 print "ok $test\n"; $test++;
473 # 164: is unpack U the reverse of pack U for Unicode string?
475 unless "@{[unpack('U*', pack('U*', 100, 200, 300))]}" eq "100 200 300";
476 print "ok $test\n"; $test++;
478 # 165: is unpack U the reverse of pack U for byte string?
480 unless "@{[unpack('U*', pack('U*', 100, 200))]}" eq "100 200";
481 print "ok $test\n"; $test++;
483 # 166: does unpack C unravel pack U?
484 print "not " unless "@{[unpack('C*', pack('U*', 100, 200))]}" eq "100 195 136";
485 print "ok $test\n"; $test++;
487 # 167: does pack U0C create Unicode?
488 print "not " unless "@{[pack('U0C*', 100, 195, 136)]}" eq v100.v200;
489 print "ok $test\n"; $test++;
491 # 168: does pack C0U create characters?
492 print "not " unless "@{[pack('C0U*', 100, 200)]}" eq pack("C*", 100, 195, 136);
493 print "ok $test\n"; $test++;
495 # 169: does unpack U0U on byte data warn?
497 local $SIG{__WARN__} = sub { $@ = "@_" };
498 my @null = unpack('U0U', chr(255));
499 print "not " unless $@ =~ /^Malformed UTF-8 character /;
500 print "ok $test\n"; $test++;