4 # test the bit operators '&', '|', '^', '~', '<<', and '>>'
8 no warnings 'deprecated';
13 require "./test.pl"; require "./charset_tools.pl";
17 # Tests don't have names yet.
18 # If you find tests are failing, please try adding names to tests to track
19 # down where the failure is, and supply your new names as a patch.
20 # (Just-in-time test naming)
21 plan tests => 192 + (10*13*2) + 5 + 29;
24 ok ((0xdead & 0xbeef) == 0x9ead);
25 ok ((0xdead | 0xbeef) == 0xfeef);
26 ok ((0xdead ^ 0xbeef) == 0x6042);
27 ok ((~0xdead & 0xbeef) == 0x2042);
30 ok ((257 << 7) == 32896);
31 ok ((33023 >> 7) == 257);
34 ok ((~0 > 0 && do { use integer; ~0 } == -1));
37 for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
38 my $cusp = 1 << ($bits - 1);
41 ok (($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0);
42 ok (($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0);
43 ok (($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0);
44 ok ((1 << ($bits - 1)) == $cusp &&
45 do { use integer; 1 << ($bits - 1) } == -$cusp);
46 ok (($cusp >> 1) == ($cusp / 2) &&
47 do { use integer; abs($cusp >> 1) } == ($cusp / 2));
49 $Aaz = chr(ord("A") & ord("z"));
50 $Aoz = chr(ord("A") | ord("z"));
51 $Axz = chr(ord("A") ^ ord("z"));
54 is (("AAAAA" & "zzzzz"), ($Aaz x 5));
55 is (("AAAAA" | "zzzzz"), ($Aoz x 5));
56 is (("AAAAA" ^ "zzzzz"), ($Axz x 5));
63 is (($foo & $bar), ($Aaz x 75 ));
65 is (($foo | $bar), ($Aoz x 75 . $zap));
67 is (($foo ^ $bar), ($Axz x 75 . $zap));
69 # string constants. These tests expect the bit patterns of these strings in
70 # ASCII, so convert to that.
71 sub _and($) { $_[0] & native_to_uni("+0") }
72 sub _oar($) { $_[0] | native_to_uni("+0") }
73 sub _xor($) { $_[0] ^ native_to_uni("+0") }
74 is _and native_to_uni("waf"), native_to_uni('# '), 'str var & const str'; # [perl #20661]
75 is _and native_to_uni("waf"), native_to_uni('# '), 'str var & const str again'; # [perl #20661]
76 is _oar native_to_uni("yit"), native_to_uni('{yt'), 'str var | const str';
77 is _oar native_to_uni("yit"), native_to_uni('{yt'), 'str var | const str again';
78 is _xor native_to_uni("yit"), native_to_uni('RYt'), 'str var ^ const str';
79 is _xor native_to_uni("yit"), native_to_uni('RYt'), 'str var ^ const str again';
82 skip "Converting a numeric doesn't work with EBCDIC unlike the above tests",
84 is _and 0, '0', 'num var & const str'; # [perl #20661]
85 is _oar 0, '0', 'num var | const str';
86 is _xor 0, '0', 'num var ^ const str';
89 # But don’t mistake a COW for a constant when assigning to it
93 is $i, 255, '[perl #108480] $cow |= number';
96 is $i, 0, '[perl #108480] $cow &= number';
99 is $i, 255, '[perl #108480] $cow ^= number';
102 is ("ok \xFF\xFF\n" & "ok 19\n", "ok 19\n");
103 is ("ok 20\n" | "ok \0\0\n", "ok 20\n");
104 is ("o\000 \0001\000" ^ "\000k\0002\000\n", "ok 21\n");
107 is ("ok \x{FF}\x{FF}\n" & "ok 22\n", "ok 22\n");
108 is ("ok 23\n" | "ok \x{0}\x{0}\n", "ok 23\n");
109 is ("o\x{0} \x{0}4\x{0}" ^ "\x{0}k\x{0}2\x{0}\n", "ok 24\n");
112 is (sprintf("%vd", v4095 & v801), 801);
113 is (sprintf("%vd", v4095 | v801), 4095);
114 is (sprintf("%vd", v4095 ^ v801), 3294);
117 is (sprintf("%vd", v4095.801.4095 & v801.4095), '801.801');
118 is (sprintf("%vd", v4095.801.4095 | v801.4095), '4095.4095.4095');
119 is (sprintf("%vd", v801.4095 ^ v4095.801.4095), '3294.3294.4095');
121 is (sprintf("%vd", v120.300 & v200.400), '72.256');
122 is (sprintf("%vd", v120.300 | v200.400), '248.444');
123 is (sprintf("%vd", v120.300 ^ v200.400), '176.188');
129 is (sprintf("%vd", $a), '176.188');
135 is (sprintf("%vd", $a), '248.444');
145 for (0x100...0xFFF) {
147 push @not36, sprintf("%#03X", $_)
148 if $a ne chr(~$_) or length($a) != 1 or ~$a ne chr($_);
150 is (join (', ', @not36), '');
154 for my $i (0xEEE...0xF00) {
155 for my $j (0x0..0x120) {
156 $a = ~(chr ($i) . chr $j);
157 push @not37, sprintf("%#03X %#03X", $i, $j)
158 if $a ne chr(~$i).chr(~$j) or
160 ~$a ne chr($i).chr($j);
163 is (join (', ', @not37), '');
170 for my $i (0x100..0x120) {
171 for my $j (0x100...0x120) {
172 push @not39, sprintf("%#03X %#03X", $i, $j)
173 if ~(chr($i)|chr($j)) ne (~chr($i)&~chr($j));
176 is (join (', ', @not39), '');
180 for my $i (0x100..0x120) {
181 for my $j (0x100...0x120) {
182 push @not40, sprintf("%#03X %#03X", $i, $j)
183 if ~(chr($i)&chr($j)) ne (~chr($i)|~chr($j));
186 is (join (', ', @not40), '');
190 # More variations on 19 and 22.
191 is ("ok \xFF\x{FF}\n" & "ok 41\n", "ok 41\n");
192 is ("ok \x{FF}\xFF\n" & "ok 42\n", "ok 42\n");
194 # Tests to see if you really can do casts negative floats to unsigned properly
203 sub TIESCALAR { bless { value => $_[1], orig => $_[1] } }
204 sub STORE { $_[0]{store}++; $_[0]{value} = $_[1] }
205 sub FETCH { $_[0]{fetch}++; $_[0]{value} }
206 sub stores { tied($_[0])->{value} = tied($_[0])->{orig};
207 delete(tied($_[0])->{store}) || 0 }
208 sub fetches { delete(tied($_[0])->{fetch}) || 0 }
210 # numeric double magic tests
297 } # end of use integer;
299 # stringwise double magic tests
304 is(($x | $y), ("a" | "c"));
310 is(($x & $y), ("a" & "c"));
316 is(($x ^ $y), ("a" ^ "c"));
322 is(($x |= $y), ("a" | "c"));
328 is(($x &= $y), ("a" & "c"));
334 is(($x ^= $y), ("a" ^ "c"));
344 $a = "\0\x{100}"; chop($a);
345 ok(utf8::is_utf8($a)); # make sure UTF8 flag is still there
347 is($a, "\xFF", "~ works with utf-8");
349 # [rt.perl.org 33003]
350 # This would cause a segfault without malloc wrap
352 skip "No malloc wrap checks" unless $Config::Config{usemallocwrap};
353 like( runperl(prog => 'eval q($#a>>=1); print 1'), qr/^1\n?/ );
356 # [perl #37616] Bug in &= (string) and/or m//
360 ok($a =~ /a+$/, 'ASCII "a" is NUL-terminated');
364 ok($b =~ /b+$/, 'Unicode "b" is NUL-terminated');
368 $a = chr(0x101) x 0x101;
369 $b = chr(0x0FF) x 0x0FF;
372 is($c, chr(0x1FF) x 0xFF . chr(0x101) x 2);
375 is($c, chr(0x1FF) x 0xFF . chr(0x101) x 2);
378 is($c, chr(0x001) x 0x0FF);
381 is($c, chr(0x001) x 0x0FF);
384 is($c, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
387 is($c, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
391 $a = chr(0x101) x 0x101;
392 $b = chr(0x0FF) x 0x0FF;
395 is($a, chr(0x1FF) x 0xFF . chr(0x101) x 2);
399 $a = chr(0x101) x 0x101;
400 $b = chr(0x0FF) x 0x0FF;
403 is($b, chr(0x1FF) x 0xFF . chr(0x101) x 2);
407 $a = chr(0x101) x 0x101;
408 $b = chr(0x0FF) x 0x0FF;
411 is($a, chr(0x001) x 0x0FF);
415 $a = chr(0x101) x 0x101;
416 $b = chr(0x0FF) x 0x0FF;
419 is($b, chr(0x001) x 0x0FF);
423 $a = chr(0x101) x 0x101;
424 $b = chr(0x0FF) x 0x0FF;
427 is($a, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
431 $a = chr(0x101) x 0x101;
432 $b = chr(0x0FF) x 0x0FF;
435 is($b, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
439 # New string- and number-specific bitwise ops
441 use feature "bitwise";
442 no warnings "experimental::bitwise";
443 is "22" & "66", 2, 'numeric & with strings';
444 is "22" | "66", 86, 'numeric | with strings';
445 is "22" ^ "66", 84, 'numeric ^ with strings';
446 is ~"22" & 0xff, 233, 'numeric ~ with string';
447 is 22 &. 66, 22, '&. with numbers';
448 is 22 |. 66, 66, '|. with numbers';
449 is 22 ^. 66, "\4\4", '^. with numbers';
451 # ord('2') is 0xF2 on EBCDIC
452 is ~.22, "\x0d\x0d", '~. with number';
455 # ord('2') is 0x32 on ASCII
456 is ~.22, "\xcd\xcd", '~. with number';
459 is $_ &= "66", 2, 'numeric &= with strings';
461 is $_ |= "66", 86, 'numeric |= with strings';
463 is $_ ^= "66", 84, 'numeric ^= with strings';
465 is $_ &.= 66, 22, '&.= with numbers';
467 is $_ |.= 66, 66, '|.= with numbers';
469 is $_ ^.= 66, "\4\4", '^.= with numbers';
471 # signed vs. unsigned
472 ok ((~0 > 0 && do { use integer; ~0 } == -1));
475 for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
476 my $cusp = 1 << ($bits - 1);
478 ok (($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0);
479 ok (($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0);
480 ok (($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0);
481 ok ((1 << ($bits - 1)) == $cusp &&
482 do { use integer; 1 << ($bits - 1) } == -$cusp);
483 ok (($cusp >> 1) == ($cusp / 2) &&
484 do { use integer; abs($cusp >> 1) } == ($cusp / 2));
491 for my $str ("x", "\x{100}") {
492 for my $chr (qw/S A H G X ( * F/) {
493 for my $op (qw/| & ^/) {
496 $res{"$chr$op$str"} = eval qq/chr($co $op $so)/;
499 $res{"undef|$str"} = $str;
500 $res{"undef&$str"} = "";
501 $res{"undef^$str"} = $str;
505 1 if index "foo", PVBM;
509 local $SIG{__WARN__} = sub { $warn++ };
512 my ($got, $orig, $op, $str, $name) = @_;
513 is(substr($got, 0, 1), $res{"$orig$op$str"}, $name);
517 # [object to test, first char of stringification, name]
518 [undef, "undef", "undef" ],
519 [\1, "S", "scalar ref" ],
520 [[], "A", "array ref" ],
521 [{}, "H", "hash ref" ],
522 [qr/x/, "(", "qr//" ],
523 [*foo, "*", "glob" ],
524 [\*foo, "G", "glob ref" ],
525 [PVBM, "X", "PVBM" ],
526 [\PVBM, "S", "PVBM ref" ],
527 [bless([], "Foo"), "F", "object" ],
529 my ($val, $orig, $type) = @$_;
531 for (["x", "string"], ["\x{100}", "utf8"]) {
532 my ($str, $desc) = @$_;
536 is_first($val | $str, $orig, "|", $str, "$type | $desc");
537 is_first($val & $str, $orig, "&", $str, "$type & $desc");
538 is_first($val ^ $str, $orig, "^", $str, "$type ^ $desc");
540 is_first($str | $val, $orig, "|", $str, "$desc | $type");
541 is_first($str & $val, $orig, "&", $str, "$desc & $type");
542 is_first($str ^ $val, $orig, "^", $str, "$desc ^ $type");
545 ($new = $val) |= $str;
546 is_first($new, $orig, "|", $str, "$type |= $desc");
547 ($new = $val) &= $str;
548 is_first($new, $orig, "&", $str, "$type &= $desc");
549 ($new = $val) ^= $str;
550 is_first($new, $orig, "^", $str, "$type ^= $desc");
552 ($new = $str) |= $val;
553 is_first($new, $orig, "|", $str, "$desc |= $type");
554 ($new = $str) &= $val;
555 is_first($new, $orig, "&", $str, "$desc &= $type");
556 ($new = $str) ^= $val;
557 is_first($new, $orig, "^", $str, "$desc ^= $type");
559 if ($orig eq "undef") {
560 # undef |= and undef ^= don't warn
561 is($warn, 10, "no duplicate warnings");
564 is($warn, 0, "no warnings");
573 use overload q/""/ => sub { $strval };
576 use overload q/|/ => sub { "y" };
579 ok(!eval { 1 if bless([], "Bar") | "x"; 1 },"string overload can't use |");
580 like($@, qr/no method found/, "correct error");
581 is(eval { bless([], "Baz") | "x" }, "y", "| overload works");
583 my $obj = bless [], "Bar";
585 eval { $obj |= "Q" };
587 is("$obj", "z", "|= doesn't break string overload");
590 $^A .= new version ~$_ for eval sprintf('"\\x%02x"', 0xff - ord("1")),
591 $::IS_EBCDIC ? v13 : v205, # 255 - ord('2')
592 eval sprintf('"\\x%02x"', 0xff - ord("3"));
593 is $^A, "123", '~v0 clears vstring magic on retval';
596 my $w = $Config::Config{ivsize} * 8;
598 fail("unexpected w $w") unless $w == 32 || $w == 64;
600 is(1 << 1, 2, "UV 1 left shift 1");
601 is(1 >> 1, 0, "UV 1 right shift 1");
603 is(0x7b << -4, 0x007, "UV left negative shift == right shift");
604 is(0x7b >> -4, 0x7b0, "UV right negative shift == left shift");
606 is(0x7b << 0, 0x07b, "UV left zero shift == identity");
607 is(0x7b >> 0, 0x07b, "UV right zero shift == identity");
609 is(0x0 << -1, 0x0, "zero left negative shift == zero");
610 is(0x0 >> -1, 0x0, "zero right negative shift == zero");
612 cmp_ok(1 << $w - 1, '==', 2 ** ($w - 1), # not is() because NV stringify.
613 "UV left $w - 1 shift == 2 ** ($w - 1)");
614 is(1 << $w, 0, "UV left shift $w == zero");
615 is(1 << $w + 1, 0, "UV left shift $w + 1 == zero");
617 is(1 >> $w - 1, 0, "UV right shift $w - 1 == zero");
618 is(1 >> $w, 0, "UV right shift $w == zero");
619 is(1 >> $w + 1, 0, "UV right shift $w + 1 == zero");
621 # Negative shiftees get promoted to UVs before shifting. This is
622 # not necessarily the ideal behavior, but that is what is happening.
624 no warnings "portable";
625 no warnings "overflow"; # prevent compile-time warning for ivsize=4
626 is(-1 << 1, 0xFFFF_FFFF_FFFF_FFFE,
627 "neg UV (sic) left shift = 0xFF..E");
628 is(-1 >> 1, 0x7FFF_FFFF_FFFF_FFFF,
629 "neg UV (sic) right right = 0x7F..F");
631 no warnings "portable";
632 is(-1 << 1, 0xFFFF_FFFE, "neg left shift == 0xFF..E");
633 is(-1 >> 1, 0x7FFF_FFFF, "neg right right == 0x7F..F");
637 # 'use integer' means use IVs instead of UVs.
641 is(1 << 1, 2, "IV 1 left shift 1 == 2");
642 is(1 >> 1, 0, "IV 1 right shift 1 == 0");
644 # The left overshift should behave like without 'use integer',
645 # that is, return zero.
646 is(1 << $w, 0, "IV 1 left shift $w == 0");
647 is(1 << $w + 1, 0, "IV 1 left shift $w + 1 == 0");
648 is(-1 << $w, 0, "IV -1 left shift $w == 0");
649 is(-1 << $w + 1, 0, "IV -1 left shift $w + 1 == 0");
651 # Even for negative IVs, left shift is multiplication.
652 # But right shift should display the stuckiness to -1.
653 is(-1 << 1, -2, "IV -1 left shift 1 == -2");
654 is(-1 >> 1, -1, "IV -1 right shift 1 == -1");
656 # As for UVs, negative shifting means the reverse shift.
657 is(-1 << -1, -1, "IV -1 left shift -1 == -1");
658 is(-1 >> -1, -2, "IV -1 right shift -1 == -2");
660 # Test also at and around wordsize, expect stuckiness to -1.
661 is(-1 >> $w - 1, -1, "IV -1 right shift $w - 1 == -1");
662 is(-1 >> $w, -1, "IV -1 right shift $w == -1");
663 is(-1 >> $w + 1, -1, "IV -1 right shift $w + 1 == -1");