Initial import from FreeBSD RELENG_4:
[games.git] / contrib / perl5 / t / op / bop.t
1 #!./perl
2
3 #
4 # test the bit operators '&', '|', '^', '~', '<<', and '>>'
5 #
6
7 BEGIN {
8     chdir 't' if -d 't';
9     @INC = '../lib';
10 }
11
12 print "1..18\n";
13
14 # numerics
15 print ((0xdead & 0xbeef) == 0x9ead ? "ok 1\n" : "not ok 1\n");
16 print ((0xdead | 0xbeef) == 0xfeef ? "ok 2\n" : "not ok 2\n");
17 print ((0xdead ^ 0xbeef) == 0x6042 ? "ok 3\n" : "not ok 3\n");
18 print ((~0xdead & 0xbeef) == 0x2042 ? "ok 4\n" : "not ok 4\n");
19
20 # shifts
21 print ((257 << 7) == 32896 ? "ok 5\n" : "not ok 5\n");
22 print ((33023 >> 7) == 257 ? "ok 6\n" : "not ok 6\n");
23
24 # signed vs. unsigned
25 print ((~0 > 0 && do { use integer; ~0 } == -1)
26        ? "ok 7\n" : "not ok 7\n");
27
28 my $bits = 0;
29 for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
30 my $cusp = 1 << ($bits - 1);
31
32 print ((($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0)
33        ? "ok 8\n" : "not ok 8\n");
34 print ((($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0)
35        ? "ok 9\n" : "not ok 9\n");
36 print ((($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0)
37        ? "ok 10\n" : "not ok 10\n");
38 print (((1 << ($bits - 1)) == $cusp &&
39         do { use integer; 1 << ($bits - 1) } == -$cusp)
40        ? "ok 11\n" : "not ok 11\n");
41 print ((($cusp >> 1) == ($cusp / 2) &&
42         do { use integer; $cusp >> 1 } == -($cusp / 2))
43        ? "ok 12\n" : "not ok 12\n");
44
45 $Aaz = chr(ord("A") & ord("z"));
46 $Aoz = chr(ord("A") | ord("z"));
47 $Axz = chr(ord("A") ^ ord("z"));
48
49 # short strings
50 print (("AAAAA" & "zzzzz") eq ($Aaz x 5) ? "ok 13\n" : "not ok 13\n");
51 print (("AAAAA" | "zzzzz") eq ($Aoz x 5) ? "ok 14\n" : "not ok 14\n");
52 print (("AAAAA" ^ "zzzzz") eq ($Axz x 5) ? "ok 15\n" : "not ok 15\n");
53
54 # long strings
55 $foo = "A" x 150;
56 $bar = "z" x 75;
57 $zap = "A" x 75;
58 # & truncates
59 print (($foo & $bar) eq ($Aaz x 75 ) ? "ok 16\n" : "not ok 16\n");
60 # | does not truncate
61 print (($foo | $bar) eq ($Aoz x 75 . $zap) ? "ok 17\n" : "not ok 17\n");
62 # ^ does not truncate
63 print (($foo ^ $bar) eq ($Axz x 75 . $zap) ? "ok 18\n" : "not ok 18\n");
64