5.005_03-MAINT_TRIAL_5 utils/h2xs fixing -A & more
[p5sagit/p5-mst-13.2.git] / t / op / pack.t
CommitLineData
a687059c 1#!./perl
2
79072805 3# $RCSfile: pack.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:11 $
a687059c 4
a1a0e61e 5BEGIN {
6 chdir 't' if -d 't';
93430cb4 7 unshift @INC, '../lib' if -d '../lib';
a1a0e61e 8 require Config; import Config;
9}
10
5a929a98 11print "1..72\n";
a687059c 12
bbdab043 13$format = "c2 x5 C C x s d i l a6";
450a55e4 14# Need the expression in here to force ary[5] to be numeric. This avoids
15# test2 failing because ary2 goes str->numeric->str and ary doesn't.
16@ary = (1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,"abcdef");
a687059c 17$foo = pack($format,@ary);
18@ary2 = unpack($format,$foo);
19
20print ($#ary == $#ary2 ? "ok 1\n" : "not ok 1\n");
21
22$out1=join(':',@ary);
23$out2=join(':',@ary2);
24print ($out1 eq $out2 ? "ok 2\n" : "not ok 2\n");
25
26print ($foo =~ /def/ ? "ok 3\n" : "not ok 3\n");
79072805 27
28# How about counting bits?
29
30print +($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")) == 16
31 ? "ok 4\n" : "not ok 4 $x\n";
32
33print +($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")) == 12
34 ? "ok 5\n" : "not ok 5 $x\n";
35
36print +($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")) == 9
37 ? "ok 6\n" : "not ok 6 $x\n";
38
9d116dd7 39my $sum = 129; # ASCII
a1a0e61e 40$sum = 103 if ($Config{ebcdic} eq 'define');
9d116dd7 41
42print +($x = unpack("%32B*", "Now is the time for all good blurfl")) == $sum
79072805 43 ? "ok 7\n" : "not ok 7 $x\n";
44
b8440792 45open(BIN, "./perl") || open(BIN, "./perl.exe")
46 || die "Can't open ../perl or ../perl.exe: $!\n";
79072805 47sysread BIN, $foo, 8192;
48close BIN;
49
50$sum = unpack("%32b*", $foo);
51$longway = unpack("b*", $foo);
52print $sum == $longway =~ tr/1/1/ ? "ok 8\n" : "not ok 8\n";
73a1c01a 53
54print +($x = unpack("I",pack("I", 0xFFFFFFFF))) == 0xFFFFFFFF
55 ? "ok 9\n" : "not ok 9 $x\n";
def98dd4 56
57# check 'w'
58my $test=10;
55497cff 59my @x = (5,130,256,560,32000,3097152,268435455,1073741844,
60 '4503599627365785','23728385234614992549757750638446');
def98dd4 61my $x = pack('w*', @x);
55497cff 62my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f848080801487ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e';
def98dd4 63
64print $x eq $y ? "ok $test\n" : "not ok $test\n"; $test++;
65
66@y = unpack('w*', $y);
55497cff 67my $a;
68while ($a = pop @x) {
69 my $b = pop @y;
70 print $a eq $b ? "ok $test\n" : "not ok $test\n$a\n$b\n"; $test++;
71}
def98dd4 72
73@y = unpack('w2', $x);
74
75print scalar(@y) == 2 ? "ok $test\n" : "not ok $test\n"; $test++;
76print $y[1] == 130 ? "ok $test\n" : "not ok $test\n"; $test++;
77
55497cff 78# test exeptions
def98dd4 79eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
80print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
81
82eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
83print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
84
85eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
86print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
87
84902520 88#
89# test the "p" template
90
91# literals
92print((unpack("p",pack("p","foo")) eq "foo" ? "ok " : "not ok "),$test++,"\n");
93
94# scalars
95print((unpack("p",pack("p",$test)) == $test ? "ok " : "not ok "),$test++,"\n");
96
97# temps
98sub foo { my $a = "a"; return $a . $a++ . $a++ }
99{
100 local $^W = 1;
101 my $last = $test;
102 local $SIG{__WARN__} = sub {
103 print "ok ",$test++,"\n" if $_[0] =~ /temporary val/
104 };
105 my $junk = pack("p", &foo);
106 print "not ok ", $test++, "\n" if $last == $test;
107}
108
109# undef should give null pointer
110print((pack("p", undef) =~ /^\0+/ ? "ok " : "not ok "),$test++,"\n");
111
20408e3c 112# Check for optimizer bug (e.g. Digital Unix GEM cc with -O4 on DU V4.0B gives
113# 4294967295 instead of -1)
114# see #ifdef __osf__ in pp.c pp_unpack
115# Test 30:
116print( ((unpack("i",pack("i",-1))) == -1 ? "ok " : "not ok "),$test++,"\n");
117
d4217c7e 118# 31..36: test the pack lengths of s S i I l L
119print "not " unless length(pack("s", 0)) == 2;
120print "ok ", $test++, "\n";
121
122print "not " unless length(pack("S", 0)) == 2;
123print "ok ", $test++, "\n";
124
125print "not " unless length(pack("i", 0)) >= 4;
126print "ok ", $test++, "\n";
127
128print "not " unless length(pack("I", 0)) >= 4;
129print "ok ", $test++, "\n";
130
131print "not " unless length(pack("l", 0)) == 4;
132print "ok ", $test++, "\n";
133
134print "not " unless length(pack("L", 0)) == 4;
135print "ok ", $test++, "\n";
136
137# 37..40: test the pack lengths of n N v V
138
139print "not " unless length(pack("n", 0)) == 2;
140print "ok ", $test++, "\n";
141
142print "not " unless length(pack("N", 0)) == 4;
143print "ok ", $test++, "\n";
144
145print "not " unless length(pack("v", 0)) == 2;
146print "ok ", $test++, "\n";
147
148print "not " unless length(pack("V", 0)) == 4;
149print "ok ", $test++, "\n";
150
151# 41..56: test unpack-pack lengths
152
153my @templates = qw(c C i I s S l L n N v V f d);
154
155# quads not supported everywhere: if not, retest floats/doubles
156# to preserve the test count...
157eval { my $q = pack("q",0) };
158push @templates, $@ !~ /Invalid type in pack/ ? qw(q Q) : qw(f d);
159
160foreach my $t (@templates) {
161 my @t = unpack("$t*", pack("$t*", 12, 34));
162 print "not "
163 unless @t == 2 and (($t[0] == 12 and $t[1] == 34) or ($t =~ /[nv]/i));
164 print "ok ", $test++, "\n";
165}
9d116dd7 166
eddc390b 167# 57..60: uuencode/decode
9d116dd7 168
342930fb 169$in = pack 'C*', 0 .. 255;
ba1ac976 170
171# just to be anal, we do some random tr/`/ /
9d116dd7 172$uu = <<'EOUU';
ba1ac976 173M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL
9d116dd7 174M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9
175M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6&
176MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S
177MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@
ba1ac976 178?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P `
9d116dd7 179EOUU
180
ba1ac976 181$_ = $uu;
182tr/ /`/;
183print "not " unless pack('u', $in) eq $_;
9d116dd7 184print "ok ", $test++, "\n";
185
186print "not " unless unpack('u', $uu) eq $in;
187print "ok ", $test++, "\n";
188
eddc390b 189$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";
190$uu = <<'EOUU';
191M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
192&8%P:````
193EOUU
194
195print "not " unless unpack('u', $uu) eq $in;
196print "ok ", $test++, "\n";
197
198# 60 identical to 59 except that backquotes have been changed to spaces
199
200$uu = <<'EOUU';
201M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
202&8%P:
203EOUU
204
205print "not " unless unpack('u', $uu) eq $in;
206print "ok ", $test++, "\n";
207
208# Note that first uuencoding known 'text' data and then checking the
209# binary values of the uuencoded version would not be portable between
210# character sets. Uuencoding is meant for encoding binary data, not
211# text data.
5a929a98 212
213# test the ascii template types (A, a, Z)
214
215print "not " unless pack('A*', "foo\0bar\0 ") eq "foo\0bar\0 ";
216print "ok ", $test++, "\n";
217
218print "not " unless pack('A11', "foo\0bar\0 ") eq "foo\0bar\0 ";
219print "ok ", $test++, "\n";
220
221print "not " unless unpack('A*', "foo\0bar \0") eq "foo\0bar";
222print "ok ", $test++, "\n";
223
224print "not " unless unpack('A8', "foo\0bar \0") eq "foo\0bar";
225print "ok ", $test++, "\n";
226
227print "not " unless pack('a*', "foo\0bar\0 ") eq "foo\0bar\0 ";
228print "ok ", $test++, "\n";
229
230print "not " unless pack('a11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0";
231print "ok ", $test++, "\n";
232
233print "not " unless unpack('a*', "foo\0bar \0") eq "foo\0bar \0";
234print "ok ", $test++, "\n";
235
236print "not " unless unpack('a8', "foo\0bar \0") eq "foo\0bar ";
237print "ok ", $test++, "\n";
238
239print "not " unless pack('Z*', "foo\0bar\0 ") eq "foo\0bar\0 ";
240print "ok ", $test++, "\n";
241
242print "not " unless pack('Z11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0";
243print "ok ", $test++, "\n";
244
245print "not " unless unpack('Z*', "foo\0bar \0") eq "foo";
246print "ok ", $test++, "\n";
247
248print "not " unless unpack('Z8', "foo\0bar \0") eq "foo";
249print "ok ", $test++, "\n";
250