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)
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 is ("ok \xFF\xFF\n" & "ok 19\n", "ok 19\n");
68 is ("ok 20\n" | "ok \0\0\n", "ok 20\n");
69 is ("o\000 \0001\000" ^ "\000k\0002\000\n", "ok 21\n");
72 is ("ok \x{FF}\x{FF}\n" & "ok 22\n", "ok 22\n");
73 is ("ok 23\n" | "ok \x{0}\x{0}\n", "ok 23\n");
74 is ("o\x{0} \x{0}4\x{0}" ^ "\x{0}k\x{0}2\x{0}\n", "ok 24\n");
77 is (sprintf("%vd", v4095 & v801), 801);
78 is (sprintf("%vd", v4095 | v801), 4095);
79 is (sprintf("%vd", v4095 ^ v801), 3294);
82 is (sprintf("%vd", v4095.801.4095 & v801.4095), '801.801');
83 is (sprintf("%vd", v4095.801.4095 | v801.4095), '4095.4095.4095');
84 is (sprintf("%vd", v801.4095 ^ v4095.801.4095), '3294.3294.4095');
86 is (sprintf("%vd", v120.300 & v200.400), '72.256');
87 is (sprintf("%vd", v120.300 | v200.400), '248.444');
88 is (sprintf("%vd", v120.300 ^ v200.400), '176.188');
93 is (sprintf("%vd", $a), '176.188');
97 is (sprintf("%vd", $a), '248.444');
103 my $Is_EBCDIC = (ord('A') == 193) ? 1 : 0;
107 for (0x100...0xFFF) {
110 push @not36, sprintf("%#03X", $_)
111 if $a ne chr(~$_) or length($a) != 1;
114 push @not36, sprintf("%#03X", $_)
115 if $a ne chr(~$_) or length($a) != 1 or ~$a ne chr($_);
118 is (join (', ', @not36), '');
122 for my $i (0xEEE...0xF00) {
123 for my $j (0x0..0x120) {
124 $a = ~(chr ($i) . chr $j);
126 push @not37, sprintf("%#03X %#03X", $i, $j)
127 if $a ne chr(~$i).chr(~$j) or
131 push @not37, sprintf("%#03X %#03X", $i, $j)
132 if $a ne chr(~$i).chr(~$j) or
134 ~$a ne chr($i).chr($j);
138 is (join (', ', @not37), '');
141 skip "EBCDIC" if $Is_EBCDIC;
148 for my $i (0x100..0x120) {
149 for my $j (0x100...0x120) {
150 push @not39, sprintf("%#03X %#03X", $i, $j)
151 if ~(chr($i)|chr($j)) ne (~chr($i)&~chr($j));
154 is (join (', ', @not39), '');
158 for my $i (0x100..0x120) {
159 for my $j (0x100...0x120) {
160 push @not40, sprintf("%#03X %#03X", $i, $j)
161 if ~(chr($i)&chr($j)) ne (~chr($i)|~chr($j));
164 is (join (', ', @not40), '');
167 # More variations on 19 and 22.
168 is ("ok \xFF\x{FF}\n" & "ok 41\n", "ok 41\n");
169 is ("ok \x{FF}\xFF\n" & "ok 42\n", "ok 42\n");
171 # Tests to see if you really can do casts negative floats to unsigned properly
180 sub TIESCALAR { bless { value => $_[1], orig => $_[1] } }
181 sub STORE { $_[0]{store}++; $_[0]{value} = $_[1] }
182 sub FETCH { $_[0]{fetch}++; $_[0]{value} }
183 sub stores { tied($_[0])->{value} = tied($_[0])->{orig};
184 delete(tied($_[0])->{store}) || 0 }
185 sub fetches { delete(tied($_[0])->{fetch}) || 0 }
187 # numeric double magic tests
274 } # end of use integer;
276 # stringwise double magic tests
281 is(($x | $y), ("a" | "c"));
287 is(($x & $y), ("a" & "c"));
293 is(($x ^ $y), ("a" ^ "c"));
299 is(($x |= $y), ("a" | "c"));
305 is(($x &= $y), ("a" & "c"));
311 is(($x ^= $y), ("a" ^ "c"));
321 $a = "\0\x{100}"; chop($a);
322 ok(utf8::is_utf8($a)); # make sure UTF8 flag is still there
324 is($a, "\xFF", "~ works with utf-8");
326 # [rt.perl.org 33003]
327 # This would cause a segfault without malloc wrap
329 skip "No malloc wrap checks" unless $Config::Config{usemallocwrap};
330 like( runperl(prog => 'eval q($#a>>=1); print 1'), "^1\n?" );
333 # [perl #37616] Bug in &= (string) and/or m//
337 ok($a =~ /a+$/, 'ASCII "a" is NUL-terminated');
341 ok($b =~ /b+$/, 'Unicode "b" is NUL-terminated');
345 $a = chr(0x101) x 0x101;
346 $b = chr(0x0FF) x 0x0FF;
349 is($c, chr(0x1FF) x 0xFF . chr(0x101) x 2);
352 is($c, chr(0x1FF) x 0xFF . chr(0x101) x 2);
355 is($c, chr(0x001) x 0x0FF);
358 is($c, chr(0x001) x 0x0FF);
361 is($c, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
364 is($c, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
368 $a = chr(0x101) x 0x101;
369 $b = chr(0x0FF) x 0x0FF;
372 is($a, chr(0x1FF) x 0xFF . chr(0x101) x 2);
376 $a = chr(0x101) x 0x101;
377 $b = chr(0x0FF) x 0x0FF;
380 is($b, chr(0x1FF) x 0xFF . chr(0x101) x 2);
384 $a = chr(0x101) x 0x101;
385 $b = chr(0x0FF) x 0x0FF;
388 is($a, chr(0x001) x 0x0FF);
392 $a = chr(0x101) x 0x101;
393 $b = chr(0x0FF) x 0x0FF;
396 is($b, chr(0x001) x 0x0FF);
400 $a = chr(0x101) x 0x101;
401 $b = chr(0x0FF) x 0x0FF;
404 is($a, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
408 $a = chr(0x101) x 0x101;
409 $b = chr(0x0FF) x 0x0FF;
412 is($b, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
415 # update to pp_complement() via Coverity
417 # UTF-EBCDIC is limited to 0x7fffffff and can't encode ~0.
418 skip "EBCDIC" if $Is_EBCDIC;
420 my $str = "\x{10000}\x{800}";
421 # U+10000 is four bytes in UTF-8/UTF-EBCDIC.
422 # U+0800 is three bytes in UTF-8/UTF-EBCDIC.
425 { use bytes; $str =~ s/\C\C\z//; }
427 # it's really bogus that (~~malformed) is \0.
428 my $ref = "\x{10000}\0";