Drop all the unnecessary "use utf8" clauses and some of
[p5sagit/p5-mst-13.2.git] / t / op / pack.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     require Config; import Config;
7 }
8
9 print "1..161\n";
10 # Note: All test numbers in comments are off by 1 after the comment below..
11
12 $format = "c2 x5 C C x s d i l a6";
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");
16 $foo = pack($format,@ary);
17 @ary2 = unpack($format,$foo);
18
19 print ($#ary == $#ary2 ? "ok 1\n" : "not ok 1\n");
20
21 $out1=join(':',@ary);
22 $out2=join(':',@ary2);
23 # Using long double NVs may introduce greater accuracy than wanted.
24 $out1 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
25 $out2 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
26 print ($out1 eq $out2? "ok 2\n" : "not ok 2\n");
27
28 print ($foo =~ /def/ ? "ok 3\n" : "not ok 3\n");
29
30 # How about counting bits?
31
32 print +($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")) == 16
33         ? "ok 4\n" : "not ok 4 $x\n";
34
35 print +($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")) == 12
36         ? "ok 5\n" : "not ok 5 $x\n";
37
38 print +($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")) == 9
39         ? "ok 6\n" : "not ok 6 $x\n";
40
41 my $sum = 129; # ASCII
42 $sum = 103 if ($Config{ebcdic} eq 'define');
43
44 print +($x = unpack("%32B*", "Now is the time for all good blurfl")) == $sum
45         ? "ok 7\n" : "not ok 7 $x\n";
46
47 open(BIN, "./perl") || open(BIN, "./perl.exe") || open(BIN, $^X)
48     || die "Can't open ../perl or ../perl.exe: $!\n";
49 sysread BIN, $foo, 8192;
50 close BIN;
51
52 $sum = unpack("%32b*", $foo);
53 $longway = unpack("b*", $foo);
54 print $sum == $longway =~ tr/1/1/ ? "ok 8\n" : "not ok 8\n";
55
56 print +($x = unpack("I",pack("I", 0xFFFFFFFF))) == 0xFFFFFFFF
57         ? "ok 9\n" : "not ok 9 $x\n";
58
59 # check 'w'
60 my $test=10;
61 my @x = (5,130,256,560,32000,3097152,268435455,1073741844, 2**33,
62          '4503599627365785','23728385234614992549757750638446');
63 my $x = pack('w*', @x);
64 my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f8480808014A08080800087ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e';
65
66 if ($x eq $y) {
67   print "ok $test\n";
68 } else {
69   printf "not ok $test # %s\n", unpack 'H*', $x;
70 }
71 $test++;
72
73 @y = unpack('w*', $y);
74 my $a;
75 while ($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 }
79
80 # XXX All test numbers in comments are off by 1 after this point.
81
82 @y = unpack('w2', $x);
83
84 print scalar(@y) == 2 ? "ok $test\n" : "not ok $test\n"; $test++;
85 print $y[1] == 130 ? "ok $test\n" : "not ok $test # $y[1]\n"; $test++;
86
87 # test exeptions
88 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
89 print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
90
91 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
92 print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
93
94 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
95 print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
96
97 #
98 # test the "p" template
99
100 # literals
101 print((unpack("p",pack("p","foo")) eq "foo" ? "ok " : "not ok "),$test++,"\n");
102
103 # scalars
104 print((unpack("p",pack("p",$test)) == $test ? "ok " : "not ok "),$test++,"\n");
105
106 # temps
107 sub foo { my $a = "a"; return $a . $a++ . $a++ }
108 {
109   use warnings;
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
119 print((pack("p", undef) =~ /^\0+/ ? "ok " : "not ok "),$test++,"\n");
120
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:
125 print( ((unpack("i",pack("i",-1))) == -1 ? "ok " : "not ok "),$test++,"\n");
126
127 # 31..36: test the pack lengths of s S i I l L
128 print "not " unless length(pack("s", 0)) == 2;
129 print "ok ", $test++, "\n";
130
131 print "not " unless length(pack("S", 0)) == 2;
132 print "ok ", $test++, "\n";
133
134 print "not " unless length(pack("i", 0)) >= 4;
135 print "ok ", $test++, "\n";
136
137 print "not " unless length(pack("I", 0)) >= 4;
138 print "ok ", $test++, "\n";
139
140 print "not " unless length(pack("l", 0)) == 4;
141 print "ok ", $test++, "\n";
142
143 print "not " unless length(pack("L", 0)) == 4;
144 print "ok ", $test++, "\n";
145
146 # 37..40: test the pack lengths of n N v V
147
148 print "not " unless length(pack("n", 0)) == 2;
149 print "ok ", $test++, "\n";
150
151 print "not " unless length(pack("N", 0)) == 4;
152 print "ok ", $test++, "\n";
153
154 print "not " unless length(pack("v", 0)) == 2;
155 print "ok ", $test++, "\n";
156
157 print "not " unless length(pack("V", 0)) == 4;
158 print "ok ", $test++, "\n";
159
160 # 41..56: test unpack-pack lengths
161
162 my @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...
166 eval { my $q = pack("q",0) };
167 push @templates, $@ !~ /Invalid type in pack/ ? qw(q Q) : qw(f d);
168
169 foreach 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 }
175
176 # 57..60: uuencode/decode
177
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.
182
183 $in = pack 'C*', 0 .. 255;
184
185 # just to be anal, we do some random tr/`/ /
186 $uu = <<'EOUU';
187 M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL
188 M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9
189 M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6&
190 MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S
191 MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@
192 ?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P `
193 EOUU
194
195 $_ = $uu;
196 tr/ /`/;
197 print "not " unless pack('u', $in) eq $_;
198 print "ok ", $test++, "\n";
199
200 print "not " unless unpack('u', $uu) eq $in;
201 print "ok ", $test++, "\n";
202
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';
205 M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
206 &8%P:````
207 EOUU
208
209 print "not " unless unpack('u', $uu) eq $in;
210 print "ok ", $test++, "\n";
211
212 # 60 identical to 59 except that backquotes have been changed to spaces
213
214 $uu = <<'EOUU';
215 M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
216 &8%P:
217 EOUU
218
219 print "not " unless unpack('u', $uu) eq $in;
220 print "ok ", $test++, "\n";
221
222 # 61..73: test the ascii template types (A, a, Z)
223
224 print "not " unless pack('A*', "foo\0bar\0 ") eq "foo\0bar\0 ";
225 print "ok ", $test++, "\n";
226
227 print "not " unless pack('A11', "foo\0bar\0 ") eq "foo\0bar\0   ";
228 print "ok ", $test++, "\n";
229
230 print "not " unless unpack('A*', "foo\0bar \0") eq "foo\0bar";
231 print "ok ", $test++, "\n";
232
233 print "not " unless unpack('A8', "foo\0bar \0") eq "foo\0bar";
234 print "ok ", $test++, "\n";
235
236 print "not " unless pack('a*', "foo\0bar\0 ") eq "foo\0bar\0 ";
237 print "ok ", $test++, "\n";
238
239 print "not " unless pack('a11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0";
240 print "ok ", $test++, "\n";
241
242 print "not " unless unpack('a*', "foo\0bar \0") eq "foo\0bar \0";
243 print "ok ", $test++, "\n";
244
245 print "not " unless unpack('a8', "foo\0bar \0") eq "foo\0bar ";
246 print "ok ", $test++, "\n";
247
248 print "not " unless pack('Z*', "foo\0bar\0 ") eq "foo\0bar\0 \0";
249 print "ok ", $test++, "\n";
250
251 print "not " unless pack('Z11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0";
252 print "ok ", $test++, "\n";
253
254 print "not " unless pack('Z3', "foo") eq "fo\0";
255 print "ok ", $test++, "\n";
256
257 print "not " unless unpack('Z*', "foo\0bar \0") eq "foo";
258 print "ok ", $test++, "\n";
259
260 print "not " unless unpack('Z8', "foo\0bar \0") eq "foo";
261 print "ok ", $test++, "\n";
262
263 # 74..79: packing native shorts/ints/longs
264
265 print "not " unless length(pack("s!", 0)) == $Config{shortsize};
266 print "ok ", $test++, "\n";
267
268 print "not " unless length(pack("i!", 0)) == $Config{intsize};
269 print "ok ", $test++, "\n";
270
271 print "not " unless length(pack("l!", 0)) == $Config{longsize};
272 print "ok ", $test++, "\n";
273
274 print "not " unless length(pack("s!", 0)) <= length(pack("i!", 0));
275 print "ok ", $test++, "\n";
276
277 print "not " unless length(pack("i!", 0)) <= length(pack("l!", 0));
278 print "ok ", $test++, "\n";
279
280 print "not " unless length(pack("i!", 0)) == length(pack("i", 0));
281 print "ok ", $test++, "\n";
282
283 # 80..139: pack <-> unpack bijectionism
284
285 #  80.. 84 c
286 foreach my $c (-128, -1, 0, 1, 127) {
287     print "not " unless unpack("c", pack("c", $c)) == $c;
288     print "ok ", $test++, "\n";
289 }
290
291 #  85.. 89: C
292 foreach my $C (0, 1, 127, 128, 255) {
293     print "not " unless unpack("C", pack("C", $C)) == $C;
294     print "ok ", $test++, "\n";
295 }
296
297 #  90.. 94: s
298 foreach my $s (-32768, -1, 0, 1, 32767) {
299     print "not " unless unpack("s", pack("s", $s)) == $s;
300     print "ok ", $test++, "\n";
301 }
302
303 #  95.. 99: S
304 foreach my $S (0, 1, 32767, 32768, 65535) {
305     print "not " unless unpack("S", pack("S", $S)) == $S;
306     print "ok ", $test++, "\n";
307 }
308
309 #  100..104: i
310 foreach my $i (-2147483648, -1, 0, 1, 2147483647) {
311     print "not " unless unpack("i", pack("i", $i)) == $i;
312     print "ok ", $test++, "\n";
313 }
314
315 # 105..109: I
316 foreach my $I (0, 1, 2147483647, 2147483648, 4294967295) {
317     print "not " unless unpack("I", pack("I", $I)) == $I;
318     print "ok ", $test++, "\n";
319 }
320
321 # 110..114: l
322 foreach my $l (-2147483648, -1, 0, 1, 2147483647) {
323     print "not " unless unpack("l", pack("l", $l)) == $l;
324     print "ok ", $test++, "\n";
325 }
326
327 # 115..119: L
328 foreach my $L (0, 1, 2147483647, 2147483648, 4294967295) {
329     print "not " unless unpack("L", pack("L", $L)) == $L;
330     print "ok ", $test++, "\n";
331 }
332
333 # 120..124: n
334 foreach my $n (0, 1, 32767, 32768, 65535) {
335     print "not " unless unpack("n", pack("n", $n)) == $n;
336     print "ok ", $test++, "\n";
337 }
338
339 # 125..129: v
340 foreach my $v (0, 1, 32767, 32768, 65535) {
341     print "not " unless unpack("v", pack("v", $v)) == $v;
342     print "ok ", $test++, "\n";
343 }
344
345 # 130..134: N
346 foreach my $N (0, 1, 2147483647, 2147483648, 4294967295) {
347     print "not " unless unpack("N", pack("N", $N)) == $N;
348     print "ok ", $test++, "\n";
349 }
350
351 # 135..139: V
352 foreach my $V (0, 1, 2147483647, 2147483648, 4294967295) {
353     print "not " unless unpack("V", pack("V", $V)) == $V;
354     print "ok ", $test++, "\n";
355 }
356
357 # 140..143: pack nvNV byteorders
358
359 print "not " unless pack("n", 0xdead) eq "\xde\xad";
360 print "ok ", $test++, "\n";
361
362 print "not " unless pack("v", 0xdead) eq "\xad\xde";
363 print "ok ", $test++, "\n";
364
365 print "not " unless pack("N", 0xdeadbeef) eq "\xde\xad\xbe\xef";
366 print "ok ", $test++, "\n";
367
368 print "not " unless pack("V", 0xdeadbeef) eq "\xef\xbe\xad\xde";
369 print "ok ", $test++, "\n";
370
371 # 144..152: /
372
373 # Using Test considered bad plan in op/*.t ?
374
375 sub 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
390 my $z;
391 eval { ($x) = unpack '/a*','hello' };
392 print 'not ' unless $@; print "ok $test\n"; $test++;
393 eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
394 print $@ eq '' && $z eq 'ok' ? "ok $test\n" : "not ok $test\n"; $test++;
395 print $@ eq '' && $x eq 'yes' ? "ok $test\n" : "not ok $test\n"; $test++;
396 print $@ eq '' && $y eq 'z' ? "ok $test\n" : "not ok $test\n"; $test++;
397
398 eval { ($x) = pack '/a*','hello' };
399 print 'not ' unless $@; print "ok $test\n"; $test++;
400 $z = pack 'n/a* N/Z* w/A*','string','hi there ','etc';
401 my $expect = "\000\006string\0\0\0\012hi there \000\003etc";
402 report ($z eq $expect, $test++, '', $z);
403
404 eval { ($x) = unpack 'a/a*/a*', '212ab345678901234567' };
405 print $@ eq '' && $x eq 'ab3456789012' ? "ok $test\n" : "#$x,$@\nnot ok $test\n";
406 $test++;
407
408 eval { ($x) = unpack 'a/a*/a*', '3012ab345678901234567' };
409 print $@ eq '' && $x eq 'ab3456789012' ? "ok $test\n" : "not ok $test\n";
410 $test++;
411
412 eval { ($x) = unpack 'a/a*/b*', '212ab' };
413 my $expected_x = '100001100100';
414 if ($Config{ebcdic} eq 'define') { $expected_x = '100000010100'; }
415 print $@ eq '' && $x eq $expected_x ? "ok $test\n" : "#$x,$@\nnot ok $test\n";
416 $test++;
417
418 # 153..156: / with #
419
420 eval { ($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
424 EOU
425 print $@ eq '' && $z eq 'ok' ? "ok $test\n" : "not ok $test\n"; $test++;
426 print $@ eq '' && $x eq 'yes' ? "ok $test\n" : "not ok $test\n"; $test++;
427 print $@ 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
432 EOP
433 $expect = "\000\006string\003etc";
434 report ($z eq $expect, $test++, '', $z);
435
436 print 'not ' unless "1.20.300.4000" eq sprintf "%vd", pack("U*",1,20,300,4000);
437 print "ok $test\n"; $test++;
438 print 'not ' unless "1.20.300.4000" eq
439                     sprintf "%vd", pack("  U*",1,20,300,4000);
440 print "ok $test\n"; $test++;
441 print 'not ' unless v1.20.300.4000 ne
442                     sprintf "%vd", pack("C0U*",1,20,300,4000);
443 print "ok $test\n"; $test++;
444
445 # 160
446 print "not " unless join(" ", unpack("C*", chr(0x1e2)))
447         eq ((ord(A) == 193) ? "156 67" : "199 162");
448 print "ok $test\n"; $test++;