| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 // Testing Bigints with and without intrinsics. | 5 // Testing Bigints with and without intrinsics. |
| 6 // VMOptions= | 6 // VMOptions= |
| 7 // VMOptions=--no_intrinsify | 7 // VMOptions=--no_intrinsify |
| 8 // VMOptions=--no-background-compilation | 8 // VMOptions=--no-background-compilation |
| 9 // VMOptions=--optimization_counter_threshold=10 --no-background_compilation | 9 // VMOptions=--optimization_counter_threshold=10 --no-background_compilation |
| 10 | 10 |
| 11 library big_integer_test; | 11 library big_integer_test; |
| 12 |
| 12 import "package:expect/expect.dart"; | 13 import "package:expect/expect.dart"; |
| 13 | 14 |
| 14 foo() => 1234567890123456789; | 15 foo() => 1234567890123456789; |
| 15 bar() => 12345678901234567890; | 16 bar() => 12345678901234567890; |
| 16 | 17 |
| 17 testSmiOverflow() { | 18 testSmiOverflow() { |
| 18 var a = 1073741823; | 19 var a = 1073741823; |
| 19 var b = 1073741822; | 20 var b = 1073741822; |
| 20 Expect.equals(2147483645, a + b); | 21 Expect.equals(2147483645, a + b); |
| 21 a = -1000000000; | 22 a = -1000000000; |
| 22 b = 1000000001; | 23 b = 1000000001; |
| 23 Expect.equals(-2000000001, a - b); | 24 Expect.equals(-2000000001, a - b); |
| 24 Expect.equals(-1000000001000000000, a * b); | 25 Expect.equals(-1000000001000000000, a * b); |
| 25 } | 26 } |
| 26 | 27 |
| 27 testBigintAdd() { | 28 testBigintAdd() { |
| 28 // Bigint and Smi. | 29 // Bigint and Smi. |
| 29 var a = 12345678901234567890; | 30 var a = 12345678901234567890; |
| 30 var b = 2; | 31 var b = 2; |
| 31 Expect.equals(12345678901234567892, a + b); | 32 Expect.equals(12345678901234567892, a + b); |
| 32 Expect.equals(12345678901234567892, b + a); | 33 Expect.equals(12345678901234567892, b + a); |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 Expect.equals(40128068573873018143207285483, x.modPow(e, m)); | 226 Expect.equals(40128068573873018143207285483, x.modPow(e, m)); |
| 226 } | 227 } |
| 227 | 228 |
| 228 testBigintModInverse() { | 229 testBigintModInverse() { |
| 229 var x, m; | 230 var x, m; |
| 230 x = 1; | 231 x = 1; |
| 231 m = 1; | 232 m = 1; |
| 232 Expect.equals(0, x.modInverse(m)); | 233 Expect.equals(0, x.modInverse(m)); |
| 233 x = 0; | 234 x = 0; |
| 234 m = 1000000001; | 235 m = 1000000001; |
| 235 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. | 236 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. |
| 236 x = 1234567890; | 237 x = 1234567890; |
| 237 m = 19; | 238 m = 19; |
| 238 Expect.equals(11, x.modInverse(m)); | 239 Expect.equals(11, x.modInverse(m)); |
| 239 x = 1234567890; | 240 x = 1234567890; |
| 240 m = 1000000001; | 241 m = 1000000001; |
| 241 Expect.equals(189108911, x.modInverse(m)); | 242 Expect.equals(189108911, x.modInverse(m)); |
| 242 x = 19; | 243 x = 19; |
| 243 m = 1000000001; | 244 m = 1000000001; |
| 244 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. | 245 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. |
| 245 x = 19; | 246 x = 19; |
| 246 m = 1234567890; | 247 m = 1234567890; |
| 247 Expect.equals(519818059, x.modInverse(m)); | 248 Expect.equals(519818059, x.modInverse(m)); |
| 248 x = 1000000001; | 249 x = 1000000001; |
| 249 m = 1234567890; | 250 m = 1234567890; |
| 250 Expect.equals(1001100101, x.modInverse(m)); | 251 Expect.equals(1001100101, x.modInverse(m)); |
| 251 x = 1000000001; | 252 x = 1000000001; |
| 252 m = 19; | 253 m = 19; |
| 253 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. | 254 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. |
| 254 x = 12345678901234567890; | 255 x = 12345678901234567890; |
| 255 m = 19; | 256 m = 19; |
| 256 Expect.equals(3, x.modInverse(m)); | 257 Expect.equals(3, x.modInverse(m)); |
| 257 x = 12345678901234567890; | 258 x = 12345678901234567890; |
| 258 m = 10000000000000000001; | 259 m = 10000000000000000001; |
| 259 Expect.equals(9736746307686209582, x.modInverse(m)); | 260 Expect.equals(9736746307686209582, x.modInverse(m)); |
| 260 x = 19; | 261 x = 19; |
| 261 m = 10000000000000000001; | 262 m = 10000000000000000001; |
| 262 Expect.equals(6315789473684210527, x.modInverse(m)); | 263 Expect.equals(6315789473684210527, x.modInverse(m)); |
| 263 x = 19; | 264 x = 19; |
| 264 m = 12345678901234567890; | 265 m = 12345678901234567890; |
| 265 Expect.equals(10396361179987004539, x.modInverse(m)); | 266 Expect.equals(10396361179987004539, x.modInverse(m)); |
| 266 x = 10000000000000000001; | 267 x = 10000000000000000001; |
| 267 m = 12345678901234567890; | 268 m = 12345678901234567890; |
| 268 Expect.equals(325004555487045911, x.modInverse(m)); | 269 Expect.equals(325004555487045911, x.modInverse(m)); |
| 269 x = 10000000000000000001; | 270 x = 10000000000000000001; |
| 270 m = 19; | 271 m = 19; |
| 271 Expect.equals(7, x.modInverse(m)); | 272 Expect.equals(7, x.modInverse(m)); |
| 272 x = 12345678901234567890; | 273 x = 12345678901234567890; |
| 273 m = 10000000000000000001; | 274 m = 10000000000000000001; |
| 274 Expect.equals(9736746307686209582, x.modInverse(m)); | 275 Expect.equals(9736746307686209582, x.modInverse(m)); |
| 275 x = 12345678901234567890; | 276 x = 12345678901234567890; |
| 276 m = 19; | 277 m = 19; |
| 277 Expect.equals(3, x.modInverse(m)); | 278 Expect.equals(3, x.modInverse(m)); |
| 278 x = 123456789012345678901234567890; | 279 x = 123456789012345678901234567890; |
| 279 m = 123456789012345678901234567899; | 280 m = 123456789012345678901234567899; |
| 280 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. | 281 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. |
| 281 x = 123456789012345678901234567890; | 282 x = 123456789012345678901234567890; |
| 282 m = 123456789012345678901234567891; | 283 m = 123456789012345678901234567891; |
| 283 Expect.equals(123456789012345678901234567890, x.modInverse(m)); | 284 Expect.equals(123456789012345678901234567890, x.modInverse(m)); |
| 284 x = 123456789012345678901234567899; | 285 x = 123456789012345678901234567899; |
| 285 m = 123456789012345678901234567891; | 286 m = 123456789012345678901234567891; |
| 286 Expect.equals(77160493132716049313271604932, x.modInverse(m)); | 287 Expect.equals(77160493132716049313271604932, x.modInverse(m)); |
| 287 x = 123456789012345678901234567899; | 288 x = 123456789012345678901234567899; |
| 288 m = 123456789012345678901234567890; | 289 m = 123456789012345678901234567890; |
| 289 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. | 290 Expect.throws(() => x.modInverse(m), (e) => e is Exception); // Not coprime. |
| 290 x = 123456789012345678901234567891; | 291 x = 123456789012345678901234567891; |
| 291 m = 123456789012345678901234567890; | 292 m = 123456789012345678901234567890; |
| 292 Expect.equals(1, x.modInverse(m)); | 293 Expect.equals(1, x.modInverse(m)); |
| 293 x = 123456789012345678901234567891; | 294 x = 123456789012345678901234567891; |
| 294 m = 123456789012345678901234567899; | 295 m = 123456789012345678901234567899; |
| 295 Expect.equals(46296295879629629587962962962, x.modInverse(m)); | 296 Expect.equals(46296295879629629587962962962, x.modInverse(m)); |
| 296 } | 297 } |
| 297 | 298 |
| 298 testBigintGcd() { | 299 testBigintGcd() { |
| 299 var x, m; | 300 var x, m; |
| 300 x = 1; | 301 x = 1; |
| 301 m = 1; | 302 m = 1; |
| 302 Expect.equals(1, x.gcd(m)); | 303 Expect.equals(1, x.gcd(m)); |
| 303 x = 693; | 304 x = 693; |
| 304 m = 609; | 305 m = 609; |
| 305 Expect.equals(21, x.gcd(m)); | 306 Expect.equals(21, x.gcd(m)); |
| 306 x = 693 << 40; | 307 x = 693 << 40; |
| 307 m = 609 << 40; | 308 m = 609 << 40; |
| 308 Expect.equals(21 << 40, x.gcd(m)); | 309 Expect.equals(21 << 40, x.gcd(m)); |
| 309 x = 609 << 40;; | 310 x = 609 << 40; |
| 310 m = 693 << 40;; | 311 ; |
| 311 Expect.equals(21 <<40, x.gcd(m)); | 312 m = 693 << 40; |
| 313 ; |
| 314 Expect.equals(21 << 40, x.gcd(m)); |
| 312 x = 0; | 315 x = 0; |
| 313 m = 1000000001; | 316 m = 1000000001; |
| 314 Expect.equals(m, x.gcd(m)); | 317 Expect.equals(m, x.gcd(m)); |
| 315 x = 1000000001; | 318 x = 1000000001; |
| 316 m = 0; | 319 m = 0; |
| 317 Expect.equals(x, x.gcd(m)); | 320 Expect.equals(x, x.gcd(m)); |
| 318 x = 0; | 321 x = 0; |
| 319 m = -1000000001; | 322 m = -1000000001; |
| 320 Expect.equals(-m, x.gcd(m)); | 323 Expect.equals(-m, x.gcd(m)); |
| 321 x = -1000000001; | 324 x = -1000000001; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 x = 123456789012345678901234567891; | 396 x = 123456789012345678901234567891; |
| 394 m = 123456789012345678901234567890; | 397 m = 123456789012345678901234567890; |
| 395 Expect.equals(1, x.gcd(m)); | 398 Expect.equals(1, x.gcd(m)); |
| 396 x = 123456789012345678901234567891; | 399 x = 123456789012345678901234567891; |
| 397 m = 123456789012345678901234567899; | 400 m = 123456789012345678901234567899; |
| 398 Expect.equals(1, x.gcd(m)); | 401 Expect.equals(1, x.gcd(m)); |
| 399 } | 402 } |
| 400 | 403 |
| 401 testBigintNegate() { | 404 testBigintNegate() { |
| 402 var a = 0xF000000000000000F; | 405 var a = 0xF000000000000000F; |
| 403 var b = ~a; // negate. | 406 var b = ~a; // negate. |
| 404 Expect.equals(-0xF0000000000000010, b); | 407 Expect.equals(-0xF0000000000000010, b); |
| 405 Expect.equals(0, a & b); | 408 Expect.equals(0, a & b); |
| 406 Expect.equals(-1, a | b); | 409 Expect.equals(-1, a | b); |
| 407 } | 410 } |
| 408 | 411 |
| 409 testShiftAmount() { | 412 testShiftAmount() { |
| 410 Expect.equals(0, 12 >> 111111111111111111111111111111); | 413 Expect.equals(0, 12 >> 111111111111111111111111111111); |
| 411 Expect.equals(-1, -12 >> 111111111111111111111111111111); | 414 Expect.equals(-1, -12 >> 111111111111111111111111111111); |
| 412 bool exceptionCaught = false; | 415 bool exceptionCaught = false; |
| 413 try { | 416 try { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 435 testBigintNegate(); // //# negate: ok | 438 testBigintNegate(); // //# negate: ok |
| 436 testShiftAmount(); // //# shift: ok | 439 testShiftAmount(); // //# shift: ok |
| 437 Expect.equals(12345678901234567890, (12345678901234567890).abs()); | 440 Expect.equals(12345678901234567890, (12345678901234567890).abs()); |
| 438 Expect.equals(12345678901234567890, (-12345678901234567890).abs()); | 441 Expect.equals(12345678901234567890, (-12345678901234567890).abs()); |
| 439 var a = 10000000000000000000; | 442 var a = 10000000000000000000; |
| 440 var b = 10000000000000000001; | 443 var b = 10000000000000000001; |
| 441 Expect.equals(false, a.hashCode == b.hashCode); | 444 Expect.equals(false, a.hashCode == b.hashCode); |
| 442 Expect.equals(true, a.hashCode == (b - 1).hashCode); | 445 Expect.equals(true, a.hashCode == (b - 1).hashCode); |
| 443 } | 446 } |
| 444 } | 447 } |
| OLD | NEW |