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

Side by Side Diff: pkg/fixnum/test/int_64_test.dart

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

Powered by Google App Engine
This is Rietveld 408576698