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 | 8 |
9 library big_integer_test; | 9 library big_integer_test; |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 x = 123456789012345678901234567891; | 216 x = 123456789012345678901234567891; |
217 e = 123456789012345678901234567899; | 217 e = 123456789012345678901234567899; |
218 m = 123456789012345678901234567890; | 218 m = 123456789012345678901234567890; |
219 Expect.equals(1, x.modPow(e, m)); | 219 Expect.equals(1, x.modPow(e, m)); |
220 x = 123456789012345678901234567891; | 220 x = 123456789012345678901234567891; |
221 e = 123456789012345678901234567890; | 221 e = 123456789012345678901234567890; |
222 m = 123456789012345678901234567899; | 222 m = 123456789012345678901234567899; |
223 Expect.equals(40128068573873018143207285483, x.modPow(e, m)); | 223 Expect.equals(40128068573873018143207285483, x.modPow(e, m)); |
224 } | 224 } |
225 | 225 |
| 226 // TODO(regis): Remove once even modulus is implemented. |
| 227 UnimplementedEvenModulusModInverse(x, m) { |
| 228 Expect.equals(true, m.isEven); |
| 229 try { |
| 230 x.modInverse(m); |
| 231 Expect.fail("Did not throw UnimplementedError"); |
| 232 } on UnimplementedError catch (e) { |
| 233 } |
| 234 } |
| 235 |
| 236 testBigintModInverse() { |
| 237 var x, m; |
| 238 x = 1234567890; |
| 239 m = 19; |
| 240 Expect.equals(11, x.modInverse(m)); |
| 241 x = 1234567890; |
| 242 m = 1000000001; |
| 243 Expect.equals(189108911, x.modInverse(m)); |
| 244 x = 19; |
| 245 m = 1000000001; |
| 246 Expect.equals(0, x.modInverse(m)); // Not coprime. |
| 247 x = 19; |
| 248 m = 1234567890; |
| 249 Expect.equals(519818059, x.modInverse(m)); |
| 250 x = 1000000001; |
| 251 m = 1234567890; |
| 252 Expect.equals(1001100101, x.modInverse(m)); |
| 253 x = 1000000001; |
| 254 m = 19; |
| 255 Expect.equals(0, x.modInverse(m)); // Not coprime. |
| 256 x = 12345678901234567890; |
| 257 m = 19; |
| 258 Expect.equals(3, x.modInverse(m)); |
| 259 x = 12345678901234567890; |
| 260 m = 10000000000000000001; |
| 261 Expect.equals(9736746307686209582, x.modInverse(m)); |
| 262 x = 19; |
| 263 m = 10000000000000000001; |
| 264 Expect.equals(6315789473684210527, x.modInverse(m)); |
| 265 x = 19; |
| 266 m = 12345678901234567890; |
| 267 UnimplementedEvenModulusModInverse(x, m); |
| 268 x = 10000000000000000001; |
| 269 m = 12345678901234567890; |
| 270 UnimplementedEvenModulusModInverse(x, m); |
| 271 x = 10000000000000000001; |
| 272 m = 19; |
| 273 Expect.equals(7, x.modInverse(m)); |
| 274 x = 12345678901234567890; |
| 275 m = 10000000000000000001; |
| 276 Expect.equals(9736746307686209582, x.modInverse(m)); |
| 277 x = 12345678901234567890; |
| 278 m = 19; |
| 279 Expect.equals(3, x.modInverse(m)); |
| 280 x = 123456789012345678901234567890; |
| 281 m = 123456789012345678901234567899; |
| 282 Expect.equals(0, x.modInverse(m)); // Not coprime. |
| 283 x = 123456789012345678901234567890; |
| 284 m = 123456789012345678901234567891; |
| 285 Expect.equals(123456789012345678901234567890, x.modInverse(m)); |
| 286 x = 123456789012345678901234567899; |
| 287 m = 123456789012345678901234567891; |
| 288 Expect.equals(77160493132716049313271604932, x.modInverse(m)); |
| 289 x = 123456789012345678901234567899; |
| 290 m = 123456789012345678901234567890; |
| 291 UnimplementedEvenModulusModInverse(x, m); |
| 292 x = 123456789012345678901234567891; |
| 293 m = 123456789012345678901234567890; |
| 294 UnimplementedEvenModulusModInverse(x, m); |
| 295 x = 123456789012345678901234567891; |
| 296 m = 123456789012345678901234567899; |
| 297 Expect.equals(46296295879629629587962962962, x.modInverse(m)); |
| 298 } |
| 299 |
226 testBigintNegate() { | 300 testBigintNegate() { |
227 var a = 0xF000000000000000F; | 301 var a = 0xF000000000000000F; |
228 var b = ~a; // negate. | 302 var b = ~a; // negate. |
229 Expect.equals(-0xF0000000000000010, b); | 303 Expect.equals(-0xF0000000000000010, b); |
230 Expect.equals(0, a & b); | 304 Expect.equals(0, a & b); |
231 Expect.equals(-1, a | b); | 305 Expect.equals(-1, a | b); |
232 } | 306 } |
233 | 307 |
234 testShiftAmount() { | 308 testShiftAmount() { |
235 Expect.equals(0, 12 >> 111111111111111111111111111111); | 309 Expect.equals(0, 12 >> 111111111111111111111111111111); |
(...skipping 11 matching lines...) Expand all Loading... |
247 Expect.equals(1234567890123456789, foo()); | 321 Expect.equals(1234567890123456789, foo()); |
248 Expect.equals(12345678901234567890, bar()); | 322 Expect.equals(12345678901234567890, bar()); |
249 testSmiOverflow(); | 323 testSmiOverflow(); |
250 testBigintAdd(); | 324 testBigintAdd(); |
251 testBigintSub(); | 325 testBigintSub(); |
252 testBigintMul(); | 326 testBigintMul(); |
253 testBigintTruncDiv(); | 327 testBigintTruncDiv(); |
254 testBigintDiv(); | 328 testBigintDiv(); |
255 testBigintModulo(); | 329 testBigintModulo(); |
256 testBigintModPow(); | 330 testBigintModPow(); |
| 331 testBigintModInverse(); |
257 testBigintNegate(); | 332 testBigintNegate(); |
258 testShiftAmount(); | 333 testShiftAmount(); |
259 Expect.equals(12345678901234567890, (12345678901234567890).abs()); | 334 Expect.equals(12345678901234567890, (12345678901234567890).abs()); |
260 Expect.equals(12345678901234567890, (-12345678901234567890).abs()); | 335 Expect.equals(12345678901234567890, (-12345678901234567890).abs()); |
261 var a = 10000000000000000000; | 336 var a = 10000000000000000000; |
262 var b = 10000000000000000001; | 337 var b = 10000000000000000001; |
263 Expect.equals(false, a.hashCode == b.hashCode); | 338 Expect.equals(false, a.hashCode == b.hashCode); |
264 Expect.equals(true, a.hashCode == (b - 1).hashCode); | 339 Expect.equals(true, a.hashCode == (b - 1).hashCode); |
265 } | 340 } |
OLD | NEW |