Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(993)

Side by Side Diff: runtime/vm/bigint_test.cc

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

Powered by Google App Engine
This is Rietveld 408576698