4 # test the bit operators '&', '|', '^', '~', '<<', and '>>'
10 require "./test.pl"; require "./charset_tools.pl";
14 # Tests don't have names yet.
15 # If you find tests are failing, please try adding names to tests to track
16 # down where the failure is, and supply your new names as a patch.
17 # (Just-in-time test naming)
18 plan tests => 192 + (10*13*2) + 5 + 29;
21 ok ((0xdead & 0xbeef) == 0x9ead);
22 ok ((0xdead | 0xbeef) == 0xfeef);
23 ok ((0xdead ^ 0xbeef) == 0x6042);
24 ok ((~0xdead & 0xbeef) == 0x2042);
27 ok ((257 << 7) == 32896);
28 ok ((33023 >> 7) == 257);
31 ok ((~0 > 0 && do { use integer; ~0 } == -1));
34 for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
35 my $cusp = 1 << ($bits - 1);
38 ok (($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0);
39 ok (($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0);
40 ok (($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0);
41 ok ((1 << ($bits - 1)) == $cusp &&
42 do { use integer; 1 << ($bits - 1) } == -$cusp);
43 ok (($cusp >> 1) == ($cusp / 2) &&
44 do { use integer; abs($cusp >> 1) } == ($cusp / 2));
46 $Aaz = chr(ord("A") & ord("z"));
47 $Aoz = chr(ord("A") | ord("z"));
48 $Axz = chr(ord("A") ^ ord("z"));
51 is (("AAAAA" & "zzzzz"), ($Aaz x 5));
52 is (("AAAAA" | "zzzzz"), ($Aoz x 5));
53 is (("AAAAA" ^ "zzzzz"), ($Axz x 5));
60 is (($foo & $bar), ($Aaz x 75 ));
62 is (($foo | $bar), ($Aoz x 75 . $zap));
64 is (($foo ^ $bar), ($Axz x 75 . $zap));
66 # string constants. These tests expect the bit patterns of these strings in
67 # ASCII, so convert to that.
68 sub _and($) { $_[0] & native_to_uni("+0") }
69 sub _oar($) { $_[0] | native_to_uni("+0") }
70 sub _xor($) { $_[0] ^ native_to_uni("+0") }
71 is _and native_to_uni("waf"), native_to_uni('# '), 'str var & const str'; # [perl #20661]
72 is _and native_to_uni("waf"), native_to_uni('# '), 'str var & const str again'; # [perl #20661]
73 is _oar native_to_uni("yit"), native_to_uni('{yt'), 'str var | const str';
74 is _oar native_to_uni("yit"), native_to_uni('{yt'), 'str var | const str again';
75 is _xor native_to_uni("yit"), native_to_uni('RYt'), 'str var ^ const str';
76 is _xor native_to_uni("yit"), native_to_uni('RYt'), 'str var ^ const str again';
79 skip "Converting a numeric doesn't work with EBCDIC unlike the above tests",
81 is _and 0, '0', 'num var & const str'; # [perl #20661]
82 is _oar 0, '0', 'num var | const str';
83 is _xor 0, '0', 'num var ^ const str';
86 # But don’t mistake a COW for a constant when assigning to it
90 is $i, 255, '[perl #108480] $cow |= number';
93 is $i, 0, '[perl #108480] $cow &= number';
96 is $i, 255, '[perl #108480] $cow ^= number';
99 is ("ok \xFF\xFF\n" & "ok 19\n", "ok 19\n");
100 is ("ok 20\n" | "ok \0\0\n", "ok 20\n");
101 is ("o\000 \0001\000" ^ "\000k\0002\000\n", "ok 21\n");
104 is ("ok \x{FF}\x{FF}\n" & "ok 22\n", "ok 22\n");
105 is ("ok 23\n" | "ok \x{0}\x{0}\n", "ok 23\n");
106 is ("o\x{0} \x{0}4\x{0}" ^ "\x{0}k\x{0}2\x{0}\n", "ok 24\n");
109 is (sprintf("%vd", v4095 & v801), 801);
110 is (sprintf("%vd", v4095 | v801), 4095);
111 is (sprintf("%vd", v4095 ^ v801), 3294);
114 is (sprintf("%vd", v4095.801.4095 & v801.4095), '801.801');
115 is (sprintf("%vd", v4095.801.4095 | v801.4095), '4095.4095.4095');
116 is (sprintf("%vd", v801.4095 ^ v4095.801.4095), '3294.3294.4095');
118 is (sprintf("%vd", v120.300 & v200.400), '72.256');
119 is (sprintf("%vd", v120.300 | v200.400), '248.444');
120 is (sprintf("%vd", v120.300 ^ v200.400), '176.188');
126 is (sprintf("%vd", $a), '176.188');
132 is (sprintf("%vd", $a), '248.444');
140 skip "Complements exceed maximum representable on EBCDIC ", 5 if $::IS_EBCDIC;
144 for (0x100...0xFFF) {
146 push @not36, sprintf("%#03X", $_)
147 if $a ne chr(~$_) or length($a) != 1 or ~$a ne chr($_);
149 is (join (', ', @not36), '');
153 for my $i (0xEEE...0xF00) {
154 for my $j (0x0..0x120) {
155 $a = ~(chr ($i) . chr $j);
156 push @not37, sprintf("%#03X %#03X", $i, $j)
157 if $a ne chr(~$i).chr(~$j) or
159 ~$a ne chr($i).chr($j);
162 is (join (', ', @not37), '');
169 for my $i (0x100..0x120) {
170 for my $j (0x100...0x120) {
171 push @not39, sprintf("%#03X %#03X", $i, $j)
172 if ~(chr($i)|chr($j)) ne (~chr($i)&~chr($j));
175 is (join (', ', @not39), '');
179 for my $i (0x100..0x120) {
180 for my $j (0x100...0x120) {
181 push @not40, sprintf("%#03X %#03X", $i, $j)
182 if ~(chr($i)&chr($j)) ne (~chr($i)|~chr($j));
185 is (join (', ', @not40), '');
189 # More variations on 19 and 22.
190 is ("ok \xFF\x{FF}\n" & "ok 41\n", "ok 41\n");
191 is ("ok \x{FF}\xFF\n" & "ok 42\n", "ok 42\n");
193 # Tests to see if you really can do casts negative floats to unsigned properly
202 sub TIESCALAR { bless { value => $_[1], orig => $_[1] } }
203 sub STORE { $_[0]{store}++; $_[0]{value} = $_[1] }
204 sub FETCH { $_[0]{fetch}++; $_[0]{value} }
205 sub stores { tied($_[0])->{value} = tied($_[0])->{orig};
206 delete(tied($_[0])->{store}) || 0 }
207 sub fetches { delete(tied($_[0])->{fetch}) || 0 }
209 # numeric double magic tests
296 } # end of use integer;
298 # stringwise double magic tests
303 is(($x | $y), ("a" | "c"));
309 is(($x & $y), ("a" & "c"));
315 is(($x ^ $y), ("a" ^ "c"));
321 is(($x |= $y), ("a" | "c"));
327 is(($x &= $y), ("a" & "c"));
333 is(($x ^= $y), ("a" ^ "c"));
343 $a = "\0\x{100}"; chop($a);
344 ok(utf8::is_utf8($a)); # make sure UTF8 flag is still there
346 is($a, "\xFF", "~ works with utf-8");
348 # [rt.perl.org 33003]
349 # This would cause a segfault without malloc wrap
351 skip "No malloc wrap checks" unless $Config::Config{usemallocwrap};
352 like( runperl(prog => 'eval q($#a>>=1); print 1'), qr/^1\n?/ );
355 # [perl #37616] Bug in &= (string) and/or m//
359 ok($a =~ /a+$/, 'ASCII "a" is NUL-terminated');
363 ok($b =~ /b+$/, 'Unicode "b" is NUL-terminated');
367 $a = chr(0x101) x 0x101;
368 $b = chr(0x0FF) x 0x0FF;
371 is($c, chr(0x1FF) x 0xFF . chr(0x101) x 2);
374 is($c, chr(0x1FF) x 0xFF . chr(0x101) x 2);
377 is($c, chr(0x001) x 0x0FF);
380 is($c, chr(0x001) x 0x0FF);
383 is($c, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
386 is($c, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
390 $a = chr(0x101) x 0x101;
391 $b = chr(0x0FF) x 0x0FF;
394 is($a, chr(0x1FF) x 0xFF . chr(0x101) x 2);
398 $a = chr(0x101) x 0x101;
399 $b = chr(0x0FF) x 0x0FF;
402 is($b, chr(0x1FF) x 0xFF . chr(0x101) x 2);
406 $a = chr(0x101) x 0x101;
407 $b = chr(0x0FF) x 0x0FF;
410 is($a, chr(0x001) x 0x0FF);
414 $a = chr(0x101) x 0x101;
415 $b = chr(0x0FF) x 0x0FF;
418 is($b, chr(0x001) x 0x0FF);
422 $a = chr(0x101) x 0x101;
423 $b = chr(0x0FF) x 0x0FF;
426 is($a, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
430 $a = chr(0x101) x 0x101;
431 $b = chr(0x0FF) x 0x0FF;
434 is($b, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
438 # New string- and number-specific bitwise ops
440 use feature "bitwise";
441 no warnings "experimental::bitwise";
442 is "22" & "66", 2, 'numeric & with strings';
443 is "22" | "66", 86, 'numeric | with strings';
444 is "22" ^ "66", 84, 'numeric ^ with strings';
445 is ~"22" & 0xff, 233, 'numeric ~ with string';
446 is 22 &. 66, 22, '&. with numbers';
447 is 22 |. 66, 66, '|. with numbers';
448 is 22 ^. 66, "\4\4", '^. with numbers';
450 # ord('2') is 0xF2 on EBCDIC
451 is ~.22, "\x0d\x0d", '~. with number';
454 # ord('2') is 0x32 on ASCII
455 is ~.22, "\xcd\xcd", '~. with number';
458 is $_ &= "66", 2, 'numeric &= with strings';
460 is $_ |= "66", 86, 'numeric |= with strings';
462 is $_ ^= "66", 84, 'numeric ^= with strings';
464 is $_ &.= 66, 22, '&.= with numbers';
466 is $_ |.= 66, 66, '|.= with numbers';
468 is $_ ^.= 66, "\4\4", '^.= with numbers';
470 # signed vs. unsigned
471 ok ((~0 > 0 && do { use integer; ~0 } == -1));
474 for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
475 my $cusp = 1 << ($bits - 1);
477 ok (($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0);
478 ok (($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0);
479 ok (($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0);
480 ok ((1 << ($bits - 1)) == $cusp &&
481 do { use integer; 1 << ($bits - 1) } == -$cusp);
482 ok (($cusp >> 1) == ($cusp / 2) &&
483 do { use integer; abs($cusp >> 1) } == ($cusp / 2));
490 for my $str ("x", "\x{100}") {
491 for my $chr (qw/S A H G X ( * F/) {
492 for my $op (qw/| & ^/) {
495 $res{"$chr$op$str"} = eval qq/chr($co $op $so)/;
498 $res{"undef|$str"} = $str;
499 $res{"undef&$str"} = "";
500 $res{"undef^$str"} = $str;
504 1 if index "foo", PVBM;
508 local $SIG{__WARN__} = sub { $warn++ };
511 my ($got, $orig, $op, $str, $name) = @_;
512 is(substr($got, 0, 1), $res{"$orig$op$str"}, $name);
516 # [object to test, first char of stringification, name]
517 [undef, "undef", "undef" ],
518 [\1, "S", "scalar ref" ],
519 [[], "A", "array ref" ],
520 [{}, "H", "hash ref" ],
521 [qr/x/, "(", "qr//" ],
522 [*foo, "*", "glob" ],
523 [\*foo, "G", "glob ref" ],
524 [PVBM, "X", "PVBM" ],
525 [\PVBM, "S", "PVBM ref" ],
526 [bless([], "Foo"), "F", "object" ],
528 my ($val, $orig, $type) = @$_;
530 for (["x", "string"], ["\x{100}", "utf8"]) {
531 my ($str, $desc) = @$_;
535 is_first($val | $str, $orig, "|", $str, "$type | $desc");
536 is_first($val & $str, $orig, "&", $str, "$type & $desc");
537 is_first($val ^ $str, $orig, "^", $str, "$type ^ $desc");
539 is_first($str | $val, $orig, "|", $str, "$desc | $type");
540 is_first($str & $val, $orig, "&", $str, "$desc & $type");
541 is_first($str ^ $val, $orig, "^", $str, "$desc ^ $type");
544 ($new = $val) |= $str;
545 is_first($new, $orig, "|", $str, "$type |= $desc");
546 ($new = $val) &= $str;
547 is_first($new, $orig, "&", $str, "$type &= $desc");
548 ($new = $val) ^= $str;
549 is_first($new, $orig, "^", $str, "$type ^= $desc");
551 ($new = $str) |= $val;
552 is_first($new, $orig, "|", $str, "$desc |= $type");
553 ($new = $str) &= $val;
554 is_first($new, $orig, "&", $str, "$desc &= $type");
555 ($new = $str) ^= $val;
556 is_first($new, $orig, "^", $str, "$desc ^= $type");
558 if ($orig eq "undef") {
559 # undef |= and undef ^= don't warn
560 is($warn, 10, "no duplicate warnings");
563 is($warn, 0, "no warnings");
572 use overload q/""/ => sub { $strval };
575 use overload q/|/ => sub { "y" };
578 ok(!eval { 1 if bless([], "Bar") | "x"; 1 },"string overload can't use |");
579 like($@, qr/no method found/, "correct error");
580 is(eval { bless([], "Baz") | "x" }, "y", "| overload works");
582 my $obj = bless [], "Bar";
584 eval { $obj |= "Q" };
586 is("$obj", "z", "|= doesn't break string overload");
589 $^A .= new version ~$_ for eval sprintf('"\\x%02x"', 0xff - ord("1")),
590 $::IS_EBCDIC ? v13 : v205, # 255 - ord('2')
591 eval sprintf('"\\x%02x"', 0xff - ord("3"));
592 is $^A, "123", '~v0 clears vstring magic on retval';
595 my $w = $Config::Config{ivsize} * 8;
597 fail("unexpected w $w") unless $w == 32 || $w == 64;
599 is(1 << 1, 2, "UV 1 left shift 1");
600 is(1 >> 1, 0, "UV 1 right shift 1");
602 is(0x7b << -4, 0x007, "UV left negative shift == right shift");
603 is(0x7b >> -4, 0x7b0, "UV right negative shift == left shift");
605 is(0x7b << 0, 0x07b, "UV left zero shift == identity");
606 is(0x7b >> 0, 0x07b, "UV right zero shift == identity");
608 is(0x0 << -1, 0x0, "zero left negative shift == zero");
609 is(0x0 >> -1, 0x0, "zero right negative shift == zero");
611 cmp_ok(1 << $w - 1, '==', 2 ** ($w - 1), # not is() because NV stringify.
612 "UV left $w - 1 shift == 2 ** ($w - 1)");
613 is(1 << $w, 0, "UV left shift $w == zero");
614 is(1 << $w + 1, 0, "UV left shift $w + 1 == zero");
616 is(1 >> $w - 1, 0, "UV right shift $w - 1 == zero");
617 is(1 >> $w, 0, "UV right shift $w == zero");
618 is(1 >> $w + 1, 0, "UV right shift $w + 1 == zero");
620 # Negative shiftees get promoted to UVs before shifting. This is
621 # not necessarily the ideal behavior, but that is what is happening.
623 no warnings "portable";
624 is(-1 << 1, 0xFFFF_FFFF_FFFF_FFFE,
625 "neg UV (sic) left shift = 0xFF..E");
626 is(-1 >> 1, 0x7FFF_FFFF_FFFF_FFFF,
627 "neg UV (sic) right right = 0x7F..F");
629 no warnings "portable";
630 is(-1 << 1, 0xFFFF_FFFE, "neg left shift == 0xFF..E");
631 is(-1 >> 1, 0x7FFF_FFFF, "neg right right == 0x7F..F");
635 # 'use integer' means use IVs instead of UVs.
639 is(1 << 1, 2, "IV 1 left shift 1 == 2");
640 is(1 >> 1, 0, "IV 1 right shift 1 == 0");
642 # The left overshift should behave like without 'use integer',
643 # that is, return zero.
644 is(1 << $w, 0, "IV 1 left shift $w == 0");
645 is(1 << $w + 1, 0, "IV 1 left shift $w + 1 == 0");
646 is(-1 << $w, 0, "IV -1 left shift $w == 0");
647 is(-1 << $w + 1, 0, "IV -1 left shift $w + 1 == 0");
649 # Even for negative IVs, left shift is multiplication.
650 # But right shift should display the stuckiness to -1.
651 is(-1 << 1, -2, "IV -1 left shift 1 == -2");
652 is(-1 >> 1, -1, "IV -1 right shift 1 == -1");
654 # As for UVs, negative shifting means the reverse shift.
655 is(-1 << -1, -1, "IV -1 left shift -1 == -1");
656 is(-1 >> -1, -2, "IV -1 right shift -1 == -2");
658 # Test also at and around wordsize, expect stuckiness to -1.
659 is(-1 >> $w - 1, -1, "IV -1 right shift $w - 1 == -1");
660 is(-1 >> $w, -1, "IV -1 right shift $w == -1");
661 is(-1 >> $w + 1, -1, "IV -1 right shift $w + 1 == -1");