| 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 | 5 // VMOptions=--optimization-counter-threshold=10 --no-use-osr |
| 6 | 6 |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 | 8 |
| 9 class BitOperationsTest { | 9 void main() { |
| 10 static testMain() { | 10 for (int i = 0; i < 4; i++) { |
| 11 for (int i = 0; i < 4; i++) { | 11 test(); |
| 12 testOne(); | |
| 13 } | |
| 14 } | 12 } |
| 15 static testOne() { | 13 } |
| 16 Expect.equals(3, (3 & 7)); | |
| 17 Expect.equals(7, (3 | 7)); | |
| 18 Expect.equals(4, (3 ^ 7)); | |
| 19 Expect.equals(25, (100 >> 2)); | |
| 20 Expect.equals(400, (100 << 2)); | |
| 21 Expect.equals(-25, (-100 >> 2)); | |
| 22 Expect.equals(-101, ~100); | |
| 23 Expect.equals(0x10000000000000000, 1 << 64); | |
| 24 Expect.equals(-0x10000000000000000, -1 << 64); | |
| 25 Expect.equals(0x40000000, 0x04000000 << 4); | |
| 26 Expect.equals(0x4000000000000000, 0x0400000000000000 << 4); | |
| 27 Expect.equals(0, ~-1); | |
| 28 Expect.equals(-1, ~0); | |
| 29 | 14 |
| 30 Expect.equals(0, 1 >> 160); | 15 void test() { |
| 31 Expect.equals(-1, -1 >> 160); | 16 Expect.equals(3, (3 & 7)); |
| 17 Expect.equals(7, (3 | 7)); |
| 18 Expect.equals(4, (3 ^ 7)); |
| 19 Expect.equals(25, (100 >> 2)); |
| 20 Expect.equals(400, (100 << 2)); |
| 21 Expect.equals(-25, (-100 >> 2)); |
| 22 Expect.equals(-101, ~100); |
| 23 Expect.equals(0x10000000000000000, 1 << 64); |
| 24 Expect.equals(-0x10000000000000000, -1 << 64); |
| 25 Expect.equals(0x40000000, 0x04000000 << 4); |
| 26 Expect.equals(0x4000000000000000, 0x0400000000000000 << 4); |
| 27 Expect.equals(0, ~-1); |
| 28 Expect.equals(-1, ~0); |
| 32 | 29 |
| 33 Expect.equals(0x100000000000000001, | 30 Expect.equals(0, 1 >> 160); |
| 34 0x100000000000000001 & 0x100000100F00000001); | 31 Expect.equals(-1, -1 >> 160); |
| 35 Expect.equals(0x1, 0x1 & 0x100000100F00000001); | |
| 36 Expect.equals(0x1, 0x100000100F00000001 & 0x1); | |
| 37 | 32 |
| 38 Expect.equals(0x100000100F00000001, | 33 Expect.equals(0x100000000000000001, |
| 39 0x100000000000000001 | 0x100000100F00000001); | 34 0x100000000000000001 & 0x100000100F00000001); |
| 40 Expect.equals(0x100000100F00000011, 0x11 | 0x100000100F00000001); | 35 Expect.equals(0x1, 0x1 & 0x100000100F00000001); |
| 41 Expect.equals(0x100000100F00000011, 0x100000100F00000001 | 0x11); | 36 Expect.equals(0x1, 0x100000100F00000001 & 0x1); |
| 42 | 37 |
| 43 Expect.equals(0x0F000F00000000000000, | 38 Expect.equals(0x100000100F00000001, |
| 44 0x0F00F00000000000001 ^ 0xFF00000000000000001); | 39 0x100000000000000001 | 0x100000100F00000001); |
| 45 Expect.equals(0x31, 0xF00F00000000000001 ^ 0xF00F00000000000030); | 40 Expect.equals(0x100000100F00000011, 0x11 | 0x100000100F00000001); |
| 46 Expect.equals(0xF00F00000000000031, 0xF00F00000000000001 ^ 0x30); | 41 Expect.equals(0x100000100F00000011, 0x100000100F00000001 | 0x11); |
| 47 Expect.equals(0xF00F00000000000031, 0x30 ^ 0xF00F00000000000001); | |
| 48 | 42 |
| 49 Expect.equals(0xF0000000000000000F, 0xF0000000000000000F7 >> 4); | 43 Expect.equals(0x0F000F00000000000000, |
| 50 Expect.equals(15, 0xF00000000 >> 32); | 44 0x0F00F00000000000001 ^ 0xFF00000000000000001); |
| 51 Expect.equals(1030792151040, 16492674416655 >> 4); | 45 Expect.equals(0x31, 0xF00F00000000000001 ^ 0xF00F00000000000030); |
| 46 Expect.equals(0xF00F00000000000031, 0xF00F00000000000001 ^ 0x30); |
| 47 Expect.equals(0xF00F00000000000031, 0x30 ^ 0xF00F00000000000001); |
| 52 | 48 |
| 53 Expect.equals(0xF0000000000000000F0, 0xF0000000000000000F << 4); | 49 Expect.equals(0xF0000000000000000F, 0xF0000000000000000F7 >> 4); |
| 54 Expect.equals(0xF00000000, 15 << 32); | 50 Expect.equals(15, 0xF00000000 >> 32); |
| 51 Expect.equals(1030792151040, 16492674416655 >> 4); |
| 55 | 52 |
| 56 TestNegativeValueShifts(); | 53 Expect.equals(0xF0000000000000000F0, 0xF0000000000000000F << 4); |
| 57 TestPositiveValueShifts(); | 54 Expect.equals(0xF00000000, 15 << 32); |
| 58 TestNoMaskingOfShiftCount(); | 55 |
| 59 TestNegativeCountShifts(); | 56 testNegativeValueShifts(); |
| 60 for (int i = 0; i < 20; i++) { | 57 testPositiveValueShifts(); |
| 61 TestCornerCasesRightShifts(); | 58 testNoMaskingOfShiftCount(); |
| 62 TestRightShift64Bit(); | 59 testNegativeCountShifts(); |
| 63 TestLeftShift64Bit(); | 60 for (int i = 0; i < 20; i++) { |
| 64 TestLeftShift64BitWithOverflow1(); | 61 testCornerCasesRightShifts(); |
| 65 TestLeftShift64BitWithOverflow2(); | 62 testRightShift64Bit(); |
| 66 TestLeftShift64BitWithOverflow3(); | 63 testLeftShift64Bit(); |
| 64 testLeftShift64BitWithOverflow1(); |
| 65 testLeftShift64BitWithOverflow2(); |
| 66 testLeftShift64BitWithOverflow3(); |
| 67 } |
| 68 |
| 69 // Test precedence. |
| 70 testPrecedence(4,5,3,1); |
| 71 testPrecedence(3,4,5,9); |
| 72 testPrecedence(0x5c71, 0x6b92, 0x7654, 0x7d28); |
| 73 } |
| 74 |
| 75 void testCornerCasesRightShifts() { |
| 76 var v32 = 0xFF000000; |
| 77 var v64 = 0xFF00000000000000; |
| 78 Expect.equals(0x3, v32 >> 0x1E); |
| 79 Expect.equals(0x1, v32 >> 0x1F); |
| 80 Expect.equals(0x0, v32 >> 0x20); |
| 81 Expect.equals(0x3, v64 >> 0x3E); |
| 82 Expect.equals(0x1, v64 >> 0x3F); |
| 83 Expect.equals(0x0, v64 >> 0x40); |
| 84 } |
| 85 |
| 86 void testRightShift64Bit() { |
| 87 var t = 0x1ffffffff; |
| 88 Expect.equals(0xffffffff, t >> 1); |
| 89 } |
| 90 |
| 91 void testLeftShift64Bit() { |
| 92 var t = 0xffffffff; |
| 93 Expect.equals(0xffffffff, t << 0); |
| 94 Expect.equals(0x1fffffffe, t << 1); |
| 95 Expect.equals(0x7fffffff80000000, t << 31); |
| 96 Expect.equals(0x10000000000000000, 2*(t+1) << 31); |
| 97 Expect.equals(0x20000000000000000, 4*(t+1) << 31); |
| 98 Expect.equals(0x8000000000000000, (t+1) << 31); |
| 99 } |
| 100 |
| 101 void testLeftShift64BitWithOverflow1() { |
| 102 var t = 0xffffffff; |
| 103 Expect.equals(0x10000000000000000, 2*(t+1) << 31); |
| 104 } |
| 105 |
| 106 void testLeftShift64BitWithOverflow2() { |
| 107 var t = 0xffffffff; |
| 108 Expect.equals(0x20000000000000000, 4*(t+1) << 31); |
| 109 } |
| 110 |
| 111 void testLeftShift64BitWithOverflow3() { |
| 112 var t = 0xffffffff; |
| 113 Expect.equals(0x8000000000000000, (t+1) << 31); |
| 114 } |
| 115 |
| 116 void testNegativeCountShifts() { |
| 117 bool throwOnLeft(a, b) { |
| 118 try { |
| 119 var x = a << b; |
| 120 return false; |
| 121 } catch (e) { |
| 122 return true; |
| 67 } | 123 } |
| 68 } | 124 } |
| 69 | 125 |
| 70 static void TestCornerCasesRightShifts() { | 126 bool throwOnRight(a, b) { |
| 71 var v32 = 0xFF000000; | 127 try { |
| 72 var v64 = 0xFF00000000000000; | 128 var x = a >> b; |
| 73 Expect.equals(0x3, v32 >> 0x1E); | 129 return false; |
| 74 Expect.equals(0x1, v32 >> 0x1F); | 130 } catch (e) { |
| 75 Expect.equals(0x0, v32 >> 0x20); | 131 return true; |
| 76 Expect.equals(0x3, v64 >> 0x3E); | |
| 77 Expect.equals(0x1, v64 >> 0x3F); | |
| 78 Expect.equals(0x0, v64 >> 0x40); | |
| 79 } | |
| 80 | |
| 81 static void TestRightShift64Bit() { | |
| 82 var t = 0x1ffffffff; | |
| 83 Expect.equals(0xffffffff, t >> 1); | |
| 84 } | |
| 85 | |
| 86 static void TestLeftShift64Bit() { | |
| 87 var t = 0xffffffff; | |
| 88 Expect.equals(0xffffffff, t << 0); | |
| 89 Expect.equals(0x1fffffffe, t << 1); | |
| 90 Expect.equals(0x7fffffff80000000, t << 31); | |
| 91 Expect.equals(0x10000000000000000, 2*(t+1) << 31); | |
| 92 Expect.equals(0x20000000000000000, 4*(t+1) << 31); | |
| 93 Expect.equals(0x8000000000000000, (t+1) << 31); | |
| 94 } | |
| 95 | |
| 96 static void TestLeftShift64BitWithOverflow1() { | |
| 97 var t = 0xffffffff; | |
| 98 Expect.equals(0x10000000000000000, 2*(t+1) << 31); | |
| 99 } | |
| 100 | |
| 101 static void TestLeftShift64BitWithOverflow2() { | |
| 102 var t = 0xffffffff; | |
| 103 Expect.equals(0x20000000000000000, 4*(t+1) << 31); | |
| 104 } | |
| 105 | |
| 106 static void TestLeftShift64BitWithOverflow3() { | |
| 107 var t = 0xffffffff; | |
| 108 Expect.equals(0x8000000000000000, (t+1) << 31); | |
| 109 } | |
| 110 | |
| 111 static void TestNegativeCountShifts() { | |
| 112 bool throwOnLeft(a, b) { | |
| 113 try { | |
| 114 var x = a << b; | |
| 115 return false; | |
| 116 } catch (e) { | |
| 117 return true; | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 bool throwOnRight(a, b) { | |
| 122 try { | |
| 123 var x = a >> b; | |
| 124 return false; | |
| 125 } catch (e) { | |
| 126 return true; | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 Expect.isTrue(throwOnLeft(12, -3)); | |
| 131 Expect.isTrue(throwOnRight(12, -3)); | |
| 132 for (int i = 0; i < 20; i++) { | |
| 133 Expect.isFalse(throwOnLeft(12, 3)); | |
| 134 Expect.isFalse(throwOnRight(12, 3)); | |
| 135 } | 132 } |
| 136 } | 133 } |
| 137 | 134 |
| 138 static void TestNegativeValueShifts() { | 135 Expect.isTrue(throwOnLeft(12, -3)); |
| 139 for (int value = 0; value > -100; value--) { | 136 Expect.isTrue(throwOnRight(12, -3)); |
| 140 for (int i = 0; i < 300; i++) { | 137 for (int i = 0; i < 20; i++) { |
| 141 int b = (value << i) >> i; | 138 Expect.isFalse(throwOnLeft(12, 3)); |
| 142 Expect.equals(value, b); | 139 Expect.isFalse(throwOnRight(12, 3)); |
| 143 } | 140 } |
| 141 } |
| 142 |
| 143 void testNegativeValueShifts() { |
| 144 for (int value = 0; value > -100; value--) { |
| 145 for (int i = 0; i < 300; i++) { |
| 146 int b = (value << i) >> i; |
| 147 Expect.equals(value, b); |
| 144 } | 148 } |
| 145 } | 149 } |
| 150 } |
| 146 | 151 |
| 147 static void TestPositiveValueShifts() { | 152 void testPositiveValueShifts() { |
| 148 for (int value = 0; value < 100; value++) { | 153 for (int value = 0; value < 100; value++) { |
| 149 for (int i = 0; i < 300; i++) { | 154 for (int i = 0; i < 300; i++) { |
| 150 int b = (value << i) >> i; | 155 int b = (value << i) >> i; |
| 151 Expect.equals(value, b); | 156 Expect.equals(value, b); |
| 152 } | |
| 153 } | 157 } |
| 154 } | 158 } |
| 155 | |
| 156 static void TestNoMaskingOfShiftCount() { | |
| 157 // Shifts which would behave differently if shift count was masked into a | |
| 158 // range. | |
| 159 Expect.equals(0, 0 >> 256); | |
| 160 Expect.equals(0, 1 >> 256); | |
| 161 Expect.equals(0, 2 >> 256); | |
| 162 Expect.equals(0, ShiftRight(0, 256)); | |
| 163 Expect.equals(0, ShiftRight(1, 256)); | |
| 164 Expect.equals(0, ShiftRight(2, 256)); | |
| 165 | |
| 166 for (int shift = 1; shift <= 256; shift++) { | |
| 167 Expect.equals(0, ShiftRight(1, shift)); | |
| 168 Expect.equals(-1, ShiftRight(-1, shift)); | |
| 169 Expect.equals(true, ShiftLeft(1, shift) > ShiftLeft(1, shift - 1)); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 static int ShiftLeft(int a, int b) { return a << b; } | |
| 174 static int ShiftRight(int a, int b) { return a >> b; } | |
| 175 } | 159 } |
| 176 | 160 |
| 177 main() { | 161 void testNoMaskingOfShiftCount() { |
| 178 BitOperationsTest.testMain(); | 162 // Shifts which would behave differently if shift count was masked into a |
| 163 // range. |
| 164 Expect.equals(0, 0 >> 256); |
| 165 Expect.equals(0, 1 >> 256); |
| 166 Expect.equals(0, 2 >> 256); |
| 167 Expect.equals(0, shiftRight(0, 256)); |
| 168 Expect.equals(0, shiftRight(1, 256)); |
| 169 Expect.equals(0, shiftRight(2, 256)); |
| 170 |
| 171 for (int shift = 1; shift <= 256; shift++) { |
| 172 Expect.equals(0, shiftRight(1, shift)); |
| 173 Expect.equals(-1, shiftRight(-1, shift)); |
| 174 Expect.equals(true, shiftLeft(1, shift) > shiftLeft(1, shift - 1)); |
| 175 } |
| 179 } | 176 } |
| 177 |
| 178 int shiftLeft(int a, int b) { return a << b; } |
| 179 int shiftRight(int a, int b) { return a >> b; } |
| 180 |
| 181 void testPrecedence(int a, int b, int c, int d) { |
| 182 // & binds stronger than ^, which binds stronger than |. |
| 183 int result = a & b ^ c | d & b ^ c; |
| 184 Expect.equals(((a & b) ^ c) | ((d & b) ^ c), result); // &^| |
| 185 Expect.notEquals((a & (b ^ c)) | (d & (b ^ c)), result); // ^&| |
| 186 Expect.notEquals((a & b) ^ (c | (d & b)) ^ c, result); // &|^ |
| 187 Expect.notEquals((a & b) ^ ((c | d) & b) ^ c, result); // |&^ |
| 188 Expect.notEquals(a & (b ^ (c | d)) & (b ^ c), result); // |^& |
| 189 Expect.notEquals(a & ((b ^ c) | d) & (b ^ c), result); // ^|& |
| 190 // Binds stronger than relational operators. |
| 191 Expect.equals((a & b) < (c & d), a & b < c & d); |
| 192 // Binds weaker than shift operators. |
| 193 Expect.equals((a & (b << c)) ^ d, a & b << c ^ d); |
| 194 Expect.notEquals((a & b) << (c ^ d), a & b << c ^ d); |
| 195 } |
| OLD | NEW |