1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14
15 #include "unittest.h"
16
17 #include "rapidjson/internal/biginteger.h"
18
19 using namespace rapidjson::internal;
20
21 #define BIGINTEGER_LITERAL(s) BigInteger(s, sizeof(s) - 1)
22
23 static const BigInteger kZero(0);
24 static const BigInteger kOne(1);
25 static const BigInteger kUint64Max = BIGINTEGER_LITERAL("18446744073709551615");
26 static const BigInteger kTwo64 = BIGINTEGER_LITERAL("18446744073709551616");
27
TEST(BigInteger,Constructor)28 TEST(BigInteger, Constructor) {
29 EXPECT_TRUE(kZero.IsZero());
30 EXPECT_TRUE(kZero == kZero);
31 EXPECT_TRUE(kZero == BIGINTEGER_LITERAL("0"));
32 EXPECT_TRUE(kZero == BIGINTEGER_LITERAL("00"));
33
34 const BigInteger a(123);
35 EXPECT_TRUE(a == a);
36 EXPECT_TRUE(a == BIGINTEGER_LITERAL("123"));
37 EXPECT_TRUE(a == BIGINTEGER_LITERAL("0123"));
38
39 EXPECT_EQ(2u, kTwo64.GetCount());
40 EXPECT_EQ(0u, kTwo64.GetDigit(0));
41 EXPECT_EQ(1u, kTwo64.GetDigit(1));
42 }
43
TEST(BigInteger,AddUint64)44 TEST(BigInteger, AddUint64) {
45 BigInteger a = kZero;
46 a += 0u;
47 EXPECT_TRUE(kZero == a);
48
49 a += 1u;
50 EXPECT_TRUE(kOne == a);
51
52 a += 1u;
53 EXPECT_TRUE(BigInteger(2) == a);
54
55 EXPECT_TRUE(BigInteger(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFFF)) == kUint64Max);
56 BigInteger b = kUint64Max;
57 b += 1u;
58 EXPECT_TRUE(kTwo64 == b);
59 b += RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFFF);
60 EXPECT_TRUE(BIGINTEGER_LITERAL("36893488147419103231") == b);
61 }
62
TEST(BigInteger,MultiplyUint64)63 TEST(BigInteger, MultiplyUint64) {
64 BigInteger a = kZero;
65 a *= static_cast <uint64_t>(0);
66 EXPECT_TRUE(kZero == a);
67 a *= static_cast <uint64_t>(123);
68 EXPECT_TRUE(kZero == a);
69
70 BigInteger b = kOne;
71 b *= static_cast<uint64_t>(1);
72 EXPECT_TRUE(kOne == b);
73 b *= static_cast<uint64_t>(0);
74 EXPECT_TRUE(kZero == b);
75
76 BigInteger c(123);
77 c *= static_cast<uint64_t>(456u);
78 EXPECT_TRUE(BigInteger(123u * 456u) == c);
79 c *= RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFFF);
80 EXPECT_TRUE(BIGINTEGER_LITERAL("1034640981606221330982120") == c);
81 c *= RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFFF);
82 EXPECT_TRUE(BIGINTEGER_LITERAL("19085757395861596536664473018420572782123800") == c);
83 }
84
TEST(BigInteger,MultiplyUint32)85 TEST(BigInteger, MultiplyUint32) {
86 BigInteger a = kZero;
87 a *= static_cast <uint32_t>(0);
88 EXPECT_TRUE(kZero == a);
89 a *= static_cast <uint32_t>(123);
90 EXPECT_TRUE(kZero == a);
91
92 BigInteger b = kOne;
93 b *= static_cast<uint32_t>(1);
94 EXPECT_TRUE(kOne == b);
95 b *= static_cast<uint32_t>(0);
96 EXPECT_TRUE(kZero == b);
97
98 BigInteger c(123);
99 c *= static_cast<uint32_t>(456u);
100 EXPECT_TRUE(BigInteger(123u * 456u) == c);
101 c *= 0xFFFFFFFFu;
102 EXPECT_TRUE(BIGINTEGER_LITERAL("240896125641960") == c);
103 c *= 0xFFFFFFFFu;
104 EXPECT_TRUE(BIGINTEGER_LITERAL("1034640981124429079698200") == c);
105 }
106
TEST(BigInteger,LeftShift)107 TEST(BigInteger, LeftShift) {
108 BigInteger a = kZero;
109 a <<= 1;
110 EXPECT_TRUE(kZero == a);
111 a <<= 64;
112 EXPECT_TRUE(kZero == a);
113
114 a = BigInteger(123);
115 a <<= 0;
116 EXPECT_TRUE(BigInteger(123) == a);
117 a <<= 1;
118 EXPECT_TRUE(BigInteger(246) == a);
119 a <<= 64;
120 EXPECT_TRUE(BIGINTEGER_LITERAL("4537899042132549697536") == a);
121 a <<= 99;
122 EXPECT_TRUE(BIGINTEGER_LITERAL("2876235222267216943024851750785644982682875244576768") == a);
123 }
124
TEST(BigInteger,Compare)125 TEST(BigInteger, Compare) {
126 EXPECT_EQ(0, kZero.Compare(kZero));
127 EXPECT_EQ(1, kOne.Compare(kZero));
128 EXPECT_EQ(-1, kZero.Compare(kOne));
129 EXPECT_EQ(0, kUint64Max.Compare(kUint64Max));
130 EXPECT_EQ(0, kTwo64.Compare(kTwo64));
131 EXPECT_EQ(-1, kUint64Max.Compare(kTwo64));
132 EXPECT_EQ(1, kTwo64.Compare(kUint64Max));
133 }
134