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. This test only includes small |
5 // enough numbers that even a dart implementation with the JS exemption is | |
6 // expected to pass them. See BitOperationsVMTest for the tests that a | |
7 // fully spec compliant implementation is expected to pass. | |
5 | 8 |
6 class BitOperationsTest { | 9 class BitOperationsTest { |
7 static testMain() { | 10 static testMain() { |
8 for (int i = 0; i < 4; i++) { | 11 for (int i = 0; i < 4; i++) { |
9 testOne(); | 12 testOne(); |
10 } | 13 } |
11 } | 14 } |
12 static testOne() { | 15 static testOne() { |
13 Expect.equals(3, (3 & 7)); | 16 Expect.equals(3, (3 & 7)); |
14 Expect.equals(7, (3 | 7)); | 17 Expect.equals(7, (3 | 7)); |
15 Expect.equals(4, (3 ^ 7)); | 18 Expect.equals(4, (3 ^ 7)); |
16 Expect.equals(25, (100 >> 2)); | 19 Expect.equals(25, (100 >> 2)); |
17 Expect.equals(400, (100 << 2)); | 20 Expect.equals(400, (100 << 2)); |
18 Expect.equals(-25, (-100 >> 2)); | 21 Expect.equals(-25, (-100 >> 2)); |
19 Expect.equals(-101, ~100); | 22 Expect.equals(-101, ~100); |
20 Expect.equals(0x10000000000000000, 1 << 64); | 23 Expect.equals(0x10000000, 1 << 28); |
Ivan Posva
2012/01/13 00:54:25
Isn't the range representable in bit operations in
| |
21 Expect.equals(-0x10000000000000000, -1 << 64); | 24 Expect.equals(-0x10000000, -1 << 28); |
22 Expect.equals(0x40000000, 0x04000000 << 4); | 25 Expect.equals(0x40000000, 0x04000000 << 4); |
23 Expect.equals(0x4000000000000000, 0x0400000000000000 << 4); | 26 Expect.equals(0x4000000, 0x0400000 << 4); |
24 Expect.equals(0, ~-1); | 27 Expect.equals(0, ~-1); |
25 Expect.equals(-1, ~0); | 28 Expect.equals(-1, ~0); |
26 | 29 |
27 Expect.equals(0, 1 >> 160); | 30 Expect.equals(0x10100F01, 0x10000001 | 0x10100F01); |
28 Expect.equals(-1, -1 >> 160); | 31 Expect.equals(0x10100F11, 0x11 | 0x10100F01); |
32 Expect.equals(0x10100F11, 0x10100F01 | 0x11); | |
29 | 33 |
30 Expect.equals(0x100000000000000001, | 34 Expect.equals(0x0F000F0, |
31 0x100000000000000001 & 0x100000100F00000001); | 35 0x0F00F1 ^ 0xFF0001); |
32 Expect.equals(0x1, 0x1 & 0x100000100F00000001); | |
33 Expect.equals(0x1, 0x100000100F00000001 & 0x1); | |
34 | 36 |
35 Expect.equals(0x100000100F00000001, | 37 Expect.equals(0x31, 0xF0F0001 ^ 0xF0F0030); |
36 0x100000000000000001 | 0x100000100F00000001); | 38 Expect.equals(0xF0F0031, 0xF0F0001 ^ 0x30); |
37 Expect.equals(0x100000100F00000011, 0x11 | 0x100000100F00000001); | 39 Expect.equals(0xF0F0031, 0x30 ^ 0xF0F0001); |
38 Expect.equals(0x100000100F00000011, 0x100000100F00000001 | 0x11); | |
39 | 40 |
40 Expect.equals(0x0F000F00000000000000, | 41 Expect.equals(0xF0000F, 0xF0000F7 >> 4); |
41 0x0F00F00000000000001 ^ 0xFF00000000000000001); | 42 Expect.equals(15, 0xF000000 >> 24); |
42 Expect.equals(0x31, 0xF00F00000000000001 ^ 0xF00F00000000000030); | |
43 Expect.equals(0xF00F00000000000031, 0xF00F00000000000001 ^ 0x30); | |
44 Expect.equals(0xF00F00000000000031, 0x30 ^ 0xF00F00000000000001); | |
45 | 43 |
46 Expect.equals(0xF0000000000000000F, 0xF0000000000000000F7 >> 4); | 44 Expect.equals(0xF0000F0, 0xF0000F << 4); |
47 Expect.equals(15, 0xF00000000 >> 32); | 45 Expect.equals(0xF000000, 15 << 24); |
48 Expect.equals(1030792151040, 16492674416655 >> 4); | |
49 | |
50 Expect.equals(0xF0000000000000000F0, 0xF0000000000000000F << 4); | |
51 Expect.equals(0xF00000000, 15 << 32); | |
52 | 46 |
53 TestNegativeValueShifts(); | 47 TestNegativeValueShifts(); |
54 TestPositiveValueShifts(); | 48 TestPositiveValueShifts(); |
55 TestNoMaskingOfShiftCount(); | 49 TestNoMaskingOfShiftCount(); |
56 } | 50 } |
57 | 51 |
58 static void TestNegativeValueShifts() { | 52 static void TestNegativeValueShifts() { |
59 for (int value = 0; value > -100; value--) { | 53 for (int value = 0; value > -100; value--) { |
60 for (int i = 0; i < 300; i++) { | 54 for (int i = 0; i < 24; i++) { |
61 int b = (value << i) >> i; | 55 int b = (value << i) >> i; |
62 Expect.equals(value, b); | 56 Expect.equals(value, b); |
63 } | 57 } |
64 } | 58 } |
65 } | 59 } |
66 | 60 |
67 static void TestPositiveValueShifts() { | 61 static void TestPositiveValueShifts() { |
68 for (int value = 0; value < 100; value++) { | 62 for (int value = 0; value < 100; value++) { |
69 for (int i = 0; i < 300; i++) { | 63 for (int i = 0; i < 24; i++) { |
70 int b = (value << i) >> i; | 64 int b = (value << i) >> i; |
71 Expect.equals(value, b); | 65 Expect.equals(value, b); |
72 } | 66 } |
73 } | 67 } |
74 } | 68 } |
75 | 69 |
76 static void TestNoMaskingOfShiftCount() { | 70 static void TestNoMaskingOfShiftCount() { |
77 // Shifts which would behave differently if shift count was masked into a | 71 // Shifts which would behave differently if shift count was masked into a |
78 // range. | 72 // range. |
79 Expect.equals(0, 0 >> 256); | 73 Expect.equals(0, 0 >> 30); |
Ivan Posva
2012/01/13 00:54:25
Not sure about this. Does the JS exemption also re
kasperl
2012/01/16 08:37:32
This is an interesting question. We clearly need t
| |
80 Expect.equals(0, 1 >> 256); | 74 Expect.equals(0, 1 >> 30); |
81 Expect.equals(0, 2 >> 256); | 75 Expect.equals(0, 2 >> 30); |
82 Expect.equals(0, ShiftRight(0, 256)); | 76 Expect.equals(0, ShiftRight(0, 30)); |
83 Expect.equals(0, ShiftRight(1, 256)); | 77 Expect.equals(0, ShiftRight(1, 30)); |
84 Expect.equals(0, ShiftRight(2, 256)); | 78 Expect.equals(0, ShiftRight(2, 30)); |
85 | 79 |
86 for (int shift = 1; shift <= 256; shift++) { | 80 for (int shift = 1; shift <= 30; shift++) { |
87 Expect.equals(0, ShiftRight(1, shift)); | 81 Expect.equals(0, ShiftRight(1, shift)); |
88 Expect.equals(-1, ShiftRight(-1, shift)); | 82 Expect.equals(-1, ShiftRight(-1, shift)); |
89 Expect.equals(true, ShiftLeft(1, shift) > ShiftLeft(1, shift - 1)); | 83 Expect.equals(true, ShiftLeft(1, shift) > ShiftLeft(1, shift - 1)); |
90 } | 84 } |
91 } | 85 } |
92 | 86 |
93 static int ShiftLeft(int a, int b) { return a << b; } | 87 static int ShiftLeft(int a, int b) { return a << b; } |
94 static int ShiftRight(int a, int b) { return a >> b; } | 88 static int ShiftRight(int a, int b) { return a >> b; } |
95 } | 89 } |
96 | 90 |
97 main() { | 91 main() { |
98 BitOperationsTest.testMain(); | 92 BitOperationsTest.testMain(); |
99 } | 93 } |
OLD | NEW |