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