4 # test the bit operators '&', '|', '^', '~', '<<', and '>>'
11 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)
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 my $iv_min = -(~0 >> 1) - 1;
39 { use integer; $shifted = $iv_min << 0 };
40 is($shifted, $iv_min, "IV_MIN << 0 yields IV_MIN under 'use integer'");
44 # Exercise some corner cases on shifting more bits than the size of IV/UV.
45 # All these should work even if the shift amount doesn't fit in IV or UV.
46 is(4 << 2147483648, 0, "4 << 2147483648 yields 0");
47 is(16 << 4294967295, 0, "16 << 4294967295 yields 0");
48 is(8 >> 4294967296, 0, "8 >> 4294967296 yields 0");
49 is(11 << 18446744073709551615, 0, "11 << 18446744073709551615 yields 0");
50 is(do { use integer; -9 >> 18446744073709551616 }, -1,
51 "-9 >> 18446744073709551616 under 'use integer' yields -1");
52 is(do { use integer; -4 << -2147483648 }, -1,
53 "-4 << -2147483648 under 'use integer' yields -1");
54 # Quotes around -9223372036854775808 below are to make it a single term.
55 # Without quotes, it will be parsed as an expression with an unary minus
56 # operator which will clip the result to IV range under "use integer".
57 is(do { use integer; -5 >> '-9223372036854775808' }, 0,
58 "-5 >> -9223372036854775808 under 'use integer' yields 0");
61 for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
62 my $cusp = 1 << ($bits - 1);
65 ok (($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0);
66 ok (($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0);
67 ok (($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0);
68 ok ((1 << ($bits - 1)) == $cusp &&
69 do { use integer; 1 << ($bits - 1) } == -$cusp);
70 ok (($cusp >> 1) == ($cusp / 2) &&
71 do { use integer; abs($cusp >> 1) } == ($cusp / 2));
73 $Aaz = chr(ord("A") & ord("z"));
74 $Aoz = chr(ord("A") | ord("z"));
75 $Axz = chr(ord("A") ^ ord("z"));
78 is (("AAAAA" & "zzzzz"), ($Aaz x 5));
79 is (("AAAAA" | "zzzzz"), ($Aoz x 5));
80 is (("AAAAA" ^ "zzzzz"), ($Axz x 5));
87 is (($foo & $bar), ($Aaz x 75 ));
89 is (($foo | $bar), ($Aoz x 75 . $zap));
91 is (($foo ^ $bar), ($Axz x 75 . $zap));
93 # string constants. These tests expect the bit patterns of these strings in
94 # ASCII, so convert to that.
95 sub _and($) { $_[0] & native_to_uni("+0") }
96 sub _oar($) { $_[0] | native_to_uni("+0") }
97 sub _xor($) { $_[0] ^ native_to_uni("+0") }
98 is _and native_to_uni("waf"), native_to_uni('# '), 'str var & const str'; # [perl #20661]
99 is _and native_to_uni("waf"), native_to_uni('# '), 'str var & const str again'; # [perl #20661]
100 is _oar native_to_uni("yit"), native_to_uni('{yt'), 'str var | const str';
101 is _oar native_to_uni("yit"), native_to_uni('{yt'), 'str var | const str again';
102 is _xor native_to_uni("yit"), native_to_uni('RYt'), 'str var ^ const str';
103 is _xor native_to_uni("yit"), native_to_uni('RYt'), 'str var ^ const str again';
106 skip "Converting a numeric doesn't work with EBCDIC unlike the above tests",
108 is _and 0, '0', 'num var & const str'; # [perl #20661]
109 is _oar 0, '0', 'num var | const str';
110 is _xor 0, '0', 'num var ^ const str';
113 # But don’t mistake a COW for a constant when assigning to it
117 is $i, 255, '[perl #108480] $cow |= number';
120 is $i, 0, '[perl #108480] $cow &= number';
123 is $i, 255, '[perl #108480] $cow ^= number';
126 is ("ok \xFF\xFF\n" & "ok 19\n", "ok 19\n");
127 is ("ok 20\n" | "ok \0\0\n", "ok 20\n");
128 is ("o\000 \0001\000" ^ "\000k\0002\000\n", "ok 21\n");
131 is ("ok \x{FF}\x{FF}\n" & "ok 22\n", "ok 22\n");
132 is ("ok 23\n" | "ok \x{0}\x{0}\n", "ok 23\n");
133 is ("o\x{0} \x{0}4\x{0}" ^ "\x{0}k\x{0}2\x{0}\n", "ok 24\n");
135 # More variations on 19 and 22.
136 is ("ok \xFF\x{FF}\n" & "ok 41\n", "ok 41\n");
137 is ("ok \x{FF}\xFF\n" & "ok 42\n", "ok 42\n");
139 # Tests to see if you really can do casts negative floats to unsigned properly
148 sub TIESCALAR { bless { value => $_[1], orig => $_[1] } }
149 sub STORE { $_[0]{store}++; $_[0]{value} = $_[1] }
150 sub FETCH { $_[0]{fetch}++; $_[0]{value} }
151 sub stores { tied($_[0])->{value} = tied($_[0])->{orig};
152 delete(tied($_[0])->{store}) || 0 }
153 sub fetches { delete(tied($_[0])->{fetch}) || 0 }
155 # numeric double magic tests
242 } # end of use integer;
244 # stringwise double magic tests
249 is(($x | $y), ("a" | "c"));
255 is(($x & $y), ("a" & "c"));
261 is(($x ^ $y), ("a" ^ "c"));
267 is(($x |= $y), ("a" | "c"));
273 is(($x &= $y), ("a" & "c"));
279 is(($x ^= $y), ("a" ^ "c"));
290 # Note: if the vec() reads are part of the is() calls it's treated as
291 # in lvalue context, so we save it separately
293 is($g, (ord("a") & 0x01), "check vec value");
294 is(fetches($x), 1, "fetches for vec read");
295 is(stores($x), 0, "stores for vec read");
296 # similarly here, and code like:
297 # $g = (vec($x, 0, 1) = 0)
298 # results in an extra fetch, since the inner assignment returns the LV
300 # one fetch in vec() another when the LV is assigned to
301 is(fetches($x), 2, "fetches for vec write");
302 is(stores($x), 1, "stores for vec write");
309 is($g, (ord("a") & 0x01), "check vec value (utf8)");
310 is(fetches($x), 1, "fetches for vec read (utf8)");
311 is(stores($x), 0, "stores for vec read (utf8)");
313 # one fetch in vec() another when the LV is assigned to
314 is(fetches($x), 2, "fetches for vec write (utf8)");
315 is(stores($x), 1, "stores for vec write (utf8)");
318 $a = "\0\x{100}"; chop($a);
319 ok(utf8::is_utf8($a)); # make sure UTF8 flag is still there
321 is($a, "\xFF", "~ works with utf-8");
322 ok(! utf8::is_utf8($a), " and turns off the UTF-8 flag");
324 $a = "\0\x{100}"; chop($a);
327 ok(utf8::is_utf8($b), "Verify UTF-8 | non-UTF-8 retains UTF-8 flag");
330 ok(utf8::is_utf8($b), "Verify non-UTF-8 | UTF-8 retains UTF-8 flag");
333 ok(utf8::is_utf8($b), "Verify UTF-8 & non-UTF-8 retains UTF-8 flag");
336 ok(utf8::is_utf8($b), "Verify non-UTF-8 & UTF-8 retains UTF-8 flag");
339 ok(utf8::is_utf8($b), "Verify UTF-8 ^ non-UTF-8 retains UTF-8 flag");
342 ok(utf8::is_utf8($b), "Verify non-UTF-8 ^ UTF-8 retains UTF-8 flag");
345 # [rt.perl.org 33003]
346 # This would cause a segfault without malloc wrap
348 skip "No malloc wrap checks" unless $Config::Config{usemallocwrap};
349 like( runperl(prog => 'eval q($#a>>=1); print 1'), qr/^1\n?/ );
352 # [perl #37616] Bug in &= (string) and/or m//
356 ok($a =~ /a+$/, 'ASCII "a" is NUL-terminated');
361 ok($b =~ /b+$/, 'Unicode "b" is NUL-terminated');
364 # New string- and number-specific bitwise ops
366 use feature "bitwise";
367 no warnings "experimental::bitwise";
368 is "22" & "66", 2, 'numeric & with strings';
369 is "22" | "66", 86, 'numeric | with strings';
370 is "22" ^ "66", 84, 'numeric ^ with strings';
371 is ~"22" & 0xff, 233, 'numeric ~ with string';
372 is 22 &. 66, 22, '&. with numbers';
373 is 22 |. 66, 66, '|. with numbers';
374 is 22 ^. 66, "\4\4", '^. with numbers';
376 # ord('2') is 0xF2 on EBCDIC
377 is ~.22, "\x0d\x0d", '~. with number';
380 # ord('2') is 0x32 on ASCII
381 is ~.22, "\xcd\xcd", '~. with number';
384 is $_ &= "66", 2, 'numeric &= with strings';
386 is $_ |= "66", 86, 'numeric |= with strings';
388 is $_ ^= "66", 84, 'numeric ^= with strings';
390 is $_ &.= 66, 22, '&.= with numbers';
392 is $_ |.= 66, 66, '|.= with numbers';
394 is $_ ^.= 66, "\4\4", '^.= with numbers';
396 # signed vs. unsigned
397 ok ((~0 > 0 && do { use integer; ~0 } == -1));
400 for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
401 my $cusp = 1 << ($bits - 1);
403 ok (($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0);
404 ok (($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0);
405 ok (($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0);
406 ok ((1 << ($bits - 1)) == $cusp &&
407 do { use integer; 1 << ($bits - 1) } == -$cusp);
408 ok (($cusp >> 1) == ($cusp / 2) &&
409 do { use integer; abs($cusp >> 1) } == ($cusp / 2));
411 # Repeat some of those, with 'use v5.27'
415 is "22" & "66", 2, 'numeric & with strings';
416 is "22" | "66", 86, 'numeric | with strings';
417 is "22" ^ "66", 84, 'numeric ^ with strings';
418 is ~"22" & 0xff, 233, 'numeric ~ with string';
419 is 22 &. 66, 22, '&. with numbers';
420 is 22 |. 66, 66, '|. with numbers';
421 is 22 ^. 66, "\4\4", '^. with numbers';
423 # ord('2') is 0xF2 on EBCDIC
424 is ~.22, "\x0d\x0d", '~. with number';
427 # ord('2') is 0x32 on ASCII
428 is ~.22, "\xcd\xcd", '~. with number';
431 is $_ &= "66", 2, 'numeric &= with strings';
433 is $_ |= "66", 86, 'numeric |= with strings';
435 is $_ ^= "66", 84, 'numeric ^= with strings';
437 is $_ &.= 66, 22, '&.= with numbers';
439 is $_ |.= 66, 66, '|.= with numbers';
441 is $_ ^.= 66, "\4\4", '^.= with numbers';
448 for my $str ("x", "\x{B6}") {
449 utf8::upgrade($str) if $str !~ /x/;
450 for my $chr (qw/S A H G X ( * F/) {
451 for my $op (qw/| & ^/) {
454 $res{"$chr$op$str"} = eval qq/chr($co $op $so)/;
457 $res{"undef|$str"} = $str;
458 $res{"undef&$str"} = "";
459 $res{"undef^$str"} = $str;
463 1 if index "foo", PVBM;
467 local $SIG{__WARN__} = sub { $warn++ };
470 my ($got, $orig, $op, $str, $name) = @_;
471 is(substr($got, 0, 1), $res{"$orig$op$str"}, $name);
475 # [object to test, first char of stringification, name]
476 [undef, "undef", "undef" ],
477 [\1, "S", "scalar ref" ],
478 [[], "A", "array ref" ],
479 [{}, "H", "hash ref" ],
480 [qr/x/, "(", "qr//" ],
481 [*foo, "*", "glob" ],
482 [\*foo, "G", "glob ref" ],
483 [PVBM, "X", "PVBM" ],
484 [\PVBM, "S", "PVBM ref" ],
485 [bless([], "Foo"), "F", "object" ],
487 my ($val, $orig, $type) = @$_;
489 for (["x", "string"], ["\x{B6}", "utf8"]) {
490 my ($str, $desc) = @$_;
491 utf8::upgrade($str) if $desc =~ /utf8/;
495 is_first($val | $str, $orig, "|", $str, "$type | $desc");
496 is_first($val & $str, $orig, "&", $str, "$type & $desc");
497 is_first($val ^ $str, $orig, "^", $str, "$type ^ $desc");
499 is_first($str | $val, $orig, "|", $str, "$desc | $type");
500 is_first($str & $val, $orig, "&", $str, "$desc & $type");
501 is_first($str ^ $val, $orig, "^", $str, "$desc ^ $type");
504 ($new = $val) |= $str;
505 is_first($new, $orig, "|", $str, "$type |= $desc");
506 ($new = $val) &= $str;
507 is_first($new, $orig, "&", $str, "$type &= $desc");
508 ($new = $val) ^= $str;
509 is_first($new, $orig, "^", $str, "$type ^= $desc");
511 ($new = $str) |= $val;
512 is_first($new, $orig, "|", $str, "$desc |= $type");
513 ($new = $str) &= $val;
514 is_first($new, $orig, "&", $str, "$desc &= $type");
515 ($new = $str) ^= $val;
516 is_first($new, $orig, "^", $str, "$desc ^= $type");
518 if ($orig eq "undef") {
519 # undef |= and undef ^= don't warn
520 is($warn, 10, "no duplicate warnings");
523 is($warn, 0, "no warnings");
528 delete $SIG{__WARN__};
534 use overload q/""/ => sub { $strval };
537 use overload q/|/ => sub { "y" };
540 ok(!eval { 1 if bless([], "Bar") | "x"; 1 },"string overload can't use |");
541 like($@, qr/no method found/, "correct error");
542 is(eval { bless([], "Baz") | "x" }, "y", "| overload works");
544 my $obj = bless [], "Bar";
546 eval { $obj |= "Q" };
548 is("$obj", "z", "|= doesn't break string overload");
551 $^A .= new version ~$_ for eval sprintf('"\\x%02x"', 0xff - ord("1")),
552 $::IS_EBCDIC ? v13 : v205, # 255 - ord('2')
553 eval sprintf('"\\x%02x"', 0xff - ord("3"));
554 is $^A, "123", '~v0 clears vstring magic on retval';
557 my $w = $Config::Config{ivsize} * 8;
559 fail("unexpected w $w") unless $w == 32 || $w == 64;
561 is(1 << 1, 2, "UV 1 left shift 1");
562 is(1 >> 1, 0, "UV 1 right shift 1");
564 is(0x7b << -4, 0x007, "UV left negative shift == right shift");
565 is(0x7b >> -4, 0x7b0, "UV right negative shift == left shift");
567 is(0x7b << 0, 0x07b, "UV left zero shift == identity");
568 is(0x7b >> 0, 0x07b, "UV right zero shift == identity");
570 is(0x0 << -1, 0x0, "zero left negative shift == zero");
571 is(0x0 >> -1, 0x0, "zero right negative shift == zero");
573 cmp_ok(1 << $w - 1, '==', 2 ** ($w - 1), # not is() because NV stringify.
574 "UV left $w - 1 shift == 2 ** ($w - 1)");
575 is(1 << $w, 0, "UV left shift $w == zero");
576 is(1 << $w + 1, 0, "UV left shift $w + 1 == zero");
578 is(1 >> $w - 1, 0, "UV right shift $w - 1 == zero");
579 is(1 >> $w, 0, "UV right shift $w == zero");
580 is(1 >> $w + 1, 0, "UV right shift $w + 1 == zero");
582 # Negative shiftees get promoted to UVs before shifting. This is
583 # not necessarily the ideal behavior, but that is what is happening.
585 no warnings "portable";
586 no warnings "overflow"; # prevent compile-time warning for ivsize=4
587 is(-1 << 1, 0xFFFF_FFFF_FFFF_FFFE,
588 "neg UV (sic) left shift = 0xFF..E");
589 is(-1 >> 1, 0x7FFF_FFFF_FFFF_FFFF,
590 "neg UV (sic) right shift = 0x7F..F");
592 no warnings "portable";
593 is(-1 << 1, 0xFFFF_FFFE, "neg left shift == 0xFF..E");
594 is(-1 >> 1, 0x7FFF_FFFF, "neg right shift == 0x7F..F");
598 # 'use integer' means use IVs instead of UVs.
602 is(1 << 1, 2, "IV 1 left shift 1 == 2");
603 is(1 >> 1, 0, "IV 1 right shift 1 == 0");
605 # The left overshift should behave like without 'use integer',
606 # that is, return zero.
607 is(1 << $w, 0, "IV 1 left shift $w == 0");
608 is(1 << $w + 1, 0, "IV 1 left shift $w + 1 == 0");
609 is(-1 << $w, 0, "IV -1 left shift $w == 0");
610 is(-1 << $w + 1, 0, "IV -1 left shift $w + 1 == 0");
612 # Even for negative IVs, left shift is multiplication.
613 # But right shift should display the stuckiness to -1.
614 is(-1 << 1, -2, "IV -1 left shift 1 == -2");
615 is(-1 >> 1, -1, "IV -1 right shift 1 == -1");
617 # As for UVs, negative shifting means the reverse shift.
618 is(-1 << -1, -1, "IV -1 left shift -1 == -1");
619 is(-1 >> -1, -2, "IV -1 right shift -1 == -2");
621 # Test also at and around wordsize, expect stuckiness to -1.
622 is(-1 >> $w - 1, -1, "IV -1 right shift $w - 1 == -1");
623 is(-1 >> $w, -1, "IV -1 right shift $w == -1");
624 is(-1 >> $w + 1, -1, "IV -1 right shift $w + 1 == -1");
628 # [perl #129287] UTF8 & was not providing a trailing null byte.
629 # This test is a bit convoluted, as we want to make sure that the string
630 # allocated for &’s target contains memory initialised to something other
631 # than a null byte. Uninitialised memory does not make for a reliable
632 # test. So we do &. on a longer non-utf8 string first.
633 for (["aaa","aaa"],[substr ("a\x{100}",0,1), "a"]) {
634 use feature "bitwise";
635 no warnings "experimental::bitwise", "pack";
636 $byte = substr unpack("P2", pack "P", $$_[0] &. $$_[1]), -1;
638 is $byte, "\0", "utf8 &. appends null byte";
640 # only visible under sanitize
641 fresh_perl_is('$x = "UUUUUUUV"; $y = "xxxxxxx"; $x |= $y; print $x',
642 ( $::IS_EBCDIC) ? 'XXXXXXXV' : '}}}}}}}V',
643 {}, "[perl #129995] access to freed memory");
647 # Using code points above 0xFF is fatal
649 foreach my $op_info ([and => "&"], [or => "|"], [xor => "^"]) {
650 my ($op_name, $op) = @$op_info;
652 eval '$_ = "\xFF" ' . $op . ' "\x{100}";';
653 like $@, qr /^Use of strings with code points over 0xFF as arguments (?#
654 )to bitwise $op_name \Q($op)\E operator is not allowed/,
655 "Use of code points above 0xFF as arguments to bitwise " .
656 "$op_name ($op) is not allowed";
661 eval '$_ = ~ "\x{100}";';
662 like $@, qr /^Use of strings with code points over 0xFF as arguments (?#
663 )to 1's complement \(~\) operator is not allowed/,
664 "Use of code points above 0xFF as argument to 1's complement " .
665 "(~) is not allowed";
669 # RT 134140 fatalizations
671 and => { low => 'and', high => '&', regex => qr/&/ },
672 or => { low => 'or', high => '|', regex => qr/\|/ },
673 xor => { low => 'xor', high => '^', regex => qr/\^/ },
676 { string => '"abc" & "abc\x{100}"', op_pair => $op_pairs{and} },
677 { string => '"abc" | "abc\x{100}"', op_pair => $op_pairs{or} },
678 { string => '"abc" ^ "abc\x{100}"', op_pair => $op_pairs{xor} },
679 { string => '"abc\x{100}" & "abc"', op_pair => $op_pairs{and} },
680 { string => '"abc\x{100}" | "abc"', op_pair => $op_pairs{or} },
681 { string => '"abc\x{100}" ^ "abc"', op_pair => $op_pairs{xor} },
685 # Use of strings with code points over 0xFF as arguments to %s operator is not allowed
686 for my $h (@combos) {
687 my $s1 = "Use of strings with code points over 0xFF as arguments to bitwise";
688 my $s2 = "operator is not allowed";
689 my $expected = qr/$s1 $h->{op_pair}->{low} \($h->{op_pair}->{regex}\) $s2/;
690 my $description = "$s1 $h->{op_pair}->{low} ($h->{op_pair}->{high}) operator is not allowed";
693 like $@, $expected, $description;
698 # perl #17844 - only visible with valgrind/ASAN
699 fresh_perl_is(<<'EOS',
700 formline X000n^\\0,\\0^\\0for\0,0..10
703 {}, "[perl #17844] access beyond end of block");