Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
bigintegertest.cpp
Go to the documentation of this file.
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
18
19using namespace rapidjson::internal;
20
21#define BIGINTEGER_LITERAL(s) BigInteger(s, sizeof(s) - 1)
22
23static const BigInteger kZero(0);
24static const BigInteger kOne(1);
25static const BigInteger kUint64Max = BIGINTEGER_LITERAL("18446744073709551615");
26static const BigInteger kTwo64 = BIGINTEGER_LITERAL("18446744073709551616");
27
28TEST(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);
38
39 EXPECT_EQ(2u, kTwo64.GetCount());
40 EXPECT_EQ(0u, kTwo64.GetDigit(0));
41 EXPECT_EQ(1u, kTwo64.GetDigit(1));
42}
43
44TEST(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
63TEST(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
85TEST(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
107TEST(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 a = 1;
125 a <<= 64; // a.count_ != 1
126 a <<= 256; // interShift == 0
127 EXPECT_TRUE(BIGINTEGER_LITERAL("2135987035920910082395021706169552114602704522356652769947041607822219725780640550022962086936576") == a);
128}
129
130TEST(BigInteger, Compare) {
131 EXPECT_EQ(0, kZero.Compare(kZero));
132 EXPECT_EQ(1, kOne.Compare(kZero));
133 EXPECT_EQ(-1, kZero.Compare(kOne));
134 EXPECT_EQ(0, kUint64Max.Compare(kUint64Max));
135 EXPECT_EQ(0, kTwo64.Compare(kTwo64));
136 EXPECT_EQ(-1, kUint64Max.Compare(kTwo64));
137 EXPECT_EQ(1, kTwo64.Compare(kUint64Max));
138}
#define BIGINTEGER_LITERAL(s)
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1954
#define EXPECT_TRUE(condition)
Definition gtest.h:1895
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition rapidjson.h:289
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136