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