OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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/object.h" | 6 #include "vm/object.h" |
7 #include "vm/object_store.h" | 7 #include "vm/object_store.h" |
8 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
9 | 9 |
10 namespace dart { | 10 namespace dart { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 const Smi& smi = Smi::Handle(Smi::New(0x10000000)); | 57 const Smi& smi = Smi::Handle(Smi::New(0x10000000)); |
58 const Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(smi.Value())); | 58 const Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(smi.Value())); |
59 EXPECT_EQ(0x10000000, bigint.AsInt64Value()); | 59 EXPECT_EQ(0x10000000, bigint.AsInt64Value()); |
60 EXPECT(bigint.FitsIntoSmi()); | 60 EXPECT(bigint.FitsIntoSmi()); |
61 Smi& smi_back = Smi::Handle(); | 61 Smi& smi_back = Smi::Handle(); |
62 smi_back ^= bigint.AsValidInteger(); | 62 smi_back ^= bigint.AsValidInteger(); |
63 EXPECT(0x10000000 == smi_back.Value()); | 63 EXPECT(0x10000000 == smi_back.Value()); |
64 } | 64 } |
65 } | 65 } |
66 | 66 |
67 | |
68 TEST_CASE(BigintInt64) { | 67 TEST_CASE(BigintInt64) { |
69 const int64_t kValue = 100000000; | 68 const int64_t kValue = 100000000; |
70 const int64_t kValue64 = kValue * kValue; | 69 const int64_t kValue64 = kValue * kValue; |
71 Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(kValue)); | 70 Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(kValue)); |
72 EXPECT_EQ(kValue, bigint.AsInt64Value()); | 71 EXPECT_EQ(kValue, bigint.AsInt64Value()); |
73 bigint = Bigint::NewFromInt64(kValue64); | 72 bigint = Bigint::NewFromInt64(kValue64); |
74 EXPECT_EQ(kValue64, bigint.AsInt64Value()); | 73 EXPECT_EQ(kValue64, bigint.AsInt64Value()); |
75 bigint = Bigint::NewFromInt64(-kValue64); | 74 bigint = Bigint::NewFromInt64(-kValue64); |
76 EXPECT_EQ(-kValue64, bigint.AsInt64Value()); | 75 EXPECT_EQ(-kValue64, bigint.AsInt64Value()); |
77 bigint = Bigint::NewFromInt64(kMinInt64); | 76 bigint = Bigint::NewFromInt64(kMinInt64); |
78 EXPECT(bigint.FitsIntoInt64()); | 77 EXPECT(bigint.FitsIntoInt64()); |
79 EXPECT_EQ(kMinInt64, bigint.AsInt64Value()); | 78 EXPECT_EQ(kMinInt64, bigint.AsInt64Value()); |
80 } | 79 } |
81 | 80 |
82 | |
83 TEST_CASE(BigintUint64) { | 81 TEST_CASE(BigintUint64) { |
84 const Bigint& one = Bigint::Handle(Bigint::NewFromUint64(1)); | 82 const Bigint& one = Bigint::Handle(Bigint::NewFromUint64(1)); |
85 EXPECT(one.FitsIntoInt64()); | 83 EXPECT(one.FitsIntoInt64()); |
86 EXPECT(one.FitsIntoUint64()); | 84 EXPECT(one.FitsIntoUint64()); |
87 | 85 |
88 const Bigint& big = Bigint::Handle(Bigint::NewFromUint64(kMaxUint64)); | 86 const Bigint& big = Bigint::Handle(Bigint::NewFromUint64(kMaxUint64)); |
89 EXPECT(!big.FitsIntoInt64()); | 87 EXPECT(!big.FitsIntoInt64()); |
90 EXPECT(big.FitsIntoUint64()); | 88 EXPECT(big.FitsIntoUint64()); |
91 | 89 |
92 uint64_t back = big.AsUint64Value(); | 90 uint64_t back = big.AsUint64Value(); |
93 EXPECT_EQ(kMaxUint64, back); | 91 EXPECT_EQ(kMaxUint64, back); |
94 } | 92 } |
95 | 93 |
96 | |
97 TEST_CASE(BigintDouble) { | 94 TEST_CASE(BigintDouble) { |
98 Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(5)); | 95 Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(5)); |
99 EXPECT_EQ(5.0, bigint.AsDoubleValue()); | 96 EXPECT_EQ(5.0, bigint.AsDoubleValue()); |
100 | 97 |
101 bigint = Bigint::NewFromInt64(0); | 98 bigint = Bigint::NewFromInt64(0); |
102 EXPECT_EQ(0.0, bigint.AsDoubleValue()); | 99 EXPECT_EQ(0.0, bigint.AsDoubleValue()); |
103 | 100 |
104 bigint = Bigint::NewFromInt64(-12345678); | 101 bigint = Bigint::NewFromInt64(-12345678); |
105 EXPECT_EQ(-1.2345678e+7, bigint.AsDoubleValue()); | 102 EXPECT_EQ(-1.2345678e+7, bigint.AsDoubleValue()); |
106 | 103 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 // Same but shifted 64 bits to the left. | 216 // Same but shifted 64 bits to the left. |
220 bigint = | 217 bigint = |
221 Bigint::NewFromCString("1844674407370955161600000000000000000000000"); | 218 Bigint::NewFromCString("1844674407370955161600000000000000000000000"); |
222 EXPECT_EQ(1.844674407370955e+42, bigint.AsDoubleValue()); | 219 EXPECT_EQ(1.844674407370955e+42, bigint.AsDoubleValue()); |
223 | 220 |
224 bigint = | 221 bigint = |
225 Bigint::NewFromCString("1844674407370955161600000000000000000000001"); | 222 Bigint::NewFromCString("1844674407370955161600000000000000000000001"); |
226 EXPECT_EQ(1.8446744073709553e+42, bigint.AsDoubleValue()); | 223 EXPECT_EQ(1.8446744073709553e+42, bigint.AsDoubleValue()); |
227 } | 224 } |
228 | 225 |
229 | |
230 TEST_CASE(BigintHexStrings) { | 226 TEST_CASE(BigintHexStrings) { |
231 Zone* zone = Thread::Current()->zone(); | 227 Zone* zone = Thread::Current()->zone(); |
232 { | 228 { |
233 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x0")); | 229 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x0")); |
234 EXPECT(bigint.FitsIntoSmi()); | 230 EXPECT(bigint.FitsIntoSmi()); |
235 EXPECT_EQ(0, bigint.AsInt64Value()); | 231 EXPECT_EQ(0, bigint.AsInt64Value()); |
236 } | 232 } |
237 | 233 |
238 { | 234 { |
239 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x1")); | 235 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x1")); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 } | 396 } |
401 { | 397 { |
402 const char* test = "-12345678901234567890"; | 398 const char* test = "-12345678901234567890"; |
403 const char* out = "-0xAB54A98CEB1F0AD2"; | 399 const char* out = "-0xAB54A98CEB1F0AD2"; |
404 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString(test)); | 400 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString(test)); |
405 const char* str = bigint.ToHexCString(zone); | 401 const char* str = bigint.ToHexCString(zone); |
406 EXPECT_STREQ(out, str); | 402 EXPECT_STREQ(out, str); |
407 } | 403 } |
408 } | 404 } |
409 | 405 |
410 | |
411 TEST_CASE(BigintDecStrings) { | 406 TEST_CASE(BigintDecStrings) { |
412 Zone* zone = Thread::Current()->zone(); | 407 Zone* zone = Thread::Current()->zone(); |
413 { | 408 { |
414 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x0")); | 409 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x0")); |
415 const char* str = bigint.ToDecCString(zone); | 410 const char* str = bigint.ToDecCString(zone); |
416 EXPECT_STREQ("0", str); | 411 EXPECT_STREQ("0", str); |
417 } | 412 } |
418 | 413 |
419 { | 414 { |
420 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x123")); | 415 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x123")); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 } | 499 } |
505 | 500 |
506 { | 501 { |
507 const Bigint& bigint = Bigint::Handle( | 502 const Bigint& bigint = Bigint::Handle( |
508 Bigint::NewFromCString("100000000000000000000000000000000")); | 503 Bigint::NewFromCString("100000000000000000000000000000000")); |
509 const char* str = bigint.ToDecCString(zone); | 504 const char* str = bigint.ToDecCString(zone); |
510 EXPECT_STREQ("100000000000000000000000000000000", str); | 505 EXPECT_STREQ("100000000000000000000000000000000", str); |
511 } | 506 } |
512 } | 507 } |
513 | 508 |
514 | |
515 static void TestBigintCompare(const char* a, const char* b, int compare) { | 509 static void TestBigintCompare(const char* a, const char* b, int compare) { |
516 const Bigint& bigint_a = Bigint::Handle(Bigint::NewFromCString(a)); | 510 const Bigint& bigint_a = Bigint::Handle(Bigint::NewFromCString(a)); |
517 const Bigint& bigint_b = Bigint::Handle(Bigint::NewFromCString(b)); | 511 const Bigint& bigint_b = Bigint::Handle(Bigint::NewFromCString(b)); |
518 const Integer& int_a = Integer::Handle(bigint_a.AsValidInteger()); | 512 const Integer& int_a = Integer::Handle(bigint_a.AsValidInteger()); |
519 const Integer& int_b = Integer::Handle(bigint_b.AsValidInteger()); | 513 const Integer& int_b = Integer::Handle(bigint_b.AsValidInteger()); |
520 int computed_compare = int_a.CompareWith(int_b); | 514 int computed_compare = int_a.CompareWith(int_b); |
521 int inverted_compare = int_b.CompareWith(int_a); | 515 int inverted_compare = int_b.CompareWith(int_a); |
522 if (compare == 0) { | 516 if (compare == 0) { |
523 EXPECT(computed_compare == 0); | 517 EXPECT(computed_compare == 0); |
524 EXPECT(inverted_compare == 0); | 518 EXPECT(inverted_compare == 0); |
525 } else if (compare < 0) { | 519 } else if (compare < 0) { |
526 ASSERT(computed_compare < 0); | 520 ASSERT(computed_compare < 0); |
527 EXPECT(computed_compare < 0); | 521 EXPECT(computed_compare < 0); |
528 EXPECT(inverted_compare > 0); | 522 EXPECT(inverted_compare > 0); |
529 } else { | 523 } else { |
530 ASSERT(compare > 0); | 524 ASSERT(compare > 0); |
531 EXPECT(computed_compare > 0); | 525 EXPECT(computed_compare > 0); |
532 EXPECT(inverted_compare < 0); | 526 EXPECT(inverted_compare < 0); |
533 } | 527 } |
534 } | 528 } |
535 | 529 |
536 | |
537 TEST_CASE(BigintCompare) { | 530 TEST_CASE(BigintCompare) { |
538 TestBigintCompare("0x0", "0x0", 0); | 531 TestBigintCompare("0x0", "0x0", 0); |
539 TestBigintCompare("0x1", "0x1", 0); | 532 TestBigintCompare("0x1", "0x1", 0); |
540 TestBigintCompare("-0x1", "-0x1", 0); | 533 TestBigintCompare("-0x1", "-0x1", 0); |
541 TestBigintCompare("0x1234567", "0x1234567", 0); | 534 TestBigintCompare("0x1234567", "0x1234567", 0); |
542 TestBigintCompare("-0x1234567", "-0x1234567", 0); | 535 TestBigintCompare("-0x1234567", "-0x1234567", 0); |
543 TestBigintCompare("0x12345678", "0x12345678", 0); | 536 TestBigintCompare("0x12345678", "0x12345678", 0); |
544 TestBigintCompare("-0x12345678", "-0x12345678", 0); | 537 TestBigintCompare("-0x12345678", "-0x12345678", 0); |
545 TestBigintCompare("0x123456789ABCDEF0", "0x123456789ABCDEF0", 0); | 538 TestBigintCompare("0x123456789ABCDEF0", "0x123456789ABCDEF0", 0); |
546 TestBigintCompare("-0x123456789ABCDEF0", "-0x123456789ABCDEF0", 0); | 539 TestBigintCompare("-0x123456789ABCDEF0", "-0x123456789ABCDEF0", 0); |
(...skipping 28 matching lines...) Expand all Loading... |
575 TestBigintCompare("-0x10000000", "0xFFFFFFF", -1); | 568 TestBigintCompare("-0x10000000", "0xFFFFFFF", -1); |
576 TestBigintCompare("-0x10000000", "0xFFFFFFF", -1); | 569 TestBigintCompare("-0x10000000", "0xFFFFFFF", -1); |
577 TestBigintCompare("-0x100000000", "0xFFFFFFFF", -1); | 570 TestBigintCompare("-0x100000000", "0xFFFFFFFF", -1); |
578 TestBigintCompare("-0x100000000", "0xFFFFFFFF", -1); | 571 TestBigintCompare("-0x100000000", "0xFFFFFFFF", -1); |
579 TestBigintCompare("-0x10000000000000000", "0xFFFFFFFFFFFFFFFF", -1); | 572 TestBigintCompare("-0x10000000000000000", "0xFFFFFFFFFFFFFFFF", -1); |
580 TestBigintCompare("-0x10000000000000000", "0xFFFFFFFFFFFFFFFF", -1); | 573 TestBigintCompare("-0x10000000000000000", "0xFFFFFFFFFFFFFFFF", -1); |
581 TestBigintCompare("-0x10000000000000000", "0x0", -1); | 574 TestBigintCompare("-0x10000000000000000", "0x0", -1); |
582 TestBigintCompare("-0x10000000000000000", "0x0", -1); | 575 TestBigintCompare("-0x10000000000000000", "0x0", -1); |
583 } | 576 } |
584 | 577 |
585 | |
586 TEST_CASE(BigintDecimalStrings) { | 578 TEST_CASE(BigintDecimalStrings) { |
587 Zone* zone = Thread::Current()->zone(); | 579 Zone* zone = Thread::Current()->zone(); |
588 { | 580 { |
589 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0")); | 581 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0")); |
590 EXPECT(bigint.FitsIntoSmi()); | 582 EXPECT(bigint.FitsIntoSmi()); |
591 EXPECT_EQ(0, bigint.AsInt64Value()); | 583 EXPECT_EQ(0, bigint.AsInt64Value()); |
592 } | 584 } |
593 | 585 |
594 { | 586 { |
595 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("1")); | 587 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("1")); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 | 634 |
643 { | 635 { |
644 const Bigint& bigint = | 636 const Bigint& bigint = |
645 Bigint::Handle(Bigint::NewFromCString("-1311768467463790320")); | 637 Bigint::Handle(Bigint::NewFromCString("-1311768467463790320")); |
646 const char* str = bigint.ToHexCString(zone); | 638 const char* str = bigint.ToHexCString(zone); |
647 EXPECT_STREQ("-0x123456789ABCDEF0", str); | 639 EXPECT_STREQ("-0x123456789ABCDEF0", str); |
648 } | 640 } |
649 } | 641 } |
650 | 642 |
651 } // namespace dart | 643 } // namespace dart |
OLD | NEW |