1*0Sstevel@tonic-gate#!./perl -w 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateBEGIN { 4*0Sstevel@tonic-gate chdir 't' if -d 't'; 5*0Sstevel@tonic-gate @INC = '../lib'; 6*0Sstevel@tonic-gate require './test.pl'; 7*0Sstevel@tonic-gate} 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gateplan tests => 5852; 10*0Sstevel@tonic-gate 11*0Sstevel@tonic-gateuse strict; 12*0Sstevel@tonic-gateuse warnings; 13*0Sstevel@tonic-gateuse Config; 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gatemy $Is_EBCDIC = (defined $Config{ebcdic} && $Config{ebcdic} eq 'define'); 16*0Sstevel@tonic-gatemy $Perl = which_perl(); 17*0Sstevel@tonic-gate 18*0Sstevel@tonic-gatesub encode_list { 19*0Sstevel@tonic-gate my @result = map {_qq($_)} @_; 20*0Sstevel@tonic-gate if (@result == 1) { 21*0Sstevel@tonic-gate return @result; 22*0Sstevel@tonic-gate } 23*0Sstevel@tonic-gate return '(' . join (', ', @result) . ')'; 24*0Sstevel@tonic-gate} 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gatesub list_eq ($$) { 28*0Sstevel@tonic-gate my ($l, $r) = @_; 29*0Sstevel@tonic-gate return 0 unless @$l == @$r; 30*0Sstevel@tonic-gate for my $i (0..$#$l) { 31*0Sstevel@tonic-gate if (defined $l->[$i]) { 32*0Sstevel@tonic-gate return 0 unless defined ($r->[$i]) && $l->[$i] eq $r->[$i]; 33*0Sstevel@tonic-gate } else { 34*0Sstevel@tonic-gate return 0 if defined $r->[$i] 35*0Sstevel@tonic-gate } 36*0Sstevel@tonic-gate } 37*0Sstevel@tonic-gate return 1; 38*0Sstevel@tonic-gate} 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate############################################################################## 41*0Sstevel@tonic-gate# 42*0Sstevel@tonic-gate# Here starteth the tests 43*0Sstevel@tonic-gate# 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate{ 46*0Sstevel@tonic-gate my $format = "c2 x5 C C x s d i l a6"; 47*0Sstevel@tonic-gate # Need the expression in here to force ary[5] to be numeric. This avoids 48*0Sstevel@tonic-gate # test2 failing because ary2 goes str->numeric->str and ary doesn't. 49*0Sstevel@tonic-gate my @ary = (1,-100,127,128,32767,987.654321098 / 100.0,12345,123456, 50*0Sstevel@tonic-gate "abcdef"); 51*0Sstevel@tonic-gate my $foo = pack($format,@ary); 52*0Sstevel@tonic-gate my @ary2 = unpack($format,$foo); 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate is($#ary, $#ary2); 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate my $out1=join(':',@ary); 57*0Sstevel@tonic-gate my $out2=join(':',@ary2); 58*0Sstevel@tonic-gate # Using long double NVs may introduce greater accuracy than wanted. 59*0Sstevel@tonic-gate $out1 =~ s/:9\.87654321097999\d*:/:9.87654321098:/; 60*0Sstevel@tonic-gate $out2 =~ s/:9\.87654321097999\d*:/:9.87654321098:/; 61*0Sstevel@tonic-gate is($out1, $out2); 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate like($foo, qr/def/); 64*0Sstevel@tonic-gate} 65*0Sstevel@tonic-gate# How about counting bits? 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate{ 68*0Sstevel@tonic-gate my $x; 69*0Sstevel@tonic-gate is( ($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")), 16 ); 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate is( ($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")), 12 ); 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate is( ($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")), 9 ); 74*0Sstevel@tonic-gate} 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate{ 77*0Sstevel@tonic-gate my $sum = 129; # ASCII 78*0Sstevel@tonic-gate $sum = 103 if $Is_EBCDIC; 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate my $x; 81*0Sstevel@tonic-gate is( ($x = unpack("%32B*", "Now is the time for all good blurfl")), $sum ); 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate my $foo; 84*0Sstevel@tonic-gate open(BIN, $Perl) || die "Can't open $Perl: $!\n"; 85*0Sstevel@tonic-gate sysread BIN, $foo, 8192; 86*0Sstevel@tonic-gate close BIN; 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate $sum = unpack("%32b*", $foo); 89*0Sstevel@tonic-gate my $longway = unpack("b*", $foo); 90*0Sstevel@tonic-gate is( $sum, $longway =~ tr/1/1/ ); 91*0Sstevel@tonic-gate} 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate{ 94*0Sstevel@tonic-gate my $x; 95*0Sstevel@tonic-gate is( ($x = unpack("I",pack("I", 0xFFFFFFFF))), 0xFFFFFFFF ); 96*0Sstevel@tonic-gate} 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate{ 99*0Sstevel@tonic-gate # check 'w' 100*0Sstevel@tonic-gate my @x = (5,130,256,560,32000,3097152,268435455,1073741844, 2**33, 101*0Sstevel@tonic-gate '4503599627365785','23728385234614992549757750638446'); 102*0Sstevel@tonic-gate my $x = pack('w*', @x); 103*0Sstevel@tonic-gate my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f8480808014A0808'. 104*0Sstevel@tonic-gate '0800087ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e'; 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate is($x, $y); 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate my @y = unpack('w*', $y); 109*0Sstevel@tonic-gate my $a; 110*0Sstevel@tonic-gate while ($a = pop @x) { 111*0Sstevel@tonic-gate my $b = pop @y; 112*0Sstevel@tonic-gate is($a, $b); 113*0Sstevel@tonic-gate } 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate @y = unpack('w2', $x); 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate is(scalar(@y), 2); 118*0Sstevel@tonic-gate is($y[1], 130); 119*0Sstevel@tonic-gate $x = pack('w*', 5000000000); $y = ''; 120*0Sstevel@tonic-gate eval { 121*0Sstevel@tonic-gate use Math::BigInt; 122*0Sstevel@tonic-gate $y = pack('w*', Math::BigInt::->new(5000000000)); 123*0Sstevel@tonic-gate }; 124*0Sstevel@tonic-gate is($x, $y); 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate $x = pack 'w', ~0; 127*0Sstevel@tonic-gate $y = pack 'w', (~0).''; 128*0Sstevel@tonic-gate is($x, $y); 129*0Sstevel@tonic-gate is(unpack ('w',$x), ~0); 130*0Sstevel@tonic-gate is(unpack ('w',$y), ~0); 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate $x = pack 'w', ~0 - 1; 133*0Sstevel@tonic-gate $y = pack 'w', (~0) - 2; 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate if (~0 - 1 == (~0) - 2) { 136*0Sstevel@tonic-gate is($x, $y, "NV arithmetic"); 137*0Sstevel@tonic-gate } else { 138*0Sstevel@tonic-gate isnt($x, $y, "IV/NV arithmetic"); 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate cmp_ok(unpack ('w',$x), '==', ~0 - 1); 141*0Sstevel@tonic-gate cmp_ok(unpack ('w',$y), '==', ~0 - 2); 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate # These should spot that pack 'w' is using NV, not double, on platforms 144*0Sstevel@tonic-gate # where IVs are smaller than doubles, and harmlessly pass elsewhere. 145*0Sstevel@tonic-gate # (tests for change 16861) 146*0Sstevel@tonic-gate my $x0 = 2**54+3; 147*0Sstevel@tonic-gate my $y0 = 2**54-2; 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate $x = pack 'w', $x0; 150*0Sstevel@tonic-gate $y = pack 'w', $y0; 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate if ($x0 == $y0) { 153*0Sstevel@tonic-gate is($x, $y, "NV arithmetic"); 154*0Sstevel@tonic-gate } else { 155*0Sstevel@tonic-gate isnt($x, $y, "IV/NV arithmetic"); 156*0Sstevel@tonic-gate } 157*0Sstevel@tonic-gate cmp_ok(unpack ('w',$x), '==', $x0); 158*0Sstevel@tonic-gate cmp_ok(unpack ('w',$y), '==', $y0); 159*0Sstevel@tonic-gate} 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate{ 163*0Sstevel@tonic-gate print "# test exceptions\n"; 164*0Sstevel@tonic-gate my $x; 165*0Sstevel@tonic-gate eval { $x = unpack 'w', pack 'C*', 0xff, 0xff}; 166*0Sstevel@tonic-gate like($@, qr/^Unterminated compressed integer/); 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff}; 169*0Sstevel@tonic-gate like($@, qr/^Unterminated compressed integer/); 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 172*0Sstevel@tonic-gate like($@, qr/^Unterminated compressed integer/); 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate eval { $x = pack 'w', -1 }; 175*0Sstevel@tonic-gate like ($@, qr/^Cannot compress negative numbers/); 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate eval { $x = pack 'w', '1'x(1 + length ~0) . 'e0' }; 178*0Sstevel@tonic-gate like ($@, qr/^Can only compress unsigned integers/); 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate SKIP: { 181*0Sstevel@tonic-gate # Is this a stupid thing to do on VMS, VOS and other unusual platforms? 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate skip("-- the IEEE infinity model is unavailable in this configuration.", 1) 184*0Sstevel@tonic-gate if (($^O eq 'VMS') && !defined($Config{useieee})); 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate skip("-- $^O has serious fp indigestion on w-packed infinities", 1) 187*0Sstevel@tonic-gate if ( 188*0Sstevel@tonic-gate ($^O eq 'mpeix') 189*0Sstevel@tonic-gate || 190*0Sstevel@tonic-gate ($^O eq 'ultrix') 191*0Sstevel@tonic-gate || 192*0Sstevel@tonic-gate ($^O =~ /^svr4/ && -f "/etc/issue" && -f "/etc/.relid") # NCR MP-RAS 193*0Sstevel@tonic-gate ); 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate my $inf = eval '2**10000'; 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate skip("Couldn't generate infinity - got error '$@'", 1) 198*0Sstevel@tonic-gate unless defined $inf and $inf == $inf / 2 and $inf + 1 == $inf; 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate local our $TODO; 201*0Sstevel@tonic-gate $TODO = "VOS needs a fix for posix-1022 to pass this test." 202*0Sstevel@tonic-gate if ($^O eq 'vos'); 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate eval { $x = pack 'w', $inf }; 205*0Sstevel@tonic-gate like ($@, qr/^Cannot compress integer/, "Cannot compress integer"); 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate SKIP: { 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate skip("-- the full range of an IEEE double may not be available in this configuration.", 3) 211*0Sstevel@tonic-gate if (($^O eq 'VMS') && !defined($Config{useieee})); 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate skip("-- $^O does not like 2**1023", 3) 214*0Sstevel@tonic-gate if (($^O eq 'ultrix')); 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate # This should be about the biggest thing possible on an IEEE double 217*0Sstevel@tonic-gate my $big = eval '2**1023'; 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gate skip("Couldn't generate 2**1023 - got error '$@'", 3) 220*0Sstevel@tonic-gate unless defined $big and $big != $big / 2; 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate eval { $x = pack 'w', $big }; 223*0Sstevel@tonic-gate is ($@, '', "Should be able to pack 'w', $big # 2**1023"); 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate my $y = eval {unpack 'w', $x}; 226*0Sstevel@tonic-gate is ($@, '', 227*0Sstevel@tonic-gate "Should be able to unpack 'w' the result of pack 'w', $big # 2**1023"); 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate # I'm getting about 1e-16 on FreeBSD 230*0Sstevel@tonic-gate my $quotient = int (100 * ($y - $big) / $big); 231*0Sstevel@tonic-gate ok($quotient < 2 && $quotient > -2, 232*0Sstevel@tonic-gate "Round trip pack, unpack 'w' of $big is withing 1% ($quotient%)"); 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate} 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gateprint "# test the 'p' template\n"; 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate# literals 240*0Sstevel@tonic-gateis(unpack("p",pack("p","foo")), "foo"); 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate# scalars 243*0Sstevel@tonic-gateis(unpack("p",pack("p",239)), 239); 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate# temps 246*0Sstevel@tonic-gatesub foo { my $a = "a"; return $a . $a++ . $a++ } 247*0Sstevel@tonic-gate{ 248*0Sstevel@tonic-gate use warnings; 249*0Sstevel@tonic-gate my $warning; 250*0Sstevel@tonic-gate local $SIG{__WARN__} = sub { 251*0Sstevel@tonic-gate $warning = $_[0]; 252*0Sstevel@tonic-gate }; 253*0Sstevel@tonic-gate my $junk = pack("p", &foo); 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate like($warning, qr/temporary val/); 256*0Sstevel@tonic-gate} 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate# undef should give null pointer 259*0Sstevel@tonic-gatelike(pack("p", undef), qr/^\0+/); 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gate# Check for optimizer bug (e.g. Digital Unix GEM cc with -O4 on DU V4.0B gives 262*0Sstevel@tonic-gate# 4294967295 instead of -1) 263*0Sstevel@tonic-gate# see #ifdef __osf__ in pp.c pp_unpack 264*0Sstevel@tonic-gateis((unpack("i",pack("i",-1))), -1); 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gateprint "# test the pack lengths of s S i I l L n N v V\n"; 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gatemy @lengths = qw(s 2 S 2 i -4 I -4 l 4 L 4 n 2 N 4 v 2 V 4); 269*0Sstevel@tonic-gatewhile (my ($format, $expect) = splice @lengths, 0, 2) { 270*0Sstevel@tonic-gate my $len = length(pack($format, 0)); 271*0Sstevel@tonic-gate if ($expect > 0) { 272*0Sstevel@tonic-gate is($expect, $len, "format '$format'"); 273*0Sstevel@tonic-gate } else { 274*0Sstevel@tonic-gate $expect = -$expect; 275*0Sstevel@tonic-gate ok ($len >= $expect, "format '$format'") || 276*0Sstevel@tonic-gate print "# format '$format' has length $len, expected >= $expect\n"; 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate} 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gateprint "# test unpack-pack lengths\n"; 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gatemy @templates = qw(c C i I s S l L n N v V f d q Q); 284*0Sstevel@tonic-gate 285*0Sstevel@tonic-gateforeach my $t (@templates) { 286*0Sstevel@tonic-gate SKIP: { 287*0Sstevel@tonic-gate my @t = eval { unpack("$t*", pack("$t*", 12, 34)) }; 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate # quads not supported everywhere 290*0Sstevel@tonic-gate skip "Quads not supported", 4 if $@ =~ /Invalid type/; 291*0Sstevel@tonic-gate is( $@, '' ); 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate is(scalar @t, 2); 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gate SKIP: { 296*0Sstevel@tonic-gate skip "$t not expected to work for some reason", 2 if $t =~ /[nv]/i; 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate is($t[0], 12); 299*0Sstevel@tonic-gate is($t[1], 34); 300*0Sstevel@tonic-gate } 301*0Sstevel@tonic-gate } 302*0Sstevel@tonic-gate} 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gate{ 305*0Sstevel@tonic-gate # uuencode/decode 306*0Sstevel@tonic-gate 307*0Sstevel@tonic-gate # Note that first uuencoding known 'text' data and then checking the 308*0Sstevel@tonic-gate # binary values of the uuencoded version would not be portable between 309*0Sstevel@tonic-gate # character sets. Uuencoding is meant for encoding binary data, not 310*0Sstevel@tonic-gate # text data. 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate my $in = pack 'C*', 0 .. 255; 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gate # just to be anal, we do some random tr/`/ / 315*0Sstevel@tonic-gate my $uu = <<'EOUU'; 316*0Sstevel@tonic-gateM` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL 317*0Sstevel@tonic-gateM+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9 318*0Sstevel@tonic-gateM6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6& 319*0Sstevel@tonic-gateMAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S 320*0Sstevel@tonic-gateMM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@ 321*0Sstevel@tonic-gate?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P ` 322*0Sstevel@tonic-gateEOUU 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gate $_ = $uu; 325*0Sstevel@tonic-gate tr/ /`/; 326*0Sstevel@tonic-gate 327*0Sstevel@tonic-gate is(pack('u', $in), $_); 328*0Sstevel@tonic-gate 329*0Sstevel@tonic-gate is(unpack('u', $uu), $in); 330*0Sstevel@tonic-gate 331*0Sstevel@tonic-gate $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"; 332*0Sstevel@tonic-gate $uu = <<'EOUU'; 333*0Sstevel@tonic-gateM'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F 334*0Sstevel@tonic-gate&8%P:```` 335*0Sstevel@tonic-gateEOUU 336*0Sstevel@tonic-gate 337*0Sstevel@tonic-gate is(unpack('u', $uu), $in); 338*0Sstevel@tonic-gate 339*0Sstevel@tonic-gate# This is identical to the above except that backquotes have been 340*0Sstevel@tonic-gate# changed to spaces 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate $uu = <<'EOUU'; 343*0Sstevel@tonic-gateM'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F 344*0Sstevel@tonic-gate&8%P: 345*0Sstevel@tonic-gateEOUU 346*0Sstevel@tonic-gate 347*0Sstevel@tonic-gate # ' # Grr 348*0Sstevel@tonic-gate is(unpack('u', $uu), $in); 349*0Sstevel@tonic-gate 350*0Sstevel@tonic-gate} 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gate# test the ascii template types (A, a, Z) 353*0Sstevel@tonic-gate 354*0Sstevel@tonic-gateforeach ( 355*0Sstevel@tonic-gate['p', 'A*', "foo\0bar\0 ", "foo\0bar\0 "], 356*0Sstevel@tonic-gate['p', 'A11', "foo\0bar\0 ", "foo\0bar\0 "], 357*0Sstevel@tonic-gate['u', 'A*', "foo\0bar \0", "foo\0bar"], 358*0Sstevel@tonic-gate['u', 'A8', "foo\0bar \0", "foo\0bar"], 359*0Sstevel@tonic-gate['p', 'a*', "foo\0bar\0 ", "foo\0bar\0 "], 360*0Sstevel@tonic-gate['p', 'a11', "foo\0bar\0 ", "foo\0bar\0 \0\0"], 361*0Sstevel@tonic-gate['u', 'a*', "foo\0bar \0", "foo\0bar \0"], 362*0Sstevel@tonic-gate['u', 'a8', "foo\0bar \0", "foo\0bar "], 363*0Sstevel@tonic-gate['p', 'Z*', "foo\0bar\0 ", "foo\0bar\0 \0"], 364*0Sstevel@tonic-gate['p', 'Z11', "foo\0bar\0 ", "foo\0bar\0 \0\0"], 365*0Sstevel@tonic-gate['p', 'Z3', "foo", "fo\0"], 366*0Sstevel@tonic-gate['u', 'Z*', "foo\0bar \0", "foo"], 367*0Sstevel@tonic-gate['u', 'Z8', "foo\0bar \0", "foo"], 368*0Sstevel@tonic-gate) 369*0Sstevel@tonic-gate{ 370*0Sstevel@tonic-gate my ($what, $template, $in, $out) = @$_; 371*0Sstevel@tonic-gate my $got = $what eq 'u' ? (unpack $template, $in) : (pack $template, $in); 372*0Sstevel@tonic-gate unless (is($got, $out)) { 373*0Sstevel@tonic-gate my $un = $what eq 'u' ? 'un' : ''; 374*0Sstevel@tonic-gate print "# ${un}pack ('$template', "._qq($in).') gave '._qq($out). 375*0Sstevel@tonic-gate ' not '._qq($got)."\n"; 376*0Sstevel@tonic-gate } 377*0Sstevel@tonic-gate} 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gateprint "# packing native shorts/ints/longs\n"; 380*0Sstevel@tonic-gate 381*0Sstevel@tonic-gateis(length(pack("s!", 0)), $Config{shortsize}); 382*0Sstevel@tonic-gateis(length(pack("i!", 0)), $Config{intsize}); 383*0Sstevel@tonic-gateis(length(pack("l!", 0)), $Config{longsize}); 384*0Sstevel@tonic-gateok(length(pack("s!", 0)) <= length(pack("i!", 0))); 385*0Sstevel@tonic-gateok(length(pack("i!", 0)) <= length(pack("l!", 0))); 386*0Sstevel@tonic-gateis(length(pack("i!", 0)), length(pack("i", 0))); 387*0Sstevel@tonic-gate 388*0Sstevel@tonic-gatesub numbers { 389*0Sstevel@tonic-gate my $format = shift; 390*0Sstevel@tonic-gate return numbers_with_total ($format, undef, @_); 391*0Sstevel@tonic-gate} 392*0Sstevel@tonic-gate 393*0Sstevel@tonic-gatesub numbers_with_total { 394*0Sstevel@tonic-gate my $format = shift; 395*0Sstevel@tonic-gate my $total = shift; 396*0Sstevel@tonic-gate if (!defined $total) { 397*0Sstevel@tonic-gate foreach (@_) { 398*0Sstevel@tonic-gate $total += $_; 399*0Sstevel@tonic-gate } 400*0Sstevel@tonic-gate } 401*0Sstevel@tonic-gate print "# numbers test for $format\n"; 402*0Sstevel@tonic-gate foreach (@_) { 403*0Sstevel@tonic-gate SKIP: { 404*0Sstevel@tonic-gate my $out = eval {unpack($format, pack($format, $_))}; 405*0Sstevel@tonic-gate skip "cannot pack '$format' on this perl", 2 if 406*0Sstevel@tonic-gate $@ =~ /Invalid type '$format'/; 407*0Sstevel@tonic-gate 408*0Sstevel@tonic-gate is($@, ''); 409*0Sstevel@tonic-gate is($out, $_); 410*0Sstevel@tonic-gate } 411*0Sstevel@tonic-gate } 412*0Sstevel@tonic-gate 413*0Sstevel@tonic-gate my $skip_if_longer_than = ~0; # "Infinity" 414*0Sstevel@tonic-gate if (~0 - 1 == ~0) { 415*0Sstevel@tonic-gate # If we're running with -DNO_PERLPRESERVE_IVUV and NVs don't preserve all 416*0Sstevel@tonic-gate # UVs (in which case ~0 is NV, ~0-1 will be the same NV) then we can't 417*0Sstevel@tonic-gate # correctly in perl calculate UV totals for long checksums, as pp_unpack 418*0Sstevel@tonic-gate # is using UV maths, and we've only got NVs. 419*0Sstevel@tonic-gate $skip_if_longer_than = $Config{nv_preserves_uv_bits}; 420*0Sstevel@tonic-gate } 421*0Sstevel@tonic-gate 422*0Sstevel@tonic-gate foreach ('', 1, 2, 3, 15, 16, 17, 31, 32, 33, 53, 54, 63, 64, 65) { 423*0Sstevel@tonic-gate SKIP: { 424*0Sstevel@tonic-gate my $sum = eval {unpack "%$_$format*", pack "$format*", @_}; 425*0Sstevel@tonic-gate skip "cannot pack '$format' on this perl", 3 426*0Sstevel@tonic-gate if $@ =~ /Invalid type '$format'/; 427*0Sstevel@tonic-gate 428*0Sstevel@tonic-gate is($@, ''); 429*0Sstevel@tonic-gate ok(defined $sum); 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate my $len = $_; # Copy, so that we can reassign '' 432*0Sstevel@tonic-gate $len = 16 unless length $len; 433*0Sstevel@tonic-gate 434*0Sstevel@tonic-gate SKIP: { 435*0Sstevel@tonic-gate skip "cannot test checksums over $skip_if_longer_than bits", 1 436*0Sstevel@tonic-gate if $len > $skip_if_longer_than; 437*0Sstevel@tonic-gate 438*0Sstevel@tonic-gate # Our problem with testing this portably is that the checksum code in 439*0Sstevel@tonic-gate # pp_unpack is able to cast signed to unsigned, and do modulo 2**n 440*0Sstevel@tonic-gate # arithmetic in unsigned ints, which perl has no operators to do. 441*0Sstevel@tonic-gate # (use integer; does signed ints, which won't wrap on UTS, which is just 442*0Sstevel@tonic-gate # fine with ANSI, but not with most people's assumptions. 443*0Sstevel@tonic-gate # This is why we need to supply the totals for 'Q' as there's no way in 444*0Sstevel@tonic-gate # perl to calculate them, short of unpack '%0Q' (is that documented?) 445*0Sstevel@tonic-gate # ** returns NVs; make sure it's IV. 446*0Sstevel@tonic-gate my $max = 1 + 2 * (int (2 ** ($len-1))-1); # The max possible checksum 447*0Sstevel@tonic-gate my $max_p1 = $max + 1; 448*0Sstevel@tonic-gate my ($max_is_integer, $max_p1_is_integer); 449*0Sstevel@tonic-gate $max_p1_is_integer = 1 unless $max_p1 + 1 == $max_p1; 450*0Sstevel@tonic-gate $max_is_integer = 1 if $max - 1 < ~0; 451*0Sstevel@tonic-gate 452*0Sstevel@tonic-gate my $calc_sum; 453*0Sstevel@tonic-gate if (ref $total) { 454*0Sstevel@tonic-gate $calc_sum = &$total($len); 455*0Sstevel@tonic-gate } else { 456*0Sstevel@tonic-gate $calc_sum = $total; 457*0Sstevel@tonic-gate # Shift into range by some multiple of the total 458*0Sstevel@tonic-gate my $mult = $max_p1 ? int ($total / $max_p1) : undef; 459*0Sstevel@tonic-gate # Need this to make sure that -1 + (~0+1) is ~0 (ie still integer) 460*0Sstevel@tonic-gate $calc_sum = $total - $mult; 461*0Sstevel@tonic-gate $calc_sum -= $mult * $max; 462*0Sstevel@tonic-gate if ($calc_sum < 0) { 463*0Sstevel@tonic-gate $calc_sum += 1; 464*0Sstevel@tonic-gate $calc_sum += $max; 465*0Sstevel@tonic-gate } 466*0Sstevel@tonic-gate } 467*0Sstevel@tonic-gate if ($calc_sum == $calc_sum - 1 && $calc_sum == $max_p1) { 468*0Sstevel@tonic-gate # we're into floating point (either by getting out of the range of 469*0Sstevel@tonic-gate # UV arithmetic, or because we're doing a floating point checksum) 470*0Sstevel@tonic-gate # and our calculation of the checksum has become rounded up to 471*0Sstevel@tonic-gate # max_checksum + 1 472*0Sstevel@tonic-gate $calc_sum = 0; 473*0Sstevel@tonic-gate } 474*0Sstevel@tonic-gate 475*0Sstevel@tonic-gate if ($calc_sum == $sum) { # HAS to be ==, not eq (so no is()). 476*0Sstevel@tonic-gate ok ("unpack '%$_$format' gave $sum"); 477*0Sstevel@tonic-gate } else { 478*0Sstevel@tonic-gate my $delta = 1.000001; 479*0Sstevel@tonic-gate if ($format =~ tr /dDfF// 480*0Sstevel@tonic-gate && ($calc_sum <= $sum * $delta && $calc_sum >= $sum / $delta)) { 481*0Sstevel@tonic-gate pass ("unpack '%$_$format' gave $sum, expected $calc_sum"); 482*0Sstevel@tonic-gate } else { 483*0Sstevel@tonic-gate my $text = ref $total ? &$total($len) : $total; 484*0Sstevel@tonic-gate fail; 485*0Sstevel@tonic-gate print "# For list (" . join (", ", @_) . ") (total $text)" 486*0Sstevel@tonic-gate . " packed with $format unpack '%$_$format' gave $sum," 487*0Sstevel@tonic-gate . " expected $calc_sum\n"; 488*0Sstevel@tonic-gate } 489*0Sstevel@tonic-gate } 490*0Sstevel@tonic-gate } 491*0Sstevel@tonic-gate } 492*0Sstevel@tonic-gate } 493*0Sstevel@tonic-gate} 494*0Sstevel@tonic-gate 495*0Sstevel@tonic-gatenumbers ('c', -128, -1, 0, 1, 127); 496*0Sstevel@tonic-gatenumbers ('C', 0, 1, 127, 128, 255); 497*0Sstevel@tonic-gatenumbers ('s', -32768, -1, 0, 1, 32767); 498*0Sstevel@tonic-gatenumbers ('S', 0, 1, 32767, 32768, 65535); 499*0Sstevel@tonic-gatenumbers ('i', -2147483648, -1, 0, 1, 2147483647); 500*0Sstevel@tonic-gatenumbers ('I', 0, 1, 2147483647, 2147483648, 4294967295); 501*0Sstevel@tonic-gatenumbers ('l', -2147483648, -1, 0, 1, 2147483647); 502*0Sstevel@tonic-gatenumbers ('L', 0, 1, 2147483647, 2147483648, 4294967295); 503*0Sstevel@tonic-gatenumbers ('s!', -32768, -1, 0, 1, 32767); 504*0Sstevel@tonic-gatenumbers ('S!', 0, 1, 32767, 32768, 65535); 505*0Sstevel@tonic-gatenumbers ('i!', -2147483648, -1, 0, 1, 2147483647); 506*0Sstevel@tonic-gatenumbers ('I!', 0, 1, 2147483647, 2147483648, 4294967295); 507*0Sstevel@tonic-gatenumbers ('l!', -2147483648, -1, 0, 1, 2147483647); 508*0Sstevel@tonic-gatenumbers ('L!', 0, 1, 2147483647, 2147483648, 4294967295); 509*0Sstevel@tonic-gatenumbers ('n', 0, 1, 32767, 32768, 65535); 510*0Sstevel@tonic-gatenumbers ('v', 0, 1, 32767, 32768, 65535); 511*0Sstevel@tonic-gatenumbers ('N', 0, 1, 2147483647, 2147483648, 4294967295); 512*0Sstevel@tonic-gatenumbers ('V', 0, 1, 2147483647, 2147483648, 4294967295); 513*0Sstevel@tonic-gate# All these should have exact binary representations: 514*0Sstevel@tonic-gatenumbers ('f', -1, 0, 0.5, 42, 2**34); 515*0Sstevel@tonic-gatenumbers ('d', -(2**34), -1, 0, 1, 2**34); 516*0Sstevel@tonic-gate## These don't, but 'd' is NV. XXX wrong, it's double 517*0Sstevel@tonic-gate#numbers ('d', -1, 0, 1, 1-exp(-1), -exp(1)); 518*0Sstevel@tonic-gate 519*0Sstevel@tonic-gatenumbers_with_total ('q', -1, 520*0Sstevel@tonic-gate -9223372036854775808, -1, 0, 1,9223372036854775807); 521*0Sstevel@tonic-gate# This total is icky, but the true total is 2**65-1, and need a way to generate 522*0Sstevel@tonic-gate# the epxected checksum on any system including those where NVs can preserve 523*0Sstevel@tonic-gate# 65 bits. (long double is 128 bits on sparc, so they certainly can) 524*0Sstevel@tonic-gate# or where rounding is down not up on binary conversion (crays) 525*0Sstevel@tonic-gatenumbers_with_total ('Q', sub { 526*0Sstevel@tonic-gate my $len = shift; 527*0Sstevel@tonic-gate $len = 65 if $len > 65; # unmasked total is 2**65-1 here 528*0Sstevel@tonic-gate my $total = 1 + 2 * (int (2**($len - 1)) - 1); 529*0Sstevel@tonic-gate return 0 if $total == $total - 1; # Overflowed integers 530*0Sstevel@tonic-gate return $total; # NVs still accurate to nearest integer 531*0Sstevel@tonic-gate }, 532*0Sstevel@tonic-gate 0, 1,9223372036854775807, 9223372036854775808, 533*0Sstevel@tonic-gate 18446744073709551615); 534*0Sstevel@tonic-gate 535*0Sstevel@tonic-gateprint "# pack nvNV byteorders\n"; 536*0Sstevel@tonic-gate 537*0Sstevel@tonic-gateis(pack("n", 0xdead), "\xde\xad"); 538*0Sstevel@tonic-gateis(pack("v", 0xdead), "\xad\xde"); 539*0Sstevel@tonic-gateis(pack("N", 0xdeadbeef), "\xde\xad\xbe\xef"); 540*0Sstevel@tonic-gateis(pack("V", 0xdeadbeef), "\xef\xbe\xad\xde"); 541*0Sstevel@tonic-gate 542*0Sstevel@tonic-gate{ 543*0Sstevel@tonic-gate # / 544*0Sstevel@tonic-gate 545*0Sstevel@tonic-gate my ($x, $y, $z); 546*0Sstevel@tonic-gate eval { ($x) = unpack '/a*','hello' }; 547*0Sstevel@tonic-gate like($@, qr!'/' must follow a numeric type!); 548*0Sstevel@tonic-gate undef $x; 549*0Sstevel@tonic-gate eval { $x = unpack '/a*','hello' }; 550*0Sstevel@tonic-gate like($@, qr!'/' must follow a numeric type!); 551*0Sstevel@tonic-gate 552*0Sstevel@tonic-gate undef $x; 553*0Sstevel@tonic-gate eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" }; 554*0Sstevel@tonic-gate is($@, ''); 555*0Sstevel@tonic-gate is($z, 'ok'); 556*0Sstevel@tonic-gate is($x, 'yes'); 557*0Sstevel@tonic-gate is($y, 'z'); 558*0Sstevel@tonic-gate undef $z; 559*0Sstevel@tonic-gate eval { $z = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" }; 560*0Sstevel@tonic-gate is($@, ''); 561*0Sstevel@tonic-gate is($z, 'ok'); 562*0Sstevel@tonic-gate 563*0Sstevel@tonic-gate 564*0Sstevel@tonic-gate undef $x; 565*0Sstevel@tonic-gate eval { ($x) = pack '/a*','hello' }; 566*0Sstevel@tonic-gate like($@, qr!Invalid type '/'!); 567*0Sstevel@tonic-gate undef $x; 568*0Sstevel@tonic-gate eval { $x = pack '/a*','hello' }; 569*0Sstevel@tonic-gate like($@, qr!Invalid type '/'!); 570*0Sstevel@tonic-gate 571*0Sstevel@tonic-gate $z = pack 'n/a* N/Z* w/A*','string','hi there ','etc'; 572*0Sstevel@tonic-gate my $expect = "\000\006string\0\0\0\012hi there \000\003etc"; 573*0Sstevel@tonic-gate is($z, $expect); 574*0Sstevel@tonic-gate 575*0Sstevel@tonic-gate undef $x; 576*0Sstevel@tonic-gate $expect = 'hello world'; 577*0Sstevel@tonic-gate eval { ($x) = unpack ("w/a", chr (11) . "hello world!")}; 578*0Sstevel@tonic-gate is($x, $expect); 579*0Sstevel@tonic-gate is($@, ''); 580*0Sstevel@tonic-gate 581*0Sstevel@tonic-gate undef $x; 582*0Sstevel@tonic-gate # Doing this in scalar context used to fail. 583*0Sstevel@tonic-gate eval { $x = unpack ("w/a", chr (11) . "hello world!")}; 584*0Sstevel@tonic-gate is($@, ''); 585*0Sstevel@tonic-gate is($x, $expect); 586*0Sstevel@tonic-gate 587*0Sstevel@tonic-gate foreach ( 588*0Sstevel@tonic-gate ['a/a*/a*', '212ab345678901234567','ab3456789012'], 589*0Sstevel@tonic-gate ['a/a*/a*', '3012ab345678901234567', 'ab3456789012'], 590*0Sstevel@tonic-gate ['a/a*/b*', '212ab', $Is_EBCDIC ? '100000010100' : '100001100100'], 591*0Sstevel@tonic-gate ) 592*0Sstevel@tonic-gate { 593*0Sstevel@tonic-gate my ($pat, $in, $expect) = @$_; 594*0Sstevel@tonic-gate undef $x; 595*0Sstevel@tonic-gate eval { ($x) = unpack $pat, $in }; 596*0Sstevel@tonic-gate is($@, ''); 597*0Sstevel@tonic-gate is($x, $expect) || 598*0Sstevel@tonic-gate printf "# list unpack ('$pat', '$in') gave %s, expected '$expect'\n", 599*0Sstevel@tonic-gate encode_list ($x); 600*0Sstevel@tonic-gate 601*0Sstevel@tonic-gate undef $x; 602*0Sstevel@tonic-gate eval { $x = unpack $pat, $in }; 603*0Sstevel@tonic-gate is($@, ''); 604*0Sstevel@tonic-gate is($x, $expect) || 605*0Sstevel@tonic-gate printf "# scalar unpack ('$pat', '$in') gave %s, expected '$expect'\n", 606*0Sstevel@tonic-gate encode_list ($x); 607*0Sstevel@tonic-gate } 608*0Sstevel@tonic-gate 609*0Sstevel@tonic-gate # / with # 610*0Sstevel@tonic-gate 611*0Sstevel@tonic-gate my $pattern = <<'EOU'; 612*0Sstevel@tonic-gate a3/A # Count in ASCII 613*0Sstevel@tonic-gate C/a* # Count in a C char 614*0Sstevel@tonic-gate C/Z # Count in a C char but skip after \0 615*0Sstevel@tonic-gateEOU 616*0Sstevel@tonic-gate 617*0Sstevel@tonic-gate $x = $y = $z =undef; 618*0Sstevel@tonic-gate eval { ($z,$x,$y) = unpack $pattern, "003ok \003yes\004z\000abc" }; 619*0Sstevel@tonic-gate is($@, ''); 620*0Sstevel@tonic-gate is($z, 'ok'); 621*0Sstevel@tonic-gate is($x, 'yes'); 622*0Sstevel@tonic-gate is($y, 'z'); 623*0Sstevel@tonic-gate undef $x; 624*0Sstevel@tonic-gate eval { $z = unpack $pattern, "003ok \003yes\004z\000abc" }; 625*0Sstevel@tonic-gate is($@, ''); 626*0Sstevel@tonic-gate is($z, 'ok'); 627*0Sstevel@tonic-gate 628*0Sstevel@tonic-gate $pattern = <<'EOP'; 629*0Sstevel@tonic-gate n/a* # Count as network short 630*0Sstevel@tonic-gate w/A* # Count a BER integer 631*0Sstevel@tonic-gateEOP 632*0Sstevel@tonic-gate $expect = "\000\006string\003etc"; 633*0Sstevel@tonic-gate $z = pack $pattern,'string','etc'; 634*0Sstevel@tonic-gate is($z, $expect); 635*0Sstevel@tonic-gate} 636*0Sstevel@tonic-gate 637*0Sstevel@tonic-gate 638*0Sstevel@tonic-gateSKIP: { 639*0Sstevel@tonic-gate skip("(EBCDIC and) version strings are bad idea", 2) if $Is_EBCDIC; 640*0Sstevel@tonic-gate 641*0Sstevel@tonic-gate is("1.20.300.4000", sprintf "%vd", pack("U*",1,20,300,4000)); 642*0Sstevel@tonic-gate is("1.20.300.4000", sprintf "%vd", pack(" U*",1,20,300,4000)); 643*0Sstevel@tonic-gate} 644*0Sstevel@tonic-gateisnt(v1.20.300.4000, sprintf "%vd", pack("C0U*",1,20,300,4000)); 645*0Sstevel@tonic-gate 646*0Sstevel@tonic-gatemy $rslt = $Is_EBCDIC ? "156 67" : "199 162"; 647*0Sstevel@tonic-gateis(join(" ", unpack("C*", chr(0x1e2))), $rslt); 648*0Sstevel@tonic-gate 649*0Sstevel@tonic-gate# does pack U create Unicode? 650*0Sstevel@tonic-gateis(ord(pack('U', 300)), 300); 651*0Sstevel@tonic-gate 652*0Sstevel@tonic-gate# does unpack U deref Unicode? 653*0Sstevel@tonic-gateis((unpack('U', chr(300)))[0], 300); 654*0Sstevel@tonic-gate 655*0Sstevel@tonic-gate# is unpack U the reverse of pack U for Unicode string? 656*0Sstevel@tonic-gateis("@{[unpack('U*', pack('U*', 100, 200, 300))]}", "100 200 300"); 657*0Sstevel@tonic-gate 658*0Sstevel@tonic-gate# is unpack U the reverse of pack U for byte string? 659*0Sstevel@tonic-gateis("@{[unpack('U*', pack('U*', 100, 200))]}", "100 200"); 660*0Sstevel@tonic-gate 661*0Sstevel@tonic-gate 662*0Sstevel@tonic-gateSKIP: { 663*0Sstevel@tonic-gate skip "Not for EBCDIC", 4 if $Is_EBCDIC; 664*0Sstevel@tonic-gate 665*0Sstevel@tonic-gate # does unpack C unravel pack U? 666*0Sstevel@tonic-gate is("@{[unpack('C*', pack('U*', 100, 200))]}", "100 195 136"); 667*0Sstevel@tonic-gate 668*0Sstevel@tonic-gate # does pack U0C create Unicode? 669*0Sstevel@tonic-gate is("@{[pack('U0C*', 100, 195, 136)]}", v100.v200); 670*0Sstevel@tonic-gate 671*0Sstevel@tonic-gate # does pack C0U create characters? 672*0Sstevel@tonic-gate is("@{[pack('C0U*', 100, 200)]}", pack("C*", 100, 195, 136)); 673*0Sstevel@tonic-gate 674*0Sstevel@tonic-gate # does unpack U0U on byte data warn? 675*0Sstevel@tonic-gate { 676*0Sstevel@tonic-gate local $SIG{__WARN__} = sub { $@ = "@_" }; 677*0Sstevel@tonic-gate my @null = unpack('U0U', chr(255)); 678*0Sstevel@tonic-gate like($@, /^Malformed UTF-8 character /); 679*0Sstevel@tonic-gate } 680*0Sstevel@tonic-gate} 681*0Sstevel@tonic-gate 682*0Sstevel@tonic-gate{ 683*0Sstevel@tonic-gate my $p = pack 'i*', -2147483648, ~0, 0, 1, 2147483647; 684*0Sstevel@tonic-gate my (@a); 685*0Sstevel@tonic-gate # bug - % had to be at the start of the pattern, no leading whitespace or 686*0Sstevel@tonic-gate # comments. %i! didn't work at all. 687*0Sstevel@tonic-gate foreach my $pat ('%32i*', ' %32i*', "# Muhahahaha\n%32i*", '%32i* ', 688*0Sstevel@tonic-gate '%32i!*', ' %32i!*', "\n#\n#\n\r \t\f%32i!*", '%32i!*#') { 689*0Sstevel@tonic-gate @a = unpack $pat, $p; 690*0Sstevel@tonic-gate is($a[0], 0xFFFFFFFF) || print "# $pat\n"; 691*0Sstevel@tonic-gate @a = scalar unpack $pat, $p; 692*0Sstevel@tonic-gate is($a[0], 0xFFFFFFFF) || print "# $pat\n"; 693*0Sstevel@tonic-gate } 694*0Sstevel@tonic-gate 695*0Sstevel@tonic-gate 696*0Sstevel@tonic-gate $p = pack 'I*', 42, 12; 697*0Sstevel@tonic-gate # Multiline patterns in scalar context failed. 698*0Sstevel@tonic-gate foreach my $pat ('I', <<EOPOEMSNIPPET, 'I#I', 'I # I', 'I # !!!') { 699*0Sstevel@tonic-gate# On the Ning Nang Nong 700*0Sstevel@tonic-gate# Where the Cows go Bong! 701*0Sstevel@tonic-gate# And the Monkeys all say Boo! 702*0Sstevel@tonic-gateI 703*0Sstevel@tonic-gateEOPOEMSNIPPET 704*0Sstevel@tonic-gate @a = unpack $pat, $p; 705*0Sstevel@tonic-gate is(scalar @a, 1); 706*0Sstevel@tonic-gate is($a[0], 42); 707*0Sstevel@tonic-gate @a = scalar unpack $pat, $p; 708*0Sstevel@tonic-gate is(scalar @a, 1); 709*0Sstevel@tonic-gate is($a[0], 42); 710*0Sstevel@tonic-gate } 711*0Sstevel@tonic-gate 712*0Sstevel@tonic-gate # shorts (of all flavours) didn't calculate checksums > 32 bits with floating 713*0Sstevel@tonic-gate # point, so a pathologically long pattern would wrap at 32 bits. 714*0Sstevel@tonic-gate my $pat = "\xff\xff"x65538; # Start with it long, to save any copying. 715*0Sstevel@tonic-gate foreach (4,3,2,1,0) { 716*0Sstevel@tonic-gate my $len = 65534 + $_; 717*0Sstevel@tonic-gate is(unpack ("%33n$len", $pat), 65535 * $len); 718*0Sstevel@tonic-gate } 719*0Sstevel@tonic-gate} 720*0Sstevel@tonic-gate 721*0Sstevel@tonic-gate 722*0Sstevel@tonic-gate# pack x X @ 723*0Sstevel@tonic-gateforeach ( 724*0Sstevel@tonic-gate ['x', "N", "\0"], 725*0Sstevel@tonic-gate ['x4', "N", "\0"x4], 726*0Sstevel@tonic-gate ['xX', "N", ""], 727*0Sstevel@tonic-gate ['xXa*', "Nick", "Nick"], 728*0Sstevel@tonic-gate ['a5Xa5', "cameL", "llama", "camellama"], 729*0Sstevel@tonic-gate ['@4', 'N', "\0"x4], 730*0Sstevel@tonic-gate ['a*@8a*', 'Camel', 'Dromedary', "Camel\0\0\0Dromedary"], 731*0Sstevel@tonic-gate ['a*@4a', 'Perl rules', '!', 'Perl!'], 732*0Sstevel@tonic-gate) 733*0Sstevel@tonic-gate{ 734*0Sstevel@tonic-gate my ($template, @in) = @$_; 735*0Sstevel@tonic-gate my $out = pop @in; 736*0Sstevel@tonic-gate my $got = eval {pack $template, @in}; 737*0Sstevel@tonic-gate is($@, ''); 738*0Sstevel@tonic-gate is($out, $got) || 739*0Sstevel@tonic-gate printf "# pack ('$template', %s) gave %s expected %s\n", 740*0Sstevel@tonic-gate encode_list (@in), encode_list ($got), encode_list ($out); 741*0Sstevel@tonic-gate} 742*0Sstevel@tonic-gate 743*0Sstevel@tonic-gate# unpack x X @ 744*0Sstevel@tonic-gateforeach ( 745*0Sstevel@tonic-gate ['x', "N"], 746*0Sstevel@tonic-gate ['xX', "N"], 747*0Sstevel@tonic-gate ['xXa*', "Nick", "Nick"], 748*0Sstevel@tonic-gate ['a5Xa5', "camellama", "camel", "llama"], 749*0Sstevel@tonic-gate ['@3', "ice"], 750*0Sstevel@tonic-gate ['@2a2', "water", "te"], 751*0Sstevel@tonic-gate ['a*@1a3', "steam", "steam", "tea"], 752*0Sstevel@tonic-gate) 753*0Sstevel@tonic-gate{ 754*0Sstevel@tonic-gate my ($template, $in, @out) = @$_; 755*0Sstevel@tonic-gate my @got = eval {unpack $template, $in}; 756*0Sstevel@tonic-gate is($@, ''); 757*0Sstevel@tonic-gate ok (list_eq (\@got, \@out)) || 758*0Sstevel@tonic-gate printf "# list unpack ('$template', %s) gave %s expected %s\n", 759*0Sstevel@tonic-gate _qq($in), encode_list (@got), encode_list (@out); 760*0Sstevel@tonic-gate 761*0Sstevel@tonic-gate my $got = eval {unpack $template, $in}; 762*0Sstevel@tonic-gate is($@, ''); 763*0Sstevel@tonic-gate @out ? is( $got, $out[0] ) # 1 or more items; should get first 764*0Sstevel@tonic-gate : ok( !defined $got ) # 0 items; should get undef 765*0Sstevel@tonic-gate or printf "# scalar unpack ('$template', %s) gave %s expected %s\n", 766*0Sstevel@tonic-gate _qq($in), encode_list ($got), encode_list ($out[0]); 767*0Sstevel@tonic-gate} 768*0Sstevel@tonic-gate 769*0Sstevel@tonic-gate{ 770*0Sstevel@tonic-gate my $t = 'Z*Z*'; 771*0Sstevel@tonic-gate my ($u, $v) = qw(foo xyzzy); 772*0Sstevel@tonic-gate my $p = pack($t, $u, $v); 773*0Sstevel@tonic-gate my @u = unpack($t, $p); 774*0Sstevel@tonic-gate is(scalar @u, 2); 775*0Sstevel@tonic-gate is($u[0], $u); 776*0Sstevel@tonic-gate is($u[1], $v); 777*0Sstevel@tonic-gate} 778*0Sstevel@tonic-gate 779*0Sstevel@tonic-gate{ 780*0Sstevel@tonic-gate is((unpack("w/a*", "\x02abc"))[0], "ab"); 781*0Sstevel@tonic-gate 782*0Sstevel@tonic-gate # "w/a*" should be seen as one unit 783*0Sstevel@tonic-gate 784*0Sstevel@tonic-gate is(scalar unpack("w/a*", "\x02abc"), "ab"); 785*0Sstevel@tonic-gate} 786*0Sstevel@tonic-gate 787*0Sstevel@tonic-gate{ 788*0Sstevel@tonic-gate # from Wolfgang Laun: fix in change #13163 789*0Sstevel@tonic-gate 790*0Sstevel@tonic-gate my $s = 'ABC' x 10; 791*0Sstevel@tonic-gate my $t = '*'; 792*0Sstevel@tonic-gate my $x = ord($t); 793*0Sstevel@tonic-gate my $buf = pack( 'Z*/A* C', $s, $x ); 794*0Sstevel@tonic-gate my $y; 795*0Sstevel@tonic-gate 796*0Sstevel@tonic-gate my $h = $buf; 797*0Sstevel@tonic-gate $h =~ s/[^[:print:]]/./g; 798*0Sstevel@tonic-gate ( $s, $y ) = unpack( "Z*/A* C", $buf ); 799*0Sstevel@tonic-gate is($h, "30.ABCABCABCABCABCABCABCABCABCABC$t"); 800*0Sstevel@tonic-gate is(length $buf, 34); 801*0Sstevel@tonic-gate is($s, "ABCABCABCABCABCABCABCABCABCABC"); 802*0Sstevel@tonic-gate is($y, $x); 803*0Sstevel@tonic-gate} 804*0Sstevel@tonic-gate 805*0Sstevel@tonic-gate{ 806*0Sstevel@tonic-gate # from Wolfgang Laun: fix in change #13288 807*0Sstevel@tonic-gate 808*0Sstevel@tonic-gate eval { my $t=unpack("P*", "abc") }; 809*0Sstevel@tonic-gate like($@, qr/'P' must have an explicit size/); 810*0Sstevel@tonic-gate} 811*0Sstevel@tonic-gate 812*0Sstevel@tonic-gate{ # Grouping constructs 813*0Sstevel@tonic-gate my (@a, @b); 814*0Sstevel@tonic-gate @a = unpack '(SL)', pack 'SLSLSL', 67..90; 815*0Sstevel@tonic-gate is("@a", "67 68"); 816*0Sstevel@tonic-gate @a = unpack '(SL)3', pack 'SLSLSL', 67..90; 817*0Sstevel@tonic-gate @b = (67..72); 818*0Sstevel@tonic-gate is("@a", "@b"); 819*0Sstevel@tonic-gate @a = unpack '(SL)3', pack 'SLSLSLSL', 67..90; 820*0Sstevel@tonic-gate is("@a", "@b"); 821*0Sstevel@tonic-gate @a = unpack '(SL)[3]', pack 'SLSLSLSL', 67..90; 822*0Sstevel@tonic-gate is("@a", "@b"); 823*0Sstevel@tonic-gate @a = unpack '(SL)[2] SL', pack 'SLSLSLSL', 67..90; 824*0Sstevel@tonic-gate is("@a", "@b"); 825*0Sstevel@tonic-gate @a = unpack 'A/(SL)', pack 'ASLSLSLSL', 3, 67..90; 826*0Sstevel@tonic-gate is("@a", "@b"); 827*0Sstevel@tonic-gate @a = unpack 'A/(SL)SL', pack 'ASLSLSLSL', 2, 67..90; 828*0Sstevel@tonic-gate is("@a", "@b"); 829*0Sstevel@tonic-gate @a = unpack '(SL)*', pack 'SLSLSLSL', 67..90; 830*0Sstevel@tonic-gate @b = (67..74); 831*0Sstevel@tonic-gate is("@a", "@b"); 832*0Sstevel@tonic-gate @a = unpack '(SL)*SL', pack 'SLSLSLSL', 67..90; 833*0Sstevel@tonic-gate is("@a", "@b"); 834*0Sstevel@tonic-gate eval { @a = unpack '(*SL)', '' }; 835*0Sstevel@tonic-gate like($@, qr/\(\)-group starts with a count/); 836*0Sstevel@tonic-gate eval { @a = unpack '(3SL)', '' }; 837*0Sstevel@tonic-gate like($@, qr/\(\)-group starts with a count/); 838*0Sstevel@tonic-gate eval { @a = unpack '([3]SL)', '' }; 839*0Sstevel@tonic-gate like($@, qr/\(\)-group starts with a count/); 840*0Sstevel@tonic-gate eval { @a = pack '(*SL)' }; 841*0Sstevel@tonic-gate like($@, qr/\(\)-group starts with a count/); 842*0Sstevel@tonic-gate @a = unpack '(SL)3 SL', pack '(SL)4', 67..74; 843*0Sstevel@tonic-gate is("@a", "@b"); 844*0Sstevel@tonic-gate @a = unpack '(SL)3 SL', pack '(SL)[4]', 67..74; 845*0Sstevel@tonic-gate is("@a", "@b"); 846*0Sstevel@tonic-gate @a = unpack '(SL)3 SL', pack '(SL)*', 67..74; 847*0Sstevel@tonic-gate is("@a", "@b"); 848*0Sstevel@tonic-gate} 849*0Sstevel@tonic-gate 850*0Sstevel@tonic-gate{ # more on grouping (W.Laun) 851*0Sstevel@tonic-gate use warnings; 852*0Sstevel@tonic-gate my $warning; 853*0Sstevel@tonic-gate local $SIG{__WARN__} = sub { 854*0Sstevel@tonic-gate $warning = $_[0]; 855*0Sstevel@tonic-gate }; 856*0Sstevel@tonic-gate # @ absolute within ()-group 857*0Sstevel@tonic-gate my $badc = pack( '(a)*', unpack( '(@1a @0a @2)*', 'abcd' ) ); 858*0Sstevel@tonic-gate is( $badc, 'badc' ); 859*0Sstevel@tonic-gate my @b = ( 1, 2, 3 ); 860*0Sstevel@tonic-gate my $buf = pack( '(@1c)((@2C)@3c)', @b ); 861*0Sstevel@tonic-gate is( $buf, "\0\1\0\0\2\3" ); 862*0Sstevel@tonic-gate my @a = unpack( '(@1c)((@2c)@3c)', $buf ); 863*0Sstevel@tonic-gate is( "@a", "@b" ); 864*0Sstevel@tonic-gate 865*0Sstevel@tonic-gate # various unpack count/code scenarios 866*0Sstevel@tonic-gate my @Env = ( a => 'AAA', b => 'BBB' ); 867*0Sstevel@tonic-gate my $env = pack( 'S(S/A*S/A*)*', @Env/2, @Env ); 868*0Sstevel@tonic-gate 869*0Sstevel@tonic-gate # unpack full length - ok 870*0Sstevel@tonic-gate my @pup = unpack( 'S/(S/A* S/A*)', $env ); 871*0Sstevel@tonic-gate is( "@pup", "@Env" ); 872*0Sstevel@tonic-gate 873*0Sstevel@tonic-gate # warn when count/code goes beyond end of string 874*0Sstevel@tonic-gate # \0002 \0001 a \0003 AAA \0001 b \0003 BBB 875*0Sstevel@tonic-gate # 2 4 5 7 10 1213 876*0Sstevel@tonic-gate eval { @pup = unpack( 'S/(S/A* S/A*)', substr( $env, 0, 13 ) ) }; 877*0Sstevel@tonic-gate like( $@, qr{length/code after end of string} ); 878*0Sstevel@tonic-gate 879*0Sstevel@tonic-gate # postfix repeat count 880*0Sstevel@tonic-gate $env = pack( '(S/A* S/A*)' . @Env/2, @Env ); 881*0Sstevel@tonic-gate 882*0Sstevel@tonic-gate # warn when count/code goes beyond end of string 883*0Sstevel@tonic-gate # \0001 a \0003 AAA \0001 b \0003 BBB 884*0Sstevel@tonic-gate # 2 3c 5 8 10 11 13 16 885*0Sstevel@tonic-gate eval { @pup = unpack( '(S/A* S/A*)' . @Env/2, substr( $env, 0, 11 ) ) }; 886*0Sstevel@tonic-gate like( $@, qr{length/code after end of string} ); 887*0Sstevel@tonic-gate 888*0Sstevel@tonic-gate # catch stack overflow/segfault 889*0Sstevel@tonic-gate eval { $_ = pack( ('(' x 105) . 'A' . (')' x 105) ); }; 890*0Sstevel@tonic-gate like( $@, qr{Too deeply nested \(\)-groups} ); 891*0Sstevel@tonic-gate} 892*0Sstevel@tonic-gate 893*0Sstevel@tonic-gate{ # syntax checks (W.Laun) 894*0Sstevel@tonic-gate use warnings; 895*0Sstevel@tonic-gate my @warning; 896*0Sstevel@tonic-gate local $SIG{__WARN__} = sub { 897*0Sstevel@tonic-gate push( @warning, $_[0] ); 898*0Sstevel@tonic-gate }; 899*0Sstevel@tonic-gate eval { my $s = pack( 'Ax![4c]A', 1..5 ); }; 900*0Sstevel@tonic-gate like( $@, qr{Malformed integer in \[\]} ); 901*0Sstevel@tonic-gate 902*0Sstevel@tonic-gate eval { my $buf = pack( '(c/*a*)', 'AAA', 'BB' ); }; 903*0Sstevel@tonic-gate like( $@, qr{'/' does not take a repeat count} ); 904*0Sstevel@tonic-gate 905*0Sstevel@tonic-gate eval { my @inf = unpack( 'c/1a', "\x03AAA\x02BB" ); }; 906*0Sstevel@tonic-gate like( $@, qr{'/' does not take a repeat count} ); 907*0Sstevel@tonic-gate 908*0Sstevel@tonic-gate eval { my @inf = unpack( 'c/*a', "\x03AAA\x02BB" ); }; 909*0Sstevel@tonic-gate like( $@, qr{'/' does not take a repeat count} ); 910*0Sstevel@tonic-gate 911*0Sstevel@tonic-gate # white space where possible 912*0Sstevel@tonic-gate my @Env = ( a => 'AAA', b => 'BBB' ); 913*0Sstevel@tonic-gate my $env = pack( ' S ( S / A* S / A* )* ', @Env/2, @Env ); 914*0Sstevel@tonic-gate my @pup = unpack( ' S / ( S / A* S / A* ) ', $env ); 915*0Sstevel@tonic-gate is( "@pup", "@Env" ); 916*0Sstevel@tonic-gate 917*0Sstevel@tonic-gate # white space in 4 wrong places 918*0Sstevel@tonic-gate for my $temp ( 'A ![4]', 'A [4]', 'A *', 'A 4' ){ 919*0Sstevel@tonic-gate eval { my $s = pack( $temp, 'B' ); }; 920*0Sstevel@tonic-gate like( $@, qr{Invalid type } ); 921*0Sstevel@tonic-gate } 922*0Sstevel@tonic-gate 923*0Sstevel@tonic-gate # warning for commas 924*0Sstevel@tonic-gate @warning = (); 925*0Sstevel@tonic-gate my $x = pack( 'I,A', 4, 'X' ); 926*0Sstevel@tonic-gate like( $warning[0], qr{Invalid type ','} ); 927*0Sstevel@tonic-gate 928*0Sstevel@tonic-gate # comma warning only once 929*0Sstevel@tonic-gate @warning = (); 930*0Sstevel@tonic-gate $x = pack( 'C(C,C)C,C', 65..71 ); 931*0Sstevel@tonic-gate like( scalar @warning, 1 ); 932*0Sstevel@tonic-gate 933*0Sstevel@tonic-gate # forbidden code in [] 934*0Sstevel@tonic-gate eval { my $x = pack( 'A[@4]', 'XXXX' ); }; 935*0Sstevel@tonic-gate like( $@, qr{Within \[\]-length '\@' not allowed} ); 936*0Sstevel@tonic-gate 937*0Sstevel@tonic-gate # @ repeat default 1 938*0Sstevel@tonic-gate my $s = pack( 'AA@A', 'A', 'B', 'C' ); 939*0Sstevel@tonic-gate my @c = unpack( 'AA@A', $s ); 940*0Sstevel@tonic-gate is( $s, 'AC' ); 941*0Sstevel@tonic-gate is( "@c", "A C C" ); 942*0Sstevel@tonic-gate 943*0Sstevel@tonic-gate # no unpack code after / 944*0Sstevel@tonic-gate eval { my @a = unpack( "C/", "\3" ); }; 945*0Sstevel@tonic-gate like( $@, qr{Code missing after '/'} ); 946*0Sstevel@tonic-gate 947*0Sstevel@tonic-gate} 948*0Sstevel@tonic-gate 949*0Sstevel@tonic-gate{ # Repeat count [SUBEXPR] 950*0Sstevel@tonic-gate my @codes = qw( x A Z a c C B b H h s v n S i I l V N L p P f F d 951*0Sstevel@tonic-gate s! S! i! I! l! L! j J); 952*0Sstevel@tonic-gate my $G; 953*0Sstevel@tonic-gate if (eval { pack 'q', 1 } ) { 954*0Sstevel@tonic-gate push @codes, qw(q Q); 955*0Sstevel@tonic-gate } else { 956*0Sstevel@tonic-gate push @codes, qw(c C); # Keep the count the same 957*0Sstevel@tonic-gate } 958*0Sstevel@tonic-gate if (eval { pack 'D', 1 } ) { 959*0Sstevel@tonic-gate push @codes, 'D'; 960*0Sstevel@tonic-gate } else { 961*0Sstevel@tonic-gate push @codes, 'd'; # Keep the count the same 962*0Sstevel@tonic-gate } 963*0Sstevel@tonic-gate 964*0Sstevel@tonic-gate my %val; 965*0Sstevel@tonic-gate @val{@codes} = map { / [Xx] (?{ undef }) 966*0Sstevel@tonic-gate | [AZa] (?{ 'something' }) 967*0Sstevel@tonic-gate | C (?{ 214 }) 968*0Sstevel@tonic-gate | c (?{ 114 }) 969*0Sstevel@tonic-gate | [Bb] (?{ '101' }) 970*0Sstevel@tonic-gate | [Hh] (?{ 'b8' }) 971*0Sstevel@tonic-gate | [svnSiIlVNLqQjJ] (?{ 10111 }) 972*0Sstevel@tonic-gate | [FfDd] (?{ 1.36514538e67 }) 973*0Sstevel@tonic-gate | [pP] (?{ "try this buffer" }) 974*0Sstevel@tonic-gate /x; $^R } @codes; 975*0Sstevel@tonic-gate my @end = (0x12345678, 0x23456781, 0x35465768, 0x15263748); 976*0Sstevel@tonic-gate my $end = "N4"; 977*0Sstevel@tonic-gate 978*0Sstevel@tonic-gate for my $type (@codes) { 979*0Sstevel@tonic-gate my @list = $val{$type}; 980*0Sstevel@tonic-gate @list = () unless defined $list[0]; 981*0Sstevel@tonic-gate for my $count ('', '3', '[11]') { 982*0Sstevel@tonic-gate my $c = 1; 983*0Sstevel@tonic-gate $c = $1 if $count =~ /(\d+)/; 984*0Sstevel@tonic-gate my @list1 = @list; 985*0Sstevel@tonic-gate @list1 = (@list1) x $c unless $type =~ /[XxAaZBbHhP]/; 986*0Sstevel@tonic-gate for my $groupend ('', ')2', ')[8]') { 987*0Sstevel@tonic-gate my $groupbegin = ($groupend ? '(' : ''); 988*0Sstevel@tonic-gate $c = 1; 989*0Sstevel@tonic-gate $c = $1 if $groupend =~ /(\d+)/; 990*0Sstevel@tonic-gate my @list2 = (@list1) x $c; 991*0Sstevel@tonic-gate 992*0Sstevel@tonic-gate my $junk1 = "$groupbegin $type$count $groupend"; 993*0Sstevel@tonic-gate # print "# junk1=$junk1\n"; 994*0Sstevel@tonic-gate my $p = pack $junk1, @list2; 995*0Sstevel@tonic-gate my $half = int( (length $p)/2 ); 996*0Sstevel@tonic-gate for my $move ('', "X$half", "X!$half", 'x1', 'x!8', "x$half") { 997*0Sstevel@tonic-gate my $junk = "$junk1 $move"; 998*0Sstevel@tonic-gate # print "# junk='$junk', list=(@list2)\n"; 999*0Sstevel@tonic-gate $p = pack "$junk $end", @list2, @end; 1000*0Sstevel@tonic-gate my @l = unpack "x[$junk] $end", $p; 1001*0Sstevel@tonic-gate is(scalar @l, scalar @end); 1002*0Sstevel@tonic-gate is("@l", "@end", "skipping x[$junk]"); 1003*0Sstevel@tonic-gate } 1004*0Sstevel@tonic-gate } 1005*0Sstevel@tonic-gate } 1006*0Sstevel@tonic-gate } 1007*0Sstevel@tonic-gate} 1008*0Sstevel@tonic-gate 1009*0Sstevel@tonic-gate# / is recognized after spaces in scalar context 1010*0Sstevel@tonic-gate# XXXX no spaces are allowed in pack... In pack only before the slash... 1011*0Sstevel@tonic-gateis(scalar unpack('A /A Z20', pack 'A/A* Z20', 'bcde', 'xxxxx'), 'bcde'); 1012*0Sstevel@tonic-gateis(scalar unpack('A /A /A Z20', '3004bcde'), 'bcde'); 1013*0Sstevel@tonic-gate 1014*0Sstevel@tonic-gate{ # X! and x! 1015*0Sstevel@tonic-gate my $t = 'C[3] x!8 C[2]'; 1016*0Sstevel@tonic-gate my @a = (0x73..0x77); 1017*0Sstevel@tonic-gate my $p = pack($t, @a); 1018*0Sstevel@tonic-gate is($p, "\x73\x74\x75\0\0\0\0\0\x76\x77"); 1019*0Sstevel@tonic-gate my @b = unpack $t, $p; 1020*0Sstevel@tonic-gate is(scalar @b, scalar @a); 1021*0Sstevel@tonic-gate is("@b", "@a", 'x!8'); 1022*0Sstevel@tonic-gate $t = 'x[5] C[6] X!8 C[2]'; 1023*0Sstevel@tonic-gate @a = (0x73..0x7a); 1024*0Sstevel@tonic-gate $p = pack($t, @a); 1025*0Sstevel@tonic-gate is($p, "\0\0\0\0\0\x73\x74\x75\x79\x7a"); 1026*0Sstevel@tonic-gate @b = unpack $t, $p; 1027*0Sstevel@tonic-gate @a = (0x73..0x75, 0x79, 0x7a, 0x79, 0x7a); 1028*0Sstevel@tonic-gate is(scalar @b, scalar @a); 1029*0Sstevel@tonic-gate is("@b", "@a"); 1030*0Sstevel@tonic-gate} 1031*0Sstevel@tonic-gate 1032*0Sstevel@tonic-gate{ # struct {char c1; double d; char cc[2];} 1033*0Sstevel@tonic-gate my $t = 'C x![d] d C[2]'; 1034*0Sstevel@tonic-gate my @a = (173, 1.283476517e-45, 42, 215); 1035*0Sstevel@tonic-gate my $p = pack $t, @a; 1036*0Sstevel@tonic-gate ok( length $p); 1037*0Sstevel@tonic-gate my @b = unpack "$t X[$t] $t", $p; # Extract, step back, extract again 1038*0Sstevel@tonic-gate is(scalar @b, 2 * scalar @a); 1039*0Sstevel@tonic-gate $b = "@b"; 1040*0Sstevel@tonic-gate $b =~ s/(?:17000+|16999+)\d+(e-45) /17$1 /gi; # stringification is gamble 1041*0Sstevel@tonic-gate is($b, "@a @a"); 1042*0Sstevel@tonic-gate 1043*0Sstevel@tonic-gate my $warning; 1044*0Sstevel@tonic-gate local $SIG{__WARN__} = sub { 1045*0Sstevel@tonic-gate $warning = $_[0]; 1046*0Sstevel@tonic-gate }; 1047*0Sstevel@tonic-gate @b = unpack "x[C] x[$t] X[$t] X[C] $t", "$p\0"; 1048*0Sstevel@tonic-gate 1049*0Sstevel@tonic-gate is($warning, undef); 1050*0Sstevel@tonic-gate is(scalar @b, scalar @a); 1051*0Sstevel@tonic-gate $b = "@b"; 1052*0Sstevel@tonic-gate $b =~ s/(?:17000+|16999+)\d+(e-45) /17$1 /gi; # stringification is gamble 1053*0Sstevel@tonic-gate is($b, "@a"); 1054*0Sstevel@tonic-gate} 1055*0Sstevel@tonic-gate 1056*0Sstevel@tonic-gateis(length(pack("j", 0)), $Config{ivsize}); 1057*0Sstevel@tonic-gateis(length(pack("J", 0)), $Config{uvsize}); 1058*0Sstevel@tonic-gateis(length(pack("F", 0)), $Config{nvsize}); 1059*0Sstevel@tonic-gate 1060*0Sstevel@tonic-gatenumbers ('j', -2147483648, -1, 0, 1, 2147483647); 1061*0Sstevel@tonic-gatenumbers ('J', 0, 1, 2147483647, 2147483648, 4294967295); 1062*0Sstevel@tonic-gatenumbers ('F', -(2**34), -1, 0, 1, 2**34); 1063*0Sstevel@tonic-gateSKIP: { 1064*0Sstevel@tonic-gate my $t = eval { unpack("D*", pack("D", 12.34)) }; 1065*0Sstevel@tonic-gate 1066*0Sstevel@tonic-gate skip "Long doubles not in use", 56 if $@ =~ /Invalid type/; 1067*0Sstevel@tonic-gate 1068*0Sstevel@tonic-gate is(length(pack("D", 0)), $Config{longdblsize}); 1069*0Sstevel@tonic-gate numbers ('D', -(2**34), -1, 0, 1, 2**34); 1070*0Sstevel@tonic-gate} 1071*0Sstevel@tonic-gate 1072*0Sstevel@tonic-gate# Maybe this knowledge needs to be "global" for all of pack.t 1073*0Sstevel@tonic-gate# Or a "can checksum" which would effectively be all the number types" 1074*0Sstevel@tonic-gatemy %cant_checksum = map {$_=> 1} qw(A Z u w); 1075*0Sstevel@tonic-gate# not a b B h H 1076*0Sstevel@tonic-gateforeach my $template (qw(A Z c C s S i I l L n N v V q Q j J f d F D u U w)) { 1077*0Sstevel@tonic-gate SKIP: { 1078*0Sstevel@tonic-gate my $packed = eval {pack "${template}4", 1, 4, 9, 16}; 1079*0Sstevel@tonic-gate if ($@) { 1080*0Sstevel@tonic-gate die unless $@ =~ /Invalid type '$template'/; 1081*0Sstevel@tonic-gate skip ("$template not supported on this perl", 1082*0Sstevel@tonic-gate $cant_checksum{$template} ? 4 : 8); 1083*0Sstevel@tonic-gate } 1084*0Sstevel@tonic-gate my @unpack4 = unpack "${template}4", $packed; 1085*0Sstevel@tonic-gate my @unpack = unpack "${template}*", $packed; 1086*0Sstevel@tonic-gate my @unpack1 = unpack "${template}", $packed; 1087*0Sstevel@tonic-gate my @unpack1s = scalar unpack "${template}", $packed; 1088*0Sstevel@tonic-gate my @unpack4s = scalar unpack "${template}4", $packed; 1089*0Sstevel@tonic-gate my @unpacks = scalar unpack "${template}*", $packed; 1090*0Sstevel@tonic-gate 1091*0Sstevel@tonic-gate my @tests = ( ["${template}4 vs ${template}*", \@unpack4, \@unpack], 1092*0Sstevel@tonic-gate ["scalar ${template} ${template}", \@unpack1s, \@unpack1], 1093*0Sstevel@tonic-gate ["scalar ${template}4 vs ${template}", \@unpack4s, \@unpack1], 1094*0Sstevel@tonic-gate ["scalar ${template}* vs ${template}", \@unpacks, \@unpack1], 1095*0Sstevel@tonic-gate ); 1096*0Sstevel@tonic-gate 1097*0Sstevel@tonic-gate unless ($cant_checksum{$template}) { 1098*0Sstevel@tonic-gate my @unpack4_c = unpack "\%${template}4", $packed; 1099*0Sstevel@tonic-gate my @unpack_c = unpack "\%${template}*", $packed; 1100*0Sstevel@tonic-gate my @unpack1_c = unpack "\%${template}", $packed; 1101*0Sstevel@tonic-gate my @unpack1s_c = scalar unpack "\%${template}", $packed; 1102*0Sstevel@tonic-gate my @unpack4s_c = scalar unpack "\%${template}4", $packed; 1103*0Sstevel@tonic-gate my @unpacks_c = scalar unpack "\%${template}*", $packed; 1104*0Sstevel@tonic-gate 1105*0Sstevel@tonic-gate push @tests, 1106*0Sstevel@tonic-gate ( ["% ${template}4 vs ${template}*", \@unpack4_c, \@unpack_c], 1107*0Sstevel@tonic-gate ["% scalar ${template} ${template}", \@unpack1s_c, \@unpack1_c], 1108*0Sstevel@tonic-gate ["% scalar ${template}4 vs ${template}*", \@unpack4s_c, \@unpack_c], 1109*0Sstevel@tonic-gate ["% scalar ${template}* vs ${template}*", \@unpacks_c, \@unpack_c], 1110*0Sstevel@tonic-gate ); 1111*0Sstevel@tonic-gate } 1112*0Sstevel@tonic-gate foreach my $test (@tests) { 1113*0Sstevel@tonic-gate ok (list_eq ($test->[1], $test->[2]), $test->[0]) || 1114*0Sstevel@tonic-gate printf "# unpack gave %s expected %s\n", 1115*0Sstevel@tonic-gate encode_list (@{$test->[1]}), encode_list (@{$test->[2]}); 1116*0Sstevel@tonic-gate } 1117*0Sstevel@tonic-gate } 1118*0Sstevel@tonic-gate} 1119*0Sstevel@tonic-gate 1120*0Sstevel@tonic-gateok(pack('u2', 'AA'), "[perl #8026]"); # used to hang and eat RAM in perl 5.7.2 1121*0Sstevel@tonic-gate 1122*0Sstevel@tonic-gateok(1, "fake success (change #18751, feature not present in 5.8.1)"); 1123*0Sstevel@tonic-gate 1124*0Sstevel@tonic-gate{ 1125*0Sstevel@tonic-gate my $a = "X\t01234567\n" x 100; 1126*0Sstevel@tonic-gate my @a = unpack("(a1 c/a)*", $a); 1127*0Sstevel@tonic-gate is(scalar @a, 200, "[perl #15288]"); 1128*0Sstevel@tonic-gate is($a[-1], "01234567\n", "[perl #15288]"); 1129*0Sstevel@tonic-gate is($a[-2], "X", "[perl #15288]"); 1130*0Sstevel@tonic-gate} 1131