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 { |
11 | 11 |
12 TEST_CASE(BigintSmi) { | 12 TEST_CASE(BigintSmi) { |
13 if (Bigint::IsDisabled()) { | |
14 return; | |
15 } | |
16 | |
17 { | 13 { |
18 const Smi& smi = Smi::Handle(Smi::New(5)); | 14 const Smi& smi = Smi::Handle(Smi::New(5)); |
19 const Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(smi.Value())); | 15 const Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(smi.Value())); |
20 EXPECT_EQ(5, bigint.AsInt64Value()); | 16 EXPECT_EQ(5, bigint.AsInt64Value()); |
21 EXPECT(bigint.FitsIntoSmi()); | 17 EXPECT(bigint.FitsIntoSmi()); |
22 Smi& smi_back = Smi::Handle(); | 18 Smi& smi_back = Smi::Handle(); |
23 smi_back ^= bigint.AsValidInteger(); | 19 smi_back ^= bigint.AsValidInteger(); |
24 EXPECT_EQ(5, smi_back.Value()); | 20 EXPECT_EQ(5, smi_back.Value()); |
25 } | 21 } |
26 | 22 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 const Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(smi.Value())); | 58 const Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(smi.Value())); |
63 EXPECT_EQ(0x10000000, bigint.AsInt64Value()); | 59 EXPECT_EQ(0x10000000, bigint.AsInt64Value()); |
64 EXPECT(bigint.FitsIntoSmi()); | 60 EXPECT(bigint.FitsIntoSmi()); |
65 Smi& smi_back = Smi::Handle(); | 61 Smi& smi_back = Smi::Handle(); |
66 smi_back ^= bigint.AsValidInteger(); | 62 smi_back ^= bigint.AsValidInteger(); |
67 EXPECT(0x10000000 == smi_back.Value()); | 63 EXPECT(0x10000000 == smi_back.Value()); |
68 } | 64 } |
69 } | 65 } |
70 | 66 |
71 TEST_CASE(BigintInt64) { | 67 TEST_CASE(BigintInt64) { |
72 if (Bigint::IsDisabled()) { | |
73 return; | |
74 } | |
75 | |
76 const int64_t kValue = 100000000; | 68 const int64_t kValue = 100000000; |
77 const int64_t kValue64 = kValue * kValue; | 69 const int64_t kValue64 = kValue * kValue; |
78 Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(kValue)); | 70 Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(kValue)); |
79 EXPECT_EQ(kValue, bigint.AsInt64Value()); | 71 EXPECT_EQ(kValue, bigint.AsInt64Value()); |
80 bigint = Bigint::NewFromInt64(kValue64); | 72 bigint = Bigint::NewFromInt64(kValue64); |
81 EXPECT_EQ(kValue64, bigint.AsInt64Value()); | 73 EXPECT_EQ(kValue64, bigint.AsInt64Value()); |
82 bigint = Bigint::NewFromInt64(-kValue64); | 74 bigint = Bigint::NewFromInt64(-kValue64); |
83 EXPECT_EQ(-kValue64, bigint.AsInt64Value()); | 75 EXPECT_EQ(-kValue64, bigint.AsInt64Value()); |
84 bigint = Bigint::NewFromInt64(kMinInt64); | 76 bigint = Bigint::NewFromInt64(kMinInt64); |
85 EXPECT(bigint.FitsIntoInt64()); | 77 EXPECT(bigint.FitsIntoInt64()); |
86 EXPECT_EQ(kMinInt64, bigint.AsInt64Value()); | 78 EXPECT_EQ(kMinInt64, bigint.AsInt64Value()); |
87 } | 79 } |
88 | 80 |
89 TEST_CASE(BigintUint64) { | 81 TEST_CASE(BigintUint64) { |
90 if (Bigint::IsDisabled()) { | |
91 return; | |
92 } | |
93 | |
94 const Bigint& one = Bigint::Handle(Bigint::NewFromUint64(1)); | 82 const Bigint& one = Bigint::Handle(Bigint::NewFromUint64(1)); |
95 EXPECT(one.FitsIntoInt64()); | 83 EXPECT(one.FitsIntoInt64()); |
96 EXPECT(one.FitsIntoUint64()); | 84 EXPECT(one.FitsIntoUint64()); |
97 | 85 |
98 const Bigint& big = Bigint::Handle(Bigint::NewFromUint64(kMaxUint64)); | 86 const Bigint& big = Bigint::Handle(Bigint::NewFromUint64(kMaxUint64)); |
99 EXPECT(!big.FitsIntoInt64()); | 87 EXPECT(!big.FitsIntoInt64()); |
100 EXPECT(big.FitsIntoUint64()); | 88 EXPECT(big.FitsIntoUint64()); |
101 | 89 |
102 uint64_t back = big.AsUint64Value(); | 90 uint64_t back = big.AsUint64Value(); |
103 EXPECT_EQ(kMaxUint64, back); | 91 EXPECT_EQ(kMaxUint64, back); |
104 } | 92 } |
105 | 93 |
106 TEST_CASE(BigintDouble) { | 94 TEST_CASE(BigintDouble) { |
107 if (Bigint::IsDisabled()) { | |
108 return; | |
109 } | |
110 | |
111 Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(5)); | 95 Bigint& bigint = Bigint::Handle(Bigint::NewFromInt64(5)); |
112 EXPECT_EQ(5.0, bigint.AsDoubleValue()); | 96 EXPECT_EQ(5.0, bigint.AsDoubleValue()); |
113 | 97 |
114 bigint = Bigint::NewFromInt64(0); | 98 bigint = Bigint::NewFromInt64(0); |
115 EXPECT_EQ(0.0, bigint.AsDoubleValue()); | 99 EXPECT_EQ(0.0, bigint.AsDoubleValue()); |
116 | 100 |
117 bigint = Bigint::NewFromInt64(-12345678); | 101 bigint = Bigint::NewFromInt64(-12345678); |
118 EXPECT_EQ(-1.2345678e+7, bigint.AsDoubleValue()); | 102 EXPECT_EQ(-1.2345678e+7, bigint.AsDoubleValue()); |
119 | 103 |
120 bigint = Bigint::NewFromCString("1"); | 104 bigint = Bigint::NewFromCString("1"); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 bigint = | 217 bigint = |
234 Bigint::NewFromCString("1844674407370955161600000000000000000000000"); | 218 Bigint::NewFromCString("1844674407370955161600000000000000000000000"); |
235 EXPECT_EQ(1.844674407370955e+42, bigint.AsDoubleValue()); | 219 EXPECT_EQ(1.844674407370955e+42, bigint.AsDoubleValue()); |
236 | 220 |
237 bigint = | 221 bigint = |
238 Bigint::NewFromCString("1844674407370955161600000000000000000000001"); | 222 Bigint::NewFromCString("1844674407370955161600000000000000000000001"); |
239 EXPECT_EQ(1.8446744073709553e+42, bigint.AsDoubleValue()); | 223 EXPECT_EQ(1.8446744073709553e+42, bigint.AsDoubleValue()); |
240 } | 224 } |
241 | 225 |
242 TEST_CASE(BigintHexStrings) { | 226 TEST_CASE(BigintHexStrings) { |
243 if (Bigint::IsDisabled()) { | |
244 return; | |
245 } | |
246 | |
247 Zone* zone = Thread::Current()->zone(); | 227 Zone* zone = Thread::Current()->zone(); |
248 { | 228 { |
249 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x0")); | 229 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x0")); |
250 EXPECT(bigint.FitsIntoSmi()); | 230 EXPECT(bigint.FitsIntoSmi()); |
251 EXPECT_EQ(0, bigint.AsInt64Value()); | 231 EXPECT_EQ(0, bigint.AsInt64Value()); |
252 } | 232 } |
253 | 233 |
254 { | 234 { |
255 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x1")); | 235 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x1")); |
256 EXPECT(bigint.FitsIntoSmi()); | 236 EXPECT(bigint.FitsIntoSmi()); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 { | 397 { |
418 const char* test = "-12345678901234567890"; | 398 const char* test = "-12345678901234567890"; |
419 const char* out = "-0xAB54A98CEB1F0AD2"; | 399 const char* out = "-0xAB54A98CEB1F0AD2"; |
420 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString(test)); | 400 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString(test)); |
421 const char* str = bigint.ToHexCString(zone); | 401 const char* str = bigint.ToHexCString(zone); |
422 EXPECT_STREQ(out, str); | 402 EXPECT_STREQ(out, str); |
423 } | 403 } |
424 } | 404 } |
425 | 405 |
426 TEST_CASE(BigintDecStrings) { | 406 TEST_CASE(BigintDecStrings) { |
427 if (Bigint::IsDisabled()) { | |
428 return; | |
429 } | |
430 | |
431 Zone* zone = Thread::Current()->zone(); | 407 Zone* zone = Thread::Current()->zone(); |
432 { | 408 { |
433 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x0")); | 409 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x0")); |
434 const char* str = bigint.ToDecCString(zone); | 410 const char* str = bigint.ToDecCString(zone); |
435 EXPECT_STREQ("0", str); | 411 EXPECT_STREQ("0", str); |
436 } | 412 } |
437 | 413 |
438 { | 414 { |
439 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x123")); | 415 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0x123")); |
440 const char* str = bigint.ToDecCString(zone); | 416 const char* str = bigint.ToDecCString(zone); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 EXPECT(computed_compare < 0); | 521 EXPECT(computed_compare < 0); |
546 EXPECT(inverted_compare > 0); | 522 EXPECT(inverted_compare > 0); |
547 } else { | 523 } else { |
548 ASSERT(compare > 0); | 524 ASSERT(compare > 0); |
549 EXPECT(computed_compare > 0); | 525 EXPECT(computed_compare > 0); |
550 EXPECT(inverted_compare < 0); | 526 EXPECT(inverted_compare < 0); |
551 } | 527 } |
552 } | 528 } |
553 | 529 |
554 TEST_CASE(BigintCompare) { | 530 TEST_CASE(BigintCompare) { |
555 if (Bigint::IsDisabled()) { | |
556 return; | |
557 } | |
558 | |
559 TestBigintCompare("0x0", "0x0", 0); | 531 TestBigintCompare("0x0", "0x0", 0); |
560 TestBigintCompare("0x1", "0x1", 0); | 532 TestBigintCompare("0x1", "0x1", 0); |
561 TestBigintCompare("-0x1", "-0x1", 0); | 533 TestBigintCompare("-0x1", "-0x1", 0); |
562 TestBigintCompare("0x1234567", "0x1234567", 0); | 534 TestBigintCompare("0x1234567", "0x1234567", 0); |
563 TestBigintCompare("-0x1234567", "-0x1234567", 0); | 535 TestBigintCompare("-0x1234567", "-0x1234567", 0); |
564 TestBigintCompare("0x12345678", "0x12345678", 0); | 536 TestBigintCompare("0x12345678", "0x12345678", 0); |
565 TestBigintCompare("-0x12345678", "-0x12345678", 0); | 537 TestBigintCompare("-0x12345678", "-0x12345678", 0); |
566 TestBigintCompare("0x123456789ABCDEF0", "0x123456789ABCDEF0", 0); | 538 TestBigintCompare("0x123456789ABCDEF0", "0x123456789ABCDEF0", 0); |
567 TestBigintCompare("-0x123456789ABCDEF0", "-0x123456789ABCDEF0", 0); | 539 TestBigintCompare("-0x123456789ABCDEF0", "-0x123456789ABCDEF0", 0); |
568 TestBigintCompare("0x123456789ABCDEF01", "0x123456789ABCDEF01", 0); | 540 TestBigintCompare("0x123456789ABCDEF01", "0x123456789ABCDEF01", 0); |
(...skipping 28 matching lines...) Expand all Loading... |
597 TestBigintCompare("-0x10000000", "0xFFFFFFF", -1); | 569 TestBigintCompare("-0x10000000", "0xFFFFFFF", -1); |
598 TestBigintCompare("-0x100000000", "0xFFFFFFFF", -1); | 570 TestBigintCompare("-0x100000000", "0xFFFFFFFF", -1); |
599 TestBigintCompare("-0x100000000", "0xFFFFFFFF", -1); | 571 TestBigintCompare("-0x100000000", "0xFFFFFFFF", -1); |
600 TestBigintCompare("-0x10000000000000000", "0xFFFFFFFFFFFFFFFF", -1); | 572 TestBigintCompare("-0x10000000000000000", "0xFFFFFFFFFFFFFFFF", -1); |
601 TestBigintCompare("-0x10000000000000000", "0xFFFFFFFFFFFFFFFF", -1); | 573 TestBigintCompare("-0x10000000000000000", "0xFFFFFFFFFFFFFFFF", -1); |
602 TestBigintCompare("-0x10000000000000000", "0x0", -1); | 574 TestBigintCompare("-0x10000000000000000", "0x0", -1); |
603 TestBigintCompare("-0x10000000000000000", "0x0", -1); | 575 TestBigintCompare("-0x10000000000000000", "0x0", -1); |
604 } | 576 } |
605 | 577 |
606 TEST_CASE(BigintDecimalStrings) { | 578 TEST_CASE(BigintDecimalStrings) { |
607 if (Bigint::IsDisabled()) { | |
608 return; | |
609 } | |
610 | |
611 Zone* zone = Thread::Current()->zone(); | 579 Zone* zone = Thread::Current()->zone(); |
612 { | 580 { |
613 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0")); | 581 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("0")); |
614 EXPECT(bigint.FitsIntoSmi()); | 582 EXPECT(bigint.FitsIntoSmi()); |
615 EXPECT_EQ(0, bigint.AsInt64Value()); | 583 EXPECT_EQ(0, bigint.AsInt64Value()); |
616 } | 584 } |
617 | 585 |
618 { | 586 { |
619 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("1")); | 587 const Bigint& bigint = Bigint::Handle(Bigint::NewFromCString("1")); |
620 EXPECT(bigint.FitsIntoSmi()); | 588 EXPECT(bigint.FitsIntoSmi()); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 | 634 |
667 { | 635 { |
668 const Bigint& bigint = | 636 const Bigint& bigint = |
669 Bigint::Handle(Bigint::NewFromCString("-1311768467463790320")); | 637 Bigint::Handle(Bigint::NewFromCString("-1311768467463790320")); |
670 const char* str = bigint.ToHexCString(zone); | 638 const char* str = bigint.ToHexCString(zone); |
671 EXPECT_STREQ("-0x123456789ABCDEF0", str); | 639 EXPECT_STREQ("-0x123456789ABCDEF0", str); |
672 } | 640 } |
673 } | 641 } |
674 | 642 |
675 } // namespace dart | 643 } // namespace dart |
OLD | NEW |