OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Dart test for testing bitwise operations. | 4 // Dart test for testing bitwise operations. |
5 // VMOptions=--optimization-counter-threshold=10 --no-use-osr --no-background-co
mpilation | 5 // VMOptions=--optimization-counter-threshold=10 --no-use-osr --no-background-co
mpilation |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 | 8 |
9 void main() { | 9 void main() { |
10 for (int i = 0; i < 4; i++) { | 10 for (int i = 0; i < 4; i++) { |
11 test(); | 11 test(); |
12 } | 12 } |
13 } | 13 } |
14 | 14 |
15 void test() { | 15 void test() { |
16 Expect.equals(3, (3 & 7)); | 16 Expect.equals(3, (3 & 7)); |
17 Expect.equals(7, (3 | 7)); | 17 Expect.equals(7, (3 | 7)); |
18 Expect.equals(4, (3 ^ 7)); | 18 Expect.equals(4, (3 ^ 7)); |
19 Expect.equals(25, (100 >> 2)); | 19 Expect.equals(25, (100 >> 2)); |
20 Expect.equals(400, (100 << 2)); | 20 Expect.equals(400, (100 << 2)); |
21 Expect.equals(-25, (-100 >> 2)); | 21 Expect.equals(-25, (-100 >> 2)); |
22 Expect.equals(-101, ~100); | 22 Expect.equals(-101, ~100); |
23 Expect.equals(0x10000000000000000, 1 << 64); | 23 Expect.equals(0x10000000000000000, 1 << 64); |
24 Expect.equals(-0x10000000000000000, -1 << 64); | 24 Expect.equals(-0x10000000000000000, -1 << 64); |
25 Expect.equals(0x40000000, 0x04000000 << 4); | 25 Expect.equals(0x40000000, 0x04000000 << 4); |
26 Expect.equals(0x4000000000000000, 0x0400000000000000 << 4); | 26 Expect.equals(0x4000000000000000, 0x0400000000000000 << 4); |
27 Expect.equals(0, ~-1); | 27 Expect.equals(0, ~ -1); |
28 Expect.equals(-1, ~0); | 28 Expect.equals(-1, ~0); |
29 | 29 |
30 Expect.equals(0, 1 >> 160); | 30 Expect.equals(0, 1 >> 160); |
31 Expect.equals(-1, -1 >> 160); | 31 Expect.equals(-1, -1 >> 160); |
32 | 32 |
33 Expect.equals(0x100000000000000001, | 33 Expect.equals( |
34 0x100000000000000001 & 0x100000100F00000001); | 34 0x100000000000000001, 0x100000000000000001 & 0x100000100F00000001); |
35 Expect.equals(0x1, 0x1 & 0x100000100F00000001); | 35 Expect.equals(0x1, 0x1 & 0x100000100F00000001); |
36 Expect.equals(0x1, 0x100000100F00000001 & 0x1); | 36 Expect.equals(0x1, 0x100000100F00000001 & 0x1); |
37 | 37 |
38 Expect.equals(0x100000100F00000001, | 38 Expect.equals( |
39 0x100000000000000001 | 0x100000100F00000001); | 39 0x100000100F00000001, 0x100000000000000001 | 0x100000100F00000001); |
40 Expect.equals(0x100000100F00000011, 0x11 | 0x100000100F00000001); | 40 Expect.equals(0x100000100F00000011, 0x11 | 0x100000100F00000001); |
41 Expect.equals(0x100000100F00000011, 0x100000100F00000001 | 0x11); | 41 Expect.equals(0x100000100F00000011, 0x100000100F00000001 | 0x11); |
42 | 42 |
43 Expect.equals(0x0F000F00000000000000, | 43 Expect.equals( |
44 0x0F00F00000000000001 ^ 0xFF00000000000000001); | 44 0x0F000F00000000000000, 0x0F00F00000000000001 ^ 0xFF00000000000000001); |
45 Expect.equals(0x31, 0xF00F00000000000001 ^ 0xF00F00000000000030); | 45 Expect.equals(0x31, 0xF00F00000000000001 ^ 0xF00F00000000000030); |
46 Expect.equals(0xF00F00000000000031, 0xF00F00000000000001 ^ 0x30); | 46 Expect.equals(0xF00F00000000000031, 0xF00F00000000000001 ^ 0x30); |
47 Expect.equals(0xF00F00000000000031, 0x30 ^ 0xF00F00000000000001); | 47 Expect.equals(0xF00F00000000000031, 0x30 ^ 0xF00F00000000000001); |
48 | 48 |
49 Expect.equals(0xF0000000000000000F, 0xF0000000000000000F7 >> 4); | 49 Expect.equals(0xF0000000000000000F, 0xF0000000000000000F7 >> 4); |
50 Expect.equals(15, 0xF00000000 >> 32); | 50 Expect.equals(15, 0xF00000000 >> 32); |
51 Expect.equals(1030792151040, 16492674416655 >> 4); | 51 Expect.equals(1030792151040, 16492674416655 >> 4); |
52 | 52 |
53 Expect.equals(0xF0000000000000000F0, 0xF0000000000000000F << 4); | 53 Expect.equals(0xF0000000000000000F0, 0xF0000000000000000F << 4); |
54 Expect.equals(0xF00000000, 15 << 32); | 54 Expect.equals(0xF00000000, 15 << 32); |
55 | 55 |
56 testNegativeValueShifts(); | 56 testNegativeValueShifts(); |
57 testPositiveValueShifts(); | 57 testPositiveValueShifts(); |
58 testNoMaskingOfShiftCount(); | 58 testNoMaskingOfShiftCount(); |
59 testNegativeCountShifts(); | 59 testNegativeCountShifts(); |
60 for (int i = 0; i < 20; i++) { | 60 for (int i = 0; i < 20; i++) { |
61 testCornerCasesRightShifts(); | 61 testCornerCasesRightShifts(); |
62 testRightShift64Bit(); | 62 testRightShift64Bit(); |
63 testLeftShift64Bit(); | 63 testLeftShift64Bit(); |
64 testLeftShift64BitWithOverflow1(); | 64 testLeftShift64BitWithOverflow1(); |
65 testLeftShift64BitWithOverflow2(); | 65 testLeftShift64BitWithOverflow2(); |
66 testLeftShift64BitWithOverflow3(); | 66 testLeftShift64BitWithOverflow3(); |
67 } | 67 } |
68 | 68 |
69 // Test precedence. | 69 // Test precedence. |
70 testPrecedence(4,5,3,1); | 70 testPrecedence(4, 5, 3, 1); |
71 testPrecedence(3,4,5,9); | 71 testPrecedence(3, 4, 5, 9); |
72 testPrecedence(0x5c71, 0x6b92, 0x7654, 0x7d28); | 72 testPrecedence(0x5c71, 0x6b92, 0x7654, 0x7d28); |
73 } | 73 } |
74 | 74 |
75 void testCornerCasesRightShifts() { | 75 void testCornerCasesRightShifts() { |
76 var v32 = 0xFF000000; | 76 var v32 = 0xFF000000; |
77 var v64 = 0xFF00000000000000; | 77 var v64 = 0xFF00000000000000; |
78 Expect.equals(0x3, v32 >> 0x1E); | 78 Expect.equals(0x3, v32 >> 0x1E); |
79 Expect.equals(0x1, v32 >> 0x1F); | 79 Expect.equals(0x1, v32 >> 0x1F); |
80 Expect.equals(0x0, v32 >> 0x20); | 80 Expect.equals(0x0, v32 >> 0x20); |
81 Expect.equals(0x3, v64 >> 0x3E); | 81 Expect.equals(0x3, v64 >> 0x3E); |
82 Expect.equals(0x1, v64 >> 0x3F); | 82 Expect.equals(0x1, v64 >> 0x3F); |
83 Expect.equals(0x0, v64 >> 0x40); | 83 Expect.equals(0x0, v64 >> 0x40); |
84 } | 84 } |
85 | 85 |
86 void testRightShift64Bit() { | 86 void testRightShift64Bit() { |
87 var t = 0x1ffffffff; | 87 var t = 0x1ffffffff; |
88 Expect.equals(0xffffffff, t >> 1); | 88 Expect.equals(0xffffffff, t >> 1); |
89 } | 89 } |
90 | 90 |
91 void testLeftShift64Bit() { | 91 void testLeftShift64Bit() { |
92 var t = 0xffffffff; | 92 var t = 0xffffffff; |
93 Expect.equals(0xffffffff, t << 0); | 93 Expect.equals(0xffffffff, t << 0); |
94 Expect.equals(0x1fffffffe, t << 1); | 94 Expect.equals(0x1fffffffe, t << 1); |
95 Expect.equals(0x7fffffff80000000, t << 31); | 95 Expect.equals(0x7fffffff80000000, t << 31); |
96 Expect.equals(0x10000000000000000, 2*(t+1) << 31); //# 01: static type warning | 96 Expect.equals(0x10000000000000000, 2*(t+1) << 31); //# 01: static type warning |
97 Expect.equals(0x20000000000000000, 4*(t+1) << 31); //# 02: static type warning | 97 Expect.equals(0x20000000000000000, 4*(t+1) << 31); //# 02: static type warning |
98 Expect.equals(0x8000000000000000, (t+1) << 31); | 98 Expect.equals(0x8000000000000000, (t + 1) << 31); |
99 } | 99 } |
100 | 100 |
101 void testLeftShift64BitWithOverflow1() { | 101 void testLeftShift64BitWithOverflow1() { |
102 var t = 0xffffffff; | 102 var t = 0xffffffff; |
103 Expect.equals(0x10000000000000000, 2*(t+1) << 31); //# 03: static type warning | 103 Expect.equals(0x10000000000000000, 2*(t+1) << 31); //# 03: static type warning |
104 } | 104 } |
105 | 105 |
106 void testLeftShift64BitWithOverflow2() { | 106 void testLeftShift64BitWithOverflow2() { |
107 var t = 0xffffffff; | 107 var t = 0xffffffff; |
108 Expect.equals(0x20000000000000000, 4*(t+1) << 31); //# 04: static type warning | 108 Expect.equals(0x20000000000000000, 4*(t+1) << 31); //# 04: static type warning |
109 } | 109 } |
110 | 110 |
111 void testLeftShift64BitWithOverflow3() { | 111 void testLeftShift64BitWithOverflow3() { |
112 var t = 0xffffffff; | 112 var t = 0xffffffff; |
113 Expect.equals(0x8000000000000000, (t+1) << 31); | 113 Expect.equals(0x8000000000000000, (t + 1) << 31); |
114 } | 114 } |
115 | 115 |
116 void testNegativeCountShifts() { | 116 void testNegativeCountShifts() { |
117 bool throwOnLeft(a, b) { | 117 bool throwOnLeft(a, b) { |
118 try { | 118 try { |
119 var x = a << b; | 119 var x = a << b; |
120 return false; | 120 return false; |
121 } catch (e) { | 121 } catch (e) { |
122 return true; | 122 return true; |
123 } | 123 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 Expect.equals(0, shiftRight(1, 256)); | 168 Expect.equals(0, shiftRight(1, 256)); |
169 Expect.equals(0, shiftRight(2, 256)); | 169 Expect.equals(0, shiftRight(2, 256)); |
170 | 170 |
171 for (int shift = 1; shift <= 256; shift++) { | 171 for (int shift = 1; shift <= 256; shift++) { |
172 Expect.equals(0, shiftRight(1, shift)); | 172 Expect.equals(0, shiftRight(1, shift)); |
173 Expect.equals(-1, shiftRight(-1, shift)); | 173 Expect.equals(-1, shiftRight(-1, shift)); |
174 Expect.equals(true, shiftLeft(1, shift) > shiftLeft(1, shift - 1)); | 174 Expect.equals(true, shiftLeft(1, shift) > shiftLeft(1, shift - 1)); |
175 } | 175 } |
176 } | 176 } |
177 | 177 |
178 int shiftLeft(int a, int b) { return a << b; } | 178 int shiftLeft(int a, int b) { |
179 int shiftRight(int a, int b) { return a >> b; } | 179 return a << b; |
| 180 } |
| 181 |
| 182 int shiftRight(int a, int b) { |
| 183 return a >> b; |
| 184 } |
180 | 185 |
181 void testPrecedence(int a, int b, int c, int d) { | 186 void testPrecedence(int a, int b, int c, int d) { |
182 // & binds stronger than ^, which binds stronger than |. | 187 // & binds stronger than ^, which binds stronger than |. |
183 int result = a & b ^ c | d & b ^ c; | 188 int result = a & b ^ c | d & b ^ c; |
184 Expect.equals(((a & b) ^ c) | ((d & b) ^ c), result); // &^| | 189 Expect.equals(((a & b) ^ c) | ((d & b) ^ c), result); // &^| |
185 Expect.notEquals((a & (b ^ c)) | (d & (b ^ c)), result); // ^&| | 190 Expect.notEquals((a & (b ^ c)) | (d & (b ^ c)), result); // ^&| |
186 Expect.notEquals((a & b) ^ (c | (d & b)) ^ c, result); // &|^ | 191 Expect.notEquals((a & b) ^ (c | (d & b)) ^ c, result); // &|^ |
187 Expect.notEquals((a & b) ^ ((c | d) & b) ^ c, result); // |&^ | 192 Expect.notEquals((a & b) ^ ((c | d) & b) ^ c, result); // |&^ |
188 Expect.notEquals(a & (b ^ (c | d)) & (b ^ c), result); // |^& | 193 Expect.notEquals(a & (b ^ (c | d)) & (b ^ c), result); // |^& |
189 Expect.notEquals(a & ((b ^ c) | d) & (b ^ c), result); // ^|& | 194 Expect.notEquals(a & ((b ^ c) | d) & (b ^ c), result); // ^|& |
190 // Binds stronger than relational operators. | 195 // Binds stronger than relational operators. |
191 Expect.equals((a & b) < (c & d), a & b < c & d); | 196 Expect.equals((a & b) < (c & d), a & b < c & d); |
192 // Binds weaker than shift operators. | 197 // Binds weaker than shift operators. |
193 Expect.equals((a & (b << c)) ^ d, a & b << c ^ d); | 198 Expect.equals((a & (b << c)) ^ d, a & b << c ^ d); |
194 Expect.notEquals((a & b) << (c ^ d), a & b << c ^ d); | 199 Expect.notEquals((a & b) << (c ^ d), a & b << c ^ d); |
195 } | 200 } |
OLD | NEW |