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 => 174 + (10*13*2) + 5;
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';
80 # But don’t mistake a COW for a constant when assigning to it
84 is $i, 255, '[perl #108480] $cow |= number';
87 is $i, 0, '[perl #108480] $cow &= number';
90 is $i, 255, '[perl #108480] $cow ^= number';
93 is ("ok \xFF\xFF\n" & "ok 19\n", "ok 19\n");
94 is ("ok 20\n" | "ok \0\0\n", "ok 20\n");
95 is ("o\000 \0001\000" ^ "\000k\0002\000\n", "ok 21\n");
98 is ("ok \x{FF}\x{FF}\n" & "ok 22\n", "ok 22\n");
99 is ("ok 23\n" | "ok \x{0}\x{0}\n", "ok 23\n");
100 is ("o\x{0} \x{0}4\x{0}" ^ "\x{0}k\x{0}2\x{0}\n", "ok 24\n");
103 is (sprintf("%vd", v4095 & v801), 801);
104 is (sprintf("%vd", v4095 | v801), 4095);
105 is (sprintf("%vd", v4095 ^ v801), 3294);
108 is (sprintf("%vd", v4095.801.4095 & v801.4095), '801.801');
109 is (sprintf("%vd", v4095.801.4095 | v801.4095), '4095.4095.4095');
110 is (sprintf("%vd", v801.4095 ^ v4095.801.4095), '3294.3294.4095');
112 is (sprintf("%vd", v120.300 & v200.400), '72.256');
113 is (sprintf("%vd", v120.300 | v200.400), '248.444');
114 is (sprintf("%vd", v120.300 ^ v200.400), '176.188');
119 is (sprintf("%vd", $a), '176.188');
123 is (sprintf("%vd", $a), '248.444');
129 my $Is_EBCDIC = (ord('A') == 193) ? 1 : 0;
133 for (0x100...0xFFF) {
136 push @not36, sprintf("%#03X", $_)
137 if $a ne chr(~$_) or length($a) != 1;
140 push @not36, sprintf("%#03X", $_)
141 if $a ne chr(~$_) or length($a) != 1 or ~$a ne chr($_);
144 is (join (', ', @not36), '');
148 for my $i (0xEEE...0xF00) {
149 for my $j (0x0..0x120) {
150 $a = ~(chr ($i) . chr $j);
152 push @not37, sprintf("%#03X %#03X", $i, $j)
153 if $a ne chr(~$i).chr(~$j) or
157 push @not37, sprintf("%#03X %#03X", $i, $j)
158 if $a ne chr(~$i).chr(~$j) or
160 ~$a ne chr($i).chr($j);
164 is (join (', ', @not37), '');
167 skip "EBCDIC" if $Is_EBCDIC;
174 for my $i (0x100..0x120) {
175 for my $j (0x100...0x120) {
176 push @not39, sprintf("%#03X %#03X", $i, $j)
177 if ~(chr($i)|chr($j)) ne (~chr($i)&~chr($j));
180 is (join (', ', @not39), '');
184 for my $i (0x100..0x120) {
185 for my $j (0x100...0x120) {
186 push @not40, sprintf("%#03X %#03X", $i, $j)
187 if ~(chr($i)&chr($j)) ne (~chr($i)|~chr($j));
190 is (join (', ', @not40), '');
193 # More variations on 19 and 22.
194 is ("ok \xFF\x{FF}\n" & "ok 41\n", "ok 41\n");
195 is ("ok \x{FF}\xFF\n" & "ok 42\n", "ok 42\n");
197 # Tests to see if you really can do casts negative floats to unsigned properly
206 sub TIESCALAR { bless { value => $_[1], orig => $_[1] } }
207 sub STORE { $_[0]{store}++; $_[0]{value} = $_[1] }
208 sub FETCH { $_[0]{fetch}++; $_[0]{value} }
209 sub stores { tied($_[0])->{value} = tied($_[0])->{orig};
210 delete(tied($_[0])->{store}) || 0 }
211 sub fetches { delete(tied($_[0])->{fetch}) || 0 }
213 # numeric double magic tests
300 } # end of use integer;
302 # stringwise double magic tests
307 is(($x | $y), ("a" | "c"));
313 is(($x & $y), ("a" & "c"));
319 is(($x ^ $y), ("a" ^ "c"));
325 is(($x |= $y), ("a" | "c"));
331 is(($x &= $y), ("a" & "c"));
337 is(($x ^= $y), ("a" ^ "c"));
347 $a = "\0\x{100}"; chop($a);
348 ok(utf8::is_utf8($a)); # make sure UTF8 flag is still there
350 is($a, "\xFF", "~ works with utf-8");
352 # [rt.perl.org 33003]
353 # This would cause a segfault without malloc wrap
355 skip "No malloc wrap checks" unless $Config::Config{usemallocwrap};
356 like( runperl(prog => 'eval q($#a>>=1); print 1'), "^1\n?" );
359 # [perl #37616] Bug in &= (string) and/or m//
363 ok($a =~ /a+$/, 'ASCII "a" is NUL-terminated');
367 ok($b =~ /b+$/, 'Unicode "b" is NUL-terminated');
371 $a = chr(0x101) x 0x101;
372 $b = chr(0x0FF) x 0x0FF;
375 is($c, chr(0x1FF) x 0xFF . chr(0x101) x 2);
378 is($c, chr(0x1FF) x 0xFF . chr(0x101) x 2);
381 is($c, chr(0x001) x 0x0FF);
384 is($c, chr(0x001) x 0x0FF);
387 is($c, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
390 is($c, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
394 $a = chr(0x101) x 0x101;
395 $b = chr(0x0FF) x 0x0FF;
398 is($a, chr(0x1FF) x 0xFF . chr(0x101) x 2);
402 $a = chr(0x101) x 0x101;
403 $b = chr(0x0FF) x 0x0FF;
406 is($b, chr(0x1FF) x 0xFF . chr(0x101) x 2);
410 $a = chr(0x101) x 0x101;
411 $b = chr(0x0FF) x 0x0FF;
414 is($a, chr(0x001) x 0x0FF);
418 $a = chr(0x101) x 0x101;
419 $b = chr(0x0FF) x 0x0FF;
422 is($b, chr(0x001) x 0x0FF);
426 $a = chr(0x101) x 0x101;
427 $b = chr(0x0FF) x 0x0FF;
430 is($a, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
434 $a = chr(0x101) x 0x101;
435 $b = chr(0x0FF) x 0x0FF;
438 is($b, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
441 # update to pp_complement() via Coverity
443 # UTF-EBCDIC is limited to 0x7fffffff and can't encode ~0.
444 skip "EBCDIC" if $Is_EBCDIC;
446 my $str = "\x{10000}\x{800}";
447 # U+10000 is four bytes in UTF-8/UTF-EBCDIC.
448 # U+0800 is three bytes in UTF-8/UTF-EBCDIC.
451 { use bytes; $str =~ s/\C\C\z//; }
453 # it's really bogus that (~~malformed) is \0.
454 my $ref = "\x{10000}\0";
457 # same test, but this time with a longer replacement string that
458 # exercises a different branch in pp_subsr()
460 $str = "\x{10000}\x{800}";
461 { use bytes; $str =~ s/\C\C\z/\0\0\0/; }
463 # it's also bogus that (~~malformed) is \0\0\0\0.
464 my $ref = "\x{10000}\0\0\0\0";
465 is(~~$str, $ref, "use bytes with long replacement");
472 for my $str ("x", "\x{100}") {
473 for my $chr (qw/S A H G X ( * F/) {
474 for my $op (qw/| & ^/) {
477 $res{"$chr$op$str"} = eval qq/chr($co $op $so)/;
480 $res{"undef|$str"} = $str;
481 $res{"undef&$str"} = "";
482 $res{"undef^$str"} = $str;
490 local $SIG{__WARN__} = sub { $warn++ };
493 my ($got, $orig, $op, $str, $name) = @_;
494 is(substr($got, 0, 1), $res{"$orig$op$str"}, $name);
498 # [object to test, first char of stringification, name]
499 [undef, "undef", "undef" ],
500 [\1, "S", "scalar ref" ],
501 [[], "A", "array ref" ],
502 [{}, "H", "hash ref" ],
503 [qr/x/, "(", "qr//" ],
504 [*foo, "*", "glob" ],
505 [\*foo, "G", "glob ref" ],
506 [PVBM, "X", "PVBM" ],
507 [\PVBM, "S", "PVBM ref" ],
508 [bless([], "Foo"), "F", "object" ],
510 my ($val, $orig, $type) = @$_;
512 for (["x", "string"], ["\x{100}", "utf8"]) {
513 my ($str, $desc) = @$_;
517 is_first($val | $str, $orig, "|", $str, "$type | $desc");
518 is_first($val & $str, $orig, "&", $str, "$type & $desc");
519 is_first($val ^ $str, $orig, "^", $str, "$type ^ $desc");
521 is_first($str | $val, $orig, "|", $str, "$desc | $type");
522 is_first($str & $val, $orig, "&", $str, "$desc & $type");
523 is_first($str ^ $val, $orig, "^", $str, "$desc ^ $type");
526 ($new = $val) |= $str;
527 is_first($new, $orig, "|", $str, "$type |= $desc");
528 ($new = $val) &= $str;
529 is_first($new, $orig, "&", $str, "$type &= $desc");
530 ($new = $val) ^= $str;
531 is_first($new, $orig, "^", $str, "$type ^= $desc");
533 ($new = $str) |= $val;
534 is_first($new, $orig, "|", $str, "$desc |= $type");
535 ($new = $str) &= $val;
536 is_first($new, $orig, "&", $str, "$desc &= $type");
537 ($new = $str) ^= $val;
538 is_first($new, $orig, "^", $str, "$desc ^= $type");
540 if ($orig eq "undef") {
541 # undef |= and undef ^= don't warn
542 is($warn, 10, "no duplicate warnings");
545 is($warn, 0, "no warnings");
554 use overload q/""/ => sub { $strval };
557 use overload q/|/ => sub { "y" };
560 ok(!eval { bless([], "Bar") | "x"; 1 }, "string overload can't use |");
561 like($@, qr/no method found/, "correct error");
562 is(eval { bless([], "Baz") | "x" }, "y", "| overload works");
564 my $obj = bless [], "Bar";
566 eval { $obj |= "Q" };
568 is("$obj", "z", "|= doesn't break string overload");
571 $^A .= new version ~$_ for "\xce", v205, "\xcc";
572 is $^A, "123", '~v0 clears vstring magic on retval';