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