Re: the remaining bugs in \x escapes (was Re: [PATCH] oct and hex in glorious 64...
[p5sagit/p5-mst-13.2.git] / t / op / pack.t
CommitLineData
b23b8711 1#!./perl -Tw
a687059c 2
a1a0e61e 3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
b23b8711 6}
7
8use Config;
9
10$Is_EBCDIC = (defined $Config{ebcdic} && $Config{ebcdic} eq 'define');
11
12my $test = 1;
13sub ok {
14 my($ok) = @_;
c274e827 15
16 # You have to do it this way or VMS will get confused.
17 my $out = '';
18 $out = "not " unless $ok;
19 $out .= "ok $test\n";
20 print $out;
21
17639bde 22 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
23
b23b8711 24 $test++;
25 return $ok;
a1a0e61e 26}
27
c274e827 28
2e821511 29print "1..161\n";
b23b8711 30
2e821511 31# Note: All test numbers in comments are off by 1 after the comment below..
a687059c 32
bbdab043 33$format = "c2 x5 C C x s d i l a6";
450a55e4 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");
a687059c 37$foo = pack($format,@ary);
38@ary2 = unpack($format,$foo);
39
b23b8711 40ok($#ary == $#ary2);
a687059c 41
42$out1=join(':',@ary);
43$out2=join(':',@ary2);
ae165b0c 44# Using long double NVs may introduce greater accuracy than wanted.
658bbd66 45$out1 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
c4ab3e64 46$out2 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
b23b8711 47ok($out1 eq $out2);
a687059c 48
b23b8711 49ok($foo =~ /def/);
79072805 50
51# How about counting bits?
52
b23b8711 53ok( ($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")) == 16 );
79072805 54
b23b8711 55ok( ($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")) == 12 );
79072805 56
b23b8711 57ok( ($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")) == 9 );
79072805 58
9d116dd7 59my $sum = 129; # ASCII
b23b8711 60$sum = 103 if $Is_EBCDIC;
9d116dd7 61
b23b8711 62ok( ($x = unpack("%32B*", "Now is the time for all good blurfl")) == $sum );
79072805 63
95e8664e 64open(BIN, "./perl") || open(BIN, "./perl.exe") || open(BIN, $^X)
b8440792 65 || die "Can't open ../perl or ../perl.exe: $!\n";
79072805 66sysread BIN, $foo, 8192;
67close BIN;
68
69$sum = unpack("%32b*", $foo);
70$longway = unpack("b*", $foo);
b23b8711 71ok( $sum == $longway =~ tr/1/1/ );
73a1c01a 72
b23b8711 73ok( ($x = unpack("I",pack("I", 0xFFFFFFFF))) == 0xFFFFFFFF );
def98dd4 74
75# check 'w'
2e821511 76my @x = (5,130,256,560,32000,3097152,268435455,1073741844, 2**33,
55497cff 77 '4503599627365785','23728385234614992549757750638446');
def98dd4 78my $x = pack('w*', @x);
2e821511 79my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f8480808014A08080800087ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e';
def98dd4 80
2e821511 81if ($x eq $y) {
82 print "ok $test\n";
83} else {
84 printf "not ok $test # %s\n", unpack 'H*', $x;
85}
86$test++;
def98dd4 87
88@y = unpack('w*', $y);
55497cff 89my $a;
90while ($a = pop @x) {
91 my $b = pop @y;
92 print $a eq $b ? "ok $test\n" : "not ok $test\n$a\n$b\n"; $test++;
93}
def98dd4 94
2e821511 95# XXX All test numbers in comments are off by 1 after this point.
96
def98dd4 97@y = unpack('w2', $x);
98
99print scalar(@y) == 2 ? "ok $test\n" : "not ok $test\n"; $test++;
2e821511 100print $y[1] == 130 ? "ok $test\n" : "not ok $test # $y[1]\n"; $test++;
def98dd4 101
55497cff 102# test exeptions
def98dd4 103eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
104print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
105
106eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
107print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
108
109eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
110print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
111
84902520 112#
113# test the "p" template
114
115# literals
116print((unpack("p",pack("p","foo")) eq "foo" ? "ok " : "not ok "),$test++,"\n");
117
118# scalars
119print((unpack("p",pack("p",$test)) == $test ? "ok " : "not ok "),$test++,"\n");
120
121# temps
122sub foo { my $a = "a"; return $a . $a++ . $a++ }
123{
9f1b1f2d 124 use warnings;
84902520 125 my $last = $test;
126 local $SIG{__WARN__} = sub {
127 print "ok ",$test++,"\n" if $_[0] =~ /temporary val/
128 };
129 my $junk = pack("p", &foo);
130 print "not ok ", $test++, "\n" if $last == $test;
131}
132
133# undef should give null pointer
134print((pack("p", undef) =~ /^\0+/ ? "ok " : "not ok "),$test++,"\n");
135
20408e3c 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
139# Test 30:
140print( ((unpack("i",pack("i",-1))) == -1 ? "ok " : "not ok "),$test++,"\n");
141
d4217c7e 142# 31..36: test the pack lengths of s S i I l L
143print "not " unless length(pack("s", 0)) == 2;
144print "ok ", $test++, "\n";
c4d5f83a 145
d4217c7e 146print "not " unless length(pack("S", 0)) == 2;
147print "ok ", $test++, "\n";
c4d5f83a 148
d4217c7e 149print "not " unless length(pack("i", 0)) >= 4;
150print "ok ", $test++, "\n";
151
152print "not " unless length(pack("I", 0)) >= 4;
153print "ok ", $test++, "\n";
154
155print "not " unless length(pack("l", 0)) == 4;
156print "ok ", $test++, "\n";
157
158print "not " unless length(pack("L", 0)) == 4;
159print "ok ", $test++, "\n";
160
161# 37..40: test the pack lengths of n N v V
162
163print "not " unless length(pack("n", 0)) == 2;
164print "ok ", $test++, "\n";
165
166print "not " unless length(pack("N", 0)) == 4;
167print "ok ", $test++, "\n";
168
169print "not " unless length(pack("v", 0)) == 2;
170print "ok ", $test++, "\n";
171
172print "not " unless length(pack("V", 0)) == 4;
173print "ok ", $test++, "\n";
174
175# 41..56: test unpack-pack lengths
176
177my @templates = qw(c C i I s S l L n N v V f d);
178
179# quads not supported everywhere: if not, retest floats/doubles
180# to preserve the test count...
181eval { my $q = pack("q",0) };
182push @templates, $@ !~ /Invalid type in pack/ ? qw(q Q) : qw(f d);
183
184foreach my $t (@templates) {
185 my @t = unpack("$t*", pack("$t*", 12, 34));
186 print "not "
187 unless @t == 2 and (($t[0] == 12 and $t[1] == 34) or ($t =~ /[nv]/i));
188 print "ok ", $test++, "\n";
189}
9d116dd7 190
eddc390b 191# 57..60: uuencode/decode
9d116dd7 192
ef54e1a4 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
196# text data.
c4d5f83a 197
342930fb 198$in = pack 'C*', 0 .. 255;
ba1ac976 199
200# just to be anal, we do some random tr/`/ /
9d116dd7 201$uu = <<'EOUU';
ba1ac976 202M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL
9d116dd7 203M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9
204M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6&
205MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S
206MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@
ba1ac976 207?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P `
9d116dd7 208EOUU
209
ba1ac976 210$_ = $uu;
211tr/ /`/;
212print "not " unless pack('u', $in) eq $_;
9d116dd7 213print "ok ", $test++, "\n";
214
215print "not " unless unpack('u', $uu) eq $in;
216print "ok ", $test++, "\n";
217
eddc390b 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";
219$uu = <<'EOUU';
220M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
221&8%P:````
222EOUU
223
224print "not " unless unpack('u', $uu) eq $in;
225print "ok ", $test++, "\n";
226
227# 60 identical to 59 except that backquotes have been changed to spaces
228
229$uu = <<'EOUU';
230M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
c4d5f83a 231&8%P:
eddc390b 232EOUU
233
234print "not " unless unpack('u', $uu) eq $in;
235print "ok ", $test++, "\n";
236
2b6c5635 237# 61..73: test the ascii template types (A, a, Z)
5a929a98 238
239print "not " unless pack('A*', "foo\0bar\0 ") eq "foo\0bar\0 ";
240print "ok ", $test++, "\n";
241
242print "not " unless pack('A11', "foo\0bar\0 ") eq "foo\0bar\0 ";
243print "ok ", $test++, "\n";
244
245print "not " unless unpack('A*', "foo\0bar \0") eq "foo\0bar";
246print "ok ", $test++, "\n";
247
248print "not " unless unpack('A8', "foo\0bar \0") eq "foo\0bar";
249print "ok ", $test++, "\n";
250
251print "not " unless pack('a*', "foo\0bar\0 ") eq "foo\0bar\0 ";
252print "ok ", $test++, "\n";
253
254print "not " unless pack('a11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0";
255print "ok ", $test++, "\n";
256
257print "not " unless unpack('a*', "foo\0bar \0") eq "foo\0bar \0";
258print "ok ", $test++, "\n";
259
260print "not " unless unpack('a8', "foo\0bar \0") eq "foo\0bar ";
261print "ok ", $test++, "\n";
262
2b6c5635 263print "not " unless pack('Z*', "foo\0bar\0 ") eq "foo\0bar\0 \0";
5a929a98 264print "ok ", $test++, "\n";
265
266print "not " unless pack('Z11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0";
267print "ok ", $test++, "\n";
268
2b6c5635 269print "not " unless pack('Z3', "foo") eq "fo\0";
270print "ok ", $test++, "\n";
271
5a929a98 272print "not " unless unpack('Z*', "foo\0bar \0") eq "foo";
273print "ok ", $test++, "\n";
274
275print "not " unless unpack('Z8', "foo\0bar \0") eq "foo";
276print "ok ", $test++, "\n";
277
2b6c5635 278# 74..79: packing native shorts/ints/longs
ef54e1a4 279
f61d411c 280print "not " unless length(pack("s!", 0)) == $Config{shortsize};
ef54e1a4 281print "ok ", $test++, "\n";
282
f61d411c 283print "not " unless length(pack("i!", 0)) == $Config{intsize};
ef54e1a4 284print "ok ", $test++, "\n";
285
f61d411c 286print "not " unless length(pack("l!", 0)) == $Config{longsize};
ef54e1a4 287print "ok ", $test++, "\n";
288
f61d411c 289print "not " unless length(pack("s!", 0)) <= length(pack("i!", 0));
ef54e1a4 290print "ok ", $test++, "\n";
291
f61d411c 292print "not " unless length(pack("i!", 0)) <= length(pack("l!", 0));
ef54e1a4 293print "ok ", $test++, "\n";
294
f61d411c 295print "not " unless length(pack("i!", 0)) == length(pack("i", 0));
ef54e1a4 296print "ok ", $test++, "\n";
297
2b6c5635 298# 80..139: pack <-> unpack bijectionism
726ea183 299
2b6c5635 300# 80.. 84 c
d99ad34e 301foreach my $c (-128, -1, 0, 1, 127) {
302 print "not " unless unpack("c", pack("c", $c)) == $c;
303 print "ok ", $test++, "\n";
304}
9de70c85 305
2b6c5635 306# 85.. 89: C
d99ad34e 307foreach my $C (0, 1, 127, 128, 255) {
308 print "not " unless unpack("C", pack("C", $C)) == $C;
309 print "ok ", $test++, "\n";
310}
726ea183 311
2b6c5635 312# 90.. 94: s
d99ad34e 313foreach my $s (-32768, -1, 0, 1, 32767) {
314 print "not " unless unpack("s", pack("s", $s)) == $s;
315 print "ok ", $test++, "\n";
316}
726ea183 317
2b6c5635 318# 95.. 99: S
d99ad34e 319foreach my $S (0, 1, 32767, 32768, 65535) {
320 print "not " unless unpack("S", pack("S", $S)) == $S;
321 print "ok ", $test++, "\n";
322}
726ea183 323
2b6c5635 324# 100..104: i
d99ad34e 325foreach my $i (-2147483648, -1, 0, 1, 2147483647) {
326 print "not " unless unpack("i", pack("i", $i)) == $i;
327 print "ok ", $test++, "\n";
328}
726ea183 329
2b6c5635 330# 105..109: I
d99ad34e 331foreach my $I (0, 1, 2147483647, 2147483648, 4294967295) {
332 print "not " unless unpack("I", pack("I", $I)) == $I;
333 print "ok ", $test++, "\n";
334}
726ea183 335
2b6c5635 336# 110..114: l
d99ad34e 337foreach my $l (-2147483648, -1, 0, 1, 2147483647) {
338 print "not " unless unpack("l", pack("l", $l)) == $l;
339 print "ok ", $test++, "\n";
340}
726ea183 341
2b6c5635 342# 115..119: L
d99ad34e 343foreach my $L (0, 1, 2147483647, 2147483648, 4294967295) {
344 print "not " unless unpack("L", pack("L", $L)) == $L;
345 print "ok ", $test++, "\n";
346}
726ea183 347
2b6c5635 348# 120..124: n
d99ad34e 349foreach my $n (0, 1, 32767, 32768, 65535) {
350 print "not " unless unpack("n", pack("n", $n)) == $n;
351 print "ok ", $test++, "\n";
352}
726ea183 353
2b6c5635 354# 125..129: v
d99ad34e 355foreach my $v (0, 1, 32767, 32768, 65535) {
356 print "not " unless unpack("v", pack("v", $v)) == $v;
357 print "ok ", $test++, "\n";
358}
726ea183 359
2b6c5635 360# 130..134: N
d99ad34e 361foreach my $N (0, 1, 2147483647, 2147483648, 4294967295) {
362 print "not " unless unpack("N", pack("N", $N)) == $N;
363 print "ok ", $test++, "\n";
364}
726ea183 365
2b6c5635 366# 135..139: V
d99ad34e 367foreach my $V (0, 1, 2147483647, 2147483648, 4294967295) {
368 print "not " unless unpack("V", pack("V", $V)) == $V;
369 print "ok ", $test++, "\n";
370}
726ea183 371
2b6c5635 372# 140..143: pack nvNV byteorders
726ea183 373
d99ad34e 374print "not " unless pack("n", 0xdead) eq "\xde\xad";
726ea183 375print "ok ", $test++, "\n";
376
d99ad34e 377print "not " unless pack("v", 0xdead) eq "\xad\xde";
c67712b2 378print "ok ", $test++, "\n";
726ea183 379
d99ad34e 380print "not " unless pack("N", 0xdeadbeef) eq "\xde\xad\xbe\xef";
726ea183 381print "ok ", $test++, "\n";
382
d99ad34e 383print "not " unless pack("V", 0xdeadbeef) eq "\xef\xbe\xad\xde";
c67712b2 384print "ok ", $test++, "\n";
43192e07 385
4b5b2118 386# 144..152: /
43192e07 387
2e821511 388# Using Test considered bad plan in op/*.t ?
389
390sub report {
391 my ($pass, $test, $err, $wrong) = @_;
392 if ($pass) {
393 print "ok $test\n"
394 } else {
395 if ($err) {
396 chomp $err;
397 print "not ok $test # \$\@ = $err\n";
398 } else {
399 $wrong =~ s/([[:cntrl:]\177 ])/sprintf "\\%03o", ord $1/ge;
400 print "not ok $test # got $wrong\n";
401 }
402 }
403}
404
43192e07 405my $z;
17f4a12d 406eval { ($x) = unpack '/a*','hello' };
43192e07 407print 'not ' unless $@; print "ok $test\n"; $test++;
17f4a12d 408eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
43192e07 409print $@ eq '' && $z eq 'ok' ? "ok $test\n" : "not ok $test\n"; $test++;
410print $@ eq '' && $x eq 'yes' ? "ok $test\n" : "not ok $test\n"; $test++;
411print $@ eq '' && $y eq 'z' ? "ok $test\n" : "not ok $test\n"; $test++;
412
17f4a12d 413eval { ($x) = pack '/a*','hello' };
43192e07 414print 'not ' unless $@; print "ok $test\n"; $test++;
3399f041 415$z = pack 'n/a* N/Z* w/A*','string','hi there ','etc';
2e821511 416my $expect = "\000\006string\0\0\0\012hi there \000\003etc";
417report ($z eq $expect, $test++, '', $z);
43192e07 418
4b5b2118 419eval { ($x) = unpack 'a/a*/a*', '212ab345678901234567' };
420print $@ eq '' && $x eq 'ab3456789012' ? "ok $test\n" : "#$x,$@\nnot ok $test\n";
421$test++;
422
423eval { ($x) = unpack 'a/a*/a*', '3012ab345678901234567' };
424print $@ eq '' && $x eq 'ab3456789012' ? "ok $test\n" : "not ok $test\n";
425$test++;
426
427eval { ($x) = unpack 'a/a*/b*', '212ab' };
ee50adbe 428my $expected_x = '100001100100';
b23b8711 429if ($Is_EBCDIC) { $expected_x = '100000010100'; }
ee50adbe 430print $@ eq '' && $x eq $expected_x ? "ok $test\n" : "#$x,$@\nnot ok $test\n";
4b5b2118 431$test++;
432
433# 153..156: / with #
17f4a12d 434
435eval { ($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
439EOU
440print $@ eq '' && $z eq 'ok' ? "ok $test\n" : "not ok $test\n"; $test++;
441print $@ eq '' && $x eq 'yes' ? "ok $test\n" : "not ok $test\n"; $test++;
442print $@ eq '' && $y eq 'z' ? "ok $test\n" : "not ok $test\n"; $test++;
443
444$z = pack <<EOP,'string','etc';
445 n/a* # Count as network short
446 w/A* # Count a BER integer
447EOP
2e821511 448$expect = "\000\006string\003etc";
449report ($z eq $expect, $test++, '', $z);
036b4402 450
c4d5f83a 451print 'not ' unless "1.20.300.4000" eq sprintf "%vd", pack("U*",1,20,300,4000);
036b4402 452print "ok $test\n"; $test++;
c4d5f83a 453print 'not ' unless "1.20.300.4000" eq
454 sprintf "%vd", pack(" U*",1,20,300,4000);
036b4402 455print "ok $test\n"; $test++;
c4d5f83a 456print 'not ' unless v1.20.300.4000 ne
457 sprintf "%vd", pack("C0U*",1,20,300,4000);
036b4402 458print "ok $test\n"; $test++;
459
4765795a 460# 160
c4d5f83a 461print "not " unless join(" ", unpack("C*", chr(0x1e2)))
462 eq ((ord(A) == 193) ? "156 67" : "199 162");
4765795a 463print "ok $test\n"; $test++;