This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
VMS patches
[perl5.git] / t / op / bop.t
CommitLineData
ddb9d9dc
PP
1#!./perl
2
3#
55497cff 4# test the bit operators '&', '|', '^', '~', '<<', and '>>'
ddb9d9dc
PP
5#
6
d1f8c7a4
CS
7BEGIN {
8 chdir 't' if -d 't';
9 @INC = '../lib';
10}
11
55497cff 12print "1..18\n";
ddb9d9dc
PP
13
14# numerics
15print ((0xdead & 0xbeef) == 0x9ead ? "ok 1\n" : "not ok 1\n");
16print ((0xdead | 0xbeef) == 0xfeef ? "ok 2\n" : "not ok 2\n");
17print ((0xdead ^ 0xbeef) == 0x6042 ? "ok 3\n" : "not ok 3\n");
55497cff
PP
18print ((~0xdead & 0xbeef) == 0x2042 ? "ok 4\n" : "not ok 4\n");
19
20# shifts
21print ((257 << 7) == 32896 ? "ok 5\n" : "not ok 5\n");
22print ((33023 >> 7) == 257 ? "ok 6\n" : "not ok 6\n");
23
24# signed vs. unsigned
25print ((~0 > 0 && do { use integer; ~0 } == -1)
26 ? "ok 7\n" : "not ok 7\n");
d1f8c7a4
CS
27
28my $bits = 0;
29for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
30my $cusp = 1 << ($bits - 1);
31
32print ((($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0)
55497cff 33 ? "ok 8\n" : "not ok 8\n");
d1f8c7a4 34print ((($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0)
55497cff 35 ? "ok 9\n" : "not ok 9\n");
d1f8c7a4 36print ((($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0)
55497cff 37 ? "ok 10\n" : "not ok 10\n");
d1f8c7a4
CS
38print (((1 << ($bits - 1)) == $cusp &&
39 do { use integer; 1 << ($bits - 1) } == -$cusp)
55497cff 40 ? "ok 11\n" : "not ok 11\n");
d1f8c7a4
CS
41print ((($cusp >> 1) == ($cusp / 2) &&
42 do { use integer; $cusp >> 1 } == -($cusp / 2))
55497cff 43 ? "ok 12\n" : "not ok 12\n");
ddb9d9dc
PP
44
45# short strings
55497cff
PP
46print (("AAAAA" & "zzzzz") eq '@@@@@' ? "ok 13\n" : "not ok 13\n");
47print (("AAAAA" | "zzzzz") eq '{{{{{' ? "ok 14\n" : "not ok 14\n");
48print (("AAAAA" ^ "zzzzz") eq ';;;;;' ? "ok 15\n" : "not ok 15\n");
ddb9d9dc
PP
49
50# long strings
51$foo = "A" x 150;
52$bar = "z" x 75;
55497cff
PP
53print (($foo & $bar) eq ('@'x75 ) ? "ok 16\n" : "not ok 16\n");
54print (($foo | $bar) eq ('{'x75 . 'A'x75) ? "ok 17\n" : "not ok 17\n");
55print (($foo ^ $bar) eq (';'x75 . 'A'x75) ? "ok 18\n" : "not ok 18\n");