Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1351)

Unified Diff: tests/language/bit_operations_test.dart

Issue 23710024: Change the order of bitwise operators and equality/relational operators. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added tests. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« tests/co19/co19-co19.status ('K') | « tests/co19/co19-co19.status ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/language/bit_operations_test.dart
diff --git a/tests/language/bit_operations_test.dart b/tests/language/bit_operations_test.dart
index dc548d16bbbce46309de0e51920eedec88183ffd..6c7b10659a887bafd6445624a4394e402bfceabd 100644
--- a/tests/language/bit_operations_test.dart
+++ b/tests/language/bit_operations_test.dart
@@ -6,174 +6,190 @@
import "package:expect/expect.dart";
-class BitOperationsTest {
- static testMain() {
- for (int i = 0; i < 4; i++) {
- testOne();
- }
- }
- static testOne() {
- Expect.equals(3, (3 & 7));
- Expect.equals(7, (3 | 7));
- Expect.equals(4, (3 ^ 7));
- Expect.equals(25, (100 >> 2));
- Expect.equals(400, (100 << 2));
- Expect.equals(-25, (-100 >> 2));
- Expect.equals(-101, ~100);
- Expect.equals(0x10000000000000000, 1 << 64);
- Expect.equals(-0x10000000000000000, -1 << 64);
- Expect.equals(0x40000000, 0x04000000 << 4);
- Expect.equals(0x4000000000000000, 0x0400000000000000 << 4);
- Expect.equals(0, ~-1);
- Expect.equals(-1, ~0);
-
- Expect.equals(0, 1 >> 160);
- Expect.equals(-1, -1 >> 160);
-
- Expect.equals(0x100000000000000001,
- 0x100000000000000001 & 0x100000100F00000001);
- Expect.equals(0x1, 0x1 & 0x100000100F00000001);
- Expect.equals(0x1, 0x100000100F00000001 & 0x1);
-
- Expect.equals(0x100000100F00000001,
- 0x100000000000000001 | 0x100000100F00000001);
- Expect.equals(0x100000100F00000011, 0x11 | 0x100000100F00000001);
- Expect.equals(0x100000100F00000011, 0x100000100F00000001 | 0x11);
-
- Expect.equals(0x0F000F00000000000000,
- 0x0F00F00000000000001 ^ 0xFF00000000000000001);
- Expect.equals(0x31, 0xF00F00000000000001 ^ 0xF00F00000000000030);
- Expect.equals(0xF00F00000000000031, 0xF00F00000000000001 ^ 0x30);
- Expect.equals(0xF00F00000000000031, 0x30 ^ 0xF00F00000000000001);
-
- Expect.equals(0xF0000000000000000F, 0xF0000000000000000F7 >> 4);
- Expect.equals(15, 0xF00000000 >> 32);
- Expect.equals(1030792151040, 16492674416655 >> 4);
-
- Expect.equals(0xF0000000000000000F0, 0xF0000000000000000F << 4);
- Expect.equals(0xF00000000, 15 << 32);
-
- TestNegativeValueShifts();
- TestPositiveValueShifts();
- TestNoMaskingOfShiftCount();
- TestNegativeCountShifts();
- for (int i = 0; i < 20; i++) {
- TestCornerCasesRightShifts();
- TestRightShift64Bit();
- TestLeftShift64Bit();
- TestLeftShift64BitWithOverflow1();
- TestLeftShift64BitWithOverflow2();
- TestLeftShift64BitWithOverflow3();
- }
+void main() {
+ for (int i = 0; i < 4; i++) {
+ test();
}
+}
- static void TestCornerCasesRightShifts() {
- var v32 = 0xFF000000;
- var v64 = 0xFF00000000000000;
- Expect.equals(0x3, v32 >> 0x1E);
- Expect.equals(0x1, v32 >> 0x1F);
- Expect.equals(0x0, v32 >> 0x20);
- Expect.equals(0x3, v64 >> 0x3E);
- Expect.equals(0x1, v64 >> 0x3F);
- Expect.equals(0x0, v64 >> 0x40);
+void test() {
+ Expect.equals(3, (3 & 7));
+ Expect.equals(7, (3 | 7));
+ Expect.equals(4, (3 ^ 7));
+ Expect.equals(25, (100 >> 2));
+ Expect.equals(400, (100 << 2));
+ Expect.equals(-25, (-100 >> 2));
+ Expect.equals(-101, ~100);
+ Expect.equals(0x10000000000000000, 1 << 64);
+ Expect.equals(-0x10000000000000000, -1 << 64);
+ Expect.equals(0x40000000, 0x04000000 << 4);
+ Expect.equals(0x4000000000000000, 0x0400000000000000 << 4);
+ Expect.equals(0, ~-1);
+ Expect.equals(-1, ~0);
+
+ Expect.equals(0, 1 >> 160);
+ Expect.equals(-1, -1 >> 160);
+
+ Expect.equals(0x100000000000000001,
+ 0x100000000000000001 & 0x100000100F00000001);
+ Expect.equals(0x1, 0x1 & 0x100000100F00000001);
+ Expect.equals(0x1, 0x100000100F00000001 & 0x1);
+
+ Expect.equals(0x100000100F00000001,
+ 0x100000000000000001 | 0x100000100F00000001);
+ Expect.equals(0x100000100F00000011, 0x11 | 0x100000100F00000001);
+ Expect.equals(0x100000100F00000011, 0x100000100F00000001 | 0x11);
+
+ Expect.equals(0x0F000F00000000000000,
+ 0x0F00F00000000000001 ^ 0xFF00000000000000001);
+ Expect.equals(0x31, 0xF00F00000000000001 ^ 0xF00F00000000000030);
+ Expect.equals(0xF00F00000000000031, 0xF00F00000000000001 ^ 0x30);
+ Expect.equals(0xF00F00000000000031, 0x30 ^ 0xF00F00000000000001);
+
+ Expect.equals(0xF0000000000000000F, 0xF0000000000000000F7 >> 4);
+ Expect.equals(15, 0xF00000000 >> 32);
+ Expect.equals(1030792151040, 16492674416655 >> 4);
+
+ Expect.equals(0xF0000000000000000F0, 0xF0000000000000000F << 4);
+ Expect.equals(0xF00000000, 15 << 32);
+
+ testNegativeValueShifts();
+ testPositiveValueShifts();
+ testNoMaskingOfShiftCount();
+ testNegativeCountShifts();
+ for (int i = 0; i < 20; i++) {
+ testCornerCasesRightShifts();
+ testRightShift64Bit();
+ testLeftShift64Bit();
+ testLeftShift64BitWithOverflow1();
+ testLeftShift64BitWithOverflow2();
+ testLeftShift64BitWithOverflow3();
}
- static void TestRightShift64Bit() {
- var t = 0x1ffffffff;
- Expect.equals(0xffffffff, t >> 1);
- }
+ // Test precedence.
+ testPrecedence(4,5,3,1);
+ testPrecedence(3,4,5,9);
+ testPrecedence(0x5c71, 0x6b92, 0x7654, 0x7d28);
+}
- static void TestLeftShift64Bit() {
- var t = 0xffffffff;
- Expect.equals(0xffffffff, t << 0);
- Expect.equals(0x1fffffffe, t << 1);
- Expect.equals(0x7fffffff80000000, t << 31);
- Expect.equals(0x10000000000000000, 2*(t+1) << 31);
- Expect.equals(0x20000000000000000, 4*(t+1) << 31);
- Expect.equals(0x8000000000000000, (t+1) << 31);
- }
+void testCornerCasesRightShifts() {
+ var v32 = 0xFF000000;
+ var v64 = 0xFF00000000000000;
+ Expect.equals(0x3, v32 >> 0x1E);
+ Expect.equals(0x1, v32 >> 0x1F);
+ Expect.equals(0x0, v32 >> 0x20);
+ Expect.equals(0x3, v64 >> 0x3E);
+ Expect.equals(0x1, v64 >> 0x3F);
+ Expect.equals(0x0, v64 >> 0x40);
+}
- static void TestLeftShift64BitWithOverflow1() {
- var t = 0xffffffff;
- Expect.equals(0x10000000000000000, 2*(t+1) << 31);
- }
+void testRightShift64Bit() {
+ var t = 0x1ffffffff;
+ Expect.equals(0xffffffff, t >> 1);
+}
- static void TestLeftShift64BitWithOverflow2() {
- var t = 0xffffffff;
- Expect.equals(0x20000000000000000, 4*(t+1) << 31);
- }
+void testLeftShift64Bit() {
+ var t = 0xffffffff;
+ Expect.equals(0xffffffff, t << 0);
+ Expect.equals(0x1fffffffe, t << 1);
+ Expect.equals(0x7fffffff80000000, t << 31);
+ Expect.equals(0x10000000000000000, 2*(t+1) << 31);
+ Expect.equals(0x20000000000000000, 4*(t+1) << 31);
+ Expect.equals(0x8000000000000000, (t+1) << 31);
+}
- static void TestLeftShift64BitWithOverflow3() {
- var t = 0xffffffff;
- Expect.equals(0x8000000000000000, (t+1) << 31);
- }
+void testLeftShift64BitWithOverflow1() {
+ var t = 0xffffffff;
+ Expect.equals(0x10000000000000000, 2*(t+1) << 31);
+}
- static void TestNegativeCountShifts() {
- bool throwOnLeft(a, b) {
- try {
- var x = a << b;
- return false;
- } catch (e) {
- return true;
- }
- }
+void testLeftShift64BitWithOverflow2() {
+ var t = 0xffffffff;
+ Expect.equals(0x20000000000000000, 4*(t+1) << 31);
+}
- bool throwOnRight(a, b) {
- try {
- var x = a >> b;
- return false;
- } catch (e) {
- return true;
- }
- }
+void testLeftShift64BitWithOverflow3() {
+ var t = 0xffffffff;
+ Expect.equals(0x8000000000000000, (t+1) << 31);
+}
- Expect.isTrue(throwOnLeft(12, -3));
- Expect.isTrue(throwOnRight(12, -3));
- for (int i = 0; i < 20; i++) {
- Expect.isFalse(throwOnLeft(12, 3));
- Expect.isFalse(throwOnRight(12, 3));
+void testNegativeCountShifts() {
+ bool throwOnLeft(a, b) {
+ try {
+ var x = a << b;
+ return false;
+ } catch (e) {
+ return true;
}
}
- static void TestNegativeValueShifts() {
- for (int value = 0; value > -100; value--) {
- for (int i = 0; i < 300; i++) {
- int b = (value << i) >> i;
- Expect.equals(value, b);
- }
+ bool throwOnRight(a, b) {
+ try {
+ var x = a >> b;
+ return false;
+ } catch (e) {
+ return true;
}
}
- static void TestPositiveValueShifts() {
- for (int value = 0; value < 100; value++) {
- for (int i = 0; i < 300; i++) {
- int b = (value << i) >> i;
- Expect.equals(value, b);
- }
+ Expect.isTrue(throwOnLeft(12, -3));
+ Expect.isTrue(throwOnRight(12, -3));
+ for (int i = 0; i < 20; i++) {
+ Expect.isFalse(throwOnLeft(12, 3));
+ Expect.isFalse(throwOnRight(12, 3));
+ }
+}
+
+void testNegativeValueShifts() {
+ for (int value = 0; value > -100; value--) {
+ for (int i = 0; i < 300; i++) {
+ int b = (value << i) >> i;
+ Expect.equals(value, b);
}
}
+}
- static void TestNoMaskingOfShiftCount() {
- // Shifts which would behave differently if shift count was masked into a
- // range.
- Expect.equals(0, 0 >> 256);
- Expect.equals(0, 1 >> 256);
- Expect.equals(0, 2 >> 256);
- Expect.equals(0, ShiftRight(0, 256));
- Expect.equals(0, ShiftRight(1, 256));
- Expect.equals(0, ShiftRight(2, 256));
-
- for (int shift = 1; shift <= 256; shift++) {
- Expect.equals(0, ShiftRight(1, shift));
- Expect.equals(-1, ShiftRight(-1, shift));
- Expect.equals(true, ShiftLeft(1, shift) > ShiftLeft(1, shift - 1));
+void testPositiveValueShifts() {
+ for (int value = 0; value < 100; value++) {
+ for (int i = 0; i < 300; i++) {
+ int b = (value << i) >> i;
+ Expect.equals(value, b);
}
}
+}
- static int ShiftLeft(int a, int b) { return a << b; }
- static int ShiftRight(int a, int b) { return a >> b; }
+void testNoMaskingOfShiftCount() {
+ // Shifts which would behave differently if shift count was masked into a
+ // range.
+ Expect.equals(0, 0 >> 256);
+ Expect.equals(0, 1 >> 256);
+ Expect.equals(0, 2 >> 256);
+ Expect.equals(0, shiftRight(0, 256));
+ Expect.equals(0, shiftRight(1, 256));
+ Expect.equals(0, shiftRight(2, 256));
+
+ for (int shift = 1; shift <= 256; shift++) {
+ Expect.equals(0, shiftRight(1, shift));
+ Expect.equals(-1, shiftRight(-1, shift));
+ Expect.equals(true, shiftLeft(1, shift) > shiftLeft(1, shift - 1));
+ }
}
-main() {
- BitOperationsTest.testMain();
+int shiftLeft(int a, int b) { return a << b; }
+int shiftRight(int a, int b) { return a >> b; }
+
+void testPrecedence(int a, int b, int c, int d) {
+ // & binds stronger than ^, which binds stronger than |.
+ int result = a & b ^ c | d & b ^ c;
+ Expect.equals(((a & b) ^ c) | ((d & b) ^ c), result); // &^|
+ Expect.notEquals((a & (b ^ c)) | (d & (b ^ c)), result); // ^&|
+ Expect.notEquals((a & b) ^ (c | (d & b)) ^ c, result); // &|^
+ Expect.notEquals((a & b) ^ ((c | d) & b) ^ c, result); // |&^
+ Expect.notEquals(a & (b ^ (c | d)) & (b ^ c), result); // |^&
+ Expect.notEquals(a & ((b ^ c) | d) & (b ^ c), result); // ^|&
+ // Binds stronger than relational operators.
+ Expect.equals((a & b) < (c & d), a & b < c & d);
+ // Binds weaker than shift operators.
+ Expect.equals((a & (b << c)) ^ d, a & b << c ^ d);
+ Expect.notEquals((a & b) << (c ^ d), a & b << c ^ d);
}
« tests/co19/co19-co19.status ('K') | « tests/co19/co19-co19.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698