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