xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/bop.t (revision 0:68f95e015346)
1#!./perl
2
3#
4# test the bit operators '&', '|', '^', '~', '<<', and '>>'
5#
6
7BEGIN {
8    chdir 't' if -d 't';
9    @INC = '../lib';
10    require "./test.pl";
11}
12
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)
17plan tests => 46;
18
19# numerics
20ok ((0xdead & 0xbeef) == 0x9ead);
21ok ((0xdead | 0xbeef) == 0xfeef);
22ok ((0xdead ^ 0xbeef) == 0x6042);
23ok ((~0xdead & 0xbeef) == 0x2042);
24
25# shifts
26ok ((257 << 7) == 32896);
27ok ((33023 >> 7) == 257);
28
29# signed vs. unsigned
30ok ((~0 > 0 && do { use integer; ~0 } == -1));
31
32my $bits = 0;
33for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
34my $cusp = 1 << ($bits - 1);
35
36
37ok (($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0);
38ok (($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0);
39ok (($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0);
40ok ((1 << ($bits - 1)) == $cusp &&
41    do { use integer; 1 << ($bits - 1) } == -$cusp);
42ok (($cusp >> 1) == ($cusp / 2) &&
43    do { use integer; abs($cusp >> 1) } == ($cusp / 2));
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
50is (("AAAAA" & "zzzzz"), ($Aaz x 5));
51is (("AAAAA" | "zzzzz"), ($Aoz x 5));
52is (("AAAAA" ^ "zzzzz"), ($Axz x 5));
53
54# long strings
55$foo = "A" x 150;
56$bar = "z" x 75;
57$zap = "A" x 75;
58# & truncates
59is (($foo & $bar), ($Aaz x 75 ));
60# | does not truncate
61is (($foo | $bar), ($Aoz x 75 . $zap));
62# ^ does not truncate
63is (($foo ^ $bar), ($Axz x 75 . $zap));
64
65#
66is ("ok \xFF\xFF\n" & "ok 19\n", "ok 19\n");
67is ("ok 20\n" | "ok \0\0\n", "ok 20\n");
68is ("o\000 \0001\000" ^ "\000k\0002\000\n", "ok 21\n");
69
70#
71is ("ok \x{FF}\x{FF}\n" & "ok 22\n", "ok 22\n");
72is ("ok 23\n" | "ok \x{0}\x{0}\n", "ok 23\n");
73is ("o\x{0} \x{0}4\x{0}" ^ "\x{0}k\x{0}2\x{0}\n", "ok 24\n");
74
75#
76is (sprintf("%vd", v4095 & v801), 801);
77is (sprintf("%vd", v4095 | v801), 4095);
78is (sprintf("%vd", v4095 ^ v801), 3294);
79
80#
81is (sprintf("%vd", v4095.801.4095 & v801.4095), '801.801');
82is (sprintf("%vd", v4095.801.4095 | v801.4095), '4095.4095.4095');
83is (sprintf("%vd", v801.4095 ^ v4095.801.4095), '3294.3294.4095');
84#
85is (sprintf("%vd", v120.300 & v200.400), '72.256');
86is (sprintf("%vd", v120.300 | v200.400), '248.444');
87is (sprintf("%vd", v120.300 ^ v200.400), '176.188');
88#
89my $a = v120.300;
90my $b = v200.400;
91$a ^= $b;
92is (sprintf("%vd", $a), '176.188');
93my $a = v120.300;
94my $b = v200.400;
95$a |= $b;
96is (sprintf("%vd", $a), '248.444');
97
98#
99# UTF8 ~ behaviour
100#
101
102my $Is_EBCDIC = (ord('A') == 193) ? 1 : 0;
103
104my @not36;
105
106for (0x100...0xFFF) {
107  $a = ~(chr $_);
108  if ($Is_EBCDIC) {
109      push @not36, sprintf("%#03X", $_)
110          if $a ne chr(~$_) or length($a) != 1;
111  }
112  else {
113      push @not36, sprintf("%#03X", $_)
114          if $a ne chr(~$_) or length($a) != 1 or ~$a ne chr($_);
115  }
116}
117is (join (', ', @not36), '');
118
119my @not37;
120
121for my $i (0xEEE...0xF00) {
122  for my $j (0x0..0x120) {
123    $a = ~(chr ($i) . chr $j);
124    if ($Is_EBCDIC) {
125        push @not37, sprintf("%#03X %#03X", $i, $j)
126	    if $a ne chr(~$i).chr(~$j) or
127	       length($a) != 2;
128    }
129    else {
130        push @not37, sprintf("%#03X %#03X", $i, $j)
131	    if $a ne chr(~$i).chr(~$j) or
132	       length($a) != 2 or
133               ~$a ne chr($i).chr($j);
134    }
135  }
136}
137is (join (', ', @not37), '');
138
139SKIP: {
140  skip "EBCDIC" if $Is_EBCDIC;
141  is (~chr(~0), "\0");
142}
143
144
145my @not39;
146
147for 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));
151    }
152}
153is (join (', ', @not39), '');
154
155my @not40;
156
157for 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));
161    }
162}
163is (join (', ', @not40), '');
164
165
166# More variations on 19 and 22.
167is ("ok \xFF\x{FF}\n" & "ok 41\n", "ok 41\n");
168is ("ok \x{FF}\xFF\n" & "ok 42\n", "ok 42\n");
169
170# Tests to see if you really can do casts negative floats to unsigned properly
171$neg1 = -1.0;
172ok (~ $neg1 == 0);
173$neg7 = -7.0;
174ok (~ $neg7 == 6);
175
176
177$a = "\0\x{100}"; chop($a);
178ok(utf8::is_utf8($a)); # make sure UTF8 flag is still there
179$a = ~$a;
180is($a, "\xFF", "~ works with utf-8");
181