4 # test the bit operators '&', '|', '^', '~', '<<', and '>>'
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 => 171 + (10*13*2) + 4;
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));
67 sub _and($) { $_[0] & "+0" }
68 sub _oar($) { $_[0] | "+0" }
69 sub _xor($) { $_[0] ^ "+0" }
70 is _and "waf", '# ', 'str var & const str'; # These three
71 is _and 0, '0', 'num var & const str'; # are from
72 is _and "waf", '# ', 'str var & const str again'; # [perl #20661]
73 is _oar "yit", '{yt', 'str var | const str';
74 is _oar 0, '0', 'num var | const str';
75 is _oar "yit", '{yt', 'str var | const str again';
76 is _xor "yit", 'RYt', 'str var ^ const str';
77 is _xor 0, '0', 'num var ^ const str';
78 is _xor "yit", 'RYt', 'str var ^ const str again';
81 is ("ok \xFF\xFF\n" & "ok 19\n", "ok 19\n");
82 is ("ok 20\n" | "ok \0\0\n", "ok 20\n");
83 is ("o\000 \0001\000" ^ "\000k\0002\000\n", "ok 21\n");
86 is ("ok \x{FF}\x{FF}\n" & "ok 22\n", "ok 22\n");
87 is ("ok 23\n" | "ok \x{0}\x{0}\n", "ok 23\n");
88 is ("o\x{0} \x{0}4\x{0}" ^ "\x{0}k\x{0}2\x{0}\n", "ok 24\n");
91 is (sprintf("%vd", v4095 & v801), 801);
92 is (sprintf("%vd", v4095 | v801), 4095);
93 is (sprintf("%vd", v4095 ^ v801), 3294);
96 is (sprintf("%vd", v4095.801.4095 & v801.4095), '801.801');
97 is (sprintf("%vd", v4095.801.4095 | v801.4095), '4095.4095.4095');
98 is (sprintf("%vd", v801.4095 ^ v4095.801.4095), '3294.3294.4095');
100 is (sprintf("%vd", v120.300 & v200.400), '72.256');
101 is (sprintf("%vd", v120.300 | v200.400), '248.444');
102 is (sprintf("%vd", v120.300 ^ v200.400), '176.188');
107 is (sprintf("%vd", $a), '176.188');
111 is (sprintf("%vd", $a), '248.444');
117 my $Is_EBCDIC = (ord('A') == 193) ? 1 : 0;
121 for (0x100...0xFFF) {
124 push @not36, sprintf("%#03X", $_)
125 if $a ne chr(~$_) or length($a) != 1;
128 push @not36, sprintf("%#03X", $_)
129 if $a ne chr(~$_) or length($a) != 1 or ~$a ne chr($_);
132 is (join (', ', @not36), '');
136 for my $i (0xEEE...0xF00) {
137 for my $j (0x0..0x120) {
138 $a = ~(chr ($i) . chr $j);
140 push @not37, sprintf("%#03X %#03X", $i, $j)
141 if $a ne chr(~$i).chr(~$j) or
145 push @not37, sprintf("%#03X %#03X", $i, $j)
146 if $a ne chr(~$i).chr(~$j) or
148 ~$a ne chr($i).chr($j);
152 is (join (', ', @not37), '');
155 skip "EBCDIC" if $Is_EBCDIC;
162 for my $i (0x100..0x120) {
163 for my $j (0x100...0x120) {
164 push @not39, sprintf("%#03X %#03X", $i, $j)
165 if ~(chr($i)|chr($j)) ne (~chr($i)&~chr($j));
168 is (join (', ', @not39), '');
172 for my $i (0x100..0x120) {
173 for my $j (0x100...0x120) {
174 push @not40, sprintf("%#03X %#03X", $i, $j)
175 if ~(chr($i)&chr($j)) ne (~chr($i)|~chr($j));
178 is (join (', ', @not40), '');
181 # More variations on 19 and 22.
182 is ("ok \xFF\x{FF}\n" & "ok 41\n", "ok 41\n");
183 is ("ok \x{FF}\xFF\n" & "ok 42\n", "ok 42\n");
185 # Tests to see if you really can do casts negative floats to unsigned properly
194 sub TIESCALAR { bless { value => $_[1], orig => $_[1] } }
195 sub STORE { $_[0]{store}++; $_[0]{value} = $_[1] }
196 sub FETCH { $_[0]{fetch}++; $_[0]{value} }
197 sub stores { tied($_[0])->{value} = tied($_[0])->{orig};
198 delete(tied($_[0])->{store}) || 0 }
199 sub fetches { delete(tied($_[0])->{fetch}) || 0 }
201 # numeric double magic tests
288 } # end of use integer;
290 # stringwise double magic tests
295 is(($x | $y), ("a" | "c"));
301 is(($x & $y), ("a" & "c"));
307 is(($x ^ $y), ("a" ^ "c"));
313 is(($x |= $y), ("a" | "c"));
319 is(($x &= $y), ("a" & "c"));
325 is(($x ^= $y), ("a" ^ "c"));
335 $a = "\0\x{100}"; chop($a);
336 ok(utf8::is_utf8($a)); # make sure UTF8 flag is still there
338 is($a, "\xFF", "~ works with utf-8");
340 # [rt.perl.org 33003]
341 # This would cause a segfault without malloc wrap
343 skip "No malloc wrap checks" unless $Config::Config{usemallocwrap};
344 like( runperl(prog => 'eval q($#a>>=1); print 1'), "^1\n?" );
347 # [perl #37616] Bug in &= (string) and/or m//
351 ok($a =~ /a+$/, 'ASCII "a" is NUL-terminated');
355 ok($b =~ /b+$/, 'Unicode "b" is NUL-terminated');
359 $a = chr(0x101) x 0x101;
360 $b = chr(0x0FF) x 0x0FF;
363 is($c, chr(0x1FF) x 0xFF . chr(0x101) x 2);
366 is($c, chr(0x1FF) x 0xFF . chr(0x101) x 2);
369 is($c, chr(0x001) x 0x0FF);
372 is($c, chr(0x001) x 0x0FF);
375 is($c, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
378 is($c, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
382 $a = chr(0x101) x 0x101;
383 $b = chr(0x0FF) x 0x0FF;
386 is($a, chr(0x1FF) x 0xFF . chr(0x101) x 2);
390 $a = chr(0x101) x 0x101;
391 $b = chr(0x0FF) x 0x0FF;
394 is($b, chr(0x1FF) x 0xFF . chr(0x101) x 2);
398 $a = chr(0x101) x 0x101;
399 $b = chr(0x0FF) x 0x0FF;
402 is($a, chr(0x001) x 0x0FF);
406 $a = chr(0x101) x 0x101;
407 $b = chr(0x0FF) x 0x0FF;
410 is($b, chr(0x001) x 0x0FF);
414 $a = chr(0x101) x 0x101;
415 $b = chr(0x0FF) x 0x0FF;
418 is($a, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
422 $a = chr(0x101) x 0x101;
423 $b = chr(0x0FF) x 0x0FF;
426 is($b, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
429 # update to pp_complement() via Coverity
431 # UTF-EBCDIC is limited to 0x7fffffff and can't encode ~0.
432 skip "EBCDIC" if $Is_EBCDIC;
434 my $str = "\x{10000}\x{800}";
435 # U+10000 is four bytes in UTF-8/UTF-EBCDIC.
436 # U+0800 is three bytes in UTF-8/UTF-EBCDIC.
439 { use bytes; $str =~ s/\C\C\z//; }
441 # it's really bogus that (~~malformed) is \0.
442 my $ref = "\x{10000}\0";
445 # same test, but this time with a longer replacement string that
446 # exercises a different branch in pp_subsr()
448 $str = "\x{10000}\x{800}";
449 { use bytes; $str =~ s/\C\C\z/\0\0\0/; }
451 # it's also bogus that (~~malformed) is \0\0\0\0.
452 my $ref = "\x{10000}\0\0\0\0";
453 is(~~$str, $ref, "use bytes with long replacement");
460 for my $str ("x", "\x{100}") {
461 for my $chr (qw/S A H G X ( * F/) {
462 for my $op (qw/| & ^/) {
465 $res{"$chr$op$str"} = eval qq/chr($co $op $so)/;
468 $res{"undef|$str"} = $str;
469 $res{"undef&$str"} = "";
470 $res{"undef^$str"} = $str;
478 local $SIG{__WARN__} = sub { $warn++ };
481 my ($got, $orig, $op, $str, $name) = @_;
482 is(substr($got, 0, 1), $res{"$orig$op$str"}, $name);
486 # [object to test, first char of stringification, name]
487 [undef, "undef", "undef" ],
488 [\1, "S", "scalar ref" ],
489 [[], "A", "array ref" ],
490 [{}, "H", "hash ref" ],
491 [qr/x/, "(", "qr//" ],
492 [*foo, "*", "glob" ],
493 [\*foo, "G", "glob ref" ],
494 [PVBM, "X", "PVBM" ],
495 [\PVBM, "S", "PVBM ref" ],
496 [bless([], "Foo"), "F", "object" ],
498 my ($val, $orig, $type) = @$_;
500 for (["x", "string"], ["\x{100}", "utf8"]) {
501 my ($str, $desc) = @$_;
505 is_first($val | $str, $orig, "|", $str, "$type | $desc");
506 is_first($val & $str, $orig, "&", $str, "$type & $desc");
507 is_first($val ^ $str, $orig, "^", $str, "$type ^ $desc");
509 is_first($str | $val, $orig, "|", $str, "$desc | $type");
510 is_first($str & $val, $orig, "&", $str, "$desc & $type");
511 is_first($str ^ $val, $orig, "^", $str, "$desc ^ $type");
514 ($new = $val) |= $str;
515 is_first($new, $orig, "|", $str, "$type |= $desc");
516 ($new = $val) &= $str;
517 is_first($new, $orig, "&", $str, "$type &= $desc");
518 ($new = $val) ^= $str;
519 is_first($new, $orig, "^", $str, "$type ^= $desc");
521 ($new = $str) |= $val;
522 is_first($new, $orig, "|", $str, "$desc |= $type");
523 ($new = $str) &= $val;
524 is_first($new, $orig, "&", $str, "$desc &= $type");
525 ($new = $str) ^= $val;
526 is_first($new, $orig, "^", $str, "$desc ^= $type");
528 if ($orig eq "undef") {
529 # undef |= and undef ^= don't warn
530 is($warn, 10, "no duplicate warnings");
533 is($warn, 0, "no warnings");
542 use overload q/""/ => sub { $strval };
545 use overload q/|/ => sub { "y" };
548 ok(!eval { bless([], "Bar") | "x"; 1 }, "string overload can't use |");
549 like($@, qr/no method found/, "correct error");
550 is(eval { bless([], "Baz") | "x" }, "y", "| overload works");
552 my $obj = bless [], "Bar";
554 eval { $obj |= "Q" };
556 is("$obj", "z", "|= doesn't break string overload");