mirror of
https://github.com/thunderbrewhq/squall.git
synced 2025-12-12 02:22:30 +00:00
feat(big): add Compare
This commit is contained in:
parent
aad7f751dd
commit
e9d3284c70
3 changed files with 94 additions and 0 deletions
|
|
@ -22,6 +22,18 @@ void Add(BigBuffer& a, const BigBuffer& b, const BigBuffer& c) {
|
|||
a.SetCount(i);
|
||||
}
|
||||
|
||||
int32_t Compare(const BigBuffer& a, const BigBuffer& b) {
|
||||
int32_t result = 0;
|
||||
|
||||
for (int32_t i = 0; a.IsUsed(i) || b.IsUsed(i); i++) {
|
||||
if (a[i] != b[i]) {
|
||||
result = b[i] < a[i] ? 1 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t ExtractLowPart(uint64_t& value) {
|
||||
auto low = static_cast<uint32_t>(value);
|
||||
value >>= 32;
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ void Add(BigBuffer& a, const BigBuffer& b, uint32_t c);
|
|||
|
||||
void Add(BigBuffer& a, const BigBuffer& b, const BigBuffer& c);
|
||||
|
||||
int32_t Compare(const BigBuffer& a, const BigBuffer& b);
|
||||
|
||||
uint32_t ExtractLowPart(uint64_t& value);
|
||||
|
||||
uint32_t ExtractLowPartSx(uint64_t& value);
|
||||
|
|
|
|||
80
test/Big.cpp
80
test/Big.cpp
|
|
@ -68,6 +68,86 @@ TEST_CASE("Add", "[big]") {
|
|||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Compare", "[big]") {
|
||||
SECTION("compares 0 and 1") {
|
||||
BigData* a;
|
||||
SBigNew(&a);
|
||||
SBigFromUnsigned(a, 0);
|
||||
|
||||
BigData* b;
|
||||
SBigNew(&b);
|
||||
SBigFromUnsigned(b, 1);
|
||||
|
||||
CHECK(Compare(a->Primary(), b->Primary()) == -1);
|
||||
|
||||
SBigDel(a);
|
||||
SBigDel(b);
|
||||
}
|
||||
|
||||
SECTION("compares 1 and 1") {
|
||||
BigData* a;
|
||||
SBigNew(&a);
|
||||
SBigFromUnsigned(a, 1);
|
||||
|
||||
BigData* b;
|
||||
SBigNew(&b);
|
||||
SBigFromUnsigned(b, 1);
|
||||
|
||||
CHECK(Compare(a->Primary(), b->Primary()) == 0);
|
||||
|
||||
SBigDel(a);
|
||||
SBigDel(b);
|
||||
}
|
||||
|
||||
SECTION("compares 10 and 1") {
|
||||
BigData* a;
|
||||
SBigNew(&a);
|
||||
SBigFromUnsigned(a, 10);
|
||||
|
||||
BigData* b;
|
||||
SBigNew(&b);
|
||||
SBigFromUnsigned(b, 1);
|
||||
|
||||
CHECK(Compare(a->Primary(), b->Primary()) == 1);
|
||||
|
||||
SBigDel(a);
|
||||
SBigDel(b);
|
||||
}
|
||||
|
||||
SECTION("compares 0x1111111111111111 and 0x22222222") {
|
||||
BigData* a;
|
||||
SBigNew(&a);
|
||||
uint64_t data = 0x1111111111111111;
|
||||
SBigFromBinary(a, reinterpret_cast<uint8_t*>(&data), sizeof(data));
|
||||
|
||||
BigData* b;
|
||||
SBigNew(&b);
|
||||
SBigFromUnsigned(b, 0x22222222);
|
||||
|
||||
CHECK(Compare(a->Primary(), b->Primary()) == 1);
|
||||
|
||||
SBigDel(a);
|
||||
SBigDel(b);
|
||||
}
|
||||
|
||||
SECTION("compares 0x11111111 and 0x2222222222222222") {
|
||||
BigData* a;
|
||||
SBigNew(&a);
|
||||
SBigFromUnsigned(a, 0x11111111);
|
||||
|
||||
BigData* b;
|
||||
SBigNew(&b);
|
||||
uint64_t data = 0x2222222222222222;
|
||||
SBigFromBinary(b, reinterpret_cast<uint8_t*>(&data), sizeof(data));
|
||||
|
||||
CHECK(Compare(a->Primary(), b->Primary()) == -1);
|
||||
|
||||
SBigDel(a);
|
||||
SBigDel(b);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("ExtractLowPart", "[big]") {
|
||||
SECTION("extracts low part of 0") {
|
||||
uint64_t value = 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue