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