| 1 | #!./perl
|
|---|
| 2 | # FIXME - why isn't this -w clean in maint?
|
|---|
| 3 |
|
|---|
| 4 | BEGIN {
|
|---|
| 5 | chdir 't' if -d 't';
|
|---|
| 6 | @INC = '../lib';
|
|---|
| 7 | require './test.pl';
|
|---|
| 8 | }
|
|---|
| 9 |
|
|---|
| 10 | # This is truth in an if statement, and could be a skip message
|
|---|
| 11 | my $no_endianness = $] > 5.009 ? '' :
|
|---|
| 12 | "Endianness pack modifiers not available on this perl";
|
|---|
| 13 | my $no_signedness = $] > 5.009 ? '' :
|
|---|
| 14 | "Signed/unsigned pack modifiers not available on this perl";
|
|---|
| 15 |
|
|---|
| 16 | plan tests => 13864;
|
|---|
| 17 |
|
|---|
| 18 | use strict;
|
|---|
| 19 | # use warnings;
|
|---|
| 20 | use Config;
|
|---|
| 21 |
|
|---|
| 22 | my $Is_EBCDIC = (defined $Config{ebcdic} && $Config{ebcdic} eq 'define');
|
|---|
| 23 | my $Perl = which_perl();
|
|---|
| 24 | my @valid_errors = (qr/^Invalid type '\w'/);
|
|---|
| 25 |
|
|---|
| 26 | my $ByteOrder = 'unknown';
|
|---|
| 27 | my $maybe_not_avail = '(?:hto[bl]e|[bl]etoh)';
|
|---|
| 28 | if ($no_endianness) {
|
|---|
| 29 | push @valid_errors, qr/^Invalid type '[<>]'/;
|
|---|
| 30 | } elsif ($Config{byteorder} =~ /^1234(?:5678)?$/) {
|
|---|
| 31 | $ByteOrder = 'little';
|
|---|
| 32 | $maybe_not_avail = '(?:htobe|betoh)';
|
|---|
| 33 | }
|
|---|
| 34 | elsif ($Config{byteorder} =~ /^(?:8765)?4321$/) {
|
|---|
| 35 | $ByteOrder = 'big';
|
|---|
| 36 | $maybe_not_avail = '(?:htole|letoh)';
|
|---|
| 37 | }
|
|---|
| 38 | else {
|
|---|
| 39 | push @valid_errors, qr/^Can't (?:un)?pack (?:big|little)-endian .*? on this platform/;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | if ($no_signedness) {
|
|---|
| 43 | push @valid_errors, qr/^'!' allowed only after types sSiIlLxX in (?:un)?pack/;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | for my $size ( 16, 32, 64 ) {
|
|---|
| 47 | if (defined $Config{"u${size}size"} and $Config{"u${size}size"} != ($size >> 3)) {
|
|---|
| 48 | push @valid_errors, qr/^Perl_my_$maybe_not_avail$size\(\) not available/;
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | my $IsTwosComplement = pack('i', -1) eq "\xFF" x $Config{intsize};
|
|---|
| 53 | print "# \$IsTwosComplement = $IsTwosComplement\n";
|
|---|
| 54 |
|
|---|
| 55 | sub is_valid_error
|
|---|
| 56 | {
|
|---|
| 57 | my $err = shift;
|
|---|
| 58 |
|
|---|
| 59 | for my $e (@valid_errors) {
|
|---|
| 60 | $err =~ $e and return 1;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | return 0;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | sub encode_list {
|
|---|
| 67 | my @result = map {_qq($_)} @_;
|
|---|
| 68 | if (@result == 1) {
|
|---|
| 69 | return @result;
|
|---|
| 70 | }
|
|---|
| 71 | return '(' . join (', ', @result) . ')';
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | sub list_eq ($$) {
|
|---|
| 76 | my ($l, $r) = @_;
|
|---|
| 77 | return 0 unless @$l == @$r;
|
|---|
| 78 | for my $i (0..$#$l) {
|
|---|
| 79 | if (defined $l->[$i]) {
|
|---|
| 80 | return 0 unless defined ($r->[$i]) && $l->[$i] eq $r->[$i];
|
|---|
| 81 | } else {
|
|---|
| 82 | return 0 if defined $r->[$i]
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | return 1;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | ##############################################################################
|
|---|
| 89 | #
|
|---|
| 90 | # Here starteth the tests
|
|---|
| 91 | #
|
|---|
| 92 |
|
|---|
| 93 | {
|
|---|
| 94 | my $format = "c2 x5 C C x s d i l a6";
|
|---|
| 95 | # Need the expression in here to force ary[5] to be numeric. This avoids
|
|---|
| 96 | # test2 failing because ary2 goes str->numeric->str and ary doesn't.
|
|---|
| 97 | my @ary = (1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,
|
|---|
| 98 | "abcdef");
|
|---|
| 99 | my $foo = pack($format,@ary);
|
|---|
| 100 | my @ary2 = unpack($format,$foo);
|
|---|
| 101 |
|
|---|
| 102 | is($#ary, $#ary2);
|
|---|
| 103 |
|
|---|
| 104 | my $out1=join(':',@ary);
|
|---|
| 105 | my $out2=join(':',@ary2);
|
|---|
| 106 | # Using long double NVs may introduce greater accuracy than wanted.
|
|---|
| 107 | $out1 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
|
|---|
| 108 | $out2 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
|
|---|
| 109 | is($out1, $out2);
|
|---|
| 110 |
|
|---|
| 111 | like($foo, qr/def/);
|
|---|
| 112 | }
|
|---|
| 113 | # How about counting bits?
|
|---|
| 114 |
|
|---|
| 115 | {
|
|---|
| 116 | my $x;
|
|---|
| 117 | is( ($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")), 16 );
|
|---|
| 118 |
|
|---|
| 119 | is( ($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")), 12 );
|
|---|
| 120 |
|
|---|
| 121 | is( ($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")), 9 );
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | {
|
|---|
| 125 | my $sum = 129; # ASCII
|
|---|
| 126 | $sum = 103 if $Is_EBCDIC;
|
|---|
| 127 |
|
|---|
| 128 | my $x;
|
|---|
| 129 | is( ($x = unpack("%32B*", "Now is the time for all good blurfl")), $sum );
|
|---|
| 130 |
|
|---|
| 131 | my $foo;
|
|---|
| 132 | open(BIN, $Perl) || die "Can't open $Perl: $!\n";
|
|---|
| 133 | sysread BIN, $foo, 8192;
|
|---|
| 134 | close BIN;
|
|---|
| 135 |
|
|---|
| 136 | $sum = unpack("%32b*", $foo);
|
|---|
| 137 | my $longway = unpack("b*", $foo);
|
|---|
| 138 | is( $sum, $longway =~ tr/1/1/ );
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | {
|
|---|
| 142 | my $x;
|
|---|
| 143 | is( ($x = unpack("I",pack("I", 0xFFFFFFFF))), 0xFFFFFFFF );
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | {
|
|---|
| 147 | # check 'w'
|
|---|
| 148 | my @x = (5,130,256,560,32000,3097152,268435455,1073741844, 2**33,
|
|---|
| 149 | '4503599627365785','23728385234614992549757750638446');
|
|---|
| 150 | my $x = pack('w*', @x);
|
|---|
| 151 | my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f8480808014A0808'.
|
|---|
| 152 | '0800087ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e';
|
|---|
| 153 |
|
|---|
| 154 | is($x, $y);
|
|---|
| 155 |
|
|---|
| 156 | my @y = unpack('w*', $y);
|
|---|
| 157 | my $a;
|
|---|
| 158 | while ($a = pop @x) {
|
|---|
| 159 | my $b = pop @y;
|
|---|
| 160 | is($a, $b);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | @y = unpack('w2', $x);
|
|---|
| 164 |
|
|---|
| 165 | is(scalar(@y), 2);
|
|---|
| 166 | is($y[1], 130);
|
|---|
| 167 | $x = pack('w*', 5000000000); $y = '';
|
|---|
| 168 | eval {
|
|---|
| 169 | use Math::BigInt;
|
|---|
| 170 | $y = pack('w*', Math::BigInt::->new(5000000000));
|
|---|
| 171 | };
|
|---|
| 172 | is($x, $y);
|
|---|
| 173 |
|
|---|
| 174 | $x = pack 'w', ~0;
|
|---|
| 175 | $y = pack 'w', (~0).'';
|
|---|
| 176 | is($x, $y);
|
|---|
| 177 | is(unpack ('w',$x), ~0);
|
|---|
| 178 | is(unpack ('w',$y), ~0);
|
|---|
| 179 |
|
|---|
| 180 | $x = pack 'w', ~0 - 1;
|
|---|
| 181 | $y = pack 'w', (~0) - 2;
|
|---|
| 182 |
|
|---|
| 183 | if (~0 - 1 == (~0) - 2) {
|
|---|
| 184 | is($x, $y, "NV arithmetic");
|
|---|
| 185 | } else {
|
|---|
| 186 | isnt($x, $y, "IV/NV arithmetic");
|
|---|
| 187 | }
|
|---|
| 188 | cmp_ok(unpack ('w',$x), '==', ~0 - 1);
|
|---|
| 189 | cmp_ok(unpack ('w',$y), '==', ~0 - 2);
|
|---|
| 190 |
|
|---|
| 191 | # These should spot that pack 'w' is using NV, not double, on platforms
|
|---|
| 192 | # where IVs are smaller than doubles, and harmlessly pass elsewhere.
|
|---|
| 193 | # (tests for change 16861)
|
|---|
| 194 | my $x0 = 2**54+3;
|
|---|
| 195 | my $y0 = 2**54-2;
|
|---|
| 196 |
|
|---|
| 197 | $x = pack 'w', $x0;
|
|---|
| 198 | $y = pack 'w', $y0;
|
|---|
| 199 |
|
|---|
| 200 | if ($x0 == $y0) {
|
|---|
| 201 | is($x, $y, "NV arithmetic");
|
|---|
| 202 | } else {
|
|---|
| 203 | isnt($x, $y, "IV/NV arithmetic");
|
|---|
| 204 | }
|
|---|
| 205 | cmp_ok(unpack ('w',$x), '==', $x0);
|
|---|
| 206 | cmp_ok(unpack ('w',$y), '==', $y0);
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 | {
|
|---|
| 211 | print "# test exceptions\n";
|
|---|
| 212 | my $x;
|
|---|
| 213 | eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
|
|---|
| 214 | like($@, qr/^Unterminated compressed integer/);
|
|---|
| 215 |
|
|---|
| 216 | eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
|
|---|
| 217 | like($@, qr/^Unterminated compressed integer/);
|
|---|
| 218 |
|
|---|
| 219 | eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
|---|
| 220 | like($@, qr/^Unterminated compressed integer/);
|
|---|
| 221 |
|
|---|
| 222 | eval { $x = pack 'w', -1 };
|
|---|
| 223 | like ($@, qr/^Cannot compress negative numbers/);
|
|---|
| 224 |
|
|---|
| 225 | eval { $x = pack 'w', '1'x(1 + length ~0) . 'e0' };
|
|---|
| 226 | like ($@, qr/^Can only compress unsigned integers/);
|
|---|
| 227 |
|
|---|
| 228 | # Check that the warning behaviour on the modifiers !, < and > is as we
|
|---|
| 229 | # expect it for this perl.
|
|---|
| 230 | my $can_endian = $no_endianness ? '' : 'sSiIlLqQjJfFdDpP';
|
|---|
| 231 | my $can_shriek = 'sSiIlL';
|
|---|
| 232 | $can_shriek .= 'nNvV' unless $no_signedness;
|
|---|
| 233 | # h and H can't do either, so act as sanity checks in blead
|
|---|
| 234 | foreach my $base (split '', 'hHsSiIlLqQjJfFdDpPnNvV') {
|
|---|
| 235 | foreach my $mod ('', '<', '>', '!', '<!', '>!', '!<', '!>') {
|
|---|
| 236 | SKIP: {
|
|---|
| 237 | # Avoid void context warnings.
|
|---|
| 238 | my $a = eval {pack "$base$mod"};
|
|---|
| 239 | skip "pack can't $base", 1 if $@ =~ /^Invalid type '\w'/;
|
|---|
| 240 | # Which error you get when 2 would be possible seems to be emergent
|
|---|
| 241 | # behaviour of pack's format parser.
|
|---|
| 242 |
|
|---|
| 243 | my $fails_shriek = $mod =~ /!/ && index ($can_shriek, $base) == -1;
|
|---|
| 244 | my $fails_endian = $mod =~ /[<>]/ && index ($can_endian, $base) == -1;
|
|---|
| 245 | my $shriek_first = $mod =~ /^!/;
|
|---|
| 246 |
|
|---|
| 247 | if ($no_endianness and ($mod eq '<!' or $mod eq '>!')) {
|
|---|
| 248 | # The ! isn't seem as part of $base. Instead it's seen as a modifier
|
|---|
| 249 | # on > or <
|
|---|
| 250 | $fails_shriek = 1;
|
|---|
| 251 | undef $fails_endian;
|
|---|
| 252 | } elsif ($fails_shriek and $fails_endian) {
|
|---|
| 253 | if ($shriek_first) {
|
|---|
| 254 | undef $fails_endian;
|
|---|
| 255 | }
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | if ($fails_endian) {
|
|---|
| 259 | if ($no_endianness) {
|
|---|
| 260 | # < and > are seen as pattern letters, not modifiers
|
|---|
| 261 | like ($@, qr/^Invalid type '[<>]'/, "pack can't $base$mod");
|
|---|
| 262 | } else {
|
|---|
| 263 | like ($@, qr/^'[<>]' allowed only after types/,
|
|---|
| 264 | "pack can't $base$mod");
|
|---|
| 265 | }
|
|---|
| 266 | } elsif ($fails_shriek) {
|
|---|
| 267 | like ($@, qr/^'!' allowed only after types/,
|
|---|
| 268 | "pack can't $base$mod");
|
|---|
| 269 | } else {
|
|---|
| 270 | is ($@, '', "pack can $base$mod");
|
|---|
| 271 | }
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | SKIP: {
|
|---|
| 277 | skip $no_endianness, 2*3 + 2*8 if $no_endianness;
|
|---|
| 278 | for my $mod (qw( ! < > )) {
|
|---|
| 279 | eval { $x = pack "a$mod", 42 };
|
|---|
| 280 | like ($@, qr/^'$mod' allowed only after types \S+ in pack/);
|
|---|
| 281 |
|
|---|
| 282 | eval { $x = unpack "a$mod", 'x'x8 };
|
|---|
| 283 | like ($@, qr/^'$mod' allowed only after types \S+ in unpack/);
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | for my $mod (qw( <> >< !<> !>< <!> >!< <>! ><! )) {
|
|---|
| 287 | eval { $x = pack "sI${mod}s", 42, 47, 11 };
|
|---|
| 288 | like ($@, qr/^Can't use both '<' and '>' after type 'I' in pack/);
|
|---|
| 289 |
|
|---|
| 290 | eval { $x = unpack "sI${mod}s", 'x'x16 };
|
|---|
| 291 | like ($@, qr/^Can't use both '<' and '>' after type 'I' in unpack/);
|
|---|
| 292 | }
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | SKIP: {
|
|---|
| 296 | # Is this a stupid thing to do on VMS, VOS and other unusual platforms?
|
|---|
| 297 |
|
|---|
| 298 | skip("-- the IEEE infinity model is unavailable in this configuration.", 1)
|
|---|
| 299 | if (($^O eq 'VMS') && !defined($Config{useieee}));
|
|---|
| 300 |
|
|---|
| 301 | skip("-- $^O has serious fp indigestion on w-packed infinities", 1)
|
|---|
| 302 | if (
|
|---|
| 303 | ($^O eq 'mpeix')
|
|---|
| 304 | ||
|
|---|
| 305 | ($^O eq 'ultrix')
|
|---|
| 306 | ||
|
|---|
| 307 | ($^O =~ /^svr4/ && -f "/etc/issue" && -f "/etc/.relid") # NCR MP-RAS
|
|---|
| 308 | );
|
|---|
| 309 |
|
|---|
| 310 | my $inf = eval '2**1000000';
|
|---|
| 311 |
|
|---|
| 312 | skip("Couldn't generate infinity - got error '$@'", 1)
|
|---|
| 313 | unless defined $inf and $inf == $inf / 2 and $inf + 1 == $inf;
|
|---|
| 314 |
|
|---|
| 315 | local our $TODO;
|
|---|
| 316 | $TODO = "VOS needs a fix for posix-1022 to pass this test."
|
|---|
| 317 | if ($^O eq 'vos');
|
|---|
| 318 |
|
|---|
| 319 | eval { $x = pack 'w', $inf };
|
|---|
| 320 | like ($@, qr/^Cannot compress integer/, "Cannot compress integer");
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | SKIP: {
|
|---|
| 324 |
|
|---|
| 325 | skip("-- the full range of an IEEE double may not be available in this configuration.", 3)
|
|---|
| 326 | if (($^O eq 'VMS') && !defined($Config{useieee}));
|
|---|
| 327 |
|
|---|
| 328 | skip("-- $^O does not like 2**1023", 3)
|
|---|
| 329 | if (($^O eq 'ultrix'));
|
|---|
| 330 |
|
|---|
| 331 | # This should be about the biggest thing possible on an IEEE double
|
|---|
| 332 | my $big = eval '2**1023';
|
|---|
| 333 |
|
|---|
| 334 | skip("Couldn't generate 2**1023 - got error '$@'", 3)
|
|---|
| 335 | unless defined $big and $big != $big / 2;
|
|---|
| 336 |
|
|---|
| 337 | eval { $x = pack 'w', $big };
|
|---|
| 338 | is ($@, '', "Should be able to pack 'w', $big # 2**1023");
|
|---|
| 339 |
|
|---|
| 340 | my $y = eval {unpack 'w', $x};
|
|---|
| 341 | is ($@, '',
|
|---|
| 342 | "Should be able to unpack 'w' the result of pack 'w', $big # 2**1023");
|
|---|
| 343 |
|
|---|
| 344 | # I'm getting about 1e-16 on FreeBSD
|
|---|
| 345 | my $quotient = int (100 * ($y - $big) / $big);
|
|---|
| 346 | ok($quotient < 2 && $quotient > -2,
|
|---|
| 347 | "Round trip pack, unpack 'w' of $big is within 1% ($quotient%)");
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | print "# test the 'p' template\n";
|
|---|
| 353 |
|
|---|
| 354 | # literals
|
|---|
| 355 | is(unpack("p",pack("p","foo")), "foo");
|
|---|
| 356 | SKIP: {
|
|---|
| 357 | skip $no_endianness, 2 if $no_endianness;
|
|---|
| 358 | is(unpack("p<",pack("p<","foo")), "foo");
|
|---|
| 359 | is(unpack("p>",pack("p>","foo")), "foo");
|
|---|
| 360 | }
|
|---|
| 361 | # scalars
|
|---|
| 362 | is(unpack("p",pack("p",239)), 239);
|
|---|
| 363 | SKIP: {
|
|---|
| 364 | skip $no_endianness, 2 if $no_endianness;
|
|---|
| 365 | is(unpack("p<",pack("p<",239)), 239);
|
|---|
| 366 | is(unpack("p>",pack("p>",239)), 239);
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | # temps
|
|---|
| 370 | sub foo { my $a = "a"; return $a . $a++ . $a++ }
|
|---|
| 371 | {
|
|---|
| 372 | use warnings;
|
|---|
| 373 | my $warning;
|
|---|
| 374 | local $SIG{__WARN__} = sub {
|
|---|
| 375 | $warning = $_[0];
|
|---|
| 376 | };
|
|---|
| 377 | my $junk = pack("p", &foo);
|
|---|
| 378 |
|
|---|
| 379 | like($warning, qr/temporary val/);
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | # undef should give null pointer
|
|---|
| 383 | like(pack("p", undef), qr/^\0+$/);
|
|---|
| 384 | SKIP: {
|
|---|
| 385 | skip $no_endianness, 2 if $no_endianness;
|
|---|
| 386 | like(pack("p<", undef), qr/^\0+$/);
|
|---|
| 387 | like(pack("p>", undef), qr/^\0+$/);
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | # Check for optimizer bug (e.g. Digital Unix GEM cc with -O4 on DU V4.0B gives
|
|---|
| 391 | # 4294967295 instead of -1)
|
|---|
| 392 | # see #ifdef __osf__ in pp.c pp_unpack
|
|---|
| 393 | is((unpack("i",pack("i",-1))), -1);
|
|---|
| 394 |
|
|---|
| 395 | print "# test the pack lengths of s S i I l L n N v V + modifiers\n";
|
|---|
| 396 |
|
|---|
| 397 | my @lengths = (
|
|---|
| 398 | qw(s 2 S 2 i -4 I -4 l 4 L 4 n 2 N 4 v 2 V 4 n! 2 N! 4 v! 2 V! 4),
|
|---|
| 399 | 's!' => $Config{shortsize}, 'S!' => $Config{shortsize},
|
|---|
| 400 | 'i!' => $Config{intsize}, 'I!' => $Config{intsize},
|
|---|
| 401 | 'l!' => $Config{longsize}, 'L!' => $Config{longsize},
|
|---|
| 402 | );
|
|---|
| 403 |
|
|---|
| 404 | while (my ($base, $expect) = splice @lengths, 0, 2) {
|
|---|
| 405 | my @formats = ($base);
|
|---|
| 406 | $base =~ /^[nv]/i or push @formats, "$base>", "$base<";
|
|---|
| 407 | for my $format (@formats) {
|
|---|
| 408 | SKIP: {
|
|---|
| 409 | skip $no_endianness, 1 if $no_endianness && $format =~ m/[<>]/;
|
|---|
| 410 | skip $no_signedness, 1 if $no_signedness && $format =~ /[nNvV]!/;
|
|---|
| 411 | my $len = length(pack($format, 0));
|
|---|
| 412 | if ($expect > 0) {
|
|---|
| 413 | is($expect, $len, "format '$format'");
|
|---|
| 414 | } else {
|
|---|
| 415 | $expect = -$expect;
|
|---|
| 416 | ok ($len >= $expect, "format '$format'") ||
|
|---|
| 417 | print "# format '$format' has length $len, expected >= $expect\n";
|
|---|
| 418 | }
|
|---|
| 419 | }
|
|---|
| 420 | }
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 | print "# test unpack-pack lengths\n";
|
|---|
| 425 |
|
|---|
| 426 | my @templates = qw(c C i I s S l L n N v V f d q Q);
|
|---|
| 427 |
|
|---|
| 428 | foreach my $base (@templates) {
|
|---|
| 429 | my @tmpl = ($base);
|
|---|
| 430 | $base =~ /^[cnv]/i or push @tmpl, "$base>", "$base<";
|
|---|
| 431 | foreach my $t (@tmpl) {
|
|---|
| 432 | SKIP: {
|
|---|
| 433 | my @t = eval { unpack("$t*", pack("$t*", 12, 34)) };
|
|---|
| 434 |
|
|---|
| 435 | skip "cannot pack '$t' on this perl", 4
|
|---|
| 436 | if is_valid_error($@);
|
|---|
| 437 |
|
|---|
| 438 | is( $@, '', "Template $t works");
|
|---|
| 439 | is(scalar @t, 2);
|
|---|
| 440 |
|
|---|
| 441 | is($t[0], 12);
|
|---|
| 442 | is($t[1], 34);
|
|---|
| 443 | }
|
|---|
| 444 | }
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | {
|
|---|
| 448 | # uuencode/decode
|
|---|
| 449 |
|
|---|
| 450 | # Note that first uuencoding known 'text' data and then checking the
|
|---|
| 451 | # binary values of the uuencoded version would not be portable between
|
|---|
| 452 | # character sets. Uuencoding is meant for encoding binary data, not
|
|---|
| 453 | # text data.
|
|---|
| 454 |
|
|---|
| 455 | my $in = pack 'C*', 0 .. 255;
|
|---|
| 456 |
|
|---|
| 457 | # just to be anal, we do some random tr/`/ /
|
|---|
| 458 | my $uu = <<'EOUU';
|
|---|
| 459 | M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL
|
|---|
| 460 | M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9
|
|---|
| 461 | M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6&
|
|---|
| 462 | MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S
|
|---|
| 463 | MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@
|
|---|
| 464 | ?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P `
|
|---|
| 465 | EOUU
|
|---|
| 466 |
|
|---|
| 467 | $_ = $uu;
|
|---|
| 468 | tr/ /`/;
|
|---|
| 469 |
|
|---|
| 470 | is(pack('u', $in), $_);
|
|---|
| 471 |
|
|---|
| 472 | is(unpack('u', $uu), $in);
|
|---|
| 473 |
|
|---|
| 474 | $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";
|
|---|
| 475 | $uu = <<'EOUU';
|
|---|
| 476 | M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
|
|---|
| 477 | &8%P:````
|
|---|
| 478 | EOUU
|
|---|
| 479 |
|
|---|
| 480 | is(unpack('u', $uu), $in);
|
|---|
| 481 |
|
|---|
| 482 | # This is identical to the above except that backquotes have been
|
|---|
| 483 | # changed to spaces
|
|---|
| 484 |
|
|---|
| 485 | $uu = <<'EOUU';
|
|---|
| 486 | M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
|
|---|
| 487 | &8%P:
|
|---|
| 488 | EOUU
|
|---|
| 489 |
|
|---|
| 490 | # ' # Grr
|
|---|
| 491 | is(unpack('u', $uu), $in);
|
|---|
| 492 |
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | # test the ascii template types (A, a, Z)
|
|---|
| 496 |
|
|---|
| 497 | foreach (
|
|---|
| 498 | ['p', 'A*', "foo\0bar\0 ", "foo\0bar\0 "],
|
|---|
| 499 | ['p', 'A11', "foo\0bar\0 ", "foo\0bar\0 "],
|
|---|
| 500 | ['u', 'A*', "foo\0bar \0", "foo\0bar"],
|
|---|
| 501 | ['u', 'A8', "foo\0bar \0", "foo\0bar"],
|
|---|
| 502 | ['p', 'a*', "foo\0bar\0 ", "foo\0bar\0 "],
|
|---|
| 503 | ['p', 'a11', "foo\0bar\0 ", "foo\0bar\0 \0\0"],
|
|---|
| 504 | ['u', 'a*', "foo\0bar \0", "foo\0bar \0"],
|
|---|
| 505 | ['u', 'a8', "foo\0bar \0", "foo\0bar "],
|
|---|
| 506 | ['p', 'Z*', "foo\0bar\0 ", "foo\0bar\0 \0"],
|
|---|
| 507 | ['p', 'Z11', "foo\0bar\0 ", "foo\0bar\0 \0\0"],
|
|---|
| 508 | ['p', 'Z3', "foo", "fo\0"],
|
|---|
| 509 | ['u', 'Z*', "foo\0bar \0", "foo"],
|
|---|
| 510 | ['u', 'Z8', "foo\0bar \0", "foo"],
|
|---|
| 511 | )
|
|---|
| 512 | {
|
|---|
| 513 | my ($what, $template, $in, $out) = @$_;
|
|---|
| 514 | my $got = $what eq 'u' ? (unpack $template, $in) : (pack $template, $in);
|
|---|
| 515 | unless (is($got, $out)) {
|
|---|
| 516 | my $un = $what eq 'u' ? 'un' : '';
|
|---|
| 517 | print "# ${un}pack ('$template', "._qq($in).') gave '._qq($out).
|
|---|
| 518 | ' not '._qq($got)."\n";
|
|---|
| 519 | }
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | print "# packing native shorts/ints/longs\n";
|
|---|
| 523 |
|
|---|
| 524 | is(length(pack("s!", 0)), $Config{shortsize});
|
|---|
| 525 | is(length(pack("i!", 0)), $Config{intsize});
|
|---|
| 526 | is(length(pack("l!", 0)), $Config{longsize});
|
|---|
| 527 | ok(length(pack("s!", 0)) <= length(pack("i!", 0)));
|
|---|
| 528 | ok(length(pack("i!", 0)) <= length(pack("l!", 0)));
|
|---|
| 529 | is(length(pack("i!", 0)), length(pack("i", 0)));
|
|---|
| 530 |
|
|---|
| 531 | sub numbers {
|
|---|
| 532 | my $base = shift;
|
|---|
| 533 | my @formats = ($base);
|
|---|
| 534 | $base =~ /^[silqjfdp]/i and push @formats, "$base>", "$base<";
|
|---|
| 535 | for my $format (@formats) {
|
|---|
| 536 | numbers_with_total ($format, undef, @_);
|
|---|
| 537 | }
|
|---|
| 538 | }
|
|---|
| 539 |
|
|---|
| 540 | sub numbers_with_total {
|
|---|
| 541 | my $format = shift;
|
|---|
| 542 | my $total = shift;
|
|---|
| 543 | if (!defined $total) {
|
|---|
| 544 | foreach (@_) {
|
|---|
| 545 | $total += $_;
|
|---|
| 546 | }
|
|---|
| 547 | }
|
|---|
| 548 | print "# numbers test for $format\n";
|
|---|
| 549 | foreach (@_) {
|
|---|
| 550 | SKIP: {
|
|---|
| 551 | my $out = eval {unpack($format, pack($format, $_))};
|
|---|
| 552 | skip "cannot pack '$format' on this perl", 2
|
|---|
| 553 | if is_valid_error($@);
|
|---|
| 554 |
|
|---|
| 555 | is($@, '', "no error");
|
|---|
| 556 | is($out, $_, "unpack pack $format $_");
|
|---|
| 557 | }
|
|---|
| 558 | }
|
|---|
| 559 |
|
|---|
| 560 | my $skip_if_longer_than = ~0; # "Infinity"
|
|---|
| 561 | if (~0 - 1 == ~0) {
|
|---|
| 562 | # If we're running with -DNO_PERLPRESERVE_IVUV and NVs don't preserve all
|
|---|
| 563 | # UVs (in which case ~0 is NV, ~0-1 will be the same NV) then we can't
|
|---|
| 564 | # correctly in perl calculate UV totals for long checksums, as pp_unpack
|
|---|
| 565 | # is using UV maths, and we've only got NVs.
|
|---|
| 566 | $skip_if_longer_than = $Config{nv_preserves_uv_bits};
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | foreach ('', 1, 2, 3, 15, 16, 17, 31, 32, 33, 53, 54, 63, 64, 65) {
|
|---|
| 570 | SKIP: {
|
|---|
| 571 | my $sum = eval {unpack "%$_$format*", pack "$format*", @_};
|
|---|
| 572 | skip "cannot pack '$format' on this perl", 3
|
|---|
| 573 | if is_valid_error($@);
|
|---|
| 574 |
|
|---|
| 575 | is($@, '', "no error");
|
|---|
| 576 | ok(defined $sum, "sum bits $_, format $format defined");
|
|---|
| 577 |
|
|---|
| 578 | my $len = $_; # Copy, so that we can reassign ''
|
|---|
| 579 | $len = 16 unless length $len;
|
|---|
| 580 |
|
|---|
| 581 | SKIP: {
|
|---|
| 582 | skip "cannot test checksums over $skip_if_longer_than bits", 1
|
|---|
| 583 | if $len > $skip_if_longer_than;
|
|---|
| 584 |
|
|---|
| 585 | # Our problem with testing this portably is that the checksum code in
|
|---|
| 586 | # pp_unpack is able to cast signed to unsigned, and do modulo 2**n
|
|---|
| 587 | # arithmetic in unsigned ints, which perl has no operators to do.
|
|---|
| 588 | # (use integer; does signed ints, which won't wrap on UTS, which is just
|
|---|
| 589 | # fine with ANSI, but not with most people's assumptions.
|
|---|
| 590 | # This is why we need to supply the totals for 'Q' as there's no way in
|
|---|
| 591 | # perl to calculate them, short of unpack '%0Q' (is that documented?)
|
|---|
| 592 | # ** returns NVs; make sure it's IV.
|
|---|
| 593 | my $max = 1 + 2 * (int (2 ** ($len-1))-1); # The max possible checksum
|
|---|
| 594 | my $max_p1 = $max + 1;
|
|---|
| 595 | my ($max_is_integer, $max_p1_is_integer);
|
|---|
| 596 | $max_p1_is_integer = 1 unless $max_p1 + 1 == $max_p1;
|
|---|
| 597 | $max_is_integer = 1 if $max - 1 < ~0;
|
|---|
| 598 |
|
|---|
| 599 | my $calc_sum;
|
|---|
| 600 | if (ref $total) {
|
|---|
| 601 | $calc_sum = &$total($len);
|
|---|
| 602 | } else {
|
|---|
| 603 | $calc_sum = $total;
|
|---|
| 604 | # Shift into range by some multiple of the total
|
|---|
| 605 | my $mult = $max_p1 ? int ($total / $max_p1) : undef;
|
|---|
| 606 | # Need this to make sure that -1 + (~0+1) is ~0 (ie still integer)
|
|---|
| 607 | $calc_sum = $total - $mult;
|
|---|
| 608 | $calc_sum -= $mult * $max;
|
|---|
| 609 | if ($calc_sum < 0) {
|
|---|
| 610 | $calc_sum += 1;
|
|---|
| 611 | $calc_sum += $max;
|
|---|
| 612 | }
|
|---|
| 613 | }
|
|---|
| 614 | if ($calc_sum == $calc_sum - 1 && $calc_sum == $max_p1) {
|
|---|
| 615 | # we're into floating point (either by getting out of the range of
|
|---|
| 616 | # UV arithmetic, or because we're doing a floating point checksum)
|
|---|
| 617 | # and our calculation of the checksum has become rounded up to
|
|---|
| 618 | # max_checksum + 1
|
|---|
| 619 | $calc_sum = 0;
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | if ($calc_sum == $sum) { # HAS to be ==, not eq (so no is()).
|
|---|
| 623 | pass ("unpack '%$_$format' gave $sum");
|
|---|
| 624 | } else {
|
|---|
| 625 | my $delta = 1.000001;
|
|---|
| 626 | if ($format =~ tr /dDfF//
|
|---|
| 627 | && ($calc_sum <= $sum * $delta && $calc_sum >= $sum / $delta)) {
|
|---|
| 628 | pass ("unpack '%$_$format' gave $sum, expected $calc_sum");
|
|---|
| 629 | } else {
|
|---|
| 630 | my $text = ref $total ? &$total($len) : $total;
|
|---|
| 631 | fail;
|
|---|
| 632 | print "# For list (" . join (", ", @_) . ") (total $text)"
|
|---|
| 633 | . " packed with $format unpack '%$_$format' gave $sum,"
|
|---|
| 634 | . " expected $calc_sum\n";
|
|---|
| 635 | }
|
|---|
| 636 | }
|
|---|
| 637 | }
|
|---|
| 638 | }
|
|---|
| 639 | }
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 | numbers ('c', -128, -1, 0, 1, 127);
|
|---|
| 643 | numbers ('C', 0, 1, 127, 128, 255);
|
|---|
| 644 | numbers ('s', -32768, -1, 0, 1, 32767);
|
|---|
| 645 | numbers ('S', 0, 1, 32767, 32768, 65535);
|
|---|
| 646 | numbers ('i', -2147483648, -1, 0, 1, 2147483647);
|
|---|
| 647 | numbers ('I', 0, 1, 2147483647, 2147483648, 4294967295);
|
|---|
| 648 | numbers ('l', -2147483648, -1, 0, 1, 2147483647);
|
|---|
| 649 | numbers ('L', 0, 1, 2147483647, 2147483648, 4294967295);
|
|---|
| 650 | numbers ('s!', -32768, -1, 0, 1, 32767);
|
|---|
| 651 | numbers ('S!', 0, 1, 32767, 32768, 65535);
|
|---|
| 652 | numbers ('i!', -2147483648, -1, 0, 1, 2147483647);
|
|---|
| 653 | numbers ('I!', 0, 1, 2147483647, 2147483648, 4294967295);
|
|---|
| 654 | numbers ('l!', -2147483648, -1, 0, 1, 2147483647);
|
|---|
| 655 | numbers ('L!', 0, 1, 2147483647, 2147483648, 4294967295);
|
|---|
| 656 | numbers ('n', 0, 1, 32767, 32768, 65535);
|
|---|
| 657 | numbers ('v', 0, 1, 32767, 32768, 65535);
|
|---|
| 658 | numbers ('N', 0, 1, 2147483647, 2147483648, 4294967295);
|
|---|
| 659 | numbers ('V', 0, 1, 2147483647, 2147483648, 4294967295);
|
|---|
| 660 | numbers ('n!', -32768, -1, 0, 1, 32767);
|
|---|
| 661 | numbers ('v!', -32768, -1, 0, 1, 32767);
|
|---|
| 662 | numbers ('N!', -2147483648, -1, 0, 1, 2147483647);
|
|---|
| 663 | numbers ('V!', -2147483648, -1, 0, 1, 2147483647);
|
|---|
| 664 | # All these should have exact binary representations:
|
|---|
| 665 | numbers ('f', -1, 0, 0.5, 42, 2**34);
|
|---|
| 666 | numbers ('d', -(2**34), -1, 0, 1, 2**34);
|
|---|
| 667 | ## These don't, but 'd' is NV. XXX wrong, it's double
|
|---|
| 668 | #numbers ('d', -1, 0, 1, 1-exp(-1), -exp(1));
|
|---|
| 669 |
|
|---|
| 670 | numbers_with_total ('q', -1,
|
|---|
| 671 | -9223372036854775808, -1, 0, 1,9223372036854775807);
|
|---|
| 672 | # This total is icky, but the true total is 2**65-1, and need a way to generate
|
|---|
| 673 | # the epxected checksum on any system including those where NVs can preserve
|
|---|
| 674 | # 65 bits. (long double is 128 bits on sparc, so they certainly can)
|
|---|
| 675 | # or where rounding is down not up on binary conversion (crays)
|
|---|
| 676 | numbers_with_total ('Q', sub {
|
|---|
| 677 | my $len = shift;
|
|---|
| 678 | $len = 65 if $len > 65; # unmasked total is 2**65-1 here
|
|---|
| 679 | my $total = 1 + 2 * (int (2**($len - 1)) - 1);
|
|---|
| 680 | return 0 if $total == $total - 1; # Overflowed integers
|
|---|
| 681 | return $total; # NVs still accurate to nearest integer
|
|---|
| 682 | },
|
|---|
| 683 | 0, 1,9223372036854775807, 9223372036854775808,
|
|---|
| 684 | 18446744073709551615);
|
|---|
| 685 |
|
|---|
| 686 | print "# pack nvNV byteorders\n";
|
|---|
| 687 |
|
|---|
| 688 | is(pack("n", 0xdead), "\xde\xad");
|
|---|
| 689 | is(pack("v", 0xdead), "\xad\xde");
|
|---|
| 690 | is(pack("N", 0xdeadbeef), "\xde\xad\xbe\xef");
|
|---|
| 691 | is(pack("V", 0xdeadbeef), "\xef\xbe\xad\xde");
|
|---|
| 692 |
|
|---|
| 693 | SKIP: {
|
|---|
| 694 | skip $no_signedness, 4 if $no_signedness;
|
|---|
| 695 | is(pack("n!", 0xdead), "\xde\xad");
|
|---|
| 696 | is(pack("v!", 0xdead), "\xad\xde");
|
|---|
| 697 | is(pack("N!", 0xdeadbeef), "\xde\xad\xbe\xef");
|
|---|
| 698 | is(pack("V!", 0xdeadbeef), "\xef\xbe\xad\xde");
|
|---|
| 699 | }
|
|---|
| 700 |
|
|---|
| 701 | print "# test big-/little-endian conversion\n";
|
|---|
| 702 |
|
|---|
| 703 | sub byteorder
|
|---|
| 704 | {
|
|---|
| 705 | my $format = shift;
|
|---|
| 706 | print "# byteorder test for $format\n";
|
|---|
| 707 | for my $value (@_) {
|
|---|
| 708 | SKIP: {
|
|---|
| 709 | my($nat,$be,$le) = eval { map { pack $format.$_, $value } '', '>', '<' };
|
|---|
| 710 | skip "cannot pack '$format' on this perl", 5
|
|---|
| 711 | if is_valid_error($@);
|
|---|
| 712 |
|
|---|
| 713 | print "# [$value][$nat][$be][$le][$@]\n";
|
|---|
| 714 |
|
|---|
| 715 | SKIP: {
|
|---|
| 716 | skip "cannot compare native byteorder with big-/little-endian", 1
|
|---|
| 717 | if $ByteOrder eq 'unknown';
|
|---|
| 718 |
|
|---|
| 719 | is($nat, $ByteOrder eq 'big' ? $be : $le);
|
|---|
| 720 | }
|
|---|
| 721 | is($be, reverse($le));
|
|---|
| 722 | my @x = eval { unpack "$format$format>$format<", $nat.$be.$le };
|
|---|
| 723 |
|
|---|
| 724 | print "# [$value][", join('][', @x), "][$@]\n";
|
|---|
| 725 |
|
|---|
| 726 | is($@, '');
|
|---|
| 727 | is($x[0], $x[1]);
|
|---|
| 728 | is($x[0], $x[2]);
|
|---|
| 729 | }
|
|---|
| 730 | }
|
|---|
| 731 | }
|
|---|
| 732 |
|
|---|
| 733 | byteorder('s', -32768, -1, 0, 1, 32767);
|
|---|
| 734 | byteorder('S', 0, 1, 32767, 32768, 65535);
|
|---|
| 735 | byteorder('i', -2147483648, -1, 0, 1, 2147483647);
|
|---|
| 736 | byteorder('I', 0, 1, 2147483647, 2147483648, 4294967295);
|
|---|
| 737 | byteorder('l', -2147483648, -1, 0, 1, 2147483647);
|
|---|
| 738 | byteorder('L', 0, 1, 2147483647, 2147483648, 4294967295);
|
|---|
| 739 | byteorder('j', -2147483648, -1, 0, 1, 2147483647);
|
|---|
| 740 | byteorder('J', 0, 1, 2147483647, 2147483648, 4294967295);
|
|---|
| 741 | byteorder('s!', -32768, -1, 0, 1, 32767);
|
|---|
| 742 | byteorder('S!', 0, 1, 32767, 32768, 65535);
|
|---|
| 743 | byteorder('i!', -2147483648, -1, 0, 1, 2147483647);
|
|---|
| 744 | byteorder('I!', 0, 1, 2147483647, 2147483648, 4294967295);
|
|---|
| 745 | byteorder('l!', -2147483648, -1, 0, 1, 2147483647);
|
|---|
| 746 | byteorder('L!', 0, 1, 2147483647, 2147483648, 4294967295);
|
|---|
| 747 | byteorder('q', -9223372036854775808, -1, 0, 1, 9223372036854775807);
|
|---|
| 748 | byteorder('Q', 0, 1, 9223372036854775807, 9223372036854775808, 18446744073709551615);
|
|---|
| 749 | byteorder('f', -1, 0, 0.5, 42, 2**34);
|
|---|
| 750 | byteorder('F', -1, 0, 0.5, 42, 2**34);
|
|---|
| 751 | byteorder('d', -(2**34), -1, 0, 1, 2**34);
|
|---|
| 752 | byteorder('D', -(2**34), -1, 0, 1, 2**34);
|
|---|
| 753 |
|
|---|
| 754 | print "# test negative numbers\n";
|
|---|
| 755 |
|
|---|
| 756 | SKIP: {
|
|---|
| 757 | skip "platform is not using two's complement for negative integers", 120
|
|---|
| 758 | unless $IsTwosComplement;
|
|---|
| 759 |
|
|---|
| 760 | for my $format (qw(s i l j s! i! l! q)) {
|
|---|
| 761 | SKIP: {
|
|---|
| 762 | my($nat,$be,$le) = eval { map { pack $format.$_, -1 } '', '>', '<' };
|
|---|
| 763 | skip "cannot pack '$format' on this perl", 15
|
|---|
| 764 | if is_valid_error($@);
|
|---|
| 765 |
|
|---|
| 766 | my $len = length $nat;
|
|---|
| 767 | is($_, "\xFF"x$len) for $nat, $be, $le;
|
|---|
| 768 |
|
|---|
| 769 | my(@val,@ref);
|
|---|
| 770 | if ($len >= 8) {
|
|---|
| 771 | @val = (-2, -81985529216486896, -9223372036854775808);
|
|---|
| 772 | @ref = ("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE",
|
|---|
| 773 | "\xFE\xDC\xBA\x98\x76\x54\x32\x10",
|
|---|
| 774 | "\x80\x00\x00\x00\x00\x00\x00\x00");
|
|---|
| 775 | }
|
|---|
| 776 | elsif ($len >= 4) {
|
|---|
| 777 | @val = (-2, -19088744, -2147483648);
|
|---|
| 778 | @ref = ("\xFF\xFF\xFF\xFE",
|
|---|
| 779 | "\xFE\xDC\xBA\x98",
|
|---|
| 780 | "\x80\x00\x00\x00");
|
|---|
| 781 | }
|
|---|
| 782 | else {
|
|---|
| 783 | @val = (-2, -292, -32768);
|
|---|
| 784 | @ref = ("\xFF\xFE",
|
|---|
| 785 | "\xFE\xDC",
|
|---|
| 786 | "\x80\x00");
|
|---|
| 787 | }
|
|---|
| 788 | for my $x (@ref) {
|
|---|
| 789 | if ($len > length $x) {
|
|---|
| 790 | $x = $x . "\xFF" x ($len - length $x);
|
|---|
| 791 | }
|
|---|
| 792 | }
|
|---|
| 793 |
|
|---|
| 794 | for my $i (0 .. $#val) {
|
|---|
| 795 | my($nat,$be,$le) = eval { map { pack $format.$_, $val[$i] } '', '>', '<' };
|
|---|
| 796 | is($@, '');
|
|---|
| 797 |
|
|---|
| 798 | SKIP: {
|
|---|
| 799 | skip "cannot compare native byteorder with big-/little-endian", 1
|
|---|
| 800 | if $ByteOrder eq 'unknown';
|
|---|
| 801 |
|
|---|
| 802 | is($nat, $ByteOrder eq 'big' ? $be : $le);
|
|---|
| 803 | }
|
|---|
| 804 |
|
|---|
| 805 | is($be, $ref[$i]);
|
|---|
| 806 | is($be, reverse($le));
|
|---|
| 807 | }
|
|---|
| 808 | }
|
|---|
| 809 | }
|
|---|
| 810 | }
|
|---|
| 811 |
|
|---|
| 812 | {
|
|---|
| 813 | # /
|
|---|
| 814 |
|
|---|
| 815 | my ($x, $y, $z);
|
|---|
| 816 | eval { ($x) = unpack '/a*','hello' };
|
|---|
| 817 | like($@, qr!'/' must follow a numeric type!);
|
|---|
| 818 | undef $x;
|
|---|
| 819 | eval { $x = unpack '/a*','hello' };
|
|---|
| 820 | like($@, qr!'/' must follow a numeric type!);
|
|---|
| 821 |
|
|---|
| 822 | undef $x;
|
|---|
| 823 | eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
|
|---|
| 824 | is($@, '');
|
|---|
| 825 | is($z, 'ok');
|
|---|
| 826 | is($x, 'yes');
|
|---|
| 827 | is($y, 'z');
|
|---|
| 828 | undef $z;
|
|---|
| 829 | eval { $z = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
|
|---|
| 830 | is($@, '');
|
|---|
| 831 | is($z, 'ok');
|
|---|
| 832 |
|
|---|
| 833 |
|
|---|
| 834 | undef $x;
|
|---|
| 835 | eval { ($x) = pack '/a*','hello' };
|
|---|
| 836 | like($@, qr!Invalid type '/'!);
|
|---|
| 837 | undef $x;
|
|---|
| 838 | eval { $x = pack '/a*','hello' };
|
|---|
| 839 | like($@, qr!Invalid type '/'!);
|
|---|
| 840 |
|
|---|
| 841 | $z = pack 'n/a* N/Z* w/A*','string','hi there ','etc';
|
|---|
| 842 | my $expect = "\000\006string\0\0\0\012hi there \000\003etc";
|
|---|
| 843 | is($z, $expect);
|
|---|
| 844 |
|
|---|
| 845 | undef $x;
|
|---|
| 846 | $expect = 'hello world';
|
|---|
| 847 | eval { ($x) = unpack ("w/a", chr (11) . "hello world!")};
|
|---|
| 848 | is($x, $expect);
|
|---|
| 849 | is($@, '');
|
|---|
| 850 |
|
|---|
| 851 | undef $x;
|
|---|
| 852 | # Doing this in scalar context used to fail.
|
|---|
| 853 | eval { $x = unpack ("w/a", chr (11) . "hello world!")};
|
|---|
| 854 | is($@, '');
|
|---|
| 855 | is($x, $expect);
|
|---|
| 856 |
|
|---|
| 857 | foreach (
|
|---|
| 858 | ['a/a*/a*', '212ab345678901234567','ab3456789012'],
|
|---|
| 859 | ['a/a*/a*', '3012ab345678901234567', 'ab3456789012'],
|
|---|
| 860 | ['a/a*/b*', '212ab', $Is_EBCDIC ? '100000010100' : '100001100100'],
|
|---|
| 861 | )
|
|---|
| 862 | {
|
|---|
| 863 | my ($pat, $in, $expect) = @$_;
|
|---|
| 864 | undef $x;
|
|---|
| 865 | eval { ($x) = unpack $pat, $in };
|
|---|
| 866 | is($@, '');
|
|---|
| 867 | is($x, $expect) ||
|
|---|
| 868 | printf "# list unpack ('$pat', '$in') gave %s, expected '$expect'\n",
|
|---|
| 869 | encode_list ($x);
|
|---|
| 870 |
|
|---|
| 871 | undef $x;
|
|---|
| 872 | eval { $x = unpack $pat, $in };
|
|---|
| 873 | is($@, '');
|
|---|
| 874 | is($x, $expect) ||
|
|---|
| 875 | printf "# scalar unpack ('$pat', '$in') gave %s, expected '$expect'\n",
|
|---|
| 876 | encode_list ($x);
|
|---|
| 877 | }
|
|---|
| 878 |
|
|---|
| 879 | # / with #
|
|---|
| 880 |
|
|---|
| 881 | my $pattern = <<'EOU';
|
|---|
| 882 | a3/A # Count in ASCII
|
|---|
| 883 | C/a* # Count in a C char
|
|---|
| 884 | C/Z # Count in a C char but skip after \0
|
|---|
| 885 | EOU
|
|---|
| 886 |
|
|---|
| 887 | $x = $y = $z =undef;
|
|---|
| 888 | eval { ($z,$x,$y) = unpack $pattern, "003ok \003yes\004z\000abc" };
|
|---|
| 889 | is($@, '');
|
|---|
| 890 | is($z, 'ok');
|
|---|
| 891 | is($x, 'yes');
|
|---|
| 892 | is($y, 'z');
|
|---|
| 893 | undef $x;
|
|---|
| 894 | eval { $z = unpack $pattern, "003ok \003yes\004z\000abc" };
|
|---|
| 895 | is($@, '');
|
|---|
| 896 | is($z, 'ok');
|
|---|
| 897 |
|
|---|
| 898 | $pattern = <<'EOP';
|
|---|
| 899 | n/a* # Count as network short
|
|---|
| 900 | w/A* # Count a BER integer
|
|---|
| 901 | EOP
|
|---|
| 902 | $expect = "\000\006string\003etc";
|
|---|
| 903 | $z = pack $pattern,'string','etc';
|
|---|
| 904 | is($z, $expect);
|
|---|
| 905 | }
|
|---|
| 906 |
|
|---|
| 907 |
|
|---|
| 908 | SKIP: {
|
|---|
| 909 | skip("(EBCDIC and) version strings are bad idea", 2) if $Is_EBCDIC;
|
|---|
| 910 |
|
|---|
| 911 | is("1.20.300.4000", sprintf "%vd", pack("U*",1,20,300,4000));
|
|---|
| 912 | is("1.20.300.4000", sprintf "%vd", pack(" U*",1,20,300,4000));
|
|---|
| 913 | }
|
|---|
| 914 | isnt(v1.20.300.4000, sprintf "%vd", pack("C0U*",1,20,300,4000));
|
|---|
| 915 |
|
|---|
| 916 | my $rslt = $Is_EBCDIC ? "156 67" : "199 162";
|
|---|
| 917 | is(join(" ", unpack("C*", chr(0x1e2))), $rslt);
|
|---|
| 918 |
|
|---|
| 919 | # does pack U create Unicode?
|
|---|
| 920 | is(ord(pack('U', 300)), 300);
|
|---|
| 921 |
|
|---|
| 922 | # does unpack U deref Unicode?
|
|---|
| 923 | is((unpack('U', chr(300)))[0], 300);
|
|---|
| 924 |
|
|---|
| 925 | # is unpack U the reverse of pack U for Unicode string?
|
|---|
| 926 | is("@{[unpack('U*', pack('U*', 100, 200, 300))]}", "100 200 300");
|
|---|
| 927 |
|
|---|
| 928 | # is unpack U the reverse of pack U for byte string?
|
|---|
| 929 | is("@{[unpack('U*', pack('U*', 100, 200))]}", "100 200");
|
|---|
| 930 |
|
|---|
| 931 |
|
|---|
| 932 | SKIP: {
|
|---|
| 933 | skip "Not for EBCDIC", 4 if $Is_EBCDIC;
|
|---|
| 934 |
|
|---|
| 935 | # does unpack C unravel pack U?
|
|---|
| 936 | is("@{[unpack('C*', pack('U*', 100, 200))]}", "100 195 136");
|
|---|
| 937 |
|
|---|
| 938 | # does pack U0C create Unicode?
|
|---|
| 939 | is("@{[pack('U0C*', 100, 195, 136)]}", v100.v200);
|
|---|
| 940 |
|
|---|
| 941 | # does pack C0U create characters?
|
|---|
| 942 | is("@{[pack('C0U*', 100, 200)]}", pack("C*", 100, 195, 136));
|
|---|
| 943 |
|
|---|
| 944 | # does unpack U0U on byte data warn?
|
|---|
| 945 | {
|
|---|
| 946 | local $SIG{__WARN__} = sub { $@ = "@_" };
|
|---|
| 947 | my @null = unpack('U0U', chr(255));
|
|---|
| 948 | like($@, qr/^Malformed UTF-8 character /);
|
|---|
| 949 | }
|
|---|
| 950 | }
|
|---|
| 951 |
|
|---|
| 952 | {
|
|---|
| 953 | my $p = pack 'i*', -2147483648, ~0, 0, 1, 2147483647;
|
|---|
| 954 | my (@a);
|
|---|
| 955 | # bug - % had to be at the start of the pattern, no leading whitespace or
|
|---|
| 956 | # comments. %i! didn't work at all.
|
|---|
| 957 | foreach my $pat ('%32i*', ' %32i*', "# Muhahahaha\n%32i*", '%32i* ',
|
|---|
| 958 | '%32i!*', ' %32i!*', "\n#\n#\n\r \t\f%32i!*", '%32i!*#') {
|
|---|
| 959 | @a = unpack $pat, $p;
|
|---|
| 960 | is($a[0], 0xFFFFFFFF) || print "# $pat\n";
|
|---|
| 961 | @a = scalar unpack $pat, $p;
|
|---|
| 962 | is($a[0], 0xFFFFFFFF) || print "# $pat\n";
|
|---|
| 963 | }
|
|---|
| 964 |
|
|---|
| 965 |
|
|---|
| 966 | $p = pack 'I*', 42, 12;
|
|---|
| 967 | # Multiline patterns in scalar context failed.
|
|---|
| 968 | foreach my $pat ('I', <<EOPOEMSNIPPET, 'I#I', 'I # I', 'I # !!!') {
|
|---|
| 969 | # On the Ning Nang Nong
|
|---|
| 970 | # Where the Cows go Bong!
|
|---|
| 971 | # And the Monkeys all say Boo!
|
|---|
| 972 | I
|
|---|
| 973 | EOPOEMSNIPPET
|
|---|
| 974 | @a = unpack $pat, $p;
|
|---|
| 975 | is(scalar @a, 1);
|
|---|
| 976 | is($a[0], 42);
|
|---|
| 977 | @a = scalar unpack $pat, $p;
|
|---|
| 978 | is(scalar @a, 1);
|
|---|
| 979 | is($a[0], 42);
|
|---|
| 980 | }
|
|---|
| 981 |
|
|---|
| 982 | # shorts (of all flavours) didn't calculate checksums > 32 bits with floating
|
|---|
| 983 | # point, so a pathologically long pattern would wrap at 32 bits.
|
|---|
| 984 | my $pat = "\xff\xff"x65538; # Start with it long, to save any copying.
|
|---|
| 985 | foreach (4,3,2,1,0) {
|
|---|
| 986 | my $len = 65534 + $_;
|
|---|
| 987 | is(unpack ("%33n$len", $pat), 65535 * $len);
|
|---|
| 988 | }
|
|---|
| 989 | }
|
|---|
| 990 |
|
|---|
| 991 |
|
|---|
| 992 | # pack x X @
|
|---|
| 993 | foreach (
|
|---|
| 994 | ['x', "N", "\0"],
|
|---|
| 995 | ['x4', "N", "\0"x4],
|
|---|
| 996 | ['xX', "N", ""],
|
|---|
| 997 | ['xXa*', "Nick", "Nick"],
|
|---|
| 998 | ['a5Xa5', "cameL", "llama", "camellama"],
|
|---|
| 999 | ['@4', 'N', "\0"x4],
|
|---|
| 1000 | ['a*@8a*', 'Camel', 'Dromedary', "Camel\0\0\0Dromedary"],
|
|---|
| 1001 | ['a*@4a', 'Perl rules', '!', 'Perl!'],
|
|---|
| 1002 | )
|
|---|
| 1003 | {
|
|---|
| 1004 | my ($template, @in) = @$_;
|
|---|
| 1005 | my $out = pop @in;
|
|---|
| 1006 | my $got = eval {pack $template, @in};
|
|---|
| 1007 | is($@, '');
|
|---|
| 1008 | is($out, $got) ||
|
|---|
| 1009 | printf "# pack ('$template', %s) gave %s expected %s\n",
|
|---|
| 1010 | encode_list (@in), encode_list ($got), encode_list ($out);
|
|---|
| 1011 | }
|
|---|
| 1012 |
|
|---|
| 1013 | # unpack x X @
|
|---|
| 1014 | foreach (
|
|---|
| 1015 | ['x', "N"],
|
|---|
| 1016 | ['xX', "N"],
|
|---|
| 1017 | ['xXa*', "Nick", "Nick"],
|
|---|
| 1018 | ['a5Xa5', "camellama", "camel", "llama"],
|
|---|
| 1019 | ['@3', "ice"],
|
|---|
| 1020 | ['@2a2', "water", "te"],
|
|---|
| 1021 | ['a*@1a3', "steam", "steam", "tea"],
|
|---|
| 1022 | )
|
|---|
| 1023 | {
|
|---|
| 1024 | my ($template, $in, @out) = @$_;
|
|---|
| 1025 | my @got = eval {unpack $template, $in};
|
|---|
| 1026 | is($@, '');
|
|---|
| 1027 | ok (list_eq (\@got, \@out)) ||
|
|---|
| 1028 | printf "# list unpack ('$template', %s) gave %s expected %s\n",
|
|---|
| 1029 | _qq($in), encode_list (@got), encode_list (@out);
|
|---|
| 1030 |
|
|---|
| 1031 | my $got = eval {unpack $template, $in};
|
|---|
| 1032 | is($@, '');
|
|---|
| 1033 | @out ? is( $got, $out[0] ) # 1 or more items; should get first
|
|---|
| 1034 | : ok( !defined $got ) # 0 items; should get undef
|
|---|
| 1035 | or printf "# scalar unpack ('$template', %s) gave %s expected %s\n",
|
|---|
| 1036 | _qq($in), encode_list ($got), encode_list ($out[0]);
|
|---|
| 1037 | }
|
|---|
| 1038 |
|
|---|
| 1039 | {
|
|---|
| 1040 | my $t = 'Z*Z*';
|
|---|
| 1041 | my ($u, $v) = qw(foo xyzzy);
|
|---|
| 1042 | my $p = pack($t, $u, $v);
|
|---|
| 1043 | my @u = unpack($t, $p);
|
|---|
| 1044 | is(scalar @u, 2);
|
|---|
| 1045 | is($u[0], $u);
|
|---|
| 1046 | is($u[1], $v);
|
|---|
| 1047 | }
|
|---|
| 1048 |
|
|---|
| 1049 | {
|
|---|
| 1050 | is((unpack("w/a*", "\x02abc"))[0], "ab");
|
|---|
| 1051 |
|
|---|
| 1052 | # "w/a*" should be seen as one unit
|
|---|
| 1053 |
|
|---|
| 1054 | is(scalar unpack("w/a*", "\x02abc"), "ab");
|
|---|
| 1055 | }
|
|---|
| 1056 |
|
|---|
| 1057 | SKIP: {
|
|---|
| 1058 | print "# group modifiers\n";
|
|---|
| 1059 |
|
|---|
| 1060 | skip $no_endianness, 3 * 2 + 3 * 2 + 1 if $no_endianness;
|
|---|
| 1061 |
|
|---|
| 1062 | for my $t (qw{ (s<)< (sl>s)> (s(l(sl)<l)s)< }) {
|
|---|
| 1063 | print "# testing pattern '$t'\n";
|
|---|
| 1064 | eval { ($_) = unpack($t, 'x'x18); };
|
|---|
| 1065 | is($@, '');
|
|---|
| 1066 | eval { $_ = pack($t, (0)x6); };
|
|---|
| 1067 | is($@, '');
|
|---|
| 1068 | }
|
|---|
| 1069 |
|
|---|
| 1070 | for my $t (qw{ (s<)> (sl>s)< (s(l(sl)<l)s)> }) {
|
|---|
| 1071 | print "# testing pattern '$t'\n";
|
|---|
| 1072 | eval { ($_) = unpack($t, 'x'x18); };
|
|---|
| 1073 | like($@, qr/Can't use '[<>]' in a group with different byte-order in unpack/);
|
|---|
| 1074 | eval { $_ = pack($t, (0)x6); };
|
|---|
| 1075 | like($@, qr/Can't use '[<>]' in a group with different byte-order in pack/);
|
|---|
| 1076 | }
|
|---|
| 1077 |
|
|---|
| 1078 | is(pack('L<L>', (0x12345678)x2),
|
|---|
| 1079 | pack('(((L1)1)<)(((L)1)1)>1', (0x12345678)x2));
|
|---|
| 1080 | }
|
|---|
| 1081 |
|
|---|
| 1082 | {
|
|---|
| 1083 | sub compress_template {
|
|---|
| 1084 | my $t = shift;
|
|---|
| 1085 | for my $mod (qw( < > )) {
|
|---|
| 1086 | $t =~ s/((?:(?:[SILQJFDP]!?$mod|[^SILQJFDP\W]!?)(?:\d+|\*|\[(?:[^]]+)\])?\/?){2,})/
|
|---|
| 1087 | my $x = $1; $x =~ s!$mod!!g ? "($x)$mod" : $x /ieg;
|
|---|
| 1088 | }
|
|---|
| 1089 | return $t;
|
|---|
| 1090 | }
|
|---|
| 1091 |
|
|---|
| 1092 | my %templates = (
|
|---|
| 1093 | 's<' => [-42],
|
|---|
| 1094 | 's<c2x![S]S<' => [-42, -11, 12, 4711],
|
|---|
| 1095 | '(i<j<[s]l<)3' => [-11, -22, -33, 1000000, 1100, 2201, 3302,
|
|---|
| 1096 | -1000000, 32767, -32768, 1, -123456789 ],
|
|---|
| 1097 | '(I!<4(J<2L<)3)5' => [1 .. 65],
|
|---|
| 1098 | 'q<Q<' => [-50000000005, 60000000006],
|
|---|
| 1099 | 'f<F<d<' => [3.14159, 111.11, 2222.22],
|
|---|
| 1100 | 'D<cCD<' => [1e42, -128, 255, 1e-42],
|
|---|
| 1101 | 'n/a*' => ['/usr/bin/perl'],
|
|---|
| 1102 | 'C/a*S</A*L</Z*I</a*' => [qw(Just another Perl hacker)],
|
|---|
| 1103 | );
|
|---|
| 1104 |
|
|---|
| 1105 | for my $tle (sort keys %templates) {
|
|---|
| 1106 | my @d = @{$templates{$tle}};
|
|---|
| 1107 | my $tbe = $tle;
|
|---|
| 1108 | $tbe =~ y/</>/;
|
|---|
| 1109 | for my $t ($tbe, $tle) {
|
|---|
| 1110 | my $c = compress_template($t);
|
|---|
| 1111 | print "# '$t' -> '$c'\n";
|
|---|
| 1112 | SKIP: {
|
|---|
| 1113 | my $p1 = eval { pack $t, @d };
|
|---|
| 1114 | skip "cannot pack '$t' on this perl", 5 if is_valid_error($@);
|
|---|
| 1115 | my $p2 = eval { pack $c, @d };
|
|---|
| 1116 | is($@, '');
|
|---|
| 1117 | is($p1, $p2);
|
|---|
| 1118 | s!(/[aAZ])\*!$1!g for $t, $c;
|
|---|
| 1119 | my @u1 = eval { unpack $t, $p1 };
|
|---|
| 1120 | is($@, '');
|
|---|
| 1121 | my @u2 = eval { unpack $c, $p2 };
|
|---|
| 1122 | is($@, '');
|
|---|
| 1123 | is(join('!', @u1), join('!', @u2));
|
|---|
| 1124 | }
|
|---|
| 1125 | }
|
|---|
| 1126 | }
|
|---|
| 1127 | }
|
|---|
| 1128 |
|
|---|
| 1129 | {
|
|---|
| 1130 | # from Wolfgang Laun: fix in change #13163
|
|---|
| 1131 |
|
|---|
| 1132 | my $s = 'ABC' x 10;
|
|---|
| 1133 | my $t = '*';
|
|---|
| 1134 | my $x = ord($t);
|
|---|
| 1135 | my $buf = pack( 'Z*/A* C', $s, $x );
|
|---|
| 1136 | my $y;
|
|---|
| 1137 |
|
|---|
| 1138 | my $h = $buf;
|
|---|
| 1139 | $h =~ s/[^[:print:]]/./g;
|
|---|
| 1140 | ( $s, $y ) = unpack( "Z*/A* C", $buf );
|
|---|
| 1141 | is($h, "30.ABCABCABCABCABCABCABCABCABCABC$t");
|
|---|
| 1142 | is(length $buf, 34);
|
|---|
| 1143 | is($s, "ABCABCABCABCABCABCABCABCABCABC");
|
|---|
| 1144 | is($y, $x);
|
|---|
| 1145 | }
|
|---|
| 1146 |
|
|---|
| 1147 | {
|
|---|
| 1148 | # from Wolfgang Laun: fix in change #13288
|
|---|
| 1149 |
|
|---|
| 1150 | eval { my $t=unpack("P*", "abc") };
|
|---|
| 1151 | like($@, qr/'P' must have an explicit size/);
|
|---|
| 1152 | }
|
|---|
| 1153 |
|
|---|
| 1154 | { # Grouping constructs
|
|---|
| 1155 | my (@a, @b);
|
|---|
| 1156 | @a = unpack '(SL)', pack 'SLSLSL', 67..90;
|
|---|
| 1157 | is("@a", "67 68");
|
|---|
| 1158 | @a = unpack '(SL)3', pack 'SLSLSL', 67..90;
|
|---|
| 1159 | @b = (67..72);
|
|---|
| 1160 | is("@a", "@b");
|
|---|
| 1161 | @a = unpack '(SL)3', pack 'SLSLSLSL', 67..90;
|
|---|
| 1162 | is("@a", "@b");
|
|---|
| 1163 | @a = unpack '(SL)[3]', pack 'SLSLSLSL', 67..90;
|
|---|
| 1164 | is("@a", "@b");
|
|---|
| 1165 | @a = unpack '(SL)[2] SL', pack 'SLSLSLSL', 67..90;
|
|---|
| 1166 | is("@a", "@b");
|
|---|
| 1167 | @a = unpack 'A/(SL)', pack 'ASLSLSLSL', 3, 67..90;
|
|---|
| 1168 | is("@a", "@b");
|
|---|
| 1169 | @a = unpack 'A/(SL)SL', pack 'ASLSLSLSL', 2, 67..90;
|
|---|
| 1170 | is("@a", "@b");
|
|---|
| 1171 | @a = unpack '(SL)*', pack 'SLSLSLSL', 67..90;
|
|---|
| 1172 | @b = (67..74);
|
|---|
| 1173 | is("@a", "@b");
|
|---|
| 1174 | @a = unpack '(SL)*SL', pack 'SLSLSLSL', 67..90;
|
|---|
| 1175 | is("@a", "@b");
|
|---|
| 1176 | eval { @a = unpack '(*SL)', '' };
|
|---|
| 1177 | like($@, qr/\(\)-group starts with a count/);
|
|---|
| 1178 | eval { @a = unpack '(3SL)', '' };
|
|---|
| 1179 | like($@, qr/\(\)-group starts with a count/);
|
|---|
| 1180 | eval { @a = unpack '([3]SL)', '' };
|
|---|
| 1181 | like($@, qr/\(\)-group starts with a count/);
|
|---|
| 1182 | eval { @a = pack '(*SL)' };
|
|---|
| 1183 | like($@, qr/\(\)-group starts with a count/);
|
|---|
| 1184 | @a = unpack '(SL)3 SL', pack '(SL)4', 67..74;
|
|---|
| 1185 | is("@a", "@b");
|
|---|
| 1186 | @a = unpack '(SL)3 SL', pack '(SL)[4]', 67..74;
|
|---|
| 1187 | is("@a", "@b");
|
|---|
| 1188 | @a = unpack '(SL)3 SL', pack '(SL)*', 67..74;
|
|---|
| 1189 | is("@a", "@b");
|
|---|
| 1190 | }
|
|---|
| 1191 |
|
|---|
| 1192 | { # more on grouping (W.Laun)
|
|---|
| 1193 | use warnings;
|
|---|
| 1194 | my $warning;
|
|---|
| 1195 | local $SIG{__WARN__} = sub {
|
|---|
| 1196 | $warning = $_[0];
|
|---|
| 1197 | };
|
|---|
| 1198 | # @ absolute within ()-group
|
|---|
| 1199 | my $badc = pack( '(a)*', unpack( '(@1a @0a @2)*', 'abcd' ) );
|
|---|
| 1200 | is( $badc, 'badc' );
|
|---|
| 1201 | my @b = ( 1, 2, 3 );
|
|---|
| 1202 | my $buf = pack( '(@1c)((@2C)@3c)', @b );
|
|---|
| 1203 | is( $buf, "\0\1\0\0\2\3" );
|
|---|
| 1204 | my @a = unpack( '(@1c)((@2c)@3c)', $buf );
|
|---|
| 1205 | is( "@a", "@b" );
|
|---|
| 1206 |
|
|---|
| 1207 | # various unpack count/code scenarios
|
|---|
| 1208 | my @Env = ( a => 'AAA', b => 'BBB' );
|
|---|
| 1209 | my $env = pack( 'S(S/A*S/A*)*', @Env/2, @Env );
|
|---|
| 1210 |
|
|---|
| 1211 | # unpack full length - ok
|
|---|
| 1212 | my @pup = unpack( 'S/(S/A* S/A*)', $env );
|
|---|
| 1213 | is( "@pup", "@Env" );
|
|---|
| 1214 |
|
|---|
| 1215 | # warn when count/code goes beyond end of string
|
|---|
| 1216 | # \0002 \0001 a \0003 AAA \0001 b \0003 BBB
|
|---|
| 1217 | # 2 4 5 7 10 1213
|
|---|
| 1218 | eval { @pup = unpack( 'S/(S/A* S/A*)', substr( $env, 0, 13 ) ) };
|
|---|
| 1219 | like( $@, qr{length/code after end of string} );
|
|---|
| 1220 |
|
|---|
| 1221 | # postfix repeat count
|
|---|
| 1222 | $env = pack( '(S/A* S/A*)' . @Env/2, @Env );
|
|---|
| 1223 |
|
|---|
| 1224 | # warn when count/code goes beyond end of string
|
|---|
| 1225 | # \0001 a \0003 AAA \0001 b \0003 BBB
|
|---|
| 1226 | # 2 3c 5 8 10 11 13 16
|
|---|
| 1227 | eval { @pup = unpack( '(S/A* S/A*)' . @Env/2, substr( $env, 0, 11 ) ) };
|
|---|
| 1228 | like( $@, qr{length/code after end of string} );
|
|---|
| 1229 |
|
|---|
| 1230 | # catch stack overflow/segfault
|
|---|
| 1231 | eval { $_ = pack( ('(' x 105) . 'A' . (')' x 105) ); };
|
|---|
| 1232 | like( $@, qr{Too deeply nested \(\)-groups} );
|
|---|
| 1233 | }
|
|---|
| 1234 |
|
|---|
| 1235 | { # syntax checks (W.Laun)
|
|---|
| 1236 | use warnings;
|
|---|
| 1237 | my @warning;
|
|---|
| 1238 | local $SIG{__WARN__} = sub {
|
|---|
| 1239 | push( @warning, $_[0] );
|
|---|
| 1240 | };
|
|---|
| 1241 | eval { my $s = pack( 'Ax![4c]A', 1..5 ); };
|
|---|
| 1242 | like( $@, qr{Malformed integer in \[\]} );
|
|---|
| 1243 |
|
|---|
| 1244 | eval { my $buf = pack( '(c/*a*)', 'AAA', 'BB' ); };
|
|---|
| 1245 | like( $@, qr{'/' does not take a repeat count} );
|
|---|
| 1246 |
|
|---|
| 1247 | eval { my @inf = unpack( 'c/1a', "\x03AAA\x02BB" ); };
|
|---|
| 1248 | like( $@, qr{'/' does not take a repeat count} );
|
|---|
| 1249 |
|
|---|
| 1250 | eval { my @inf = unpack( 'c/*a', "\x03AAA\x02BB" ); };
|
|---|
| 1251 | like( $@, qr{'/' does not take a repeat count} );
|
|---|
| 1252 |
|
|---|
| 1253 | # white space where possible
|
|---|
| 1254 | my @Env = ( a => 'AAA', b => 'BBB' );
|
|---|
| 1255 | my $env = pack( ' S ( S / A* S / A* )* ', @Env/2, @Env );
|
|---|
| 1256 | my @pup = unpack( ' S / ( S / A* S / A* ) ', $env );
|
|---|
| 1257 | is( "@pup", "@Env" );
|
|---|
| 1258 |
|
|---|
| 1259 | # white space in 4 wrong places
|
|---|
| 1260 | for my $temp ( 'A ![4]', 'A [4]', 'A *', 'A 4' ){
|
|---|
| 1261 | eval { my $s = pack( $temp, 'B' ); };
|
|---|
| 1262 | like( $@, qr{Invalid type } );
|
|---|
| 1263 | }
|
|---|
| 1264 |
|
|---|
| 1265 | # warning for commas
|
|---|
| 1266 | @warning = ();
|
|---|
| 1267 | my $x = pack( 'I,A', 4, 'X' );
|
|---|
| 1268 | like( $warning[0], qr{Invalid type ','} );
|
|---|
| 1269 |
|
|---|
| 1270 | # comma warning only once
|
|---|
| 1271 | @warning = ();
|
|---|
| 1272 | $x = pack( 'C(C,C)C,C', 65..71 );
|
|---|
| 1273 | like( scalar @warning, 1 );
|
|---|
| 1274 |
|
|---|
| 1275 | # forbidden code in []
|
|---|
| 1276 | eval { my $x = pack( 'A[@4]', 'XXXX' ); };
|
|---|
| 1277 | like( $@, qr{Within \[\]-length '\@' not allowed} );
|
|---|
| 1278 |
|
|---|
| 1279 | # @ repeat default 1
|
|---|
| 1280 | my $s = pack( 'AA@A', 'A', 'B', 'C' );
|
|---|
| 1281 | my @c = unpack( 'AA@A', $s );
|
|---|
| 1282 | is( $s, 'AC' );
|
|---|
| 1283 | is( "@c", "A C C" );
|
|---|
| 1284 |
|
|---|
| 1285 | # no unpack code after /
|
|---|
| 1286 | eval { my @a = unpack( "C/", "\3" ); };
|
|---|
| 1287 | like( $@, qr{Code missing after '/'} );
|
|---|
| 1288 |
|
|---|
| 1289 | SKIP: {
|
|---|
| 1290 | skip $no_endianness, 6 if $no_endianness;
|
|---|
| 1291 |
|
|---|
| 1292 | # modifier warnings
|
|---|
| 1293 | @warning = ();
|
|---|
| 1294 | $x = pack "I>>s!!", 47, 11;
|
|---|
| 1295 | ($x) = unpack "I<<l!>!>", 'x'x20;
|
|---|
| 1296 | is(scalar @warning, 5);
|
|---|
| 1297 | like($warning[0], qr/Duplicate modifier '>' after 'I' in pack/);
|
|---|
| 1298 | like($warning[1], qr/Duplicate modifier '!' after 's' in pack/);
|
|---|
| 1299 | like($warning[2], qr/Duplicate modifier '<' after 'I' in unpack/);
|
|---|
| 1300 | like($warning[3], qr/Duplicate modifier '!' after 'l' in unpack/);
|
|---|
| 1301 | like($warning[4], qr/Duplicate modifier '>' after 'l' in unpack/);
|
|---|
| 1302 | }
|
|---|
| 1303 | }
|
|---|
| 1304 |
|
|---|
| 1305 | { # Repeat count [SUBEXPR]
|
|---|
| 1306 | 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
|
|---|
| 1307 | s! S! i! I! l! L! j J);
|
|---|
| 1308 | my $G;
|
|---|
| 1309 | if (eval { pack 'q', 1 } ) {
|
|---|
| 1310 | push @codes, qw(q Q);
|
|---|
| 1311 | } else {
|
|---|
| 1312 | push @codes, qw(s S); # Keep the count the same
|
|---|
| 1313 | }
|
|---|
| 1314 | if (eval { pack 'D', 1 } ) {
|
|---|
| 1315 | push @codes, 'D';
|
|---|
| 1316 | } else {
|
|---|
| 1317 | push @codes, 'd'; # Keep the count the same
|
|---|
| 1318 | }
|
|---|
| 1319 |
|
|---|
| 1320 | push @codes, map { /^[silqjfdp]/i ? ("$_<", "$_>") : () } @codes;
|
|---|
| 1321 |
|
|---|
| 1322 | my %val;
|
|---|
| 1323 | @val{@codes} = map { / [Xx] (?{ undef })
|
|---|
| 1324 | | [AZa] (?{ 'something' })
|
|---|
| 1325 | | C (?{ 214 })
|
|---|
| 1326 | | c (?{ 114 })
|
|---|
| 1327 | | [Bb] (?{ '101' })
|
|---|
| 1328 | | [Hh] (?{ 'b8' })
|
|---|
| 1329 | | [svnSiIlVNLqQjJ] (?{ 10111 })
|
|---|
| 1330 | | [FfDd] (?{ 1.36514538e67 })
|
|---|
| 1331 | | [pP] (?{ "try this buffer" })
|
|---|
| 1332 | /x; $^R } @codes;
|
|---|
| 1333 | my @end = (0x12345678, 0x23456781, 0x35465768, 0x15263748);
|
|---|
| 1334 | my $end = "N4";
|
|---|
| 1335 |
|
|---|
| 1336 | for my $type (@codes) {
|
|---|
| 1337 | my @list = $val{$type};
|
|---|
| 1338 | @list = () unless defined $list[0];
|
|---|
| 1339 | for my $count ('', '3', '[11]') {
|
|---|
| 1340 | my $c = 1;
|
|---|
| 1341 | $c = $1 if $count =~ /(\d+)/;
|
|---|
| 1342 | my @list1 = @list;
|
|---|
| 1343 | @list1 = (@list1) x $c unless $type =~ /[XxAaZBbHhP]/;
|
|---|
| 1344 | for my $groupend ('', ')2', ')[8]') {
|
|---|
| 1345 | my $groupbegin = ($groupend ? '(' : '');
|
|---|
| 1346 | $c = 1;
|
|---|
| 1347 | $c = $1 if $groupend =~ /(\d+)/;
|
|---|
| 1348 | my @list2 = (@list1) x $c;
|
|---|
| 1349 |
|
|---|
| 1350 | SKIP: {
|
|---|
| 1351 | my $junk1 = "$groupbegin $type$count $groupend";
|
|---|
| 1352 | # print "# junk1=$junk1\n";
|
|---|
| 1353 | my $p = eval { pack $junk1, @list2 };
|
|---|
| 1354 | skip "cannot pack '$type' on this perl", 12
|
|---|
| 1355 | if is_valid_error($@);
|
|---|
| 1356 | die "pack $junk1 failed: $@" if $@;
|
|---|
| 1357 |
|
|---|
| 1358 | my $half = int( (length $p)/2 );
|
|---|
| 1359 | for my $move ('', "X$half", "X!$half", 'x1', 'x!8', "x$half") {
|
|---|
| 1360 | my $junk = "$junk1 $move";
|
|---|
| 1361 | # print "# junk='$junk', list=(@list2)\n";
|
|---|
| 1362 | $p = pack "$junk $end", @list2, @end;
|
|---|
| 1363 | my @l = unpack "x[$junk] $end", $p;
|
|---|
| 1364 | is(scalar @l, scalar @end);
|
|---|
| 1365 | is("@l", "@end", "skipping x[$junk]");
|
|---|
| 1366 | }
|
|---|
| 1367 | }
|
|---|
| 1368 | }
|
|---|
| 1369 | }
|
|---|
| 1370 | }
|
|---|
| 1371 | }
|
|---|
| 1372 |
|
|---|
| 1373 | # / is recognized after spaces in scalar context
|
|---|
| 1374 | # XXXX no spaces are allowed in pack... In pack only before the slash...
|
|---|
| 1375 | is(scalar unpack('A /A Z20', pack 'A/A* Z20', 'bcde', 'xxxxx'), 'bcde');
|
|---|
| 1376 | is(scalar unpack('A /A /A Z20', '3004bcde'), 'bcde');
|
|---|
| 1377 |
|
|---|
| 1378 | { # X! and x!
|
|---|
| 1379 | my $t = 'C[3] x!8 C[2]';
|
|---|
| 1380 | my @a = (0x73..0x77);
|
|---|
| 1381 | my $p = pack($t, @a);
|
|---|
| 1382 | is($p, "\x73\x74\x75\0\0\0\0\0\x76\x77");
|
|---|
| 1383 | my @b = unpack $t, $p;
|
|---|
| 1384 | is(scalar @b, scalar @a);
|
|---|
| 1385 | is("@b", "@a", 'x!8');
|
|---|
| 1386 | $t = 'x[5] C[6] X!8 C[2]';
|
|---|
| 1387 | @a = (0x73..0x7a);
|
|---|
| 1388 | $p = pack($t, @a);
|
|---|
| 1389 | is($p, "\0\0\0\0\0\x73\x74\x75\x79\x7a");
|
|---|
| 1390 | @b = unpack $t, $p;
|
|---|
| 1391 | @a = (0x73..0x75, 0x79, 0x7a, 0x79, 0x7a);
|
|---|
| 1392 | is(scalar @b, scalar @a);
|
|---|
| 1393 | is("@b", "@a");
|
|---|
| 1394 | }
|
|---|
| 1395 |
|
|---|
| 1396 | { # struct {char c1; double d; char cc[2];}
|
|---|
| 1397 | my $t = 'C x![d] d C[2]';
|
|---|
| 1398 | my @a = (173, 1.283476517e-45, 42, 215);
|
|---|
| 1399 | my $p = pack $t, @a;
|
|---|
| 1400 | ok( length $p);
|
|---|
| 1401 | my @b = unpack "$t X[$t] $t", $p; # Extract, step back, extract again
|
|---|
| 1402 | is(scalar @b, 2 * scalar @a);
|
|---|
| 1403 | $b = "@b";
|
|---|
| 1404 | $b =~ s/(?:17000+|16999+)\d+(e-45) /17$1 /gi; # stringification is gamble
|
|---|
| 1405 | is($b, "@a @a");
|
|---|
| 1406 |
|
|---|
| 1407 | my $warning;
|
|---|
| 1408 | local $SIG{__WARN__} = sub {
|
|---|
| 1409 | $warning = $_[0];
|
|---|
| 1410 | };
|
|---|
| 1411 | @b = unpack "x[C] x[$t] X[$t] X[C] $t", "$p\0";
|
|---|
| 1412 |
|
|---|
| 1413 | is($warning, undef);
|
|---|
| 1414 | is(scalar @b, scalar @a);
|
|---|
| 1415 | $b = "@b";
|
|---|
| 1416 | $b =~ s/(?:17000+|16999+)\d+(e-45) /17$1 /gi; # stringification is gamble
|
|---|
| 1417 | is($b, "@a");
|
|---|
| 1418 | }
|
|---|
| 1419 |
|
|---|
| 1420 | is(length(pack("j", 0)), $Config{ivsize});
|
|---|
| 1421 | is(length(pack("J", 0)), $Config{uvsize});
|
|---|
| 1422 | is(length(pack("F", 0)), $Config{nvsize});
|
|---|
| 1423 |
|
|---|
| 1424 | numbers ('j', -2147483648, -1, 0, 1, 2147483647);
|
|---|
| 1425 | numbers ('J', 0, 1, 2147483647, 2147483648, 4294967295);
|
|---|
| 1426 | numbers ('F', -(2**34), -1, 0, 1, 2**34);
|
|---|
| 1427 | SKIP: {
|
|---|
| 1428 | my $t = eval { unpack("D*", pack("D", 12.34)) };
|
|---|
| 1429 |
|
|---|
| 1430 | skip "Long doubles not in use", 166 if $@ =~ /Invalid type/;
|
|---|
| 1431 |
|
|---|
| 1432 | is(length(pack("D", 0)), $Config{longdblsize});
|
|---|
| 1433 | numbers ('D', -(2**34), -1, 0, 1, 2**34);
|
|---|
| 1434 | }
|
|---|
| 1435 |
|
|---|
| 1436 | # Maybe this knowledge needs to be "global" for all of pack.t
|
|---|
| 1437 | # Or a "can checksum" which would effectively be all the number types"
|
|---|
| 1438 | my %cant_checksum = map {$_=> 1} qw(A Z u w);
|
|---|
| 1439 | # not a b B h H
|
|---|
| 1440 | foreach 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)) {
|
|---|
| 1441 | SKIP: {
|
|---|
| 1442 | my $packed = eval {pack "${template}4", 1, 4, 9, 16};
|
|---|
| 1443 | if ($@) {
|
|---|
| 1444 | die unless $@ =~ /Invalid type '$template'/;
|
|---|
| 1445 | skip ("$template not supported on this perl",
|
|---|
| 1446 | $cant_checksum{$template} ? 4 : 8);
|
|---|
| 1447 | }
|
|---|
| 1448 | my @unpack4 = unpack "${template}4", $packed;
|
|---|
| 1449 | my @unpack = unpack "${template}*", $packed;
|
|---|
| 1450 | my @unpack1 = unpack "${template}", $packed;
|
|---|
| 1451 | my @unpack1s = scalar unpack "${template}", $packed;
|
|---|
| 1452 | my @unpack4s = scalar unpack "${template}4", $packed;
|
|---|
| 1453 | my @unpacks = scalar unpack "${template}*", $packed;
|
|---|
| 1454 |
|
|---|
| 1455 | my @tests = ( ["${template}4 vs ${template}*", \@unpack4, \@unpack],
|
|---|
| 1456 | ["scalar ${template} ${template}", \@unpack1s, \@unpack1],
|
|---|
| 1457 | ["scalar ${template}4 vs ${template}", \@unpack4s, \@unpack1],
|
|---|
| 1458 | ["scalar ${template}* vs ${template}", \@unpacks, \@unpack1],
|
|---|
| 1459 | );
|
|---|
| 1460 |
|
|---|
| 1461 | unless ($cant_checksum{$template}) {
|
|---|
| 1462 | my @unpack4_c = unpack "\%${template}4", $packed;
|
|---|
| 1463 | my @unpack_c = unpack "\%${template}*", $packed;
|
|---|
| 1464 | my @unpack1_c = unpack "\%${template}", $packed;
|
|---|
| 1465 | my @unpack1s_c = scalar unpack "\%${template}", $packed;
|
|---|
| 1466 | my @unpack4s_c = scalar unpack "\%${template}4", $packed;
|
|---|
| 1467 | my @unpacks_c = scalar unpack "\%${template}*", $packed;
|
|---|
| 1468 |
|
|---|
| 1469 | push @tests,
|
|---|
| 1470 | ( ["% ${template}4 vs ${template}*", \@unpack4_c, \@unpack_c],
|
|---|
| 1471 | ["% scalar ${template} ${template}", \@unpack1s_c, \@unpack1_c],
|
|---|
| 1472 | ["% scalar ${template}4 vs ${template}*", \@unpack4s_c, \@unpack_c],
|
|---|
| 1473 | ["% scalar ${template}* vs ${template}*", \@unpacks_c, \@unpack_c],
|
|---|
| 1474 | );
|
|---|
| 1475 | }
|
|---|
| 1476 | foreach my $test (@tests) {
|
|---|
| 1477 | ok (list_eq ($test->[1], $test->[2]), $test->[0]) ||
|
|---|
| 1478 | printf "# unpack gave %s expected %s\n",
|
|---|
| 1479 | encode_list (@{$test->[1]}), encode_list (@{$test->[2]});
|
|---|
| 1480 | }
|
|---|
| 1481 | }
|
|---|
| 1482 | }
|
|---|
| 1483 |
|
|---|
| 1484 | ok(pack('u2', 'AA'), "[perl #8026]"); # used to hang and eat RAM in perl 5.7.2
|
|---|
| 1485 |
|
|---|
| 1486 | $_ = pack('c', 65); # 'A' would not be EBCDIC-friendly
|
|---|
| 1487 | eval "unpack('c')";
|
|---|
| 1488 | like ($@, qr/Not enough arguments for unpack/,
|
|---|
| 1489 | "one-arg unpack (change #18751) is not in maint");
|
|---|
| 1490 |
|
|---|
| 1491 | {
|
|---|
| 1492 | my $a = "X\t01234567\n" x 100;
|
|---|
| 1493 | my @a = unpack("(a1 c/a)*", $a);
|
|---|
| 1494 | is(scalar @a, 200, "[perl #15288]");
|
|---|
| 1495 | is($a[-1], "01234567\n", "[perl #15288]");
|
|---|
| 1496 | is($a[-2], "X", "[perl #15288]");
|
|---|
| 1497 | }
|
|---|
| 1498 |
|
|---|
| 1499 | # checksums
|
|---|
| 1500 | {
|
|---|
| 1501 | # verify that unpack advances correctly wrt a checksum
|
|---|
| 1502 | my (@x) = unpack("b10a", "abcd");
|
|---|
| 1503 | my (@y) = unpack("%b10a", "abcd");
|
|---|
| 1504 | is($x[1], $y[1], "checksum advance ok");
|
|---|
| 1505 |
|
|---|
| 1506 | # verify that the checksum is not overflowed with C0
|
|---|
| 1507 | is(unpack("C0%128U", "abcd"), unpack("U0%128U", "abcd"), "checksum not overflowed");
|
|---|
| 1508 | }
|
|---|
| 1509 |
|
|---|
| 1510 | {
|
|---|
| 1511 | # U0 and C0 must be scoped
|
|---|
| 1512 | my (@x) = unpack("a(U0)U", "b\341\277\274");
|
|---|
| 1513 | is($x[0], 'b', 'before scope');
|
|---|
| 1514 | is($x[1], 225, 'after scope');
|
|---|
| 1515 | }
|
|---|
| 1516 |
|
|---|
| 1517 | {
|
|---|
| 1518 | # counted length prefixes shouldn't change C0/U0 mode
|
|---|
| 1519 | # (note the length is actually 0 in this test)
|
|---|
| 1520 | is(join(',', unpack("aC/UU", "b\0\341\277\274")), 'b,225');
|
|---|
| 1521 | is(join(',', unpack("aC/CU", "b\0\341\277\274")), 'b,225');
|
|---|
| 1522 | is(join(',', unpack("aU0C/UU", "b\0\341\277\274")), 'b,8188');
|
|---|
| 1523 | is(join(',', unpack("aU0C/CU", "b\0\341\277\274")), 'b,8188');
|
|---|
| 1524 | }
|
|---|
| 1525 |
|
|---|
| 1526 | {
|
|---|
| 1527 | # "Z0" (bug #34062)
|
|---|
| 1528 | my (@x) = unpack("C*", pack("CZ0", 1, "b"));
|
|---|
| 1529 | is(join(',', @x), '1', 'pack Z0 doesn\'t destroy the character before');
|
|---|
| 1530 | }
|
|---|