OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Testing Bigints. | 4 // Testing Bigints. |
5 | 5 |
6 library bit_twiddling_test; | 6 library bit_twiddling_test; |
| 7 |
7 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
8 | 9 |
9 | |
10 bool haveBigints() { | 10 bool haveBigints() { |
11 return 100000000000000000000 + 1 != 100000000000000000000; | 11 return 100000000000000000000 + 1 != 100000000000000000000; |
12 } | 12 } |
13 | 13 |
14 testBitLength() { | 14 testBitLength() { |
15 check(int i, width) { | 15 check(int i, width) { |
16 Expect.equals(width, i.bitLength, '$i.bitLength == $width'); | 16 Expect.equals(width, i.bitLength, '$i.bitLength == $width'); |
17 // (~i) written as (-i-1) to avoid issues with limited range of dart2js ops. | 17 // (~i) written as (-i-1) to avoid issues with limited range of dart2js ops. |
18 Expect.equals(width, (-i-1).bitLength, '(~$i).bitLength == $width'); | 18 Expect.equals(width, (-i - 1).bitLength, '(~$i).bitLength == $width'); |
19 } | 19 } |
20 | 20 |
21 check(0, 0); | 21 check(0, 0); |
22 check(1, 1); | 22 check(1, 1); |
23 check(2, 2); | 23 check(2, 2); |
24 check(3, 2); | 24 check(3, 2); |
25 check(4, 3); | 25 check(4, 3); |
26 check(5, 3); | 26 check(5, 3); |
27 check(6, 3); | 27 check(6, 3); |
28 check(7, 3); | 28 check(7, 3); |
(...skipping 16 matching lines...) Expand all Loading... |
45 check(0x2000000000000, 50); | 45 check(0x2000000000000, 50); |
46 check(0x2000000000001, 50); | 46 check(0x2000000000001, 50); |
47 | 47 |
48 if (haveBigints()) { | 48 if (haveBigints()) { |
49 check(0xffffffffffffff, 56); | 49 check(0xffffffffffffff, 56); |
50 check(0xffffffffffffffff, 64); | 50 check(0xffffffffffffffff, 64); |
51 check(0xffffffffffffffffff, 72); | 51 check(0xffffffffffffffffff, 72); |
52 check(0x1000000000000000000, 73); | 52 check(0x1000000000000000000, 73); |
53 check(0x1000000000000000001, 73); | 53 check(0x1000000000000000001, 73); |
54 | 54 |
55 | |
56 check(0xfffffffffffffffffffffffffffffffffffffe, 152); | 55 check(0xfffffffffffffffffffffffffffffffffffffe, 152); |
57 check(0xffffffffffffffffffffffffffffffffffffff, 152); | 56 check(0xffffffffffffffffffffffffffffffffffffff, 152); |
58 check(0x100000000000000000000000000000000000000, 153); | 57 check(0x100000000000000000000000000000000000000, 153); |
59 check(0x100000000000000000000000000000000000001, 153); | 58 check(0x100000000000000000000000000000000000001, 153); |
60 | |
61 } | 59 } |
62 } | 60 } |
63 | 61 |
64 testToUnsigned() { | 62 testToUnsigned() { |
65 checkU(src, width, expected) { | 63 checkU(src, width, expected) { |
66 Expect.equals(expected, src.toUnsigned(width)); | 64 Expect.equals(expected, src.toUnsigned(width)); |
67 } | 65 } |
68 | 66 |
69 checkU(1, 8, 1); | 67 checkU(1, 8, 1); |
70 checkU(0xff, 8, 0xff); | 68 checkU(0xff, 8, 0xff); |
(...skipping 11 matching lines...) Expand all Loading... |
82 checkU(0xffffffff, 31, 0x7fffffff); | 80 checkU(0xffffffff, 31, 0x7fffffff); |
83 checkU(0xffffffff, 32, 0xffffffff); | 81 checkU(0xffffffff, 32, 0xffffffff); |
84 checkU(0x100000000, 30, 0); | 82 checkU(0x100000000, 30, 0); |
85 checkU(0x100000000, 31, 0); | 83 checkU(0x100000000, 31, 0); |
86 checkU(0x100000000, 32, 0); | 84 checkU(0x100000000, 32, 0); |
87 checkU(0x1ffffffff, 30, 0x3fffffff); | 85 checkU(0x1ffffffff, 30, 0x3fffffff); |
88 checkU(0x1ffffffff, 31, 0x7fffffff); | 86 checkU(0x1ffffffff, 31, 0x7fffffff); |
89 checkU(0x1ffffffff, 32, 0xffffffff); | 87 checkU(0x1ffffffff, 32, 0xffffffff); |
90 | 88 |
91 checkU(-1, 0, 0); | 89 checkU(-1, 0, 0); |
92 checkU( 0, 0, 0); | 90 checkU(0, 0, 0); |
93 checkU( 1, 0, 0); | 91 checkU(1, 0, 0); |
94 checkU( 2, 0, 0); | 92 checkU(2, 0, 0); |
95 checkU( 3, 0, 0); | 93 checkU(3, 0, 0); |
96 | 94 |
97 checkU(-1, 1, 1); | 95 checkU(-1, 1, 1); |
98 checkU( 0, 1, 0); | 96 checkU(0, 1, 0); |
99 checkU( 1, 1, 1); | 97 checkU(1, 1, 1); |
100 checkU( 2, 1, 0); | 98 checkU(2, 1, 0); |
101 checkU( 3, 1, 1); | 99 checkU(3, 1, 1); |
102 checkU( 4, 1, 0); | 100 checkU(4, 1, 0); |
103 | 101 |
104 checkU(-1, 2, 3); | 102 checkU(-1, 2, 3); |
105 checkU( 0, 2, 0); | 103 checkU(0, 2, 0); |
106 checkU( 1, 2, 1); | 104 checkU(1, 2, 1); |
107 checkU( 2, 2, 2); | 105 checkU(2, 2, 2); |
108 checkU( 3, 2, 3); | 106 checkU(3, 2, 3); |
109 checkU( 4, 2, 0); | 107 checkU(4, 2, 0); |
110 | 108 |
111 checkU(-1, 3, 7); | 109 checkU(-1, 3, 7); |
112 checkU( 0, 3, 0); | 110 checkU(0, 3, 0); |
113 checkU( 1, 3, 1); | 111 checkU(1, 3, 1); |
114 checkU( 2, 3, 2); | 112 checkU(2, 3, 2); |
115 checkU( 3, 3, 3); | 113 checkU(3, 3, 3); |
116 checkU( 4, 3, 4); | 114 checkU(4, 3, 4); |
117 } | 115 } |
118 | 116 |
119 testToSigned() { | 117 testToSigned() { |
120 checkS(src, width, expected) { | 118 checkS(src, width, expected) { |
121 Expect.equals(expected, src.toSigned(width), | 119 Expect.equals( |
122 '$src.toSigned($width) == $expected'); | 120 expected, src.toSigned(width), '$src.toSigned($width) == $expected'); |
123 } | 121 } |
124 | 122 |
125 checkS(1, 8, 1); | 123 checkS(1, 8, 1); |
126 checkS(0xff, 8, -1); | 124 checkS(0xff, 8, -1); |
127 checkS(0xffff, 8, -1); | 125 checkS(0xffff, 8, -1); |
128 checkS(-1, 8, -1); | 126 checkS(-1, 8, -1); |
129 checkS(128, 8, -128); | 127 checkS(128, 8, -128); |
130 checkS(0xffffffff, 32, -1); | 128 checkS(0xffffffff, 32, -1); |
131 | 129 |
132 checkS(0x7fffffff, 30, -1); | 130 checkS(0x7fffffff, 30, -1); |
133 checkS(0x7fffffff, 31, -1); | 131 checkS(0x7fffffff, 31, -1); |
134 checkS(0x7fffffff, 32, 0x7fffffff); | 132 checkS(0x7fffffff, 32, 0x7fffffff); |
135 checkS(0x80000000, 30, 0); | 133 checkS(0x80000000, 30, 0); |
136 checkS(0x80000000, 31, 0); | 134 checkS(0x80000000, 31, 0); |
137 checkS(0x80000000, 32, -2147483648); | 135 checkS(0x80000000, 32, -2147483648); |
138 checkS(0xffffffff, 30, -1); | 136 checkS(0xffffffff, 30, -1); |
139 checkS(0xffffffff, 31, -1); | 137 checkS(0xffffffff, 31, -1); |
140 checkS(0xffffffff, 32, -1); | 138 checkS(0xffffffff, 32, -1); |
141 | 139 |
142 checkS(0x100000000, 30, 0); | 140 checkS(0x100000000, 30, 0); |
143 checkS(0x100000000, 31, 0); | 141 checkS(0x100000000, 31, 0); |
144 checkS(0x100000000, 32, 0); | 142 checkS(0x100000000, 32, 0); |
145 checkS(0x1ffffffff, 30, -1); | 143 checkS(0x1ffffffff, 30, -1); |
146 checkS(0x1ffffffff, 31, -1); | 144 checkS(0x1ffffffff, 31, -1); |
147 checkS(0x1ffffffff, 32, -1); | 145 checkS(0x1ffffffff, 32, -1); |
148 | 146 |
149 checkS(-1, 1, -1); | 147 checkS(-1, 1, -1); |
150 checkS( 0, 1, 0); | 148 checkS(0, 1, 0); |
151 checkS( 1, 1, -1); // The only bit is the sign bit. | 149 checkS(1, 1, -1); // The only bit is the sign bit. |
152 checkS( 2, 1, 0); | 150 checkS(2, 1, 0); |
153 checkS( 3, 1, -1); | 151 checkS(3, 1, -1); |
154 checkS( 4, 1, 0); | 152 checkS(4, 1, 0); |
155 | 153 |
156 checkS(-1, 2, -1); | 154 checkS(-1, 2, -1); |
157 checkS( 0, 2, 0); | 155 checkS(0, 2, 0); |
158 checkS( 1, 2, 1); | 156 checkS(1, 2, 1); |
159 checkS( 2, 2, -2); | 157 checkS(2, 2, -2); |
160 checkS( 3, 2, -1); | 158 checkS(3, 2, -1); |
161 checkS( 4, 2, 0); | 159 checkS(4, 2, 0); |
162 | 160 |
163 checkS(-1, 3, -1); | 161 checkS(-1, 3, -1); |
164 checkS( 0, 3, 0); | 162 checkS(0, 3, 0); |
165 checkS( 1, 3, 1); | 163 checkS(1, 3, 1); |
166 checkS( 2, 3, 2); | 164 checkS(2, 3, 2); |
167 checkS( 3, 3, 3); | 165 checkS(3, 3, 3); |
168 checkS( 4, 3, -4); | 166 checkS(4, 3, -4); |
169 } | 167 } |
170 | 168 |
171 main() { | 169 main() { |
172 testBitLength(); | 170 testBitLength(); |
173 testToUnsigned(); | 171 testToUnsigned(); |
174 testToSigned(); | 172 testToSigned(); |
175 } | 173 } |
OLD | NEW |