| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // Dart test for testing bitwise operations. | |
| 5 // VMOptions=--optimization-counter-threshold=10 --no-use-osr | |
| 6 | |
| 7 import "package:expect/expect.dart"; | |
| 8 | |
| 9 void main() { | |
| 10 for (int i = 0; i < 4; i++) { | |
| 11 test(); | |
| 12 } | |
| 13 } | |
| 14 | |
| 15 void test() { | |
| 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 | |
| 30 Expect.equals(0, 1 >> 160); | |
| 31 Expect.equals(-1, -1 >> 160); | |
| 32 | |
| 33 Expect.equals( | |
| 34 0x100000000000000001, 0x100000000000000001 & 0x100000100F00000001); | |
| 35 Expect.equals(0x1, 0x1 & 0x100000100F00000001); | |
| 36 Expect.equals(0x1, 0x100000100F00000001 & 0x1); | |
| 37 | |
| 38 Expect.equals( | |
| 39 0x100000100F00000001, 0x100000000000000001 | 0x100000100F00000001); | |
| 40 Expect.equals(0x100000100F00000011, 0x11 | 0x100000100F00000001); | |
| 41 Expect.equals(0x100000100F00000011, 0x100000100F00000001 | 0x11); | |
| 42 | |
| 43 Expect.equals( | |
| 44 0x0F000F00000000000000, 0x0F00F00000000000001 ^ 0xFF00000000000000001); | |
| 45 Expect.equals(0x31, 0xF00F00000000000001 ^ 0xF00F00000000000030); | |
| 46 Expect.equals(0xF00F00000000000031, 0xF00F00000000000001 ^ 0x30); | |
| 47 Expect.equals(0xF00F00000000000031, 0x30 ^ 0xF00F00000000000001); | |
| 48 | |
| 49 Expect.equals(0xF0000000000000000F, 0xF0000000000000000F7 >> 4); | |
| 50 Expect.equals(15, 0xF00000000 >> 32); | |
| 51 Expect.equals(1030792151040, 16492674416655 >> 4); | |
| 52 | |
| 53 Expect.equals(0xF0000000000000000F0, 0xF0000000000000000F << 4); | |
| 54 Expect.equals(0xF00000000, 15 << 32); | |
| 55 | |
| 56 testNegativeValueShifts(); | |
| 57 testPositiveValueShifts(); | |
| 58 testNoMaskingOfShiftCount(); | |
| 59 testNegativeCountShifts(); | |
| 60 for (int i = 0; i < 20; i++) { | |
| 61 testCornerCasesRightShifts(); | |
| 62 testRightShift64Bit(); | |
| 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); //# 01: static type warning | |
| 97 Expect.equals(0x20000000000000000, 4*(t+1) << 31); //# 02: static type warning | |
| 98 Expect.equals(0x8000000000000000, (t + 1) << 31); | |
| 99 } | |
| 100 | |
| 101 void testLeftShift64BitWithOverflow1() { | |
| 102 var t = 0xffffffff; | |
| 103 Expect.equals(0x10000000000000000, 2*(t+1) << 31); //# 03: static type warning | |
| 104 } | |
| 105 | |
| 106 void testLeftShift64BitWithOverflow2() { | |
| 107 var t = 0xffffffff; | |
| 108 Expect.equals(0x20000000000000000, 4*(t+1) << 31); //# 04: static type warning | |
| 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; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 bool throwOnRight(a, b) { | |
| 127 try { | |
| 128 var x = a >> b; | |
| 129 return false; | |
| 130 } catch (e) { | |
| 131 return true; | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 Expect.isTrue(throwOnLeft(12, -3)); | |
| 136 Expect.isTrue(throwOnRight(12, -3)); | |
| 137 for (int i = 0; i < 20; i++) { | |
| 138 Expect.isFalse(throwOnLeft(12, 3)); | |
| 139 Expect.isFalse(throwOnRight(12, 3)); | |
| 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); | |
| 148 } | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 void testPositiveValueShifts() { | |
| 153 for (int value = 0; value < 100; value++) { | |
| 154 for (int i = 0; i < 300; i++) { | |
| 155 int b = (value << i) >> i; | |
| 156 Expect.equals(value, b); | |
| 157 } | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 void testNoMaskingOfShiftCount() { | |
| 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 } | |
| 176 } | |
| 177 | |
| 178 int shiftLeft(int a, int b) { | |
| 179 return a << b; | |
| 180 } | |
| 181 | |
| 182 int shiftRight(int a, int b) { | |
| 183 return a >> b; | |
| 184 } | |
| 185 | |
| 186 void testPrecedence(int a, int b, int c, int d) { | |
| 187 // & binds stronger than ^, which binds stronger than |. | |
| 188 int result = a & b ^ c | d & b ^ c; | |
| 189 Expect.equals(((a & b) ^ c) | ((d & b) ^ c), result); // &^| | |
| 190 Expect.notEquals((a & (b ^ c)) | (d & (b ^ c)), result); // ^&| | |
| 191 Expect.notEquals((a & b) ^ (c | (d & b)) ^ c, result); // &|^ | |
| 192 Expect.notEquals((a & b) ^ ((c | d) & b) ^ c, result); // |&^ | |
| 193 Expect.notEquals(a & (b ^ (c | d)) & (b ^ c), result); // |^& | |
| 194 Expect.notEquals(a & ((b ^ c) | d) & (b ^ c), result); // ^|& | |
| 195 // Binds stronger than relational operators. | |
| 196 Expect.equals((a & b) < (c & d), a & b < c & d); | |
| 197 // Binds weaker than shift operators. | |
| 198 Expect.equals((a & (b << c)) ^ d, a & b << c ^ d); | |
| 199 Expect.notEquals((a & b) << (c ^ d), a & b << c ^ d); | |
| 200 } | |
| OLD | NEW |