aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/vcpkg-test/uint128.cpp
diff options
context:
space:
mode:
authornicole mazzuca <mazzucan@outlook.com>2020-04-09 14:11:53 -0700
committerGitHub <noreply@github.com>2020-04-09 14:11:53 -0700
commit47a4913834d0f1e16a3340a8718c8bcdb8431f45 (patch)
tree0559a0df51afaa3a87bd800d40442bd52ba2e651 /toolsrc/src/vcpkg-test/uint128.cpp
parent0304c453157e05b52b04039602a806564bd011c2 (diff)
downloadvcpkg-47a4913834d0f1e16a3340a8718c8bcdb8431f45.tar.gz
vcpkg-47a4913834d0f1e16a3340a8718c8bcdb8431f45.zip
[vcpkg] Correct UInt128 code 😇 (#10583)
* [vcpkg] Correct UInt128 code 😇 `UInt128::operator<<(x, y)` should clear the bottom 64 bits of `x` if `y >= 64`; however, we don't do this, and so we duplicate `x`'s bottom bits into `x.top` instead of moving them. Similarly, we have the opposite problem for `UInt128::operator>>`. This commit fixes these latent bugs, which we weren't hitting because the thing we use them for never actually shifts more than 64 bits.
Diffstat (limited to 'toolsrc/src/vcpkg-test/uint128.cpp')
-rw-r--r--toolsrc/src/vcpkg-test/uint128.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/toolsrc/src/vcpkg-test/uint128.cpp b/toolsrc/src/vcpkg-test/uint128.cpp
new file mode 100644
index 000000000..57d0169af
--- /dev/null
+++ b/toolsrc/src/vcpkg-test/uint128.cpp
@@ -0,0 +1,64 @@
+#include <catch2/catch.hpp>
+
+#include <vcpkg/base/uint128.h>
+
+TEST_CASE ("uint128 constructor and assign", "[uint128]") {
+ vcpkg::UInt128 x = 120;
+ REQUIRE(x.bottom_64_bits() == 120);
+ REQUIRE(x.top_64_bits() == 0);
+
+ x = 3201;
+ REQUIRE(x.bottom_64_bits() == 3201);
+ REQUIRE(x.top_64_bits() == 0);
+
+ x = 0xFFFF'FFFF'FFFF'FFFF;
+ REQUIRE(x.bottom_64_bits() == 0xFFFF'FFFF'FFFF'FFFF);
+ REQUIRE(x.top_64_bits() == 0);
+}
+
+TEST_CASE ("uint128 add-assign", "[uint128]") {
+ vcpkg::UInt128 x = 0xFFFF'FFFF'FFFF'FFFF;
+ x += 1;
+ REQUIRE(x.bottom_64_bits() == 0);
+ REQUIRE(x.top_64_bits() == 1);
+}
+
+TEST_CASE ("uint128 shl-assign", "[uint128]") {
+ vcpkg::UInt128 x = 0xFFFF'FFFF'FFFF'FFFF;
+ x <<= 32;
+ REQUIRE(x.bottom_64_bits() == 0xFFFF'FFFF'0000'0000);
+ REQUIRE(x.top_64_bits() == 0x0000'0000'FFFF'FFFF);
+
+ x <<= 60;
+ REQUIRE(x.bottom_64_bits() == 0);
+ REQUIRE(x.top_64_bits() == 0xFFFF'FFFF'F000'0000);
+
+ x = 1;
+ x <<= 96;
+ REQUIRE(x.bottom_64_bits() == 0);
+ REQUIRE(x.top_64_bits() == (uint64_t(1) << 32));
+}
+
+TEST_CASE ("uint128 shr-assign", "[uint128]") {
+ vcpkg::UInt128 x = 0xFFFF'FFFF'FFFF'FFFF;
+ x <<= 64;
+ REQUIRE(x.bottom_64_bits() == 0x0000'0000'0000'0000);
+ REQUIRE(x.top_64_bits() == 0xFFFF'FFFF'FFFF'FFFF);
+
+ x >>= 32;
+ REQUIRE(x.bottom_64_bits() == 0xFFFF'FFFF'0000'0000);
+ REQUIRE(x.top_64_bits() == 0x0000'0000'FFFF'FFFF);
+
+ x >>= 60;
+ REQUIRE(x.bottom_64_bits() == 0x0000'000F'FFFF'FFFF);
+ REQUIRE(x.top_64_bits() == 0x0000'0000'0000'0000);
+
+ x = 0x8000'0000'0000'0000;
+ x <<= 64;
+ REQUIRE(x.bottom_64_bits() == 0);
+ REQUIRE(x.top_64_bits() == 0x8000'0000'0000'0000);
+
+ x >>= 96;
+ REQUIRE(x.bottom_64_bits() == (uint64_t(1) << 31));
+ REQUIRE(x.top_64_bits() == 0);
+}