5 unshift @INC, '../lib' if -d '../lib';
6 require Config; import Config;
11 $format = "c2 x5 C C x s d i l a6";
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");
15 $foo = pack($format,@ary);
16 @ary2 = unpack($format,$foo);
18 print ($#ary == $#ary2 ? "ok 1\n" : "not ok 1\n");
21 $out2=join(':',@ary2);
22 print ($out1 eq $out2 ? "ok 2\n" : "not ok 2\n");
24 print ($foo =~ /def/ ? "ok 3\n" : "not ok 3\n");
26 # How about counting bits?
28 print +($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")) == 16
29 ? "ok 4\n" : "not ok 4 $x\n";
31 print +($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")) == 12
32 ? "ok 5\n" : "not ok 5 $x\n";
34 print +($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")) == 9
35 ? "ok 6\n" : "not ok 6 $x\n";
37 my $sum = 129; # ASCII
38 $sum = 103 if ($Config{ebcdic} eq 'define');
40 print +($x = unpack("%32B*", "Now is the time for all good blurfl")) == $sum
41 ? "ok 7\n" : "not ok 7 $x\n";
43 open(BIN, "./perl") || open(BIN, "./perl.exe")
44 || die "Can't open ../perl or ../perl.exe: $!\n";
45 sysread BIN, $foo, 8192;
48 $sum = unpack("%32b*", $foo);
49 $longway = unpack("b*", $foo);
50 print $sum == $longway =~ tr/1/1/ ? "ok 8\n" : "not ok 8\n";
52 print +($x = unpack("I",pack("I", 0xFFFFFFFF))) == 0xFFFFFFFF
53 ? "ok 9\n" : "not ok 9 $x\n";
57 my @x = (5,130,256,560,32000,3097152,268435455,1073741844,
58 '4503599627365785','23728385234614992549757750638446');
59 my $x = pack('w*', @x);
60 my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f848080801487ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e';
62 print $x eq $y ? "ok $test\n" : "not ok $test\n"; $test++;
64 @y = unpack('w*', $y);
68 print $a eq $b ? "ok $test\n" : "not ok $test\n$a\n$b\n"; $test++;
71 @y = unpack('w2', $x);
73 print scalar(@y) == 2 ? "ok $test\n" : "not ok $test\n"; $test++;
74 print $y[1] == 130 ? "ok $test\n" : "not ok $test\n"; $test++;
77 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
78 print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
80 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
81 print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
83 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
84 print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
87 # test the "p" template
90 print((unpack("p",pack("p","foo")) eq "foo" ? "ok " : "not ok "),$test++,"\n");
93 print((unpack("p",pack("p",$test)) == $test ? "ok " : "not ok "),$test++,"\n");
96 sub foo { my $a = "a"; return $a . $a++ . $a++ }
100 local $SIG{__WARN__} = sub {
101 print "ok ",$test++,"\n" if $_[0] =~ /temporary val/
103 my $junk = pack("p", &foo);
104 print "not ok ", $test++, "\n" if $last == $test;
107 # undef should give null pointer
108 print((pack("p", undef) =~ /^\0+/ ? "ok " : "not ok "),$test++,"\n");
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
114 print( ((unpack("i",pack("i",-1))) == -1 ? "ok " : "not ok "),$test++,"\n");
116 # 31..36: test the pack lengths of s S i I l L
117 print "not " unless length(pack("s", 0)) == 2;
118 print "ok ", $test++, "\n";
120 print "not " unless length(pack("S", 0)) == 2;
121 print "ok ", $test++, "\n";
123 print "not " unless length(pack("i", 0)) >= 4;
124 print "ok ", $test++, "\n";
126 print "not " unless length(pack("I", 0)) >= 4;
127 print "ok ", $test++, "\n";
129 print "not " unless length(pack("l", 0)) == 4;
130 print "ok ", $test++, "\n";
132 print "not " unless length(pack("L", 0)) == 4;
133 print "ok ", $test++, "\n";
135 # 37..40: test the pack lengths of n N v V
137 print "not " unless length(pack("n", 0)) == 2;
138 print "ok ", $test++, "\n";
140 print "not " unless length(pack("N", 0)) == 4;
141 print "ok ", $test++, "\n";
143 print "not " unless length(pack("v", 0)) == 2;
144 print "ok ", $test++, "\n";
146 print "not " unless length(pack("V", 0)) == 4;
147 print "ok ", $test++, "\n";
149 # 41..56: test unpack-pack lengths
151 my @templates = qw(c C i I s S l L n N v V f d);
153 # quads not supported everywhere: if not, retest floats/doubles
154 # to preserve the test count...
155 eval { my $q = pack("q",0) };
156 push @templates, $@ !~ /Invalid type in pack/ ? qw(q Q) : qw(f d);
158 foreach my $t (@templates) {
159 my @t = unpack("$t*", pack("$t*", 12, 34));
161 unless @t == 2 and (($t[0] == 12 and $t[1] == 34) or ($t =~ /[nv]/i));
162 print "ok ", $test++, "\n";
165 # 57..60: uuencode/decode
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
172 $in = pack 'C*', 0 .. 255;
174 # just to be anal, we do some random tr/`/ /
176 M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL
177 M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9
178 M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6&
179 MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S
180 MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@
181 ?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P `
186 print "not " unless pack('u', $in) eq $_;
187 print "ok ", $test++, "\n";
189 print "not " unless unpack('u', $uu) eq $in;
190 print "ok ", $test++, "\n";
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";
194 M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
198 print "not " unless unpack('u', $uu) eq $in;
199 print "ok ", $test++, "\n";
201 # 60 identical to 59 except that backquotes have been changed to spaces
204 M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
208 print "not " unless unpack('u', $uu) eq $in;
209 print "ok ", $test++, "\n";
211 # 61..72: test the ascii template types (A, a, Z)
213 print "not " unless pack('A*', "foo\0bar\0 ") eq "foo\0bar\0 ";
214 print "ok ", $test++, "\n";
216 print "not " unless pack('A11', "foo\0bar\0 ") eq "foo\0bar\0 ";
217 print "ok ", $test++, "\n";
219 print "not " unless unpack('A*', "foo\0bar \0") eq "foo\0bar";
220 print "ok ", $test++, "\n";
222 print "not " unless unpack('A8', "foo\0bar \0") eq "foo\0bar";
223 print "ok ", $test++, "\n";
225 print "not " unless pack('a*', "foo\0bar\0 ") eq "foo\0bar\0 ";
226 print "ok ", $test++, "\n";
228 print "not " unless pack('a11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0";
229 print "ok ", $test++, "\n";
231 print "not " unless unpack('a*', "foo\0bar \0") eq "foo\0bar \0";
232 print "ok ", $test++, "\n";
234 print "not " unless unpack('a8', "foo\0bar \0") eq "foo\0bar ";
235 print "ok ", $test++, "\n";
237 print "not " unless pack('Z*', "foo\0bar\0 ") eq "foo\0bar\0 ";
238 print "ok ", $test++, "\n";
240 print "not " unless pack('Z11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0";
241 print "ok ", $test++, "\n";
243 print "not " unless unpack('Z*', "foo\0bar \0") eq "foo";
244 print "ok ", $test++, "\n";
246 print "not " unless unpack('Z8', "foo\0bar \0") eq "foo";
247 print "ok ", $test++, "\n";
249 # 73..78: packing native shorts/ints/longs
251 print "not " unless length(pack("s_", 0)) == $Config{shortsize};
252 print "ok ", $test++, "\n";
254 print "not " unless length(pack("i_", 0)) == $Config{intsize};
255 print "ok ", $test++, "\n";
257 print "not " unless length(pack("l_", 0)) == $Config{longsize};
258 print "ok ", $test++, "\n";
260 print "not " unless length(pack("s_", 0)) <= length(pack("i_", 0));
261 print "ok ", $test++, "\n";
263 print "not " unless length(pack("i_", 0)) <= length(pack("l_", 0));
264 print "ok ", $test++, "\n";
266 print "not " unless length(pack("i_", 0)) == length(pack("i", 0));
267 print "ok ", $test++, "\n";
269 # 79..94: test the limits
271 # Some possibilities for $Config{byteorder} and $Config{.*size}.
272 # Note that CPUs can feature at several places.
278 # 1234 x86, vax, (DEC) mips
280 # 4321 sparc, ppc, hppa, (IRIX) mips, motorola
281 # 87654321 sparc, mips, hppa, cray
283 # x86 alpha sparc cray
289 print "not " unless unpack("c", pack("c", 127)) == 127;
290 print "ok ", $test++, "\n";
292 print "not " unless unpack("c", pack("c", -128)) == -128;
293 print "ok ", $test++, "\n";
295 print "not " unless unpack("C", pack("C", 255)) == 255;
296 print "ok ", $test++, "\n";
298 print "not " unless unpack("s", pack("s", 32767)) == 32767;
299 print "ok ", $test++, "\n";
301 print "not " unless unpack("s", pack("s", -32768)) == -32768;
302 print "ok ", $test++, "\n";
304 print "not " unless unpack("S", pack("S", 65535)) == 65535;
305 print "ok ", $test++, "\n";
307 print "not " unless unpack("i", pack("i", 2147483647)) == 2147483647;
308 print "ok ", $test++, "\n";
310 print "not " unless unpack("i", pack("i", -2147483648)) == -2147483648;
311 print "ok ", $test++, "\n";
313 print "not " unless unpack("I", pack("I", 4294967295)) == 4294967295;
314 print "ok ", $test++, "\n";
316 print "not " unless unpack("l", pack("l", 2147483647)) == 2147483647;
317 print "ok ", $test++, "\n";
319 print "not " unless unpack("l", pack("l", -2147483648)) == -2147483648;
320 print "ok ", $test++, "\n";
322 print "not " unless unpack("L", pack("L", 4294967295)) == 4294967295;
323 print "ok ", $test++, "\n";
325 print "not " unless unpack("n", pack("n", 65535)) == 65535;
326 print "ok ", $test++, "\n";
328 print "not " unless unpack("v", pack("v", 65535)) == 65535;
329 print "ok ", $test++, "\n";
331 print "not " unless unpack("N", pack("N", 4294967295)) == 4294967295;
332 print "ok ", $test++, "\n";
334 print "not " unless unpack("V", pack("V", 4294967295)) == 4294967295;
335 print "ok ", $test++, "\n";
337 # 95..98 test the n/v/N/V byteorder
339 if ($Config{byteorder} =~ /^1234(5678)?$/ ||
340 $Config{byteorder} =~ /^(8765)?4321$/) {
342 print "not " unless pack("n", 0xdead) eq "\xde\xad";
343 print "ok ", $test++, "\n";
345 print "not " unless pack("v", 0xdead) eq "\xad\xde";
346 print "ok ", $test++, "\n";
348 print "not " unless pack("N", 0xdeadbeef) eq "\xde\xad\xbe\xef";
349 print "ok ", $test++, "\n";
351 print "not " unless pack("V", 0xdeadbeef) eq "\xef\xbe\xad\xde";
352 print "ok ", $test++, "\n";
354 # exotic byteorder systems require more thought
356 print "ok ", $test++, " # skipped\n";