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 | 5 |
6 class BitOperationsTest { | 6 class BitOperationsTest { |
7 static testMain() { | 7 static testMain() { |
8 for (int i = 0; i < 4; i++) { | 8 for (int i = 0; i < 4; i++) { |
9 testOne(); | 9 testOne(); |
10 } | 10 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 Expect.equals(0xF0000000000000000F, 0xF0000000000000000F7 >> 4); | 46 Expect.equals(0xF0000000000000000F, 0xF0000000000000000F7 >> 4); |
47 Expect.equals(15, 0xF00000000 >> 32); | 47 Expect.equals(15, 0xF00000000 >> 32); |
48 Expect.equals(1030792151040, 16492674416655 >> 4); | 48 Expect.equals(1030792151040, 16492674416655 >> 4); |
49 | 49 |
50 Expect.equals(0xF0000000000000000F0, 0xF0000000000000000F << 4); | 50 Expect.equals(0xF0000000000000000F0, 0xF0000000000000000F << 4); |
51 Expect.equals(0xF00000000, 15 << 32); | 51 Expect.equals(0xF00000000, 15 << 32); |
52 | 52 |
53 TestNegativeValueShifts(); | 53 TestNegativeValueShifts(); |
54 TestPositiveValueShifts(); | 54 TestPositiveValueShifts(); |
55 TestNoMaskingOfShiftCount(); | 55 TestNoMaskingOfShiftCount(); |
| 56 TestNegativeCountShifts(); |
| 57 } |
| 58 |
| 59 static void TestNegativeCountShifts() { |
| 60 bool throwOnLeft(a, b) { |
| 61 try { |
| 62 var x = a << b; |
| 63 return false; |
| 64 } catch(var e) { |
| 65 return true; |
| 66 } |
| 67 } |
| 68 |
| 69 bool throwOnRight(a, b) { |
| 70 try { |
| 71 var x = a >> b; |
| 72 return false; |
| 73 } catch(var e) { |
| 74 return true; |
| 75 } |
| 76 } |
| 77 |
| 78 Expect.isTrue(throwOnLeft(12, -3)); |
| 79 Expect.isTrue(throwOnRight(12, -3)); |
| 80 for (int i = 0; i < 4000; i++) { |
| 81 Expect.isFalse(throwOnLeft(12, 3)); |
| 82 Expect.isFalse(throwOnRight(12, 3)); |
| 83 } |
56 } | 84 } |
57 | 85 |
58 static void TestNegativeValueShifts() { | 86 static void TestNegativeValueShifts() { |
59 for (int value = 0; value > -100; value--) { | 87 for (int value = 0; value > -100; value--) { |
60 for (int i = 0; i < 300; i++) { | 88 for (int i = 0; i < 300; i++) { |
61 int b = (value << i) >> i; | 89 int b = (value << i) >> i; |
62 Expect.equals(value, b); | 90 Expect.equals(value, b); |
63 } | 91 } |
64 } | 92 } |
65 } | 93 } |
(...skipping 24 matching lines...) Expand all Loading... |
90 } | 118 } |
91 } | 119 } |
92 | 120 |
93 static int ShiftLeft(int a, int b) { return a << b; } | 121 static int ShiftLeft(int a, int b) { return a << b; } |
94 static int ShiftRight(int a, int b) { return a >> b; } | 122 static int ShiftRight(int a, int b) { return a >> b; } |
95 } | 123 } |
96 | 124 |
97 main() { | 125 main() { |
98 BitOperationsTest.testMain(); | 126 BitOperationsTest.testMain(); |
99 } | 127 } |
OLD | NEW |