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