10 $Is_EBCDIC = (defined $Config{ebcdic} && $Config{ebcdic} eq 'define');
15 print "not " unless $ok;
23 # Note: All test numbers in comments are off by 1 after the comment below..
25 $format = "c2 x5 C C x s d i l a6";
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");
29 $foo = pack($format,@ary);
30 @ary2 = unpack($format,$foo);
35 $out2=join(':',@ary2);
36 # Using long double NVs may introduce greater accuracy than wanted.
37 $out1 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
38 $out2 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
43 # How about counting bits?
45 ok( ($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")) == 16 );
47 ok( ($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")) == 12 );
49 ok( ($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")) == 9 );
51 my $sum = 129; # ASCII
52 $sum = 103 if $Is_EBCDIC;
54 ok( ($x = unpack("%32B*", "Now is the time for all good blurfl")) == $sum );
56 open(BIN, "./perl") || open(BIN, "./perl.exe") || open(BIN, $^X)
57 || die "Can't open ../perl or ../perl.exe: $!\n";
58 sysread BIN, $foo, 8192;
61 $sum = unpack("%32b*", $foo);
62 $longway = unpack("b*", $foo);
63 ok( $sum == $longway =~ tr/1/1/ );
65 ok( ($x = unpack("I",pack("I", 0xFFFFFFFF))) == 0xFFFFFFFF );
68 my @x = (5,130,256,560,32000,3097152,268435455,1073741844, 2**33,
69 '4503599627365785','23728385234614992549757750638446');
70 my $x = pack('w*', @x);
71 my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f8480808014A08080800087ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e';
76 printf "not ok $test # %s\n", unpack 'H*', $x;
80 @y = unpack('w*', $y);
84 print $a eq $b ? "ok $test\n" : "not ok $test\n$a\n$b\n"; $test++;
87 # XXX All test numbers in comments are off by 1 after this point.
89 @y = unpack('w2', $x);
91 print scalar(@y) == 2 ? "ok $test\n" : "not ok $test\n"; $test++;
92 print $y[1] == 130 ? "ok $test\n" : "not ok $test # $y[1]\n"; $test++;
95 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
96 print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
98 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
99 print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
101 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
102 print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++;
105 # test the "p" template
108 print((unpack("p",pack("p","foo")) eq "foo" ? "ok " : "not ok "),$test++,"\n");
111 print((unpack("p",pack("p",$test)) == $test ? "ok " : "not ok "),$test++,"\n");
114 sub foo { my $a = "a"; return $a . $a++ . $a++ }
118 local $SIG{__WARN__} = sub {
119 print "ok ",$test++,"\n" if $_[0] =~ /temporary val/
121 my $junk = pack("p", &foo);
122 print "not ok ", $test++, "\n" if $last == $test;
125 # undef should give null pointer
126 print((pack("p", undef) =~ /^\0+/ ? "ok " : "not ok "),$test++,"\n");
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
132 print( ((unpack("i",pack("i",-1))) == -1 ? "ok " : "not ok "),$test++,"\n");
134 # 31..36: test the pack lengths of s S i I l L
135 print "not " unless length(pack("s", 0)) == 2;
136 print "ok ", $test++, "\n";
138 print "not " unless length(pack("S", 0)) == 2;
139 print "ok ", $test++, "\n";
141 print "not " unless length(pack("i", 0)) >= 4;
142 print "ok ", $test++, "\n";
144 print "not " unless length(pack("I", 0)) >= 4;
145 print "ok ", $test++, "\n";
147 print "not " unless length(pack("l", 0)) == 4;
148 print "ok ", $test++, "\n";
150 print "not " unless length(pack("L", 0)) == 4;
151 print "ok ", $test++, "\n";
153 # 37..40: test the pack lengths of n N v V
155 print "not " unless length(pack("n", 0)) == 2;
156 print "ok ", $test++, "\n";
158 print "not " unless length(pack("N", 0)) == 4;
159 print "ok ", $test++, "\n";
161 print "not " unless length(pack("v", 0)) == 2;
162 print "ok ", $test++, "\n";
164 print "not " unless length(pack("V", 0)) == 4;
165 print "ok ", $test++, "\n";
167 # 41..56: test unpack-pack lengths
169 my @templates = qw(c C i I s S l L n N v V f d);
171 # quads not supported everywhere: if not, retest floats/doubles
172 # to preserve the test count...
173 eval { my $q = pack("q",0) };
174 push @templates, $@ !~ /Invalid type in pack/ ? qw(q Q) : qw(f d);
176 foreach my $t (@templates) {
177 my @t = unpack("$t*", pack("$t*", 12, 34));
179 unless @t == 2 and (($t[0] == 12 and $t[1] == 34) or ($t =~ /[nv]/i));
180 print "ok ", $test++, "\n";
183 # 57..60: uuencode/decode
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
190 $in = pack 'C*', 0 .. 255;
192 # just to be anal, we do some random tr/`/ /
194 M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL
195 M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9
196 M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6&
197 MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S
198 MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@
199 ?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P `
204 print "not " unless pack('u', $in) eq $_;
205 print "ok ", $test++, "\n";
207 print "not " unless unpack('u', $uu) eq $in;
208 print "ok ", $test++, "\n";
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";
212 M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
216 print "not " unless unpack('u', $uu) eq $in;
217 print "ok ", $test++, "\n";
219 # 60 identical to 59 except that backquotes have been changed to spaces
222 M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
226 print "not " unless unpack('u', $uu) eq $in;
227 print "ok ", $test++, "\n";
229 # 61..73: test the ascii template types (A, a, Z)
231 print "not " unless pack('A*', "foo\0bar\0 ") eq "foo\0bar\0 ";
232 print "ok ", $test++, "\n";
234 print "not " unless pack('A11', "foo\0bar\0 ") eq "foo\0bar\0 ";
235 print "ok ", $test++, "\n";
237 print "not " unless unpack('A*', "foo\0bar \0") eq "foo\0bar";
238 print "ok ", $test++, "\n";
240 print "not " unless unpack('A8', "foo\0bar \0") eq "foo\0bar";
241 print "ok ", $test++, "\n";
243 print "not " unless pack('a*', "foo\0bar\0 ") eq "foo\0bar\0 ";
244 print "ok ", $test++, "\n";
246 print "not " unless pack('a11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0";
247 print "ok ", $test++, "\n";
249 print "not " unless unpack('a*', "foo\0bar \0") eq "foo\0bar \0";
250 print "ok ", $test++, "\n";
252 print "not " unless unpack('a8', "foo\0bar \0") eq "foo\0bar ";
253 print "ok ", $test++, "\n";
255 print "not " unless pack('Z*', "foo\0bar\0 ") eq "foo\0bar\0 \0";
256 print "ok ", $test++, "\n";
258 print "not " unless pack('Z11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0";
259 print "ok ", $test++, "\n";
261 print "not " unless pack('Z3', "foo") eq "fo\0";
262 print "ok ", $test++, "\n";
264 print "not " unless unpack('Z*', "foo\0bar \0") eq "foo";
265 print "ok ", $test++, "\n";
267 print "not " unless unpack('Z8', "foo\0bar \0") eq "foo";
268 print "ok ", $test++, "\n";
270 # 74..79: packing native shorts/ints/longs
272 print "not " unless length(pack("s!", 0)) == $Config{shortsize};
273 print "ok ", $test++, "\n";
275 print "not " unless length(pack("i!", 0)) == $Config{intsize};
276 print "ok ", $test++, "\n";
278 print "not " unless length(pack("l!", 0)) == $Config{longsize};
279 print "ok ", $test++, "\n";
281 print "not " unless length(pack("s!", 0)) <= length(pack("i!", 0));
282 print "ok ", $test++, "\n";
284 print "not " unless length(pack("i!", 0)) <= length(pack("l!", 0));
285 print "ok ", $test++, "\n";
287 print "not " unless length(pack("i!", 0)) == length(pack("i", 0));
288 print "ok ", $test++, "\n";
290 # 80..139: pack <-> unpack bijectionism
293 foreach my $c (-128, -1, 0, 1, 127) {
294 print "not " unless unpack("c", pack("c", $c)) == $c;
295 print "ok ", $test++, "\n";
299 foreach my $C (0, 1, 127, 128, 255) {
300 print "not " unless unpack("C", pack("C", $C)) == $C;
301 print "ok ", $test++, "\n";
305 foreach my $s (-32768, -1, 0, 1, 32767) {
306 print "not " unless unpack("s", pack("s", $s)) == $s;
307 print "ok ", $test++, "\n";
311 foreach my $S (0, 1, 32767, 32768, 65535) {
312 print "not " unless unpack("S", pack("S", $S)) == $S;
313 print "ok ", $test++, "\n";
317 foreach my $i (-2147483648, -1, 0, 1, 2147483647) {
318 print "not " unless unpack("i", pack("i", $i)) == $i;
319 print "ok ", $test++, "\n";
323 foreach my $I (0, 1, 2147483647, 2147483648, 4294967295) {
324 print "not " unless unpack("I", pack("I", $I)) == $I;
325 print "ok ", $test++, "\n";
329 foreach my $l (-2147483648, -1, 0, 1, 2147483647) {
330 print "not " unless unpack("l", pack("l", $l)) == $l;
331 print "ok ", $test++, "\n";
335 foreach my $L (0, 1, 2147483647, 2147483648, 4294967295) {
336 print "not " unless unpack("L", pack("L", $L)) == $L;
337 print "ok ", $test++, "\n";
341 foreach my $n (0, 1, 32767, 32768, 65535) {
342 print "not " unless unpack("n", pack("n", $n)) == $n;
343 print "ok ", $test++, "\n";
347 foreach my $v (0, 1, 32767, 32768, 65535) {
348 print "not " unless unpack("v", pack("v", $v)) == $v;
349 print "ok ", $test++, "\n";
353 foreach my $N (0, 1, 2147483647, 2147483648, 4294967295) {
354 print "not " unless unpack("N", pack("N", $N)) == $N;
355 print "ok ", $test++, "\n";
359 foreach my $V (0, 1, 2147483647, 2147483648, 4294967295) {
360 print "not " unless unpack("V", pack("V", $V)) == $V;
361 print "ok ", $test++, "\n";
364 # 140..143: pack nvNV byteorders
366 print "not " unless pack("n", 0xdead) eq "\xde\xad";
367 print "ok ", $test++, "\n";
369 print "not " unless pack("v", 0xdead) eq "\xad\xde";
370 print "ok ", $test++, "\n";
372 print "not " unless pack("N", 0xdeadbeef) eq "\xde\xad\xbe\xef";
373 print "ok ", $test++, "\n";
375 print "not " unless pack("V", 0xdeadbeef) eq "\xef\xbe\xad\xde";
376 print "ok ", $test++, "\n";
380 # Using Test considered bad plan in op/*.t ?
383 my ($pass, $test, $err, $wrong) = @_;
389 print "not ok $test # \$\@ = $err\n";
391 $wrong =~ s/([[:cntrl:]\177 ])/sprintf "\\%03o", ord $1/ge;
392 print "not ok $test # got $wrong\n";
398 eval { ($x) = unpack '/a*','hello' };
399 print 'not ' unless $@; print "ok $test\n"; $test++;
400 eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
401 print $@ eq '' && $z eq 'ok' ? "ok $test\n" : "not ok $test\n"; $test++;
402 print $@ eq '' && $x eq 'yes' ? "ok $test\n" : "not ok $test\n"; $test++;
403 print $@ eq '' && $y eq 'z' ? "ok $test\n" : "not ok $test\n"; $test++;
405 eval { ($x) = pack '/a*','hello' };
406 print 'not ' unless $@; print "ok $test\n"; $test++;
407 $z = pack 'n/a* N/Z* w/A*','string','hi there ','etc';
408 my $expect = "\000\006string\0\0\0\012hi there \000\003etc";
409 report ($z eq $expect, $test++, '', $z);
411 eval { ($x) = unpack 'a/a*/a*', '212ab345678901234567' };
412 print $@ eq '' && $x eq 'ab3456789012' ? "ok $test\n" : "#$x,$@\nnot ok $test\n";
415 eval { ($x) = unpack 'a/a*/a*', '3012ab345678901234567' };
416 print $@ eq '' && $x eq 'ab3456789012' ? "ok $test\n" : "not ok $test\n";
419 eval { ($x) = unpack 'a/a*/b*', '212ab' };
420 my $expected_x = '100001100100';
421 if ($Is_EBCDIC) { $expected_x = '100000010100'; }
422 print $@ eq '' && $x eq $expected_x ? "ok $test\n" : "#$x,$@\nnot ok $test\n";
427 eval { ($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
432 print $@ eq '' && $z eq 'ok' ? "ok $test\n" : "not ok $test\n"; $test++;
433 print $@ eq '' && $x eq 'yes' ? "ok $test\n" : "not ok $test\n"; $test++;
434 print $@ eq '' && $y eq 'z' ? "ok $test\n" : "not ok $test\n"; $test++;
436 $z = pack <<EOP,'string','etc';
437 n/a* # Count as network short
438 w/A* # Count a BER integer
440 $expect = "\000\006string\003etc";
441 report ($z eq $expect, $test++, '', $z);
443 print 'not ' unless "1.20.300.4000" eq sprintf "%vd", pack("U*",1,20,300,4000);
444 print "ok $test\n"; $test++;
445 print 'not ' unless "1.20.300.4000" eq
446 sprintf "%vd", pack(" U*",1,20,300,4000);
447 print "ok $test\n"; $test++;
448 print 'not ' unless v1.20.300.4000 ne
449 sprintf "%vd", pack("C0U*",1,20,300,4000);
450 print "ok $test\n"; $test++;
453 print "not " unless join(" ", unpack("C*", chr(0x1e2)))
454 eq ((ord(A) == 193) ? "156 67" : "199 162");
455 print "ok $test\n"; $test++;