OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Testing Bigints with and without intrinsics. | |
6 // VMOptions= | |
7 // VMOptions=--no_intrinsify | |
8 // VMOptions=--optimization_counter_threshold=10 --no-background_compilation | |
9 | |
10 library big_integer_test; | |
11 | |
12 import "package:expect/expect.dart"; | |
13 | |
14 foo() => 1234567890123456789; | |
15 bar() => 12345678901234567890; | |
16 | |
17 testSmiOverflow() { | |
18 var a = 1073741823; | |
19 var b = 1073741822; | |
20 Expect.equals(2147483645, a + b); | |
21 a = -1000000000; | |
22 b = 1000000001; | |
23 Expect.equals(-2000000001, a - b); | |
24 Expect.equals(-1000000001000000000, a * b); | |
25 } | |
26 | |
27 testBigintAdd() { | |
28 // Bigint and Smi. | |
29 var a = 12345678901234567890; | |
30 var b = 2; | |
31 Expect.equals(12345678901234567892, a + b); | |
32 Expect.equals(12345678901234567892, b + a); | |
33 // Bigint and Bigint. | |
34 a = 10000000000000000001; | |
35 Expect.equals(20000000000000000002, a + a); | |
36 // Bigint and double. | |
37 a = 100000000000000000000.0; | |
38 b = 200000000000000000000; | |
39 Expect.isTrue((a + b) is double); | |
40 Expect.equals(300000000000000000000.0, a + b); | |
41 Expect.isTrue((b + a) is double); | |
42 Expect.equals(300000000000000000000.0, b + a); | |
43 } | |
44 | |
45 testBigintSub() { | |
46 // Bigint and Smi. | |
47 var a = 12345678901234567890; | |
48 var b = 2; | |
49 Expect.equals(12345678901234567888, a - b); | |
50 Expect.equals(-12345678901234567888, b - a); | |
51 // Bigint and Bigint. | |
52 a = 10000000000000000001; | |
53 Expect.equals(20000000000000000002, a + a); | |
54 // Bigint and double. | |
55 a = 100000000000000000000.0; | |
56 b = 200000000000000000000; | |
57 Expect.isTrue((a + b) is double); | |
58 Expect.equals(-100000000000000000000.0, a - b); | |
59 Expect.isTrue((b + a) is double); | |
60 Expect.equals(100000000000000000000.0, b - a); | |
61 Expect.equals(-1, 0xF00000000 - 0xF00000001); | |
62 } | |
63 | |
64 testBigintMul() { | |
65 // Bigint and Smi. | |
66 var a = 12345678901234567890; | |
67 var b = 10; | |
68 Expect.equals(123456789012345678900, a * b); | |
69 Expect.equals(123456789012345678900, b * a); | |
70 // Bigint and Bigint. | |
71 a = 12345678901234567890; | |
72 b = 10000000000000000; | |
73 Expect.equals(123456789012345678900000000000000000, a * b); | |
74 // Bigint and double. | |
75 a = 2.0; | |
76 b = 200000000000000000000; | |
77 Expect.isTrue((a * b) is double); | |
78 Expect.equals(400000000000000000000.0, a * b); | |
79 Expect.isTrue((b * a) is double); | |
80 Expect.equals(400000000000000000000.0, b * a); | |
81 } | |
82 | |
83 testBigintTruncDiv() { | |
84 var a = 12345678901234567890; | |
85 var b = 10; | |
86 // Bigint and Smi. | |
87 Expect.equals(1234567890123456789, a ~/ b); | |
88 Expect.equals(0, b ~/ a); | |
89 Expect.equals(123456789, 123456789012345678 ~/ 1000000000); | |
90 // Bigint and Bigint. | |
91 a = 12345678901234567890; | |
92 b = 10000000000000000; | |
93 Expect.equals(1234, a ~/ b); | |
94 // Bigint and double. | |
95 a = 100000000000000000000.0; | |
96 b = 200000000000000000000; | |
97 Expect.equals(0, a ~/ b); | |
98 Expect.equals(2, b ~/ a); | |
99 } | |
100 | |
101 testBigintDiv() { | |
102 // Bigint and Smi. | |
103 Expect.equals(1234567890123456789.1, 12345678901234567891 / 10); | |
104 Expect.equals(0.000000001234, 1234 / 1000000000000); | |
105 Expect.equals(12345678901234000000.0, 123456789012340000000 / 10); | |
106 // Bigint and Bigint. | |
107 var a = 12345670000000000000; | |
108 var b = 10000000000000000; | |
109 Expect.equals(1234.567, a / b); | |
110 // Bigint and double. | |
111 a = 400000000000000000000.0; | |
112 b = 200000000000000000000; | |
113 Expect.equals(2.0, a / b); | |
114 Expect.equals(0.5, b / a); | |
115 } | |
116 | |
117 testBigintModulo() { | |
118 // Bigint and Smi. | |
119 var a = 1000000000005; | |
120 var b = 10; | |
121 Expect.equals(5, a % b); | |
122 Expect.equals(10, b % a); | |
123 // Bigint & Bigint | |
124 a = 10000000000000000001; | |
125 b = 10000000000000000000; | |
126 Expect.equals(1, a % b); | |
127 Expect.equals(10000000000000000000, b % a); | |
128 // Bigint & double. | |
129 a = 10000000100000000.0; | |
130 b = 10000000000000000; | |
131 Expect.equals(100000000.0, a % b); | |
132 Expect.equals(10000000000000000.0, b % a); | |
133 // Transitioning from Mint to Bigint. | |
134 var iStart = 4611686018427387900; | |
135 var prevX = -23 % iStart; | |
136 for (int i = iStart + 1; i < iStart + 10; i++) { | |
137 var x = -23 % i; | |
138 Expect.equals(1, x - prevX); | |
139 Expect.isTrue(x > 0); | |
140 prevX = x; | |
141 } | |
142 } | |
143 | |
144 testBigintModPow() { | |
145 var x, e, m; | |
146 x = 1234567890; | |
147 e = 1000000001; | |
148 m = 19; | |
149 Expect.equals(11, x.modPow(e, m)); | |
150 x = 1234567890; | |
151 e = 19; | |
152 m = 1000000001; | |
153 Expect.equals(122998977, x.modPow(e, m)); | |
154 x = 19; | |
155 e = 1234567890; | |
156 m = 1000000001; | |
157 Expect.equals(619059596, x.modPow(e, m)); | |
158 x = 19; | |
159 e = 1000000001; | |
160 m = 1234567890; | |
161 Expect.equals(84910879, x.modPow(e, m)); | |
162 x = 1000000001; | |
163 e = 19; | |
164 m = 1234567890; | |
165 Expect.equals(872984351, x.modPow(e, m)); | |
166 x = 1000000001; | |
167 e = 1234567890; | |
168 m = 19; | |
169 Expect.equals(0, x.modPow(e, m)); | |
170 x = 12345678901234567890; | |
171 e = 10000000000000000001; | |
172 m = 19; | |
173 Expect.equals(2, x.modPow(e, m)); | |
174 x = 12345678901234567890; | |
175 e = 19; | |
176 m = 10000000000000000001; | |
177 Expect.equals(3239137215315834625, x.modPow(e, m)); | |
178 x = 19; | |
179 e = 12345678901234567890; | |
180 m = 10000000000000000001; | |
181 Expect.equals(4544207837373941034, x.modPow(e, m)); | |
182 x = 19; | |
183 e = 10000000000000000001; | |
184 m = 12345678901234567890; | |
185 Expect.equals(11135411705397624859, x.modPow(e, m)); | |
186 x = 10000000000000000001; | |
187 e = 19; | |
188 m = 12345678901234567890; | |
189 Expect.equals(2034013733189773841, x.modPow(e, m)); | |
190 x = 10000000000000000001; | |
191 e = 12345678901234567890; | |
192 m = 19; | |
193 Expect.equals(1, x.modPow(e, m)); | |
194 x = 12345678901234567890; | |
195 e = 19; | |
196 m = 10000000000000000001; | |
197 Expect.equals(3239137215315834625, x.modPow(e, m)); | |
198 x = 12345678901234567890; | |
199 e = 10000000000000000001; | |
200 m = 19; | |
201 Expect.equals(2, x.modPow(e, m)); | |
202 x = 123456789012345678901234567890; | |
203 e = 123456789012345678901234567891; | |
204 m = 123456789012345678901234567899; | |
205 Expect.equals(116401406051033429924651549616, x.modPow(e, m)); | |
206 x = 123456789012345678901234567890; | |
207 e = 123456789012345678901234567899; | |
208 m = 123456789012345678901234567891; | |
209 Expect.equals(123456789012345678901234567890, x.modPow(e, m)); | |
210 x = 123456789012345678901234567899; | |
211 e = 123456789012345678901234567890; | |
212 m = 123456789012345678901234567891; | |
213 Expect.equals(35088523091000351053091545070, x.modPow(e, m)); | |
214 x = 123456789012345678901234567899; | |
215 e = 123456789012345678901234567891; | |
216 m = 123456789012345678901234567890; | |
217 Expect.equals(18310047270234132455316941949, x.modPow(e, m)); | |
218 x = 123456789012345678901234567891; | |
219 e = 123456789012345678901234567899; | |
220 m = 123456789012345678901234567890; | |
221 Expect.equals(1, x.modPow(e, m)); | |
222 x = 123456789012345678901234567891; | |
223 e = 123456789012345678901234567890; | |
224 m = 123456789012345678901234567899; | |
225 Expect.equals(40128068573873018143207285483, x.modPow(e, m)); | |
226 } | |
227 | |
228 testBigintModInverse() { | |
229 var x, m; | |
230 x = 1; | |
231 m = 1; | |
232 Expect.equals(0, x.modInverse(m)); | |
233 x = 0; | |
234 m = 1000000001; | |
235 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. | |
236 x = 1234567890; | |
237 m = 19; | |
238 Expect.equals(11, x.modInverse(m)); | |
239 x = 1234567890; | |
240 m = 1000000001; | |
241 Expect.equals(189108911, x.modInverse(m)); | |
242 x = 19; | |
243 m = 1000000001; | |
244 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. | |
245 x = 19; | |
246 m = 1234567890; | |
247 Expect.equals(519818059, x.modInverse(m)); | |
248 x = 1000000001; | |
249 m = 1234567890; | |
250 Expect.equals(1001100101, x.modInverse(m)); | |
251 x = 1000000001; | |
252 m = 19; | |
253 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. | |
254 x = 12345678901234567890; | |
255 m = 19; | |
256 Expect.equals(3, x.modInverse(m)); | |
257 x = 12345678901234567890; | |
258 m = 10000000000000000001; | |
259 Expect.equals(9736746307686209582, x.modInverse(m)); | |
260 x = 19; | |
261 m = 10000000000000000001; | |
262 Expect.equals(6315789473684210527, x.modInverse(m)); | |
263 x = 19; | |
264 m = 12345678901234567890; | |
265 Expect.equals(10396361179987004539, x.modInverse(m)); | |
266 x = 10000000000000000001; | |
267 m = 12345678901234567890; | |
268 Expect.equals(325004555487045911, x.modInverse(m)); | |
269 x = 10000000000000000001; | |
270 m = 19; | |
271 Expect.equals(7, x.modInverse(m)); | |
272 x = 12345678901234567890; | |
273 m = 10000000000000000001; | |
274 Expect.equals(9736746307686209582, x.modInverse(m)); | |
275 x = 12345678901234567890; | |
276 m = 19; | |
277 Expect.equals(3, x.modInverse(m)); | |
278 x = 123456789012345678901234567890; | |
279 m = 123456789012345678901234567899; | |
280 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. | |
281 x = 123456789012345678901234567890; | |
282 m = 123456789012345678901234567891; | |
283 Expect.equals(123456789012345678901234567890, x.modInverse(m)); | |
284 x = 123456789012345678901234567899; | |
285 m = 123456789012345678901234567891; | |
286 Expect.equals(77160493132716049313271604932, x.modInverse(m)); | |
287 x = 123456789012345678901234567899; | |
288 m = 123456789012345678901234567890; | |
289 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. | |
290 x = 123456789012345678901234567891; | |
291 m = 123456789012345678901234567890; | |
292 Expect.equals(1, x.modInverse(m)); | |
293 x = 123456789012345678901234567891; | |
294 m = 123456789012345678901234567899; | |
295 Expect.equals(46296295879629629587962962962, x.modInverse(m)); | |
296 } | |
297 | |
298 testBigintGcd() { | |
299 var x, m; | |
300 x = 1; | |
301 m = 1; | |
302 Expect.equals(1, x.gcd(m)); | |
303 x = 693; | |
304 m = 609; | |
305 Expect.equals(21, x.gcd(m)); | |
306 x = 693 << 40; | |
307 m = 609 << 40; | |
308 Expect.equals(21 << 40, x.gcd(m)); | |
309 x = 609 << 40; | |
310 m = 693 << 40; | |
311 Expect.equals(21 << 40, x.gcd(m)); | |
312 x = 0; | |
313 m = 1000000001; | |
314 Expect.equals(m, x.gcd(m)); | |
315 x = 1000000001; | |
316 m = 0; | |
317 Expect.equals(x, x.gcd(m)); | |
318 x = 0; | |
319 m = -1000000001; | |
320 Expect.equals(-m, x.gcd(m)); | |
321 x = -1000000001; | |
322 m = 0; | |
323 Expect.equals(-x, x.gcd(m)); | |
324 x = 0; | |
325 m = 0; | |
326 Expect.equals(0, x.gcd(m)); | |
327 x = 0; | |
328 m = 123456789012345678901234567890; | |
329 Expect.equals(m, x.gcd(m)); | |
330 x = 123456789012345678901234567890; | |
331 m = 0; | |
332 Expect.equals(x, x.gcd(m)); | |
333 x = 0; | |
334 m = -123456789012345678901234567890; | |
335 Expect.equals(-m, x.gcd(m)); | |
336 x = -123456789012345678901234567890; | |
337 m = 0; | |
338 Expect.equals(-x, x.gcd(m)); | |
339 x = 1234567890; | |
340 m = 19; | |
341 Expect.equals(1, x.gcd(m)); | |
342 x = 1234567890; | |
343 m = 1000000001; | |
344 Expect.equals(1, x.gcd(m)); | |
345 x = 19; | |
346 m = 1000000001; | |
347 Expect.equals(19, x.gcd(m)); | |
348 x = 19; | |
349 m = 1234567890; | |
350 Expect.equals(1, x.gcd(m)); | |
351 x = 1000000001; | |
352 m = 1234567890; | |
353 Expect.equals(1, x.gcd(m)); | |
354 x = 1000000001; | |
355 m = 19; | |
356 Expect.equals(19, x.gcd(m)); | |
357 x = 12345678901234567890; | |
358 m = 19; | |
359 Expect.equals(1, x.gcd(m)); | |
360 x = 12345678901234567890; | |
361 m = 10000000000000000001; | |
362 Expect.equals(1, x.gcd(m)); | |
363 x = 19; | |
364 m = 10000000000000000001; | |
365 Expect.equals(1, x.gcd(m)); | |
366 x = 19; | |
367 m = 12345678901234567890; | |
368 Expect.equals(1, x.gcd(m)); | |
369 x = 10000000000000000001; | |
370 m = 12345678901234567890; | |
371 Expect.equals(1, x.gcd(m)); | |
372 x = 10000000000000000001; | |
373 m = 19; | |
374 Expect.equals(1, x.gcd(m)); | |
375 x = 12345678901234567890; | |
376 m = 10000000000000000001; | |
377 Expect.equals(1, x.gcd(m)); | |
378 x = 12345678901234567890; | |
379 m = 19; | |
380 Expect.equals(1, x.gcd(m)); | |
381 x = 123456789012345678901234567890; | |
382 m = 123456789012345678901234567899; | |
383 Expect.equals(9, x.gcd(m)); | |
384 x = 123456789012345678901234567890; | |
385 m = 123456789012345678901234567891; | |
386 Expect.equals(1, x.gcd(m)); | |
387 x = 123456789012345678901234567899; | |
388 m = 123456789012345678901234567891; | |
389 Expect.equals(1, x.gcd(m)); | |
390 x = 123456789012345678901234567899; | |
391 m = 123456789012345678901234567890; | |
392 Expect.equals(9, x.gcd(m)); | |
393 x = 123456789012345678901234567891; | |
394 m = 123456789012345678901234567890; | |
395 Expect.equals(1, x.gcd(m)); | |
396 x = 123456789012345678901234567891; | |
397 m = 123456789012345678901234567899; | |
398 Expect.equals(1, x.gcd(m)); | |
399 } | |
400 | |
401 testBigintNegate() { | |
402 var a = 0xF000000000000000F; | |
403 var b = ~a; // negate. | |
404 Expect.equals(-0xF0000000000000010, b); | |
405 Expect.equals(0, a & b); | |
406 Expect.equals(-1, a | b); | |
407 } | |
408 | |
409 testShiftAmount() { | |
410 Expect.equals(0, 12 >> 111111111111111111111111111111); | |
411 Expect.equals(-1, -12 >> 111111111111111111111111111111); | |
412 bool exceptionCaught = false; | |
413 try { | |
414 var a = 1 << 1111111111111111111111111111; | |
415 } on OutOfMemoryError catch (e) { | |
416 exceptionCaught = true; | |
417 } | |
418 Expect.equals(true, exceptionCaught); | |
419 } | |
420 | |
421 main() { | |
422 for (int i = 0; i < 10; i++) { | |
423 Expect.equals(1234567890123456789, foo()); | |
424 Expect.equals(12345678901234567890, bar()); | |
425 testSmiOverflow(); // //# overflow: ok | |
426 testBigintAdd(); // //# add: ok | |
427 testBigintSub(); // //# sub: ok | |
428 testBigintMul(); // //# mul: ok | |
429 testBigintTruncDiv(); // //# trunDiv: ok | |
430 testBigintDiv(); // //# div: ok | |
431 testBigintModulo(); // //# mod: ok | |
432 testBigintModPow(); // //# modPow: ok | |
433 testBigintModInverse(); // //# modInv: ok | |
434 testBigintGcd(); // //# gcd: ok | |
435 testBigintNegate(); // //# negate: ok | |
436 testShiftAmount(); // //# shift: ok | |
437 Expect.equals(12345678901234567890, (12345678901234567890).abs()); | |
438 Expect.equals(12345678901234567890, (-12345678901234567890).abs()); | |
439 var a = 10000000000000000000; | |
440 var b = 10000000000000000001; | |
441 Expect.equals(false, a.hashCode == b.hashCode); | |
442 Expect.equals(true, a.hashCode == (b - 1).hashCode); | |
443 } | |
444 } | |
OLD | NEW |