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 | |
6 class BitOperationsTest { | |
7 static testMain() { | |
8 for (int i = 0; i < 4; i++) { | |
9 testOne(); | |
10 } | |
11 } | |
12 static testOne() { | |
13 Expect.equals(3, (3 & 7)); | |
14 Expect.equals(7, (3 | 7)); | |
15 Expect.equals(4, (3 ^ 7)); | |
16 Expect.equals(25, (100 >> 2)); | |
17 Expect.equals(400, (100 << 2)); | |
18 Expect.equals(-25, (-100 >> 2)); | |
19 Expect.equals(-101, ~100); | |
20 Expect.equals(0x10000000000000000, 1 << 64); | |
21 Expect.equals(-0x10000000000000000, -1 << 64); | |
22 Expect.equals(0x40000000, 0x04000000 << 4); | |
23 Expect.equals(0x4000000000000000, 0x0400000000000000 << 4); | |
24 Expect.equals(0, ~-1); | |
25 Expect.equals(-1, ~0); | |
26 | |
27 Expect.equals(0, 1 >> 160); | |
28 Expect.equals(-1, -1 >> 160); | |
29 | |
30 Expect.equals(0x100000000000000001, | |
31 0x100000000000000001 & 0x100000100F00000001); | |
32 Expect.equals(0x1, 0x1 & 0x100000100F00000001); | |
33 Expect.equals(0x1, 0x100000100F00000001 & 0x1); | |
34 | |
35 Expect.equals(0x100000100F00000001, | |
36 0x100000000000000001 | 0x100000100F00000001); | |
37 Expect.equals(0x100000100F00000011, 0x11 | 0x100000100F00000001); | |
38 Expect.equals(0x100000100F00000011, 0x100000100F00000001 | 0x11); | |
39 | |
40 Expect.equals(0x0F000F00000000000000, | |
41 0x0F00F00000000000001 ^ 0xFF00000000000000001); | |
42 Expect.equals(0x31, 0xF00F00000000000001 ^ 0xF00F00000000000030); | |
43 Expect.equals(0xF00F00000000000031, 0xF00F00000000000001 ^ 0x30); | |
44 Expect.equals(0xF00F00000000000031, 0x30 ^ 0xF00F00000000000001); | |
45 | |
46 Expect.equals(0xF0000000000000000F, 0xF0000000000000000F7 >> 4); | |
47 Expect.equals(15, 0xF00000000 >> 32); | |
48 Expect.equals(1030792151040, 16492674416655 >> 4); | |
49 | |
50 Expect.equals(0xF0000000000000000F0, 0xF0000000000000000F << 4); | |
51 Expect.equals(0xF00000000, 15 << 32); | |
52 | |
53 TestNegativeValueShifts(); | |
54 TestPositiveValueShifts(); | |
55 TestNoMaskingOfShiftCount(); | |
56 } | |
57 | |
58 static void TestNegativeValueShifts() { | |
59 for (int value = 0; value > -100; value--) { | |
60 for (int i = 0; i < 300; i++) { | |
61 int b = (value << i) >> i; | |
62 Expect.equals(value, b); | |
63 } | |
64 } | |
65 } | |
66 | |
67 static void TestPositiveValueShifts() { | |
68 for (int value = 0; value < 100; value++) { | |
69 for (int i = 0; i < 300; i++) { | |
70 int b = (value << i) >> i; | |
71 Expect.equals(value, b); | |
72 } | |
73 } | |
74 } | |
75 | |
76 static void TestNoMaskingOfShiftCount() { | |
77 // Shifts which would behave differently if shift count was masked into a | |
78 // range. | |
79 Expect.equals(0, 0 >> 256); | |
80 Expect.equals(0, 1 >> 256); | |
81 Expect.equals(0, 2 >> 256); | |
82 Expect.equals(0, ShiftRight(0, 256)); | |
83 Expect.equals(0, ShiftRight(1, 256)); | |
84 Expect.equals(0, ShiftRight(2, 256)); | |
85 | |
86 for (int shift = 1; shift <= 256; shift++) { | |
87 Expect.equals(0, ShiftRight(1, shift)); | |
88 Expect.equals(-1, ShiftRight(-1, shift)); | |
89 Expect.equals(true, ShiftLeft(1, shift) > ShiftLeft(1, shift - 1)); | |
90 } | |
91 } | |
92 | |
93 static int ShiftLeft(int a, int b) { return a << b; } | |
94 static int ShiftRight(int a, int b) { return a >> b; } | |
95 } | |
96 | |
97 main() { | |
98 BitOperationsTest.testMain(); | |
99 } | |
OLD | NEW |