OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 #include "platform/assert.h" | 5 #include "platform/assert.h" |
6 #include "vm/bigint_operations.h" | 6 #include "vm/bigint_operations.h" |
7 #include "vm/object.h" | 7 #include "vm/object.h" |
8 #include "vm/object_store.h" | 8 #include "vm/object_store.h" |
9 #include "vm/unit_test.h" | 9 #include "vm/unit_test.h" |
10 | 10 |
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 const char* test = "-12345678901234567890"; | 384 const char* test = "-12345678901234567890"; |
385 const char* out = "-0xAB54A98CEB1F0AD2"; | 385 const char* out = "-0xAB54A98CEB1F0AD2"; |
386 const Bigint& bigint = Bigint::Handle( | 386 const Bigint& bigint = Bigint::Handle( |
387 BigintOperations::NewFromCString(test)); | 387 BigintOperations::NewFromCString(test)); |
388 const char* str = BigintOperations::ToHexCString(bigint, &ZoneAllocator); | 388 const char* str = BigintOperations::ToHexCString(bigint, &ZoneAllocator); |
389 EXPECT_STREQ(out, str); | 389 EXPECT_STREQ(out, str); |
390 } | 390 } |
391 } | 391 } |
392 | 392 |
393 | 393 |
394 #if 0 | |
395 // TODO(florian): Add a ToDecString method in bigint operations. | |
396 // Turn this test back on once it is implemented. | |
397 TEST_CASE(BigintDecStrings) { | 394 TEST_CASE(BigintDecStrings) { |
398 { | 395 { |
399 const Bigint& bigint = Bigint::Handle( | 396 const Bigint& bigint = Bigint::Handle( |
400 BigintOperations::NewFromCString("0x123")); | 397 BigintOperations::NewFromCString("0x123")); |
401 const char* str = BigintOperations::ToDecCString(bigint, &ZoneAllocator); | 398 const char* str = |
| 399 BigintOperations::ToDecimalCString(bigint, &ZoneAllocator); |
402 EXPECT_STREQ("291", str); | 400 EXPECT_STREQ("291", str); |
403 } | 401 } |
404 | 402 |
405 { | 403 { |
406 const Bigint& bigint = Bigint::Handle( | 404 const Bigint& bigint = Bigint::Handle( |
407 BigintOperations::NewFromCString("0xaBcEf")); | 405 BigintOperations::NewFromCString("0xaBcEf")); |
408 const char* str = BigintOperations::ToDecCString(bigint, &ZoneAllocator); | 406 const char* str = |
| 407 BigintOperations::ToDecimalCString(bigint, &ZoneAllocator); |
409 EXPECT_STREQ("703727", str); | 408 EXPECT_STREQ("703727", str); |
410 } | 409 } |
411 | 410 |
412 { | 411 { |
413 const char* in = "0x123456789"; | 412 const char* in = "0x123456789"; |
414 const Bigint& bigint = Bigint::Handle( | 413 const Bigint& bigint = Bigint::Handle( |
415 BigintOperations::NewFromCString(in)); | 414 BigintOperations::NewFromCString(in)); |
416 const char* str = BigintOperations::ToDecCString(bigint, &ZoneAllocator); | 415 const char* str = |
| 416 BigintOperations::ToDecimalCString(bigint, &ZoneAllocator); |
417 EXPECT_STREQ("4886718345", str); | 417 EXPECT_STREQ("4886718345", str); |
418 } | 418 } |
419 | 419 |
420 { | 420 { |
421 const char* in = "0xFFFFFFF"; | 421 const char* in = "0xFFFFFFF"; |
422 const Bigint& bigint = Bigint::Handle( | 422 const Bigint& bigint = Bigint::Handle( |
423 BigintOperations::NewFromCString(in)); | 423 BigintOperations::NewFromCString(in)); |
424 const char* str = BigintOperations::ToDecCString(bigint, &ZoneAllocator); | 424 const char* str = |
| 425 BigintOperations::ToDecimalCString(bigint, &ZoneAllocator); |
425 EXPECT_STREQ("268435455", str); | 426 EXPECT_STREQ("268435455", str); |
426 } | 427 } |
427 | 428 |
428 { | 429 { |
429 const char* in = "0x10000000"; | 430 const char* in = "0x10000000"; |
430 const Bigint& bigint = Bigint::Handle( | 431 const Bigint& bigint = Bigint::Handle( |
431 BigintOperations::NewFromCString(in)); | 432 BigintOperations::NewFromCString(in)); |
432 const char* str = BigintOperations::ToDecCString(bigint, &ZoneAllocator); | 433 const char* str = |
| 434 BigintOperations::ToDecimalCString(bigint, &ZoneAllocator); |
433 EXPECT_STREQ("268435456", str); | 435 EXPECT_STREQ("268435456", str); |
434 } | 436 } |
435 | 437 |
436 { | 438 { |
437 const char* in = "0x123456789ABCDEF01234567890ABCDEF0123456789ABCDEF0"; | 439 const char* in = "0x123456789ABCDEF01234567890ABCDEF0123456789ABCDEF0"; |
438 const Bigint& bigint = Bigint::Handle(BigintOperations::NewFromCString(in)); | 440 const Bigint& bigint = Bigint::Handle(BigintOperations::NewFromCString(in)); |
439 const char* str = BigintOperations::ToDecCString(bigint, &ZoneAllocator); | 441 const char* str = |
| 442 BigintOperations::ToDecimalCString(bigint, &ZoneAllocator); |
440 EXPECT_STREQ("7141946863373290020600059860922167424469804758405880798960", | 443 EXPECT_STREQ("7141946863373290020600059860922167424469804758405880798960", |
441 str); | 444 str); |
442 } | 445 } |
443 | 446 |
444 { | 447 { |
445 const Bigint& bigint = Bigint::Handle( | 448 const Bigint& bigint = Bigint::Handle( |
446 BigintOperations::NewFromCString("-0x123")); | 449 BigintOperations::NewFromCString("-0x123")); |
447 EXPECT(BigintOperations::FitsIntoSmi(bigint)); | 450 EXPECT(BigintOperations::FitsIntoSmi(bigint)); |
448 const Smi& smi = Smi::Handle(BigintOperations::ToSmi(bigint)); | 451 const Smi& smi = Smi::Handle(BigintOperations::ToSmi(bigint)); |
449 EXPECT_EQ(-291, smi.Value()); | 452 EXPECT_EQ(-291, smi.Value()); |
450 } | 453 } |
451 | 454 |
452 { | 455 { |
453 const Bigint& bigint = Bigint::Handle( | 456 const Bigint& bigint = Bigint::Handle( |
454 BigintOperations::NewFromCString("-0x123")); | 457 BigintOperations::NewFromCString("-0x123")); |
455 const char* str = BigintOperations::ToDecCString(bigint, &ZoneAllocator); | 458 const char* str = |
| 459 BigintOperations::ToDecimalCString(bigint, &ZoneAllocator); |
456 EXPECT_STREQ("-291", str); | 460 EXPECT_STREQ("-291", str); |
457 } | 461 } |
458 | 462 |
459 { | 463 { |
460 const Bigint& bigint = Bigint::Handle( | 464 const Bigint& bigint = Bigint::Handle( |
461 BigintOperations::NewFromCString("-0xaBcEf")); | 465 BigintOperations::NewFromCString("-0xaBcEf")); |
462 const char* str = BigintOperations::ToDecCString(bigint, &ZoneAllocator); | 466 const char* str = |
| 467 BigintOperations::ToDecimalCString(bigint, &ZoneAllocator); |
463 EXPECT_STREQ("-703727", str); | 468 EXPECT_STREQ("-703727", str); |
464 } | 469 } |
465 | 470 |
466 { | 471 { |
467 const char* in = "-0x123456789"; | 472 const char* in = "-0x123456789"; |
468 const Bigint& bigint = Bigint::Handle(BigintOperations::NewFromCString(in)); | 473 const Bigint& bigint = Bigint::Handle(BigintOperations::NewFromCString(in)); |
469 const char* str = BigintOperations::ToDecCString(bigint, &ZoneAllocator); | 474 const char* str = |
| 475 BigintOperations::ToDecimalCString(bigint, &ZoneAllocator); |
470 EXPECT_STREQ("-4886718345", str); | 476 EXPECT_STREQ("-4886718345", str); |
471 } | 477 } |
472 | 478 |
473 { | 479 { |
474 const char* in = "-0x123456789ABCDEF01234567890ABCDEF0123456789ABCDEF0"; | 480 const char* in = "-0x123456789ABCDEF01234567890ABCDEF0123456789ABCDEF0"; |
475 const Bigint& bigint = Bigint::Handle(BigintOperations::NewFromCString(in)); | 481 const Bigint& bigint = Bigint::Handle(BigintOperations::NewFromCString(in)); |
476 const char* str = BigintOperations::ToDecCString(bigint, &ZoneAllocator); | 482 const char* str = |
| 483 BigintOperations::ToDecimalCString(bigint, &ZoneAllocator); |
477 EXPECT_STREQ("-7141946863373290020600059860922167424469804758405880798960", | 484 EXPECT_STREQ("-7141946863373290020600059860922167424469804758405880798960", |
478 str); | 485 str); |
479 } | 486 } |
480 | 487 |
481 { | 488 { |
482 const Bigint& bigint = Bigint::Handle( | 489 const Bigint& bigint = Bigint::Handle( |
483 BigintOperations::NewFromCString("0x00000123")); | 490 BigintOperations::NewFromCString("0x00000123")); |
484 EXPECT(BigintOperations::FitsIntoSmi(bigint)); | 491 EXPECT(BigintOperations::FitsIntoSmi(bigint)); |
485 const Smi& smi = Smi::Handle(BigintOperations::ToSmi(bigint)); | 492 const Smi& smi = Smi::Handle(BigintOperations::ToSmi(bigint)); |
486 EXPECT_EQ(0x123, smi.Value()); | 493 EXPECT_EQ(0x123, smi.Value()); |
487 } | 494 } |
488 | 495 |
489 { | 496 { |
490 const Bigint& bigint = Bigint::Handle( | 497 const Bigint& bigint = Bigint::Handle( |
491 BigintOperations::NewFromCString("0x000000123")); | 498 BigintOperations::NewFromCString("0x000000123")); |
492 const char* str = BigintOperations::ToDecCString(bigint, &ZoneAllocator); | 499 const char* str = |
| 500 BigintOperations::ToDecimalCString(bigint, &ZoneAllocator); |
493 EXPECT_STREQ("291", str); | 501 EXPECT_STREQ("291", str); |
494 } | 502 } |
495 } | 503 } |
496 #endif | |
497 | 504 |
498 | 505 |
499 static void TestBigintCompare(const char* a, const char* b, int compare) { | 506 static void TestBigintCompare(const char* a, const char* b, int compare) { |
500 const Bigint& bigint_a = Bigint::Handle(BigintOperations::NewFromCString(a)); | 507 const Bigint& bigint_a = Bigint::Handle(BigintOperations::NewFromCString(a)); |
501 const Bigint& bigint_b = Bigint::Handle(BigintOperations::NewFromCString(b)); | 508 const Bigint& bigint_b = Bigint::Handle(BigintOperations::NewFromCString(b)); |
502 int computed_compare = BigintOperations::Compare(bigint_a, bigint_b); | 509 int computed_compare = BigintOperations::Compare(bigint_a, bigint_b); |
503 int inverted_compare = BigintOperations::Compare(bigint_b, bigint_a); | 510 int inverted_compare = BigintOperations::Compare(bigint_b, bigint_a); |
504 if (compare == 0) { | 511 if (compare == 0) { |
505 EXPECT(computed_compare == 0); | 512 EXPECT(computed_compare == 0); |
506 EXPECT(inverted_compare == 0); | 513 EXPECT(inverted_compare == 0); |
(...skipping 1586 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2093 "123456789012345678901234567890123456789012345678901234567890123456789012" | 2100 "123456789012345678901234567890123456789012345678901234567890123456789012" |
2094 "345678901234567890123456789012345678901234567890123456789012345678901234" | 2101 "345678901234567890123456789012345678901234567890123456789012345678901234" |
2095 "567890123456789012345678901234567890123456789012345678901234567890123456" | 2102 "567890123456789012345678901234567890123456789012345678901234567890123456" |
2096 "789012345678901234567890123456789012345678901234567890123456789012345678" | 2103 "789012345678901234567890123456789012345678901234567890123456789012345678" |
2097 "90123456789012345678901234567890", | 2104 "90123456789012345678901234567890", |
2098 "0x1234567890ABCDEF01234567890ABCDEF01234567890ABCDEF01234567890ABCDEF" | 2105 "0x1234567890ABCDEF01234567890ABCDEF01234567890ABCDEF01234567890ABCDEF" |
2099 "01234567890ABCDEE"); | 2106 "01234567890ABCDEE"); |
2100 } | 2107 } |
2101 | 2108 |
2102 } // namespace dart | 2109 } // namespace dart |
OLD | NEW |