Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: tests/lib/fixnum/int_64_test.dart

Issue 10854162: Move fixnum to from lib/ to pkg/ . Once pub.dartlang.org (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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('../../../lib/fixnum/fixnum.dart');
7
8 void main() {
9 testAdditive();
10 testBitOps();
11 testComparisons();
12 testConversions();
13 testDiv();
14 testFactorial();
15 testMinMax();
16 testMod();
17 testMultiplicative();
18 testNegate();
19 testShift();
20 testToHexString();
21 testToString();
22 }
23
24 void testAdditive() {
25 {
26 int64 n1 = new int64.fromInt(1234);
27 int64 n2 = new int64.fromInt(9876);
28 Expect.equals(new int64.fromInt(11110), n1 + n2);
29 Expect.equals(new int64.fromInt(-8642), n1 - n2);
30 }
31
32 {
33 int64 n1 = new int64.fromInt(-1234);
34 int64 n2 = new int64.fromInt(9876);
35 Expect.equals(new int64.fromInt(8642), n1 + n2);
36 Expect.equals(new int64.fromInt(-11110), n1 - n2);
37 }
38
39 {
40 int64 n1 = new int64.fromInt(-1234);
41 int64 n2 = new int64.fromInt(-9876);
42 Expect.equals(new int64.fromInt(-11110), n1 + n2);
43 Expect.equals(new int64.fromInt(8642), n1 - n2);
44 }
45
46 {
47 int64 n1 = new int64.fromInts(0x12345678, 0xabcdabcd);
48 int64 n2 = new int64.fromInts(0x77773333, 0x22224444);
49 Expect.equals(new int64.fromInts(0x89ab89ab, 0xcdeff011), n1 + n2);
50 Expect.equals(new int64.fromInts(0x9abd2345, 0x89ab6789), n1 - n2);
51 }
52 }
53
54 void testBitOps() {
55 {
56 int64 n1 = new int64.fromInt(1234);
57 int64 n2 = new int64.fromInt(9876);
58
59 Expect.equals(new int64.fromInt(1168), n1 & n2);
60 Expect.equals(new int64.fromInt(9942), n1 | n2);
61 Expect.equals(new int64.fromInt(8774), n1 ^ n2);
62 Expect.equals(new int64.fromInt(-1235), ~n1);
63 Expect.equals(new int64.fromInt(-9877), ~n2);
64 }
65
66 {
67 int64 n1 = new int64.fromInt(-1234);
68 int64 n2 = new int64.fromInt(9876);
69 Expect.equals(new int64.fromInt(8708), n1 & n2);
70 Expect.equals(new int64.fromInt(-66), n1 | n2);
71 Expect.equals(new int64.fromInt(-8774), n1 ^ n2);
72 Expect.equals(new int64.fromInt(1233), ~n1);
73 Expect.equals(new int64.fromInt(-9877), ~n2);
74 }
75
76 {
77 int64 n1 = new int64.fromInt(0x1234) << 32;
78 int64 n2 = new int64.fromInt(0x9876) << 32;
79 Expect.equals(new int64.fromInt(0x1034) << 32, n1 & n2);
80 Expect.equals(new int64.fromInt(0x9a76) << 32, n1 | n2);
81 Expect.equals(new int64.fromInt(0x8a42) << 32, n1 ^ n2);
82 Expect.equals(new int64.fromInts(0xffffedcb, 0xffffffff), ~n1);
83 Expect.equals(new int64.fromInts(0xffff6789, 0xffffffff), ~n2);
84 }
85 }
86
87 void testComparisons() {
88 Expect.isTrue(new int64.fromInt(10) < new int64.fromInt(11));
89 Expect.isTrue(new int64.fromInt(10) <= new int64.fromInt(11));
90 Expect.isTrue(!(new int64.fromInt(10) == new int64.fromInt(11)));
91 Expect.isTrue(!(new int64.fromInt(10) >= new int64.fromInt(11)));
92 Expect.isTrue(!(new int64.fromInt(10) > new int64.fromInt(11)));
93
94 Expect.isTrue(!(new int64.fromInt(10) < new int64.fromInt(10)));
95 Expect.isTrue(new int64.fromInt(10) <= new int64.fromInt(10));
96 Expect.isTrue(new int64.fromInt(10) == new int64.fromInt(10));
97 Expect.isTrue(new int64.fromInt(10) >= new int64.fromInt(10));
98 Expect.isTrue(!(new int64.fromInt(10) > new int64.fromInt(10)));
99
100 Expect.isTrue(!(new int64.fromInt(12) < new int64.fromInt(11)));
101 Expect.isTrue(!(new int64.fromInt(12) <= new int64.fromInt(11)));
102 Expect.isTrue(!(new int64.fromInt(12) == new int64.fromInt(11)));
103 Expect.isTrue(new int64.fromInt(12) >= new int64.fromInt(11));
104 Expect.isTrue(new int64.fromInt(12) > new int64.fromInt(11));
105
106 Expect.isTrue(new int64.fromInt(-10) > new int64.fromInt(-11));
107 Expect.isTrue(new int64.fromInt(10) > new int64.fromInt(-11));
108 Expect.isTrue(!(new int64.fromInt(-10) > new int64.fromInt(11)));
109 Expect.isTrue(new int64.fromInt(-10) >= new int64.fromInt(-11));
110 Expect.isTrue(new int64.fromInt(-10) >= new int64.fromInt(-10));
111 Expect.isTrue(!(new int64.fromInt(-10) < new int64.fromInt(-11)));
112 Expect.isTrue(!(new int64.fromInt(-10) <= new int64.fromInt(-11)));
113 Expect.isTrue(new int64.fromInt(-10) <= new int64.fromInt(-10));
114 Expect.isTrue(new int64.fromInt(-10) == new int64.fromInt(-10));
115 Expect.isTrue(!(new int64.fromInt(-10) != new int64.fromInt(-10)));
116
117 // the following three comparisons cannot be implemented by
118 // subtracting the arguments, because the subtraction causes an overflow
119 int64 largeNeg = new int64.fromInts(0x82341234, 0x0);
120 int64 largePos = new int64.fromInts(0x12341234, 0x0);
121 Expect.isTrue(largeNeg < largePos);
122
123 Expect.isTrue(int64.MIN_VALUE < new int64.fromInt(0));
124 Expect.isTrue(new int64.fromInt(0) > int64.MIN_VALUE);
125
126 int64 largePosPlusOne = largePos + new int64.fromInt(1);
127
128 Expect.isTrue(largePos < largePosPlusOne);
129 Expect.isTrue(largePos <= largePosPlusOne);
130 Expect.isTrue(!(largePos == largePosPlusOne));
131 Expect.isTrue(!(largePos >= largePosPlusOne));
132 Expect.isTrue(!(largePos > largePosPlusOne));
133
134 Expect.isTrue(!(largePos < largePos));
135 Expect.isTrue(largePos <= largePos);
136 Expect.isTrue(largePos == largePos);
137 Expect.isTrue(largePos >= largePos);
138 Expect.isTrue(!(largePos > largePos));
139
140 Expect.isTrue(!(largePosPlusOne < largePos));
141 Expect.isTrue(!(largePosPlusOne <= largePos));
142 Expect.isTrue(!(largePosPlusOne == largePos));
143 Expect.isTrue(largePosPlusOne >= largePos);
144 Expect.isTrue(largePosPlusOne > largePos);
145
146 try {
147 new int64.fromInt(17) < null;
148 Expect.fail("x < null should throw NullPointerException");
149 } catch (NullPointerException e) {
150 }
151
152 try {
153 new int64.fromInt(17) <= null;
154 Expect.fail("x <= null should throw NullPointerException");
155 } catch (NullPointerException e) {
156 }
157
158 try {
159 new int64.fromInt(17) > null;
160 Expect.fail("x > null should throw NullPointerException");
161 } catch (NullPointerException e) {
162 }
163
164 try {
165 new int64.fromInt(17) < null;
166 Expect.fail("x >= null should throw NullPointerException");
167 } catch (NullPointerException e) {
168 }
169
170 Expect.isFalse(new int64.fromInt(17) == null);
171 }
172
173 void testConversions() {
174 Expect.equals(0, new int64.fromInt(0).toInt());
175 Expect.equals(100, new int64.fromInt(100).toInt());
176 Expect.equals(-100, new int64.fromInt(-100).toInt());
177 Expect.equals(2147483647, new int64.fromInt(2147483647).toInt());
178 Expect.equals(2147483648, new int64.fromInt(2147483648).toInt());
179 Expect.equals(-2147483647, new int64.fromInt(-2147483647).toInt());
180 Expect.equals(-2147483648, new int64.fromInt(-2147483648).toInt());
181 Expect.equals(4503599627370495, new int64.fromInt(4503599627370495).toInt());
182 Expect.equals(4503599627370496, new int64.fromInt(4503599627370496).toInt());
183 Expect.equals(-4503599627370495,
184 new int64.fromInt(-4503599627370495).toInt());
185 Expect.equals(-4503599627370496,
186 new int64.fromInt(-4503599627370496).toInt());
187
188 Expect.equals(new int32.fromInt(0), new int64.fromInt(0).toInt32());
189 Expect.equals(new int32.fromInt(1), new int64.fromInt(1).toInt32());
190 Expect.equals(new int32.fromInt(-1), new int64.fromInt(-1).toInt32());
191 Expect.equals(new int32.fromInt(2147483647),
192 new int64.fromInt(2147483647).toInt32());
193 Expect.equals(new int32.fromInt(-2147483648),
194 new int64.fromInt(2147483648).toInt32());
195 Expect.equals(new int32.fromInt(-2147483647),
196 new int64.fromInt(2147483649).toInt32());
197 Expect.equals(new int32.fromInt(-2147483646),
198 new int64.fromInt(2147483650).toInt32());
199
200 Expect.equals(new int32.fromInt(-2147483648),
201 new int64.fromInt(-2147483648).toInt32());
202 Expect.equals(new int32.fromInt(2147483647),
203 new int64.fromInt(-2147483649).toInt32());
204 Expect.equals(new int32.fromInt(2147483646),
205 new int64.fromInt(-2147483650).toInt32());
206 Expect.equals(new int32.fromInt(2147483645),
207 new int64.fromInt(-2147483651).toInt32());
208 }
209
210 void testDiv() {
211 int64 deadBeef = new int64.fromInts(0xDEADBEEF, 0xDEADBEEF);
212 int64 ten = new int64.fromInt(10);
213 Expect.equals(new int64.fromInts(0xfcaaf97e, 0x63115fe5), deadBeef ~/ ten);
214 Expect.equals(int64.ZERO, int64.ONE ~/ int64.TWO);
215 Expect.equals(new int64.fromInts(0x3fffffff, 0xffffffff),
216 int64.MAX_VALUE ~/ int64.TWO);
217
218 Expect.equals(int64.ZERO, int64.ZERO ~/ new int64.fromInt(1000));
219 Expect.equals(int64.ONE, int64.MIN_VALUE ~/ int64.MIN_VALUE);
220 Expect.equals(int64.ZERO, new int64.fromInt(1000) ~/ int64.MIN_VALUE);
221
222 Expect.equals("-1125899906842624",
223 (int64.MIN_VALUE ~/ new int64.fromInt(8192)).toString());
224 Expect.equals("-1125762484664320",
225 (int64.MIN_VALUE ~/ new int64.fromInt(8193)).toString());
226 Expect.equals(int64.ZERO,
227 new int64.fromInt(-1000) ~/ new int64.fromInt(8192));
228 Expect.equals(int64.ZERO,
229 new int64.fromInt(-1000) ~/ new int64.fromInt(8193));
230 Expect.equals(new int64.fromInt(-122070),
231 new int64.fromInt(-1000000000) ~/ new int64.fromInt(8192));
232 Expect.equals(new int64.fromInt(-122055),
233 new int64.fromInt(-1000000000) ~/ new int64.fromInt(8193));
234 Expect.equals(new int64.fromInt(122070),
235 new int64.fromInt(1000000000) ~/ new int64.fromInt(8192));
236 Expect.equals(new int64.fromInt(122055),
237 new int64.fromInt(1000000000) ~/ new int64.fromInt(8193));
238
239 Expect.equals(new int64.fromInts(0x1fffff, 0xffffffff),
240 int64.MAX_VALUE ~/ new int64.fromInts(0x00000000, 0x00000400));
241 Expect.equals(new int64.fromInts(0x1fff, 0xffffffff),
242 int64.MAX_VALUE ~/ new int64.fromInts(0x00000000, 0x00040000));
243 Expect.equals(new int64.fromInts(0x1f, 0xffffffff),
244 int64.MAX_VALUE ~/ new int64.fromInts(0x00000000, 0x04000000));
245 Expect.equals(new int64.fromInt(536870911),
246 int64.MAX_VALUE ~/ new int64.fromInts(0x00000004, 0x00000000));
247 Expect.equals(new int64.fromInt(2097151),
248 int64.MAX_VALUE ~/ new int64.fromInts(0x00000400, 0x00000000));
249 Expect.equals(new int64.fromInt(8191),
250 int64.MAX_VALUE ~/ new int64.fromInts(0x00040000, 0x00000000));
251 Expect.equals(new int64.fromInt(31),
252 int64.MAX_VALUE ~/ new int64.fromInts(0x04000000, 0x00000000));
253
254 Expect.equals(new int64.fromInts(0x2AAAAA, 0xAAAAAAAA),
255 int64.MAX_VALUE ~/ new int64.fromInts(0x00000000, 0x00000300));
256 Expect.equals(new int64.fromInts(0x2, 0xAAAAAAAA),
257 int64.MAX_VALUE ~/ new int64.fromInts(0x00000000, 0x30000000));
258 Expect.equals(new int64.fromInt(0x2AA),
259 int64.MAX_VALUE ~/ new int64.fromInts(0x00300000, 0x00000000));
260
261 Expect.equals(new int64.fromInts(0x708, 0x002E9501),
262 int64.MAX_VALUE ~/ new int64.fromInt(0x123456));
263 Expect.equals(new int64.fromInt(0x3BDA9),
264 int64.MAX_VALUE % new int64.fromInt(0x123456));
265 }
266
267 void testFactorial() {
268
269 int64 _fact(int64 n) {
270 if (n.isZero()) {
271 return new int64.fromInt(1);
272 } else {
273 return n * _fact(n - new int64.fromInt(1));
274 }
275 }
276
277 int64 fact18 = _fact(new int64.fromInt(18));
278 int64 fact17 = _fact(new int64.fromInt(17));
279 Expect.equals(new int64.fromInt(18), fact18 ~/ fact17);
280 }
281
282 void testMinMax() {
283 Expect.equals(int64.MIN_VALUE, new int64.fromInt(1) << 63);
284 Expect.equals(int64.MAX_VALUE, -(int64.MIN_VALUE + new int64.fromInt(1)));
285 }
286
287 // Define % as Euclidean mod, with positive result for all arguments
288 void testMod() {
289 Expect.equals(new int64.fromInt(0), int64.ZERO % new int64.fromInt(1000));
290 Expect.equals(new int64.fromInt(0), int64.MIN_VALUE % int64.MIN_VALUE);
291 Expect.equals(new int64.fromInt(1000),
292 new int64.fromInt(1000) % int64.MIN_VALUE);
293 Expect.equals(new int64.fromInt(0),
294 int64.MIN_VALUE % new int64.fromInt(8192));
295 Expect.equals(new int64.fromInt(6145),
296 int64.MIN_VALUE % new int64.fromInt(8193));
297
298 Expect.equals(new int64.fromInt(7192),
299 new int64.fromInt(-1000) % new int64.fromInt(8192));
300 Expect.equals(new int64.fromInt(7193),
301 new int64.fromInt(-1000) % new int64.fromInt(8193));
302 Expect.equals(new int64.fromInt(5632),
303 new int64.fromInt(-1000000000) % new int64.fromInt(8192));
304 Expect.equals(new int64.fromInt(4808),
305 new int64.fromInt(-1000000000) % new int64.fromInt(8193));
306 Expect.equals(new int64.fromInt(2560),
307 new int64.fromInt(1000000000) % new int64.fromInt(8192));
308 Expect.equals(new int64.fromInt(3385),
309 new int64.fromInt(1000000000) % new int64.fromInt(8193));
310
311 Expect.equals(new int64.fromInts(0x0, 0x3ff),
312 int64.MAX_VALUE % new int64.fromInts(0x00000000, 0x00000400));
313 Expect.equals(new int64.fromInts(0x0, 0x3ffff),
314 int64.MAX_VALUE % new int64.fromInts(0x00000000, 0x00040000));
315 Expect.equals(new int64.fromInts(0x0, 0x3ffffff),
316 int64.MAX_VALUE % new int64.fromInts(0x00000000, 0x04000000));
317 Expect.equals(new int64.fromInts(0x3, 0xffffffff),
318 int64.MAX_VALUE % new int64.fromInts(0x00000004, 0x00000000));
319 Expect.equals(new int64.fromInts(0x3ff, 0xffffffff),
320 int64.MAX_VALUE % new int64.fromInts(0x00000400, 0x00000000));
321 Expect.equals(new int64.fromInts(0x3ffff, 0xffffffff),
322 int64.MAX_VALUE % new int64.fromInts(0x00040000, 0x00000000));
323 Expect.equals(new int64.fromInts(0x3ffffff, 0xffffffff),
324 int64.MAX_VALUE % new int64.fromInts(0x04000000, 0x00000000));
325
326 Expect.equals(new int64.fromInt(0x12345678.remainder(0x22)),
327 new int64.fromInt(0x12345678).remainder(new int64.fromInt(0x22)));
328 Expect.equals(new int64.fromInt(0x12345678.remainder(-0x22)),
329 new int64.fromInt(0x12345678).remainder(new int64.fromInt(-0x22)));
330 Expect.equals(new int64.fromInt(-0x12345678.remainder(-0x22)),
331 new int64.fromInt(-0x12345678).remainder(new int64.fromInt(-0x22)));
332 Expect.equals(new int64.fromInt(-0x12345678.remainder(0x22)),
333 new int64.fromInt(-0x12345678).remainder(new int64.fromInt(0x22)));
334 Expect.equals(new int64.fromInt(0x12345678.remainder(0x22)),
335 new int32.fromInt(0x12345678).remainder(new int64.fromInt(0x22)));
336 }
337
338 void testMultiplicative() {
339 Expect.equals(new int64.fromInt(3333),
340 new int64.fromInt(1111) * new int64.fromInt(3));
341 Expect.equals(new int64.fromInt(-3333),
342 new int64.fromInt(1111) * new int64.fromInt(-3));
343 Expect.equals(new int64.fromInt(-3333),
344 new int64.fromInt(-1111) * new int64.fromInt(3));
345 Expect.equals(new int64.fromInt(3333),
346 new int64.fromInt(-1111) * new int64.fromInt(-3));
347 Expect.equals(new int64.fromInt(0),
348 new int64.fromInt(100) * new int64.fromInt(0));
349
350 Expect.equals(new int64.fromInts(0x7ff63f7c, 0x1df4d840),
351 new int64.fromInts(0x12345678, 0x12345678) *
352 new int64.fromInts(0x1234, 0x12345678));
353 Expect.equals(new int64.fromInts(0x7ff63f7c, 0x1df4d840),
354 new int64.fromInts(0xf2345678, 0x12345678) *
355 new int64.fromInts(0x1234, 0x12345678));
356 Expect.equals(new int64.fromInts(0x297e3f7c, 0x1df4d840),
357 new int64.fromInts(0xf2345678, 0x12345678) *
358 new int64.fromInts(0xffff1234, 0x12345678));
359
360 Expect.equals(new int64.fromInt(0), int64.MIN_VALUE * new int64.fromInt(2));
361 Expect.equals(int64.MIN_VALUE, int64.MIN_VALUE * new int64.fromInt(1));
362 Expect.equals(int64.MIN_VALUE, int64.MIN_VALUE * new int64.fromInt(-1));
363
364 Expect.equals(new int64.fromInt(1), new int64.fromInt(5) ~/
365 new int64.fromInt(5));
366 Expect.equals(new int64.fromInt(333), new int64.fromInt(1000) ~/
367 new int64.fromInt(3));
368 Expect.equals(new int64.fromInt(-333), new int64.fromInt(1000) ~/
369 new int64.fromInt(-3));
370 Expect.equals(new int64.fromInt(-333), new int64.fromInt(-1000) ~/
371 new int64.fromInt(3));
372 Expect.equals(new int64.fromInt(333), new int64.fromInt(-1000) ~/
373 new int64.fromInt(-3));
374 Expect.equals(new int64.fromInt(0), new int64.fromInt(3) ~/
375 new int64.fromInt(1000));
376 Expect.equals(new int64.fromInts(0x1003d0, 0xe84f5ae8), new int64.fromInts(
377 0x12345678, 0x12345678) ~/ new int64.fromInts(0x0, 0x123));
378 Expect.equals(new int64.fromInts(0x0, 0x10003), new int64.fromInts(
379 0x12345678, 0x12345678) ~/ new int64.fromInts(0x1234, 0x12345678));
380 Expect.equals(new int64.fromInts(0xffffffff, 0xffff3dfe),
381 new int64.fromInts(0xf2345678, 0x12345678) ~/
382 new int64.fromInts(0x1234, 0x12345678));
383 Expect.equals(new int64.fromInts(0x0, 0xeda), new int64.fromInts(0xf2345678,
384 0x12345678) ~/ new int64.fromInts(0xffff1234, 0x12345678));
385
386 try {
387 new int64.fromInt(1) ~/ new int64.fromInt(0);
388 Expect.fail("Expected an IntegerDivisionByZeroException");
389 } catch (IntegerDivisionByZeroException e) {
390 }
391
392 Expect.equals(new int64.fromInts(0xc0000000, 0x00000000),
393 int64.MIN_VALUE ~/ new int64.fromInt(2));
394 Expect.equals(int64.MIN_VALUE, int64.MIN_VALUE ~/
395 new int64.fromInt(1));
396 Expect.equals(int64.MIN_VALUE, int64.MIN_VALUE ~/
397 new int64.fromInt(-1));
398 }
399
400 void testNegate() {
401 Expect.equals(new int64.fromInt(-1), -new int64.fromInt(1));
402 Expect.equals(new int64.fromInt(1), -new int64.fromInt(-1));
403 Expect.equals(int64.MIN_VALUE, -int64.MIN_VALUE);
404 }
405
406 void testShift() {
407 Expect.equals("-1125899906842624", (int64.MIN_VALUE >> 13).toString());
408 Expect.equals(new int64.fromInts(0xd048d115, 0x9d159c00),
409 new int64.fromInts(0x12341234, 0x45674567) << 10);
410 Expect.equals(new int64.fromInts(0x48d04, 0x8d1159d1),
411 new int64.fromInts(0x12341234, 0x45674567) >> 10);
412 Expect.equals(new int64.fromInts(0x48d04, 0x8d1159d1),
413 new int64.fromInts(0x12341234, 0x45674567).shiftRightUnsigned(10));
414 Expect.equals(new int64.fromInts(0xd048d115, 0x9d159c00),
415 new int64.fromInts(0x92341234, 0x45674567) << 10);
416 Expect.equals(new int64.fromInts(0xffe48d04, 0x8d1159d1),
417 new int64.fromInts(0x92341234, 0x45674567) >> 10);
418 Expect.equals(new int64.fromInt(67108863),
419 new int64.fromInts(0xFFFFFFF, 0xFFFFFFFF) >> 34);
420 Expect.equals(new int64.fromInts(0x248d04, 0x8d1159d1),
421 new int64.fromInts(0x92341234, 0x45674567).shiftRightUnsigned(10));
422
423 for (int n = 0; n <= 66; n++) {
424 Expect.equals(new int64.fromInt(-1), new int64.fromInt(-1) >> n);
425 }
426
427 Expect.equals(new int64.fromInt(-1 << 5), new int64.fromInt(-1) << 5);
428 Expect.equals(new int64.fromInt(-1), new int64.fromInt(-1) << 0);
429 Expect.equals(-new int64.fromInts(0x40000000, 0x00000000),
430 (new int64.fromInt(1) << 63) >> 1);
431 Expect.equals(new int64.fromInt(0), (new int64.fromInt(-1) << 32) << 32);
432 Expect.equals(int64.MIN_VALUE, int64.MIN_VALUE << 0);
433 Expect.equals(new int64.fromInt(0), int64.MIN_VALUE << 1);
434 Expect.equals(new int64.fromInts(0xfffffffc, 0x00000000),
435 (-new int64.fromInts(8, 0)) >> 1);
436 Expect.equals(new int64.fromInts(0x7ffffffc, 0x0),
437 (-new int64.fromInts(8, 0)).shiftRightUnsigned(1));
438
439 Expect.equals(new int64.fromInts(0x00723456, 0x789abcde),
440 new int64.fromInts(0x72345678, 0x9abcdef0) >> 8);
441 Expect.equals(new int64.fromInts(0x00007234, 0x56789abc),
442 new int64.fromInts(0x72345678, 0x9abcdef0) >> 16);
443 Expect.equals(new int64.fromInts(0x00000072, 0x3456789a),
444 new int64.fromInts(0x72345678, 0x9abcdef0) >> 24);
445 Expect.equals(new int64.fromInts(0x00000007, 0x23456789),
446 new int64.fromInts(0x72345678, 0x9abcdef0) >> 28);
447 Expect.equals(new int64.fromInts(0x00000000, 0x72345678),
448 new int64.fromInts(0x72345678, 0x9abcdef0) >> 32);
449 Expect.equals(new int64.fromInts(0x00000000, 0x07234567),
450 new int64.fromInts(0x72345678, 0x9abcdef0) >> 36);
451 Expect.equals(new int64.fromInts(0x00000000, 0x00723456),
452 new int64.fromInts(0x72345678, 0x9abcdef0) >> 40);
453 Expect.equals(new int64.fromInts(0x00000000, 0x00072345),
454 new int64.fromInts(0x72345678, 0x9abcde00) >> 44);
455 Expect.equals(new int64.fromInts(0x00000000, 0x00007234),
456 new int64.fromInts(0x72345678, 0x9abcdef0) >> 48);
457
458 Expect.equals(new int64.fromInts(0x00723456, 0x789abcde),
459 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(8));
460 Expect.equals(new int64.fromInts(0x00007234, 0x56789abc),
461 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(16));
462 Expect.equals(new int64.fromInts(0x00000072, 0x3456789a),
463 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(24));
464 Expect.equals(new int64.fromInts(0x00000007, 0x23456789),
465 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(28));
466 Expect.equals(new int64.fromInts(0x00000000, 0x72345678),
467 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(32));
468 Expect.equals(new int64.fromInts(0x00000000, 0x07234567),
469 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(36));
470 Expect.equals(new int64.fromInts(0x00000000, 0x00723456),
471 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(40));
472 Expect.equals(new int64.fromInts(0x00000000, 0x00072345),
473 new int64.fromInts(0x72345678, 0x9abcde00).shiftRightUnsigned(44));
474 Expect.equals(new int64.fromInts(0x00000000, 0x00007234),
475 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(48));
476
477 Expect.equals(new int64.fromInts(0xff923456, 0x789abcde),
478 new int64.fromInts(0x92345678, 0x9abcdef0) >> 8);
479 Expect.equals(new int64.fromInts(0xffff9234, 0x56789abc),
480 new int64.fromInts(0x92345678, 0x9abcdef0) >> 16);
481
482 Expect.equals(new int64.fromInts(0xffffff92, 0x3456789a),
483 new int64.fromInts(0x92345678, 0x9abcdef0) >> 24);
484 Expect.equals(new int64.fromInts(0xfffffff9, 0x23456789),
485 new int64.fromInts(0x92345678, 0x9abcdef0) >> 28);
486 Expect.equals(new int64.fromInts(0xffffffff, 0x92345678),
487 new int64.fromInts(0x92345678, 0x9abcdef0) >> 32);
488 Expect.equals(new int64.fromInts(0xffffffff, 0xf9234567),
489 new int64.fromInts(0x92345678, 0x9abcdef0) >> 36);
490 Expect.equals(new int64.fromInts(0xffffffff, 0xff923456),
491 new int64.fromInts(0x92345678, 0x9abcdef0) >> 40);
492 Expect.equals(new int64.fromInts(0xffffffff, 0xfff92345),
493 new int64.fromInts(0x92345678, 0x9abcdef0) >> 44);
494 Expect.equals(new int64.fromInts(0xffffffff, 0xffff9234),
495 new int64.fromInts(0x92345678, 0x9abcdef0) >> 48);
496
497 Expect.equals(new int64.fromInts(0x00923456, 0x789abcde),
498 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(8));
499 Expect.equals(new int64.fromInts(0x00009234, 0x56789abc),
500 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(16));
501 Expect.equals(new int64.fromInts(0x00000092, 0x3456789a),
502 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(24));
503 Expect.equals(new int64.fromInts(0x00000009, 0x23456789),
504 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(28));
505 Expect.equals(new int64.fromInts(0x00000000, 0x92345678),
506 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(32));
507 Expect.equals(new int64.fromInts(0x00000000, 0x09234567),
508 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(36));
509 Expect.equals(new int64.fromInts(0x00000000, 0x00923456),
510 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(40));
511 Expect.equals(new int64.fromInts(0x00000000, 0x00092345),
512 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(44));
513 Expect.equals(new int64.fromInts(0x00000000, 0x00009234),
514 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(48));
515
516 try {
517 new int64.fromInt(17) >> -1;
518 Expect.fail("x >> -1 should throw IllegalArgumentException");
519 } catch (IllegalArgumentException e) {
520 }
521
522 try {
523 new int64.fromInt(17) << -1;
524 Expect.fail("x >> -1 should throw IllegalArgumentException");
525 } catch (IllegalArgumentException e) {
526 }
527
528 try {
529 new int64.fromInt(17).shiftRightUnsigned(-1);
530 Expect.fail("x >> -1 should throw IllegalArgumentException");
531 } catch (IllegalArgumentException e) {
532 }
533
534 }
535
536 void testToHexString() {
537 int64 deadbeef12341234 = new int64.fromInts(0xDEADBEEF, 0x12341234);
538 Expect.equals("0", int64.ZERO.toHexString());
539 Expect.equals("DEADBEEF12341234", deadbeef12341234.toHexString());
540 }
541
542 void testToString() {
543 Expect.equals("0", new int64.fromInt(0).toString());
544 Expect.equals("1", new int64.fromInt(1).toString());
545 Expect.equals("-1", new int64.fromInt(-1).toString());
546 Expect.equals("-10", new int64.fromInt(-10).toString());
547 Expect.equals("-9223372036854775808", int64.MIN_VALUE.toString());
548 Expect.equals("9223372036854775807", int64.MAX_VALUE.toString());
549
550 int top = 922337201;
551 int bottom = 967490662;
552 int64 fullnum = (new int64.fromInt(1000000000) * new int64.fromInt(top)) +
553 new int64.fromInt(bottom);
554
555 Expect.equals("922337201967490662", fullnum.toString());
556 Expect.equals("-922337201967490662", (-fullnum).toString());
557
558 Expect.equals("17678A7DEF01234",
559 new int64.fromInts(0x17678A7, 0xDEF01234).toHexString());
560
561 Expect.equals("123456789", new int64.fromInt(123456789).toString());
562 Expect.equals("75BCD15", new int64.fromInt(123456789).toHexString());
563 Expect.equals("223101104124", new int64.fromInt(123456789).toRadixString(5));
564
565 Expect.equals(
566 "-1000000000000000000000000000000000000000000000000000000000000000",
567 int64.MIN_VALUE.toRadixString(2));
568 Expect.equals("-2021110011022210012102010021220101220222",
569 int64.MIN_VALUE.toRadixString(3));
570 Expect.equals("-20000000000000000000000000000000",
571 int64.MIN_VALUE.toRadixString(4));
572 Expect.equals("-1104332401304422434310311213",
573 int64.MIN_VALUE.toRadixString(5));
574 Expect.equals("-1540241003031030222122212", int64.MIN_VALUE.toRadixString(6));
575 Expect.equals("-22341010611245052052301", int64.MIN_VALUE.toRadixString(7));
576 Expect.equals("-1000000000000000000000", int64.MIN_VALUE.toRadixString(8));
577 Expect.equals("-67404283172107811828", int64.MIN_VALUE.toRadixString(9));
578 Expect.equals("-9223372036854775808", int64.MIN_VALUE.toRadixString(10));
579 Expect.equals("-1728002635214590698", int64.MIN_VALUE.toRadixString(11));
580 Expect.equals("-41A792678515120368", int64.MIN_VALUE.toRadixString(12));
581 Expect.equals("-10B269549075433C38", int64.MIN_VALUE.toRadixString(13));
582 Expect.equals("-4340724C6C71DC7A8", int64.MIN_VALUE.toRadixString(14));
583 Expect.equals("-160E2AD3246366808", int64.MIN_VALUE.toRadixString(15));
584 Expect.equals("-8000000000000000", int64.MIN_VALUE.toRadixString(16));
585
586 Expect.equals(
587 "111111111111111111111111111111111111111111111111111111111111111",
588 int64.MAX_VALUE.toRadixString(2));
589 Expect.equals("2021110011022210012102010021220101220221",
590 int64.MAX_VALUE.toRadixString(3));
591 Expect.equals("13333333333333333333333333333333",
592 int64.MAX_VALUE.toRadixString(4));
593 Expect.equals("1104332401304422434310311212",
594 int64.MAX_VALUE.toRadixString(5));
595 Expect.equals("1540241003031030222122211", int64.MAX_VALUE.toRadixString(6));
596 Expect.equals("22341010611245052052300", int64.MAX_VALUE.toRadixString(7));
597 Expect.equals("777777777777777777777", int64.MAX_VALUE.toRadixString(8));
598 Expect.equals("67404283172107811827", int64.MAX_VALUE.toRadixString(9));
599 Expect.equals("9223372036854775807", int64.MAX_VALUE.toRadixString(10));
600 Expect.equals("1728002635214590697", int64.MAX_VALUE.toRadixString(11));
601 Expect.equals("41A792678515120367", int64.MAX_VALUE.toRadixString(12));
602 Expect.equals("10B269549075433C37", int64.MAX_VALUE.toRadixString(13));
603 Expect.equals("4340724C6C71DC7A7", int64.MAX_VALUE.toRadixString(14));
604 Expect.equals("160E2AD3246366807", int64.MAX_VALUE.toRadixString(15));
605 Expect.equals("7FFFFFFFFFFFFFFF", int64.MAX_VALUE.toRadixString(16));
606 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698