4 # test the bit operators '&', '|', '^', '~', '<<', and '>>'
13 # Tests don't have names yet.
14 # If you find tests are failing, please try adding names to tests to track
15 # down where the failure is, and supply your new names as a patch.
16 # (Just-in-time test naming)
20 ok ((0xdead & 0xbeef) == 0x9ead);
21 ok ((0xdead | 0xbeef) == 0xfeef);
22 ok ((0xdead ^ 0xbeef) == 0x6042);
23 ok ((~0xdead & 0xbeef) == 0x2042);
26 ok ((257 << 7) == 32896);
27 ok ((33023 >> 7) == 257);
30 ok ((~0 > 0 && do { use integer; ~0 } == -1));
33 for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
34 my $cusp = 1 << ($bits - 1);
37 ok (($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0);
38 ok (($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0);
39 ok (($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0);
40 ok ((1 << ($bits - 1)) == $cusp &&
41 do { use integer; 1 << ($bits - 1) } == -$cusp);
42 ok (($cusp >> 1) == ($cusp / 2) &&
43 do { use integer; abs($cusp >> 1) } == ($cusp / 2));
45 $Aaz = chr(ord("A") & ord("z"));
46 $Aoz = chr(ord("A") | ord("z"));
47 $Axz = chr(ord("A") ^ ord("z"));
50 is (("AAAAA" & "zzzzz"), ($Aaz x 5));
51 is (("AAAAA" | "zzzzz"), ($Aoz x 5));
52 is (("AAAAA" ^ "zzzzz"), ($Axz x 5));
59 is (($foo & $bar), ($Aaz x 75 ));
61 is (($foo | $bar), ($Aoz x 75 . $zap));
63 is (($foo ^ $bar), ($Axz x 75 . $zap));
66 is ("ok \xFF\xFF\n" & "ok 19\n", "ok 19\n");
67 is ("ok 20\n" | "ok \0\0\n", "ok 20\n");
68 is ("o\000 \0001\000" ^ "\000k\0002\000\n", "ok 21\n");
71 is ("ok \x{FF}\x{FF}\n" & "ok 22\n", "ok 22\n");
72 is ("ok 23\n" | "ok \x{0}\x{0}\n", "ok 23\n");
73 is ("o\x{0} \x{0}4\x{0}" ^ "\x{0}k\x{0}2\x{0}\n", "ok 24\n");
76 is (sprintf("%vd", v4095 & v801), 801);
77 is (sprintf("%vd", v4095 | v801), 4095);
78 is (sprintf("%vd", v4095 ^ v801), 3294);
81 is (sprintf("%vd", v4095.801.4095 & v801.4095), '801.801');
82 is (sprintf("%vd", v4095.801.4095 | v801.4095), '4095.4095.4095');
83 is (sprintf("%vd", v801.4095 ^ v4095.801.4095), '3294.3294.4095');
85 is (sprintf("%vd", v120.300 & v200.400), '72.256');
86 is (sprintf("%vd", v120.300 | v200.400), '248.444');
87 is (sprintf("%vd", v120.300 ^ v200.400), '176.188');
92 is (sprintf("%vd", $a), '176.188');
96 is (sprintf("%vd", $a), '248.444');
102 my $Is_EBCDIC = (ord('A') == 193) ? 1 : 0;
106 for (0x100...0xFFF) {
109 push @not36, sprintf("%#03X", $_)
110 if $a ne chr(~$_) or length($a) != 1;
113 push @not36, sprintf("%#03X", $_)
114 if $a ne chr(~$_) or length($a) != 1 or ~$a ne chr($_);
117 is (join (', ', @not36), '');
121 for my $i (0xEEE...0xF00) {
122 for my $j (0x0..0x120) {
123 $a = ~(chr ($i) . chr $j);
125 push @not37, sprintf("%#03X %#03X", $i, $j)
126 if $a ne chr(~$i).chr(~$j) or
130 push @not37, sprintf("%#03X %#03X", $i, $j)
131 if $a ne chr(~$i).chr(~$j) or
133 ~$a ne chr($i).chr($j);
137 is (join (', ', @not37), '');
140 skip "EBCDIC" if $Is_EBCDIC;
147 for my $i (0x100..0x120) {
148 for my $j (0x100...0x120) {
149 push @not39, sprintf("%#03X %#03X", $i, $j)
150 if ~(chr($i)|chr($j)) ne (~chr($i)&~chr($j));
153 is (join (', ', @not39), '');
157 for my $i (0x100..0x120) {
158 for my $j (0x100...0x120) {
159 push @not40, sprintf("%#03X %#03X", $i, $j)
160 if ~(chr($i)&chr($j)) ne (~chr($i)|~chr($j));
163 is (join (', ', @not40), '');
166 # More variations on 19 and 22.
167 is ("ok \xFF\x{FF}\n" & "ok 41\n", "ok 41\n");
168 is ("ok \x{FF}\xFF\n" & "ok 42\n", "ok 42\n");
170 # Tests to see if you really can do casts negative floats to unsigned properly
179 sub TIESCALAR { bless { value => $_[1], orig => $_[1] } }
180 sub STORE { $_[0]{store}++; $_[0]{value} = $_[1] }
181 sub FETCH { $_[0]{fetch}++; $_[0]{value} }
182 sub stores { tied($_[0])->{value} = tied($_[0])->{orig};
183 delete(tied($_[0])->{store}) || 0 }
184 sub fetches { delete(tied($_[0])->{fetch}) || 0 }
186 # numeric double magic tests
273 } # end of use integer;
275 # stringwise double magic tests
280 is(($x | $y), ("a" | "c"));
286 is(($x & $y), ("a" & "c"));
292 is(($x ^ $y), ("a" ^ "c"));
298 is(($x |= $y), ("a" | "c"));
304 is(($x &= $y), ("a" & "c"));
310 is(($x ^= $y), ("a" ^ "c"));
320 $a = "\0\x{100}"; chop($a);
321 ok(utf8::is_utf8($a)); # make sure UTF8 flag is still there
323 is($a, "\xFF", "~ works with utf-8");
325 # [rt.perl.org 33003]
326 # This would cause a segfault
327 is( runperl(prog => '$#a>>=1; print 1'), 1 );