OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, 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 | |
5 library int64test; | |
6 import 'package:fixnum/fixnum.dart'; | |
7 import 'package:unittest/unittest.dart'; | |
8 | |
9 void main() { | |
10 | |
11 argumentErrorTest(name, op, [receiver = Int64.ONE]) { | |
12 throwsArgumentErrorMentioning(substring) => | |
13 throwsA((e) => e is ArgumentError && '$e'.contains(substring)); | |
14 | |
15 expect(() => op(receiver, null), throwsArgumentErrorMentioning('null')); | |
16 expect(() => op(receiver, 'foo'), throwsArgumentErrorMentioning(r'"foo"')); | |
17 } | |
18 | |
19 group("is-tests", () { | |
20 test("isEven", () { | |
21 expect((-Int64.ONE).isEven, false); | |
22 expect(Int64.ZERO.isEven, true); | |
23 expect(Int64.ONE.isEven, false); | |
24 expect(Int64.TWO.isEven, true); | |
25 }); | |
26 test("isMaxValue", () { | |
27 expect(Int64.MIN_VALUE.isMaxValue, false); | |
28 expect(Int64.ZERO.isMaxValue, false); | |
29 expect(Int64.MAX_VALUE.isMaxValue, true); | |
30 }); | |
31 test("isMinValue", () { | |
32 expect(Int64.MIN_VALUE.isMinValue, true); | |
33 expect(Int64.ZERO.isMinValue, false); | |
34 expect(Int64.MAX_VALUE.isMinValue, false); | |
35 }); | |
36 test("isNegative", () { | |
37 expect(Int64.MIN_VALUE.isNegative, true); | |
38 expect(Int64.ZERO.isNegative, false); | |
39 expect(Int64.ONE.isNegative, false); | |
40 }); | |
41 test("isOdd", () { | |
42 expect((-Int64.ONE).isOdd, true); | |
43 expect(Int64.ZERO.isOdd, false); | |
44 expect(Int64.ONE.isOdd, true); | |
45 expect(Int64.TWO.isOdd, false); | |
46 }); | |
47 test("isZero", () { | |
48 expect(Int64.MIN_VALUE.isZero, false); | |
49 expect(Int64.ZERO.isZero, true); | |
50 expect(Int64.MAX_VALUE.isZero, false); | |
51 }); | |
52 test("bitLength", () { | |
53 expect(new Int64(-2).bitLength, 1); | |
54 expect((-Int64.ONE).bitLength, 0); | |
55 expect(Int64.ZERO.bitLength, 0); | |
56 expect((Int64.ONE << 21).bitLength, 22); | |
57 expect((Int64.ONE << 22).bitLength, 23); | |
58 expect((Int64.ONE << 43).bitLength, 44); | |
59 expect((Int64.ONE << 44).bitLength, 45); | |
60 expect(new Int64(2).bitLength, 2); | |
61 expect(Int64.MAX_VALUE.bitLength, 63); | |
62 expect(Int64.MIN_VALUE.bitLength, 63); | |
63 }); | |
64 }); | |
65 | |
66 group("arithmetic operators", () { | |
67 Int64 n1 = new Int64(1234); | |
68 Int64 n2 = new Int64(9876); | |
69 Int64 n3 = new Int64(-1234); | |
70 Int64 n4 = new Int64(-9876); | |
71 Int64 n5 = new Int64.fromInts(0x12345678, 0xabcdabcd); | |
72 Int64 n6 = new Int64.fromInts(0x77773333, 0x22224444); | |
73 | |
74 test("+", () { | |
75 expect(n1 + n2, new Int64(11110)); | |
76 expect(n3 + n2, new Int64(8642)); | |
77 expect(n3 + n4, new Int64(-11110)); | |
78 expect(n5 + n6, new Int64.fromInts(0x89ab89ab, 0xcdeff011)); | |
79 expect(Int64.MAX_VALUE + 1, Int64.MIN_VALUE); | |
80 argumentErrorTest("+", (a, b) => a + b); | |
81 }); | |
82 | |
83 test("-", () { | |
84 expect(n1 - n2, new Int64(-8642)); | |
85 expect(n3 - n2, new Int64(-11110)); | |
86 expect(n3 - n4, new Int64(8642)); | |
87 expect(n5 - n6, new Int64.fromInts(0x9abd2345, 0x89ab6789)); | |
88 expect(Int64.MIN_VALUE - 1, Int64.MAX_VALUE); | |
89 argumentErrorTest("-", (a, b) => a - b); | |
90 }); | |
91 | |
92 test("unary -", () { | |
93 expect(-n1, new Int64(-1234)); | |
94 expect(-Int64.ZERO, Int64.ZERO); | |
95 }); | |
96 | |
97 test("*", () { | |
98 expect(new Int64(1111) * new Int64(3), new Int64(3333)); | |
99 expect(new Int64(1111) * new Int64(-3), new Int64(-3333)); | |
100 expect(new Int64(-1111) * new Int64(3), new Int64(-3333)); | |
101 expect(new Int64(-1111) * new Int64(-3), new Int64(3333)); | |
102 expect(new Int64(100) * Int64.ZERO, Int64.ZERO); | |
103 | |
104 expect(new Int64.fromInts(0x12345678, 0x12345678) * | |
105 new Int64.fromInts(0x1234, 0x12345678), | |
106 new Int64.fromInts(0x7ff63f7c, 0x1df4d840)); | |
107 expect(new Int64.fromInts(0xf2345678, 0x12345678) * | |
108 new Int64.fromInts(0x1234, 0x12345678), | |
109 new Int64.fromInts(0x7ff63f7c, 0x1df4d840)); | |
110 expect(new Int64.fromInts(0xf2345678, 0x12345678) * | |
111 new Int64.fromInts(0xffff1234, 0x12345678), | |
112 new Int64.fromInts(0x297e3f7c, 0x1df4d840)); | |
113 | |
114 // RHS Int32 | |
115 expect((new Int64(123456789) * new Int32(987654321)), | |
116 new Int64.fromInts(0x1b13114, 0xfbff5385)); | |
117 expect((new Int64(123456789) * new Int32(987654321)), | |
118 new Int64.fromInts(0x1b13114, 0xfbff5385)); | |
119 | |
120 // Wraparound | |
121 expect((new Int64(123456789) * new Int64(987654321)), | |
122 new Int64.fromInts(0x1b13114, 0xfbff5385)); | |
123 | |
124 expect(Int64.MIN_VALUE * new Int64(2), Int64.ZERO); | |
125 expect(Int64.MIN_VALUE * new Int64(1), Int64.MIN_VALUE); | |
126 expect(Int64.MIN_VALUE * new Int64(-1), Int64.MIN_VALUE); | |
127 argumentErrorTest("*", (a, b) => a * b); | |
128 }); | |
129 | |
130 test("~/", () { | |
131 Int64 deadBeef = new Int64.fromInts(0xDEADBEEF, 0xDEADBEEF); | |
132 Int64 ten = new Int64(10); | |
133 | |
134 expect(deadBeef ~/ ten, new Int64.fromInts(0xfcaaf97e, 0x63115fe5)); | |
135 expect(Int64.ONE ~/ Int64.TWO, Int64.ZERO); | |
136 expect(Int64.MAX_VALUE ~/ Int64.TWO, | |
137 new Int64.fromInts(0x3fffffff, 0xffffffff)); | |
138 expect(Int64.ZERO ~/ new Int64(1000), Int64.ZERO); | |
139 expect(Int64.MIN_VALUE ~/ Int64.MIN_VALUE, Int64.ONE); | |
140 expect(new Int64(1000) ~/ Int64.MIN_VALUE, Int64.ZERO); | |
141 expect(Int64.MIN_VALUE ~/ new Int64(8192), new Int64(-1125899906842624)); | |
142 expect(Int64.MIN_VALUE ~/ new Int64(8193), new Int64(-1125762484664320)); | |
143 expect(new Int64(-1000) ~/ new Int64(8192), Int64.ZERO); | |
144 expect(new Int64(-1000) ~/ new Int64(8193), Int64.ZERO); | |
145 expect(new Int64(-1000000000) ~/ new Int64(8192), new Int64(-122070)); | |
146 expect(new Int64(-1000000000) ~/ new Int64(8193), new Int64(-122055)); | |
147 expect(new Int64(1000000000) ~/ new Int64(8192), new Int64(122070)); | |
148 expect(new Int64(1000000000) ~/ new Int64(8193), new Int64(122055)); | |
149 expect(Int64.MAX_VALUE ~/ new Int64.fromInts(0x00000000, 0x00000400), | |
150 new Int64.fromInts(0x1fffff, 0xffffffff)); | |
151 expect(Int64.MAX_VALUE ~/ new Int64.fromInts(0x00000000, 0x00040000), | |
152 new Int64.fromInts(0x1fff, 0xffffffff)); | |
153 expect(Int64.MAX_VALUE ~/ new Int64.fromInts(0x00000000, 0x04000000), | |
154 new Int64.fromInts(0x1f, 0xffffffff)); | |
155 expect(Int64.MAX_VALUE ~/ new Int64.fromInts(0x00000004, 0x00000000), | |
156 new Int64(536870911)); | |
157 expect(Int64.MAX_VALUE ~/ new Int64.fromInts(0x00000400, 0x00000000), | |
158 new Int64(2097151)); | |
159 expect(Int64.MAX_VALUE ~/ new Int64.fromInts(0x00040000, 0x00000000), | |
160 new Int64(8191)); | |
161 expect(Int64.MAX_VALUE ~/ new Int64.fromInts(0x04000000, 0x00000000), | |
162 new Int64(31)); | |
163 expect(Int64.MAX_VALUE ~/ new Int64.fromInts(0x00000000, 0x00000300), | |
164 new Int64.fromInts(0x2AAAAA, 0xAAAAAAAA)); | |
165 expect(Int64.MAX_VALUE ~/ new Int64.fromInts(0x00000000, 0x30000000), | |
166 new Int64.fromInts(0x2, 0xAAAAAAAA)); | |
167 expect(Int64.MAX_VALUE ~/ new Int64.fromInts(0x00300000, 0x00000000), | |
168 new Int64(0x2AA)); | |
169 expect(Int64.MAX_VALUE ~/ new Int64(0x123456), | |
170 new Int64.fromInts(0x708, 0x002E9501)); | |
171 expect(Int64.MAX_VALUE % new Int64(0x123456), new Int64(0x3BDA9)); | |
172 expect(new Int64(5) ~/ new Int64(5), Int64.ONE); | |
173 expect(new Int64(1000) ~/ new Int64(3), new Int64(333)); | |
174 expect(new Int64(1000) ~/ new Int64(-3), new Int64(-333)); | |
175 expect(new Int64(-1000) ~/ new Int64(3), new Int64(-333)); | |
176 expect(new Int64(-1000) ~/ new Int64(-3), new Int64(333)); | |
177 expect(new Int64(3) ~/ new Int64(1000), Int64.ZERO); | |
178 expect(new Int64.fromInts( 0x12345678, 0x12345678) ~/ | |
179 new Int64.fromInts(0x0, 0x123), | |
180 new Int64.fromInts(0x1003d0, 0xe84f5ae8)); | |
181 expect(new Int64.fromInts(0x12345678, 0x12345678) ~/ | |
182 new Int64.fromInts(0x1234, 0x12345678), | |
183 new Int64.fromInts(0x0, 0x10003)); | |
184 expect(new Int64.fromInts(0xf2345678, 0x12345678) ~/ | |
185 new Int64.fromInts(0x1234, 0x12345678), | |
186 new Int64.fromInts(0xffffffff, 0xffff3dfe)); | |
187 expect(new Int64.fromInts(0xf2345678, 0x12345678) ~/ | |
188 new Int64.fromInts(0xffff1234, 0x12345678), | |
189 new Int64.fromInts(0x0, 0xeda)); | |
190 expect(new Int64(829893893) ~/ new Int32(1919), new Int32(432461)); | |
191 expect(new Int64(829893893) ~/ new Int64(1919), new Int32(432461)); | |
192 expect(new Int64(829893893) ~/ 1919, new Int32(432461)); | |
193 expect(() => new Int64(1) ~/ Int64.ZERO, | |
194 throwsA(new isInstanceOf<IntegerDivisionByZeroException>())); | |
195 expect(Int64.MIN_VALUE ~/ new Int64(2), | |
196 new Int64.fromInts(0xc0000000, 0x00000000)); | |
197 expect(Int64.MIN_VALUE ~/ new Int64(1), Int64.MIN_VALUE); | |
198 expect(Int64.MIN_VALUE ~/ new Int64(-1), Int64.MIN_VALUE); | |
199 expect(() => new Int64(17) ~/ Int64.ZERO, throws); | |
200 argumentErrorTest("~/", (a, b) => a ~/ b); | |
201 }); | |
202 | |
203 test("%", () { | |
204 // Define % as Euclidean mod, with positive result for all arguments | |
205 expect(Int64.ZERO % new Int64(1000), Int64.ZERO); | |
206 expect(Int64.MIN_VALUE % Int64.MIN_VALUE, Int64.ZERO); | |
207 expect(new Int64(1000) % Int64.MIN_VALUE, new Int64(1000)); | |
208 expect(Int64.MIN_VALUE % new Int64(8192), Int64.ZERO); | |
209 expect(Int64.MIN_VALUE % new Int64(8193), new Int64(6145)); | |
210 expect(new Int64(-1000) % new Int64(8192), new Int64(7192)); | |
211 expect(new Int64(-1000) % new Int64(8193), new Int64(7193)); | |
212 expect(new Int64(-1000000000) % new Int64(8192), new Int64(5632)); | |
213 expect(new Int64(-1000000000) % new Int64(8193), new Int64(4808)); | |
214 expect(new Int64(1000000000) % new Int64(8192), new Int64(2560)); | |
215 expect(new Int64(1000000000) % new Int64(8193), new Int64(3385)); | |
216 expect(Int64.MAX_VALUE % new Int64.fromInts(0x00000000, 0x00000400), | |
217 new Int64.fromInts(0x0, 0x3ff)); | |
218 expect(Int64.MAX_VALUE % new Int64.fromInts(0x00000000, 0x00040000), | |
219 new Int64.fromInts(0x0, 0x3ffff)); | |
220 expect(Int64.MAX_VALUE % new Int64.fromInts(0x00000000, 0x04000000), | |
221 new Int64.fromInts(0x0, 0x3ffffff)); | |
222 expect(Int64.MAX_VALUE % new Int64.fromInts(0x00000004, 0x00000000), | |
223 new Int64.fromInts(0x3, 0xffffffff)); | |
224 expect(Int64.MAX_VALUE % new Int64.fromInts(0x00000400, 0x00000000), | |
225 new Int64.fromInts(0x3ff, 0xffffffff)); | |
226 expect(Int64.MAX_VALUE % new Int64.fromInts(0x00040000, 0x00000000), | |
227 new Int64.fromInts(0x3ffff, 0xffffffff)); | |
228 expect(Int64.MAX_VALUE % new Int64.fromInts(0x04000000, 0x00000000), | |
229 new Int64.fromInts(0x3ffffff, 0xffffffff)); | |
230 expect(new Int64(0x12345678).remainder(new Int64(0x22)), | |
231 new Int64(0x12345678.remainder(0x22))); | |
232 expect(new Int64(0x12345678).remainder(new Int64(-0x22)), | |
233 new Int64(0x12345678.remainder(-0x22))); | |
234 expect(new Int64(-0x12345678).remainder(new Int64(-0x22)), | |
235 new Int64(-0x12345678.remainder(-0x22))); | |
236 expect(new Int64(-0x12345678).remainder(new Int64(0x22)), | |
237 new Int64(-0x12345678.remainder(0x22))); | |
238 expect(new Int32(0x12345678).remainder(new Int64(0x22)), | |
239 new Int64(0x12345678.remainder(0x22))); | |
240 argumentErrorTest("%", (a, b) => a % b); | |
241 }); | |
242 | |
243 test("clamp", () { | |
244 Int64 val = new Int64(17); | |
245 expect(val.clamp(20, 30), new Int64(20)); | |
246 expect(val.clamp(10, 20), new Int64(17)); | |
247 expect(val.clamp(10, 15), new Int64(15)); | |
248 | |
249 expect(val.clamp(new Int32(20), new Int32(30)), new Int64(20)); | |
250 expect(val.clamp(new Int32(10), new Int32(20)), new Int64(17)); | |
251 expect(val.clamp(new Int32(10), new Int32(15)), new Int64(15)); | |
252 | |
253 expect(val.clamp(new Int64(20), new Int64(30)), new Int64(20)); | |
254 expect(val.clamp(new Int64(10), new Int64(20)), new Int64(17)); | |
255 expect(val.clamp(new Int64(10), new Int64(15)), new Int64(15)); | |
256 expect(val.clamp(Int64.MIN_VALUE, new Int64(30)), new Int64(17)); | |
257 expect(val.clamp(new Int64(10), Int64.MAX_VALUE), new Int64(17)); | |
258 | |
259 expect(() => val.clamp(1, 'b'), throwsA(isArgumentError)); | |
260 expect(() => val.clamp('a', 1), throwsA(isArgumentError)); | |
261 }); | |
262 }); | |
263 | |
264 group("comparison operators", () { | |
265 Int64 largeNeg = new Int64.fromInts(0x82341234, 0x0); | |
266 Int64 largePos = new Int64.fromInts(0x12341234, 0x0); | |
267 Int64 largePosPlusOne = largePos + new Int64(1); | |
268 | |
269 test("<", () { | |
270 expect(new Int64(10) < new Int64(11), true); | |
271 expect(new Int64(10) < new Int64(10), false); | |
272 expect(new Int64(10) < new Int64(9), false); | |
273 expect(new Int64(10) < new Int32(11), true); | |
274 expect(new Int64(10) < new Int32(10), false); | |
275 expect(new Int64(10) < new Int32(9), false); | |
276 expect(new Int64(-10) < new Int64(-11), false); | |
277 expect(Int64.MIN_VALUE < Int64.ZERO, true); | |
278 expect(largeNeg < largePos, true); | |
279 expect(largePos < largePosPlusOne, true); | |
280 expect(largePos < largePos, false); | |
281 expect(largePosPlusOne < largePos, false); | |
282 expect(Int64.MIN_VALUE < Int64.MAX_VALUE, true); | |
283 expect(Int64.MAX_VALUE < Int64.MIN_VALUE, false); | |
284 argumentErrorTest("<", (a, b) => a < b); | |
285 }); | |
286 | |
287 test("<=", () { | |
288 expect(new Int64(10) <= new Int64(11), true); | |
289 expect(new Int64(10) <= new Int64(10), true); | |
290 expect(new Int64(10) <= new Int64(9), false); | |
291 expect(new Int64(10) <= new Int32(11), true); | |
292 expect(new Int64(10) <= new Int32(10), true); | |
293 expect(new Int64(10) <= new Int64(9), false); | |
294 expect(new Int64(-10) <= new Int64(-11), false); | |
295 expect(new Int64(-10) <= new Int64(-10), true); | |
296 expect(largeNeg <= largePos, true); | |
297 expect(largePos <= largeNeg, false); | |
298 expect(largePos <= largePosPlusOne, true); | |
299 expect(largePos <= largePos, true); | |
300 expect(largePosPlusOne <= largePos, false); | |
301 expect(Int64.MIN_VALUE <= Int64.MAX_VALUE, true); | |
302 expect(Int64.MAX_VALUE <= Int64.MIN_VALUE, false); | |
303 argumentErrorTest("<=", (a, b) => a <= b); | |
304 }); | |
305 | |
306 test("==", () { | |
307 expect(new Int64(10) == new Int64(11), false); | |
308 expect(new Int64(10) == new Int64(10), true); | |
309 expect(new Int64(10) == new Int64(9), false); | |
310 expect(new Int64(10) == new Int32(11), false); | |
311 expect(new Int64(10) == new Int32(10), true); | |
312 expect(new Int64(10) == new Int32(9), false); | |
313 expect(new Int64(-10) == new Int64(-10), true); | |
314 expect(new Int64(-10) != new Int64(-10), false); | |
315 expect(largePos == largePos, true); | |
316 expect(largePos == largePosPlusOne, false); | |
317 expect(largePosPlusOne == largePos, false); | |
318 expect(Int64.MIN_VALUE == Int64.MAX_VALUE, false); | |
319 expect(new Int64(17) == new Object(), false); | |
320 expect(new Int64(17) == null, false); | |
321 }); | |
322 | |
323 test(">=", () { | |
324 expect(new Int64(10) >= new Int64(11), false); | |
325 expect(new Int64(10) >= new Int64(10), true); | |
326 expect(new Int64(10) >= new Int64(9), true); | |
327 expect(new Int64(10) >= new Int32(11), false); | |
328 expect(new Int64(10) >= new Int32(10), true); | |
329 expect(new Int64(10) >= new Int32(9), true); | |
330 expect(new Int64(-10) >= new Int64(-11), true); | |
331 expect(new Int64(-10) >= new Int64(-10), true); | |
332 expect(largePos >= largeNeg, true); | |
333 expect(largeNeg >= largePos, false); | |
334 expect(largePos >= largePosPlusOne, false); | |
335 expect(largePos >= largePos, true); | |
336 expect(largePosPlusOne >= largePos, true); | |
337 expect(Int64.MIN_VALUE >= Int64.MAX_VALUE, false); | |
338 expect(Int64.MAX_VALUE >= Int64.MIN_VALUE, true); | |
339 argumentErrorTest(">=", (a, b) => a >= b); | |
340 }); | |
341 | |
342 test(">", () { | |
343 expect(new Int64(10) > new Int64(11), false); | |
344 expect(new Int64(10) > new Int64(10), false); | |
345 expect(new Int64(10) > new Int64(9), true); | |
346 expect(new Int64(10) > new Int32(11), false); | |
347 expect(new Int64(10) > new Int32(10), false); | |
348 expect(new Int64(10) > new Int32(9), true); | |
349 expect(new Int64(-10) > new Int64(-11), true); | |
350 expect(new Int64(10) > new Int64(-11), true); | |
351 expect(new Int64(-10) > new Int64(11), false); | |
352 expect(largePos > largeNeg, true); | |
353 expect(largeNeg > largePos, false); | |
354 expect(largePos > largePosPlusOne, false); | |
355 expect(largePos > largePos, false); | |
356 expect(largePosPlusOne > largePos, true); | |
357 expect(Int64.ZERO > Int64.MIN_VALUE, true); | |
358 expect(Int64.MIN_VALUE > Int64.MAX_VALUE, false); | |
359 expect(Int64.MAX_VALUE > Int64.MIN_VALUE, true); | |
360 argumentErrorTest(">", (a, b) => a > b); | |
361 }); | |
362 }); | |
363 | |
364 group("bitwise operators", () { | |
365 Int64 n1 = new Int64(1234); | |
366 Int64 n2 = new Int64(9876); | |
367 Int64 n3 = new Int64(-1234); | |
368 Int64 n4 = new Int64(0x1234) << 32; | |
369 Int64 n5 = new Int64(0x9876) << 32; | |
370 | |
371 test("&", () { | |
372 expect(n1 & n2, new Int64(1168)); | |
373 expect(n3 & n2, new Int64(8708)); | |
374 expect(n4 & n5, new Int64(0x1034) << 32); | |
375 expect(() => n1 & null, throwsArgumentError); | |
376 argumentErrorTest("&", (a, b) => a & b); | |
377 }); | |
378 | |
379 test("|", () { | |
380 expect(n1 | n2, new Int64(9942)); | |
381 expect(n3 | n2, new Int64(-66)); | |
382 expect(n4 | n5, new Int64(0x9a76) << 32); | |
383 expect(() => n1 | null, throwsArgumentError); | |
384 argumentErrorTest("|", (a, b) => a | b); | |
385 }); | |
386 | |
387 test("^", () { | |
388 expect(n1 ^ n2, new Int64(8774)); | |
389 expect(n3 ^ n2, new Int64(-8774)); | |
390 expect(n4 ^ n5, new Int64(0x8a42) << 32); | |
391 expect(() => n1 ^ null, throwsArgumentError); | |
392 argumentErrorTest("^", (a, b) => a ^ b); | |
393 }); | |
394 | |
395 test("~", () { | |
396 expect(-new Int64(1), new Int64(-1)); | |
397 expect(-new Int64(-1), new Int64(1)); | |
398 expect(-Int64.MIN_VALUE, Int64.MIN_VALUE); | |
399 | |
400 expect(~n1, new Int64(-1235)); | |
401 expect(~n2, new Int64(-9877)); | |
402 expect(~n3, new Int64(1233)); | |
403 expect(~n4, new Int64.fromInts(0xffffedcb, 0xffffffff)); | |
404 expect(~n5, new Int64.fromInts(0xffff6789, 0xffffffff)); | |
405 }); | |
406 }); | |
407 | |
408 group("bitshift operators", () { | |
409 test("<<", () { | |
410 expect(new Int64.fromInts(0x12341234, 0x45674567) << 10, | |
411 new Int64.fromInts(0xd048d115, 0x9d159c00)); | |
412 expect(new Int64.fromInts(0x92341234, 0x45674567) << 10, | |
413 new Int64.fromInts(0xd048d115, 0x9d159c00)); | |
414 expect(new Int64(-1) << 5, new Int64(-32)); | |
415 expect(new Int64(-1) << 0, new Int64(-1)); | |
416 expect(() => new Int64(17) << -1, throwsArgumentError); | |
417 expect(() => new Int64(17) << null, throws); | |
418 }); | |
419 | |
420 test(">>", () { | |
421 expect((Int64.MIN_VALUE >> 13).toString(), "-1125899906842624"); | |
422 expect(new Int64.fromInts(0x12341234, 0x45674567) >> 10, | |
423 new Int64.fromInts(0x48d04, 0x8d1159d1)); | |
424 expect(new Int64.fromInts(0x92341234, 0x45674567) >> 10, | |
425 new Int64.fromInts(0xffe48d04, 0x8d1159d1)); | |
426 expect(new Int64.fromInts(0xFFFFFFF, 0xFFFFFFFF) >> 34, | |
427 new Int64(67108863)); | |
428 for (int n = 0; n <= 66; n++) { | |
429 expect(new Int64(-1) >> n, new Int64(-1)); | |
430 } | |
431 expect(new Int64.fromInts(0x72345678, 0x9abcdef0) >> 8, | |
432 new Int64.fromInts(0x00723456, 0x789abcde)); | |
433 expect(new Int64.fromInts(0x72345678, 0x9abcdef0) >> 16, | |
434 new Int64.fromInts(0x00007234, 0x56789abc)); | |
435 expect(new Int64.fromInts(0x72345678, 0x9abcdef0) >> 24, | |
436 new Int64.fromInts(0x00000072, 0x3456789a)); | |
437 expect(new Int64.fromInts(0x72345678, 0x9abcdef0) >> 28, | |
438 new Int64.fromInts(0x00000007, 0x23456789)); | |
439 expect(new Int64.fromInts(0x72345678, 0x9abcdef0) >> 32, | |
440 new Int64.fromInts(0x00000000, 0x72345678)); | |
441 expect(new Int64.fromInts(0x72345678, 0x9abcdef0) >> 36, | |
442 new Int64.fromInts(0x00000000, 0x07234567)); | |
443 expect(new Int64.fromInts(0x72345678, 0x9abcdef0) >> 40, | |
444 new Int64.fromInts(0x00000000, 0x00723456)); | |
445 expect(new Int64.fromInts(0x72345678, 0x9abcde00) >> 44, | |
446 new Int64.fromInts(0x00000000, 0x00072345)); | |
447 expect(new Int64.fromInts(0x72345678, 0x9abcdef0) >> 48, | |
448 new Int64.fromInts(0x00000000, 0x00007234)); | |
449 expect(new Int64.fromInts(0x92345678, 0x9abcdef0) >> 8, | |
450 new Int64.fromInts(0xff923456, 0x789abcde)); | |
451 expect(new Int64.fromInts(0x92345678, 0x9abcdef0) >> 16, | |
452 new Int64.fromInts(0xffff9234, 0x56789abc)); | |
453 expect(new Int64.fromInts(0x92345678, 0x9abcdef0) >> 24, | |
454 new Int64.fromInts(0xffffff92, 0x3456789a)); | |
455 expect(new Int64.fromInts(0x92345678, 0x9abcdef0) >> 28, | |
456 new Int64.fromInts(0xfffffff9, 0x23456789)); | |
457 expect(new Int64.fromInts(0x92345678, 0x9abcdef0) >> 32, | |
458 new Int64.fromInts(0xffffffff, 0x92345678)); | |
459 expect(new Int64.fromInts(0x92345678, 0x9abcdef0) >> 36, | |
460 new Int64.fromInts(0xffffffff, 0xf9234567)); | |
461 expect(new Int64.fromInts(0x92345678, 0x9abcdef0) >> 40, | |
462 new Int64.fromInts(0xffffffff, 0xff923456)); | |
463 expect(new Int64.fromInts(0x92345678, 0x9abcdef0) >> 44, | |
464 new Int64.fromInts(0xffffffff, 0xfff92345)); | |
465 expect(new Int64.fromInts(0x92345678, 0x9abcdef0) >> 48, | |
466 new Int64.fromInts(0xffffffff, 0xffff9234)); | |
467 expect(() => new Int64(17) >> -1, throwsArgumentError); | |
468 expect(() => new Int64(17) >> null, throws); | |
469 }); | |
470 | |
471 test("shiftRightUnsigned", () { | |
472 expect(new Int64.fromInts(0x12341234, 0x45674567).shiftRightUnsigned(10), | |
473 new Int64.fromInts(0x48d04, 0x8d1159d1)); | |
474 expect(new Int64.fromInts(0x92341234, 0x45674567).shiftRightUnsigned(10), | |
475 new Int64.fromInts(0x248d04, 0x8d1159d1)); | |
476 expect(new Int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(8), | |
477 new Int64.fromInts(0x00723456, 0x789abcde)); | |
478 expect(new Int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(16), | |
479 new Int64.fromInts(0x00007234, 0x56789abc)); | |
480 expect(new Int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(24), | |
481 new Int64.fromInts(0x00000072, 0x3456789a)); | |
482 expect(new Int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(28), | |
483 new Int64.fromInts(0x00000007, 0x23456789)); | |
484 expect(new Int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(32), | |
485 new Int64.fromInts(0x00000000, 0x72345678)); | |
486 expect(new Int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(36), | |
487 new Int64.fromInts(0x00000000, 0x07234567)); | |
488 expect(new Int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(40), | |
489 new Int64.fromInts(0x00000000, 0x00723456)); | |
490 expect(new Int64.fromInts(0x72345678, 0x9abcde00).shiftRightUnsigned(44), | |
491 new Int64.fromInts(0x00000000, 0x00072345)); | |
492 expect(new Int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(48), | |
493 new Int64.fromInts(0x00000000, 0x00007234)); | |
494 expect(new Int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(8), | |
495 new Int64.fromInts(0x00923456, 0x789abcde)); | |
496 expect(new Int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(16), | |
497 new Int64.fromInts(0x00009234, 0x56789abc)); | |
498 expect(new Int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(24), | |
499 new Int64.fromInts(0x00000092, 0x3456789a)); | |
500 expect(new Int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(28), | |
501 new Int64.fromInts(0x00000009, 0x23456789)); | |
502 expect(new Int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(32), | |
503 new Int64.fromInts(0x00000000, 0x92345678)); | |
504 expect(new Int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(36), | |
505 new Int64.fromInts(0x00000000, 0x09234567)); | |
506 expect(new Int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(40), | |
507 new Int64.fromInts(0x00000000, 0x00923456)); | |
508 expect(new Int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(44), | |
509 new Int64.fromInts(0x00000000, 0x00092345)); | |
510 expect(new Int64.fromInts(0x00000000, 0x00009234), | |
511 new Int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(48)); | |
512 expect(() => new Int64(17).shiftRightUnsigned(-1), | |
513 throwsArgumentError); | |
514 expect(() => new Int64(17).shiftRightUnsigned(null), throws); | |
515 }); | |
516 | |
517 test("overflow", () { | |
518 expect((new Int64(1) << 63) >> 1, | |
519 -new Int64.fromInts(0x40000000, 0x00000000)); | |
520 expect((new Int64(-1) << 32) << 32, new Int64(0)); | |
521 expect(Int64.MIN_VALUE << 0, Int64.MIN_VALUE); | |
522 expect(Int64.MIN_VALUE << 1, new Int64(0)); | |
523 expect((-new Int64.fromInts(8, 0)) >> 1, | |
524 new Int64.fromInts(0xfffffffc, 0x00000000)); | |
525 expect((-new Int64.fromInts(8, 0)).shiftRightUnsigned(1), | |
526 new Int64.fromInts(0x7ffffffc, 0x0)); | |
527 }); | |
528 }); | |
529 | |
530 group("conversions", () { | |
531 test("toSigned", () { | |
532 expect((Int64.ONE << 44).toSigned(46), Int64.ONE << 44); | |
533 expect((Int64.ONE << 44).toSigned(45), -(Int64.ONE << 44)); | |
534 expect((Int64.ONE << 22).toSigned(24), Int64.ONE << 22); | |
535 expect((Int64.ONE << 22).toSigned(23), -(Int64.ONE << 22)); | |
536 expect(Int64.ONE.toSigned(2), Int64.ONE); | |
537 expect(Int64.ONE.toSigned(1), -Int64.ONE); | |
538 expect(Int64.MAX_VALUE.toSigned(64), Int64.MAX_VALUE); | |
539 expect(Int64.MIN_VALUE.toSigned(64), Int64.MIN_VALUE); | |
540 expect(Int64.MAX_VALUE.toSigned(63), -Int64.ONE); | |
541 expect(Int64.MIN_VALUE.toSigned(63), Int64.ZERO); | |
542 expect(() => Int64.ONE.toSigned(0), throwsRangeError); | |
543 expect(() => Int64.ONE.toSigned(65), throwsRangeError); | |
544 }); | |
545 test("toUnsigned", () { | |
546 expect((Int64.ONE << 44).toUnsigned(45), Int64.ONE << 44); | |
547 expect((Int64.ONE << 44).toUnsigned(44), Int64.ZERO); | |
548 expect((Int64.ONE << 22).toUnsigned(23), Int64.ONE << 22); | |
549 expect((Int64.ONE << 22).toUnsigned(22), Int64.ZERO); | |
550 expect(Int64.ONE.toUnsigned(1), Int64.ONE); | |
551 expect(Int64.ONE.toUnsigned(0), Int64.ZERO); | |
552 expect(Int64.MAX_VALUE.toUnsigned(64), Int64.MAX_VALUE); | |
553 expect(Int64.MIN_VALUE.toUnsigned(64), Int64.MIN_VALUE); | |
554 expect(Int64.MAX_VALUE.toUnsigned(63), Int64.MAX_VALUE); | |
555 expect(Int64.MIN_VALUE.toUnsigned(63), Int64.ZERO); | |
556 expect(() => Int64.ONE.toUnsigned(-1), throwsRangeError); | |
557 expect(() => Int64.ONE.toUnsigned(65), throwsRangeError); | |
558 }); | |
559 test("toDouble", () { | |
560 expect(new Int64(0).toDouble(), same(0.0)); | |
561 expect(new Int64(100).toDouble(), same(100.0)); | |
562 expect(new Int64(-100).toDouble(), same(-100.0)); | |
563 expect(new Int64(2147483647).toDouble(), same(2147483647.0)); | |
564 expect(new Int64(2147483648).toDouble(), same(2147483648.0)); | |
565 expect(new Int64(-2147483647).toDouble(), same(-2147483647.0)); | |
566 expect(new Int64(-2147483648).toDouble(), same(-2147483648.0)); | |
567 expect(new Int64(4503599627370495).toDouble(), same(4503599627370495.0)); | |
568 expect(new Int64(4503599627370496).toDouble(), same(4503599627370496.0)); | |
569 expect(new Int64(-4503599627370495).toDouble(), | |
570 same(-4503599627370495.0)); | |
571 expect(new Int64(-4503599627370496).toDouble(), | |
572 same(-4503599627370496.0)); | |
573 expect(Int64.parseInt("-10000000000000000").toDouble().toStringAsFixed(1), | |
574 "-10000000000000000.0"); | |
575 expect(Int64.parseInt("-10000000000000001").toDouble().toStringAsFixed(1), | |
576 "-10000000000000000.0"); | |
577 expect(Int64.parseInt("-10000000000000002").toDouble().toStringAsFixed(1), | |
578 "-10000000000000002.0"); | |
579 expect(Int64.parseInt("-10000000000000003").toDouble().toStringAsFixed(1), | |
580 "-10000000000000004.0"); | |
581 expect(Int64.parseInt("-10000000000000004").toDouble().toStringAsFixed(1), | |
582 "-10000000000000004.0"); | |
583 expect(Int64.parseInt("-10000000000000005").toDouble().toStringAsFixed(1), | |
584 "-10000000000000004.0"); | |
585 expect(Int64.parseInt("-10000000000000006").toDouble().toStringAsFixed(1), | |
586 "-10000000000000006.0"); | |
587 expect(Int64.parseInt("-10000000000000007").toDouble().toStringAsFixed(1), | |
588 "-10000000000000008.0"); | |
589 expect(Int64.parseInt("-10000000000000008").toDouble().toStringAsFixed(1), | |
590 "-10000000000000008.0"); | |
591 }); | |
592 | |
593 test("toInt", () { | |
594 expect(new Int64(0).toInt(), 0); | |
595 expect(new Int64(100).toInt(), 100); | |
596 expect(new Int64(-100).toInt(), -100); | |
597 expect(new Int64(2147483647).toInt(), 2147483647); | |
598 expect(new Int64(2147483648).toInt(), 2147483648); | |
599 expect(new Int64(-2147483647).toInt(), -2147483647); | |
600 expect(new Int64(-2147483648).toInt(), -2147483648); | |
601 expect(new Int64(4503599627370495).toInt(), 4503599627370495); | |
602 expect(new Int64(4503599627370496).toInt(), 4503599627370496); | |
603 expect(new Int64(-4503599627370495).toInt(), -4503599627370495); | |
604 expect(new Int64(-4503599627370496).toInt(), -4503599627370496); | |
605 expect(Int64.parseInt("-10000000000000000").toInt(), | |
606 same(-10000000000000000)); | |
607 expect(Int64.parseInt("-10000000000000001").toInt(), | |
608 same(-10000000000000001)); | |
609 expect(Int64.parseInt("-10000000000000002").toInt(), | |
610 same(-10000000000000002)); | |
611 expect(Int64.parseInt("-10000000000000003").toInt(), | |
612 same(-10000000000000003)); | |
613 expect(Int64.parseInt("-10000000000000004").toInt(), | |
614 same(-10000000000000004)); | |
615 expect(Int64.parseInt("-10000000000000005").toInt(), | |
616 same(-10000000000000005)); | |
617 expect(Int64.parseInt("-10000000000000006").toInt(), | |
618 same(-10000000000000006)); | |
619 expect(Int64.parseInt("-10000000000000007").toInt(), | |
620 same(-10000000000000007)); | |
621 expect(Int64.parseInt("-10000000000000008").toInt(), | |
622 same(-10000000000000008)); | |
623 }); | |
624 | |
625 test("toInt32", () { | |
626 expect(new Int64(0).toInt32(), new Int32(0)); | |
627 expect(new Int64(1).toInt32(), new Int32(1)); | |
628 expect(new Int64(-1).toInt32(), new Int32(-1)); | |
629 expect(new Int64(2147483647).toInt32(), new Int32(2147483647)); | |
630 expect(new Int64(2147483648).toInt32(), new Int32(-2147483648)); | |
631 expect(new Int64(2147483649).toInt32(), new Int32(-2147483647)); | |
632 expect(new Int64(2147483650).toInt32(), new Int32(-2147483646)); | |
633 expect(new Int64(-2147483648).toInt32(), new Int32(-2147483648)); | |
634 expect(new Int64(-2147483649).toInt32(), new Int32(2147483647)); | |
635 expect(new Int64(-2147483650).toInt32(), new Int32(2147483646)); | |
636 expect(new Int64(-2147483651).toInt32(), new Int32(2147483645)); | |
637 }); | |
638 }); | |
639 | |
640 test("JavaScript 53-bit integer boundary", () { | |
641 Int64 _factorial(Int64 n) { | |
642 if (n.isZero) { | |
643 return new Int64(1); | |
644 } else { | |
645 return n * _factorial(n - new Int64(1)); | |
646 } | |
647 } | |
648 Int64 fact18 = _factorial(new Int64(18)); | |
649 Int64 fact17 = _factorial(new Int64(17)); | |
650 expect(fact18 ~/ fact17, new Int64(18)); | |
651 }); | |
652 | |
653 test("min, max values", () { | |
654 expect(new Int64(1) << 63, Int64.MIN_VALUE); | |
655 expect(-(Int64.MIN_VALUE + new Int64(1)), Int64.MAX_VALUE); | |
656 }); | |
657 | |
658 group("parse", () { | |
659 test("parseRadix10", () { | |
660 checkInt(int x) { | |
661 expect(Int64.parseRadix('$x', 10), new Int64(x)); | |
662 } | |
663 checkInt(0); | |
664 checkInt(1); | |
665 checkInt(-1); | |
666 checkInt(1000); | |
667 checkInt(12345678); | |
668 checkInt(-12345678); | |
669 checkInt(2147483647); | |
670 checkInt(2147483648); | |
671 checkInt(-2147483647); | |
672 checkInt(-2147483648); | |
673 checkInt(4294967295); | |
674 checkInt(4294967296); | |
675 checkInt(-4294967295); | |
676 checkInt(-4294967296); | |
677 }); | |
678 | |
679 test("parseRadix", () { | |
680 check(String s, int r, String x) { | |
681 expect(Int64.parseRadix(s, r).toString(), x); | |
682 } | |
683 check('ghoul', 36, '27699213'); | |
684 check('ghoul', 35, '24769346'); | |
685 // Min and max value. | |
686 check("-9223372036854775808", 10, "-9223372036854775808"); | |
687 check("9223372036854775807", 10, "9223372036854775807"); | |
688 // Overflow during parsing. | |
689 check("9223372036854775808", 10, "-9223372036854775808"); | |
690 | |
691 expect(() => Int64.parseRadix('0', 1), throwsRangeError); | |
692 expect(() => Int64.parseRadix('0', 37), throwsRangeError); | |
693 expect(() => Int64.parseRadix('xyzzy', -1), throwsRangeError); | |
694 expect(() => Int64.parseRadix('xyzzy', 10), throwsFormatException); | |
695 }); | |
696 | |
697 test("parseRadixN", () { | |
698 check(String s, int r) { | |
699 expect(Int64.parseRadix(s, r).toRadixString(r), s); | |
700 } | |
701 check("2ppp111222333", 33); // This value & radix requires three chunks. | |
702 }); | |
703 }); | |
704 | |
705 group("string representation", () { | |
706 test("toString", () { | |
707 expect(new Int64(0).toString(), "0"); | |
708 expect(new Int64(1).toString(), "1"); | |
709 expect(new Int64(-1).toString(), "-1"); | |
710 expect(new Int64(-10).toString(), "-10"); | |
711 expect(Int64.MIN_VALUE.toString(), "-9223372036854775808"); | |
712 expect(Int64.MAX_VALUE.toString(), "9223372036854775807"); | |
713 | |
714 int top = 922337201; | |
715 int bottom = 967490662; | |
716 Int64 fullnum = (new Int64(1000000000) * new Int64(top)) + | |
717 new Int64(bottom); | |
718 expect(fullnum.toString(), "922337201967490662"); | |
719 expect((-fullnum).toString(), "-922337201967490662"); | |
720 expect(new Int64(123456789).toString(), "123456789"); | |
721 }); | |
722 | |
723 test("toHexString", () { | |
724 Int64 deadbeef12341234 = new Int64.fromInts(0xDEADBEEF, 0x12341234); | |
725 expect(Int64.ZERO.toHexString(), "0"); | |
726 expect(deadbeef12341234.toHexString(), "DEADBEEF12341234"); | |
727 expect(new Int64.fromInts(0x17678A7, 0xDEF01234).toHexString(), | |
728 "17678A7DEF01234"); | |
729 expect(new Int64(123456789).toHexString(), "75BCD15"); | |
730 }); | |
731 | |
732 test("toRadixString", () { | |
733 expect(new Int64(123456789).toRadixString(5), "223101104124"); | |
734 expect(Int64.MIN_VALUE.toRadixString(2), | |
735 "-1000000000000000000000000000000000000000000000000000000000000000"); | |
736 expect(Int64.MIN_VALUE.toRadixString(3), | |
737 "-2021110011022210012102010021220101220222"); | |
738 expect(Int64.MIN_VALUE.toRadixString(4), | |
739 "-20000000000000000000000000000000"); | |
740 expect(Int64.MIN_VALUE.toRadixString(5), "-1104332401304422434310311213"); | |
741 expect(Int64.MIN_VALUE.toRadixString(6), "-1540241003031030222122212"); | |
742 expect(Int64.MIN_VALUE.toRadixString(7), "-22341010611245052052301"); | |
743 expect(Int64.MIN_VALUE.toRadixString(8), "-1000000000000000000000"); | |
744 expect(Int64.MIN_VALUE.toRadixString(9), "-67404283172107811828"); | |
745 expect(Int64.MIN_VALUE.toRadixString(10), "-9223372036854775808"); | |
746 expect(Int64.MIN_VALUE.toRadixString(11), "-1728002635214590698"); | |
747 expect(Int64.MIN_VALUE.toRadixString(12), "-41a792678515120368"); | |
748 expect(Int64.MIN_VALUE.toRadixString(13), "-10b269549075433c38"); | |
749 expect(Int64.MIN_VALUE.toRadixString(14), "-4340724c6c71dc7a8"); | |
750 expect(Int64.MIN_VALUE.toRadixString(15), "-160e2ad3246366808"); | |
751 expect(Int64.MIN_VALUE.toRadixString(16), "-8000000000000000"); | |
752 expect(Int64.MAX_VALUE.toRadixString(2), | |
753 "111111111111111111111111111111111111111111111111111111111111111"); | |
754 expect(Int64.MAX_VALUE.toRadixString(3), | |
755 "2021110011022210012102010021220101220221"); | |
756 expect(Int64.MAX_VALUE.toRadixString(4), | |
757 "13333333333333333333333333333333"); | |
758 expect(Int64.MAX_VALUE.toRadixString(5), "1104332401304422434310311212"); | |
759 expect(Int64.MAX_VALUE.toRadixString(6), "1540241003031030222122211"); | |
760 expect(Int64.MAX_VALUE.toRadixString(7), "22341010611245052052300"); | |
761 expect(Int64.MAX_VALUE.toRadixString(8), "777777777777777777777"); | |
762 expect(Int64.MAX_VALUE.toRadixString(9), "67404283172107811827"); | |
763 expect(Int64.MAX_VALUE.toRadixString(10), "9223372036854775807"); | |
764 expect(Int64.MAX_VALUE.toRadixString(11), "1728002635214590697"); | |
765 expect(Int64.MAX_VALUE.toRadixString(12), "41a792678515120367"); | |
766 expect(Int64.MAX_VALUE.toRadixString(13), "10b269549075433c37"); | |
767 expect(Int64.MAX_VALUE.toRadixString(14), "4340724c6c71dc7a7"); | |
768 expect(Int64.MAX_VALUE.toRadixString(15), "160e2ad3246366807"); | |
769 expect(() => Int64.ZERO.toRadixString(1), throwsRangeError); | |
770 expect(() => Int64.ZERO.toRadixString(37), throwsRangeError); | |
771 }); | |
772 }); | |
773 } | |
OLD | NEW |