summaryrefslogtreecommitdiffstats
path: root/src/lib/util/tests/bigint_unittest.cc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 14:53:22 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 14:53:22 +0000
commit52c021ee0b0c6ad2128ed550c694aad0d11d4c3f (patch)
tree83cf8627b94336cf4bee7479b9749263bbfd3a06 /src/lib/util/tests/bigint_unittest.cc
parentInitial commit. (diff)
downloadisc-kea-52c021ee0b0c6ad2128ed550c694aad0d11d4c3f.tar.xz
isc-kea-52c021ee0b0c6ad2128ed550c694aad0d11d4c3f.zip
Adding upstream version 2.5.7.upstream/2.5.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/lib/util/tests/bigint_unittest.cc')
-rw-r--r--src/lib/util/tests/bigint_unittest.cc160
1 files changed, 160 insertions, 0 deletions
diff --git a/src/lib/util/tests/bigint_unittest.cc b/src/lib/util/tests/bigint_unittest.cc
new file mode 100644
index 0000000..bbd2c29
--- /dev/null
+++ b/src/lib/util/tests/bigint_unittest.cc
@@ -0,0 +1,160 @@
+// Copyright (C) 2023 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include <config.h>
+
+#include <testutils/gtest_utils.h>
+#include <util/bigints.h>
+
+#include <gtest/gtest.h>
+
+#include <limits>
+
+using namespace isc::util;
+
+using namespace std;
+
+namespace {
+
+// C++ doesn't allow very big integer literals, so that's why some tests on big
+// numbers might appear like they're unnecessarily circumventing a more obvious
+// test choice.
+
+// Checks that int128_t behaves like a signed integer should.
+TEST(BigintTest, int128) {
+ // Check addition with small numbers.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(24, int128_t(16) + int128_t(8));
+ });
+
+ // Check subtraction with small numbers.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(48, int128_t(64) - int128_t(16));
+ });
+
+ // Check multiplication with small numbers.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(8, int128_t(2) * int128_t(4));
+ });
+
+ // Check division with small numbers.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(16, int128_t(64) / int128_t(4));
+ });
+
+ // Check rounded division with small numbers.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(16, int128_t(65) / int128_t(4));
+ });
+
+ // Check that INT128_MIN < INT64_MIN.
+ EXPECT_NO_THROW({
+ EXPECT_LT(numeric_limits<int128_t>::min(), numeric_limits<int64_t>::min());
+ });
+
+ // Check that UINT64_MAX < INT128_MAX.
+ EXPECT_NO_THROW({
+ EXPECT_LT(numeric_limits<uint64_t>::max(), numeric_limits<int128_t>::max());
+ });
+
+ // Check that int128_t is default-initialized to zero. Not a strict
+ // requirement by Kea, but something that the current implementation ensures.
+ int128_t i128;
+ EXPECT_NO_THROW({
+ EXPECT_EQ(0, i128);
+ });
+
+ // Shifting to the right beyond zero does not result in an underflow error.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(0, uint128_t(1) >> 2);
+ });
+
+ // Check that dividing by zero throws.
+ EXPECT_THROW_MSG(int128_t(1) / 0, overflow_error, "Division by zero.");
+
+ // Check that underflowing results in an error.
+ i128 = numeric_limits<int128_t>::min();
+ EXPECT_THROW_MSG(i128 - 1, overflow_error, "overflow in addition");
+ EXPECT_THROW_MSG(i128 + i128, overflow_error, "overflow in addition");
+
+ // Check that overflowing results in an error.
+ i128 = numeric_limits<int128_t>::max();
+ EXPECT_THROW_MSG(i128 + 1, overflow_error, "overflow in addition");
+ EXPECT_THROW_MSG(i128 + i128, overflow_error, "overflow in addition");
+ EXPECT_THROW_MSG(2 * i128, overflow_error, "overflow in multiplication");
+ EXPECT_THROW_MSG(i128 << 1, overflow_error, "Shift out of range");
+}
+
+// Checks that uint128_t behaves like an unsigned integer should.
+TEST(BigintTest, uint128) {
+ // Check addition with small numbers.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(24, uint128_t(16) + uint128_t(8));
+ });
+
+ // Check subtraction with small numbers.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(48, uint128_t(64) - uint128_t(16));
+ });
+
+ // Check multiplication with small numbers.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(8, uint128_t(2) * uint128_t(4));
+ });
+
+ // Check division with small numbers.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(16, uint128_t(64) / uint128_t(4));
+ });
+
+ // Check rounded division with small numbers.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(16, uint128_t(65) / uint128_t(4));
+ });
+
+ // Check that UINT128_MIN is 0.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(0, numeric_limits<uint128_t>::min());
+ });
+
+ // Check that UINT64_MAX < UINT128_MAX.
+ EXPECT_NO_THROW({
+ EXPECT_LT(numeric_limits<uint64_t>::max(), numeric_limits<int128_t>::max());
+ });
+
+ // Check that INT128_MAX < UINT128_MAX.
+ EXPECT_NO_THROW({
+ EXPECT_LT(numeric_limits<uint64_t>::max(), numeric_limits<int128_t>::max());
+ });
+
+ // Check that uint128_t is default-initialized to zero. Not a strict
+ // requirement by Kea, but something that the current implementation ensures.
+ uint128_t u128;
+ EXPECT_NO_THROW({
+ EXPECT_EQ(0, u128);
+ });
+
+ // Shifting to the right beyond zero does not result in an underflow error.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(0, uint128_t(1) >> 2);
+ });
+
+ // Check that dividing by zero throws.
+ EXPECT_THROW_MSG(uint128_t(1) / 0, overflow_error, "Division by zero.");
+
+ // Check that underflowing results in an error.
+ u128 = numeric_limits<uint128_t>::min();
+ EXPECT_THROW_MSG(u128 - 1, range_error, "Subtraction resulted in a negative value, but the type is unsigned");
+
+ // Check that overflowing results in an error.
+ u128 = numeric_limits<uint128_t>::max();
+ EXPECT_THROW_MSG(u128 + 1, overflow_error, "overflow in addition");
+ EXPECT_THROW_MSG(u128 + u128, overflow_error, "overflow in addition");
+ EXPECT_THROW_MSG(2 * u128, overflow_error, "overflow in multiplication");
+ EXPECT_THROW_MSG(u128 << 1, overflow_error, "Shift out of range");
+}
+
+} // namespace