4 # test the bit operators '&', '|', '^', '~', '<<', and '>>'
13 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 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");
111 # More variations on 19 and 22.
112 is ("ok \xFF\x{FF}\n" & "ok 41\n", "ok 41\n");
113 is ("ok \x{FF}\xFF\n" & "ok 42\n", "ok 42\n");
115 # Tests to see if you really can do casts negative floats to unsigned properly
124 sub TIESCALAR { bless { value => $_[1], orig => $_[1] } }
125 sub STORE { $_[0]{store}++; $_[0]{value} = $_[1] }
126 sub FETCH { $_[0]{fetch}++; $_[0]{value} }
127 sub stores { tied($_[0])->{value} = tied($_[0])->{orig};
128 delete(tied($_[0])->{store}) || 0 }
129 sub fetches { delete(tied($_[0])->{fetch}) || 0 }
131 # numeric double magic tests
218 } # end of use integer;
220 # stringwise double magic tests
225 is(($x | $y), ("a" | "c"));
231 is(($x & $y), ("a" & "c"));
237 is(($x ^ $y), ("a" ^ "c"));
243 is(($x |= $y), ("a" | "c"));
249 is(($x &= $y), ("a" & "c"));
255 is(($x ^= $y), ("a" ^ "c"));
265 $a = "\0\x{100}"; chop($a);
266 ok(utf8::is_utf8($a)); # make sure UTF8 flag is still there
268 is($a, "\xFF", "~ works with utf-8");
269 ok(! utf8::is_utf8($a), " and turns off the UTF-8 flag");
271 $a = "\0\x{100}"; chop($a);
274 ok(utf8::is_utf8($b), "Verify UTF-8 | non-UTF-8 retains UTF-8 flag");
277 ok(utf8::is_utf8($b), "Verify non-UTF-8 | UTF-8 retains UTF-8 flag");
280 ok(utf8::is_utf8($b), "Verify UTF-8 & non-UTF-8 retains UTF-8 flag");
283 ok(utf8::is_utf8($b), "Verify non-UTF-8 & UTF-8 retains UTF-8 flag");
286 ok(utf8::is_utf8($b), "Verify UTF-8 ^ non-UTF-8 retains UTF-8 flag");
289 ok(utf8::is_utf8($b), "Verify non-UTF-8 ^ UTF-8 retains UTF-8 flag");
292 # [rt.perl.org 33003]
293 # This would cause a segfault without malloc wrap
295 skip "No malloc wrap checks" unless $Config::Config{usemallocwrap};
296 like( runperl(prog => 'eval q($#a>>=1); print 1'), qr/^1\n?/ );
299 # [perl #37616] Bug in &= (string) and/or m//
303 ok($a =~ /a+$/, 'ASCII "a" is NUL-terminated');
308 ok($b =~ /b+$/, 'Unicode "b" is NUL-terminated');
311 # New string- and number-specific bitwise ops
313 use feature "bitwise";
314 no warnings "experimental::bitwise";
315 is "22" & "66", 2, 'numeric & with strings';
316 is "22" | "66", 86, 'numeric | with strings';
317 is "22" ^ "66", 84, 'numeric ^ with strings';
318 is ~"22" & 0xff, 233, 'numeric ~ with string';
319 is 22 &. 66, 22, '&. with numbers';
320 is 22 |. 66, 66, '|. with numbers';
321 is 22 ^. 66, "\4\4", '^. with numbers';
323 # ord('2') is 0xF2 on EBCDIC
324 is ~.22, "\x0d\x0d", '~. with number';
327 # ord('2') is 0x32 on ASCII
328 is ~.22, "\xcd\xcd", '~. with number';
331 is $_ &= "66", 2, 'numeric &= with strings';
333 is $_ |= "66", 86, 'numeric |= with strings';
335 is $_ ^= "66", 84, 'numeric ^= with strings';
337 is $_ &.= 66, 22, '&.= with numbers';
339 is $_ |.= 66, 66, '|.= with numbers';
341 is $_ ^.= 66, "\4\4", '^.= with numbers';
343 # signed vs. unsigned
344 ok ((~0 > 0 && do { use integer; ~0 } == -1));
347 for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
348 my $cusp = 1 << ($bits - 1);
350 ok (($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0);
351 ok (($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0);
352 ok (($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0);
353 ok ((1 << ($bits - 1)) == $cusp &&
354 do { use integer; 1 << ($bits - 1) } == -$cusp);
355 ok (($cusp >> 1) == ($cusp / 2) &&
356 do { use integer; abs($cusp >> 1) } == ($cusp / 2));
363 for my $str ("x", "\x{B6}") {
364 utf8::upgrade($str) if $str !~ /x/;
365 for my $chr (qw/S A H G X ( * F/) {
366 for my $op (qw/| & ^/) {
369 $res{"$chr$op$str"} = eval qq/chr($co $op $so)/;
372 $res{"undef|$str"} = $str;
373 $res{"undef&$str"} = "";
374 $res{"undef^$str"} = $str;
378 1 if index "foo", PVBM;
382 local $SIG{__WARN__} = sub { $warn++ };
385 my ($got, $orig, $op, $str, $name) = @_;
386 is(substr($got, 0, 1), $res{"$orig$op$str"}, $name);
390 # [object to test, first char of stringification, name]
391 [undef, "undef", "undef" ],
392 [\1, "S", "scalar ref" ],
393 [[], "A", "array ref" ],
394 [{}, "H", "hash ref" ],
395 [qr/x/, "(", "qr//" ],
396 [*foo, "*", "glob" ],
397 [\*foo, "G", "glob ref" ],
398 [PVBM, "X", "PVBM" ],
399 [\PVBM, "S", "PVBM ref" ],
400 [bless([], "Foo"), "F", "object" ],
402 my ($val, $orig, $type) = @$_;
404 for (["x", "string"], ["\x{B6}", "utf8"]) {
405 my ($str, $desc) = @$_;
406 utf8::upgrade($str) if $desc =~ /utf8/;
410 is_first($val | $str, $orig, "|", $str, "$type | $desc");
411 is_first($val & $str, $orig, "&", $str, "$type & $desc");
412 is_first($val ^ $str, $orig, "^", $str, "$type ^ $desc");
414 is_first($str | $val, $orig, "|", $str, "$desc | $type");
415 is_first($str & $val, $orig, "&", $str, "$desc & $type");
416 is_first($str ^ $val, $orig, "^", $str, "$desc ^ $type");
419 ($new = $val) |= $str;
420 is_first($new, $orig, "|", $str, "$type |= $desc");
421 ($new = $val) &= $str;
422 is_first($new, $orig, "&", $str, "$type &= $desc");
423 ($new = $val) ^= $str;
424 is_first($new, $orig, "^", $str, "$type ^= $desc");
426 ($new = $str) |= $val;
427 is_first($new, $orig, "|", $str, "$desc |= $type");
428 ($new = $str) &= $val;
429 is_first($new, $orig, "&", $str, "$desc &= $type");
430 ($new = $str) ^= $val;
431 is_first($new, $orig, "^", $str, "$desc ^= $type");
433 if ($orig eq "undef") {
434 # undef |= and undef ^= don't warn
435 is($warn, 10, "no duplicate warnings");
438 is($warn, 0, "no warnings");
443 delete $SIG{__WARN__};
449 use overload q/""/ => sub { $strval };
452 use overload q/|/ => sub { "y" };
455 ok(!eval { 1 if bless([], "Bar") | "x"; 1 },"string overload can't use |");
456 like($@, qr/no method found/, "correct error");
457 is(eval { bless([], "Baz") | "x" }, "y", "| overload works");
459 my $obj = bless [], "Bar";
461 eval { $obj |= "Q" };
463 is("$obj", "z", "|= doesn't break string overload");
466 $^A .= new version ~$_ for eval sprintf('"\\x%02x"', 0xff - ord("1")),
467 $::IS_EBCDIC ? v13 : v205, # 255 - ord('2')
468 eval sprintf('"\\x%02x"', 0xff - ord("3"));
469 is $^A, "123", '~v0 clears vstring magic on retval';
472 my $w = $Config::Config{ivsize} * 8;
474 fail("unexpected w $w") unless $w == 32 || $w == 64;
476 is(1 << 1, 2, "UV 1 left shift 1");
477 is(1 >> 1, 0, "UV 1 right shift 1");
479 is(0x7b << -4, 0x007, "UV left negative shift == right shift");
480 is(0x7b >> -4, 0x7b0, "UV right negative shift == left shift");
482 is(0x7b << 0, 0x07b, "UV left zero shift == identity");
483 is(0x7b >> 0, 0x07b, "UV right zero shift == identity");
485 is(0x0 << -1, 0x0, "zero left negative shift == zero");
486 is(0x0 >> -1, 0x0, "zero right negative shift == zero");
488 cmp_ok(1 << $w - 1, '==', 2 ** ($w - 1), # not is() because NV stringify.
489 "UV left $w - 1 shift == 2 ** ($w - 1)");
490 is(1 << $w, 0, "UV left shift $w == zero");
491 is(1 << $w + 1, 0, "UV left shift $w + 1 == zero");
493 is(1 >> $w - 1, 0, "UV right shift $w - 1 == zero");
494 is(1 >> $w, 0, "UV right shift $w == zero");
495 is(1 >> $w + 1, 0, "UV right shift $w + 1 == zero");
497 # Negative shiftees get promoted to UVs before shifting. This is
498 # not necessarily the ideal behavior, but that is what is happening.
500 no warnings "portable";
501 no warnings "overflow"; # prevent compile-time warning for ivsize=4
502 is(-1 << 1, 0xFFFF_FFFF_FFFF_FFFE,
503 "neg UV (sic) left shift = 0xFF..E");
504 is(-1 >> 1, 0x7FFF_FFFF_FFFF_FFFF,
505 "neg UV (sic) right right = 0x7F..F");
507 no warnings "portable";
508 is(-1 << 1, 0xFFFF_FFFE, "neg left shift == 0xFF..E");
509 is(-1 >> 1, 0x7FFF_FFFF, "neg right right == 0x7F..F");
513 # 'use integer' means use IVs instead of UVs.
517 is(1 << 1, 2, "IV 1 left shift 1 == 2");
518 is(1 >> 1, 0, "IV 1 right shift 1 == 0");
520 # The left overshift should behave like without 'use integer',
521 # that is, return zero.
522 is(1 << $w, 0, "IV 1 left shift $w == 0");
523 is(1 << $w + 1, 0, "IV 1 left shift $w + 1 == 0");
524 is(-1 << $w, 0, "IV -1 left shift $w == 0");
525 is(-1 << $w + 1, 0, "IV -1 left shift $w + 1 == 0");
527 # Even for negative IVs, left shift is multiplication.
528 # But right shift should display the stuckiness to -1.
529 is(-1 << 1, -2, "IV -1 left shift 1 == -2");
530 is(-1 >> 1, -1, "IV -1 right shift 1 == -1");
532 # As for UVs, negative shifting means the reverse shift.
533 is(-1 << -1, -1, "IV -1 left shift -1 == -1");
534 is(-1 >> -1, -2, "IV -1 right shift -1 == -2");
536 # Test also at and around wordsize, expect stuckiness to -1.
537 is(-1 >> $w - 1, -1, "IV -1 right shift $w - 1 == -1");
538 is(-1 >> $w, -1, "IV -1 right shift $w == -1");
539 is(-1 >> $w + 1, -1, "IV -1 right shift $w + 1 == -1");
543 # [perl #129287] UTF8 & was not providing a trailing null byte.
544 # This test is a bit convoluted, as we want to make sure that the string
545 # allocated for &’s target contains memory initialised to something other
546 # than a null byte. Uninitialised memory does not make for a reliable
547 # test. So we do &. on a longer non-utf8 string first.
548 for (["aaa","aaa"],[substr ("a\x{100}",0,1), "a"]) {
549 use feature "bitwise";
550 no warnings "experimental::bitwise", "pack";
551 $byte = substr unpack("P2", pack "P", $$_[0] &. $$_[1]), -1;
553 is $byte, "\0", "utf8 &. appends null byte";
555 # only visible under sanitize
556 fresh_perl_is('$x = "UUUUUUUV"; $y = "xxxxxxx"; $x |= $y; print $x',
557 ( $::IS_EBCDIC) ? 'XXXXXXXV' : '}}}}}}}V',
558 {}, "[perl #129995] access to freed memory");
562 # Using code points above 0xFF is fatal
564 foreach my $op_info ([and => "&"], [or => "|"], [xor => "^"]) {
565 my ($op_name, $op) = @$op_info;
567 eval '$_ = "\xFF" ' . $op . ' "\x{100}";';
568 like $@, qr /^Use of strings with code points over 0xFF as arguments (?#
569 )to bitwise $op_name \Q($op)\E operator is not allowed/,
570 "Use of code points above 0xFF as arguments to bitwise " .
571 "$op_name ($op) is not allowed";
576 eval '$_ = ~ "\x{100}";';
577 like $@, qr /^Use of strings with code points over 0xFF as arguments (?#
578 )to 1's complement \(~\) operator is not allowed/,
579 "Use of code points above 0xFF as argument to 1's complement " .
580 "(~) is not allowed";