aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/int128/ut/i128_division_ut.cpp
diff options
context:
space:
mode:
authorVlad Yaroslavlev <vladon@vladon.com>2022-02-10 16:46:23 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:46:23 +0300
commit706b83ed7de5a473436620367af31fc0ceecde07 (patch)
tree103305d30dec77e8f6367753367f59b3cd68f9f1 /library/cpp/int128/ut/i128_division_ut.cpp
parent918e8a1574070d0ec733f0b76cfad8f8892ad2e5 (diff)
downloadydb-706b83ed7de5a473436620367af31fc0ceecde07.tar.gz
Restoring authorship annotation for Vlad Yaroslavlev <vladon@vladon.com>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/int128/ut/i128_division_ut.cpp')
-rw-r--r--library/cpp/int128/ut/i128_division_ut.cpp822
1 files changed, 411 insertions, 411 deletions
diff --git a/library/cpp/int128/ut/i128_division_ut.cpp b/library/cpp/int128/ut/i128_division_ut.cpp
index 46b0ca27f5..b58731dd7e 100644
--- a/library/cpp/int128/ut/i128_division_ut.cpp
+++ b/library/cpp/int128/ut/i128_division_ut.cpp
@@ -1,413 +1,413 @@
#include <library/cpp/testing/unittest/registar.h>
-
+
#include <library/cpp/int128/int128.h>
-
-#include <util/generic/cast.h>
-
-Y_UNIT_TEST_SUITE(I128DivisionBy1Suite) {
- Y_UNIT_TEST(I128Divide0By1) {
- i128 dividend = 0;
- i128 divider = 1;
- i128 expectedQuotient = 0;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128Divide1By1) {
- i128 dividend = 1;
- i128 divider = 1;
- i128 expectedQuotient = 1;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128Divide2By1) {
- i128 dividend = 2;
- i128 divider = 1;
- i128 expectedQuotient = 2;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128Divide42By1) {
- i128 dividend = 42;
- i128 divider = 1;
- i128 expectedQuotient = 42;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128DivideMaxUi64By1) {
- i128 dividend = std::numeric_limits<ui64>::max();
- i128 divider = 1;
- i128 expectedQuotient = std::numeric_limits<ui64>::max();
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128DivideMaxUi64Plus1By1) {
- i128 dividend = i128{std::numeric_limits<ui64>::max()} + i128{1};
- i128 divider = 1;
- i128 expectedQuotient = i128{std::numeric_limits<ui64>::max()} + i128{1};
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128DivideMaxUi64Plus42By1) {
- i128 dividend = i128{std::numeric_limits<ui64>::max()} + i128{42};
- i128 divider = 1;
- i128 expectedQuotient = i128{std::numeric_limits<ui64>::max()} + i128{42};
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128DivideMaxI128By1) {
- i128 dividend = std::numeric_limits<i128>::max();
- i128 divider = 1;
- i128 expectedQuotient = std::numeric_limits<i128>::max();
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128DivideMaxI128Minus1By1) {
- i128 dividend = std::numeric_limits<i128>::max() - 1;
- i128 divider = 1;
- i128 expectedQuotient = std::numeric_limits<i128>::max() - 1;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-}
-
-Y_UNIT_TEST_SUITE(I128DivisionByEqualSuite) {
- Y_UNIT_TEST(I128Divide1ByEqual) {
- i128 dividend = 1;
- i128 divider = dividend;
- i128 expectedQuotient = 1;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128Divide2ByEqual) {
- i128 dividend = 2;
- i128 divider = dividend;
- i128 expectedQuotient = 1;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128Divide42ByEqual) {
- i128 dividend = 42;
- i128 divider = dividend;
- i128 expectedQuotient = 1;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128DivideMaxUi64ByEqual) {
- i128 dividend = std::numeric_limits<ui64>::max();
- i128 divider = dividend;
- i128 expectedQuotient = 1;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128DivideMaxUi64Plus1ByEqual) {
- i128 dividend = i128{std::numeric_limits<ui64>::max()} + i128{1};
- i128 divider = dividend;
- i128 expectedQuotient = 1;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128DivideMaxUi64Plus42ByEqual) {
- i128 dividend = i128{std::numeric_limits<ui64>::max()} + i128{42};
- i128 divider = dividend;
- i128 expectedQuotient = 1;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128DivideMaxI128ByEqual) {
- i128 dividend = std::numeric_limits<i128>::max();
- i128 divider = dividend;
- i128 expectedQuotient = 1;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128DivideMaxI128Minus1ByEqual) {
- i128 dividend = std::numeric_limits<i128>::max() - 1;
- i128 divider = dividend;
- i128 expectedQuotient = 1;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-}
-
-Y_UNIT_TEST_SUITE(I128DivisionLessByHigherSuite) {
- Y_UNIT_TEST(I128Divide42By84) {
- i128 dividend = 42;
- i128 divider = 84;
- i128 expectedQuotient = 0;
- i128 expectedRemainder = 42;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128Divide42ByMaxUi64) {
- i128 dividend = 42;
- i128 divider = std::numeric_limits<ui64>::max();
- i128 expectedQuotient = 0;
- i128 expectedRemainder = 42;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128Divide42ByMaxUi64Plus1) {
- i128 dividend = 42;
- i128 divider = i128{std::numeric_limits<ui64>::max()} + i128{1};
- i128 expectedQuotient = 0;
- i128 expectedRemainder = 42;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(I128DivideMaxUi64ByMaxUi64Plus1) {
- i128 dividend = i128{std::numeric_limits<ui64>::max()};
- i128 divider = i128{std::numeric_limits<ui64>::max()} + i128{1};
- i128 expectedQuotient = 0;
- i128 expectedRemainder = i128{std::numeric_limits<ui64>::max()};
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-}
-
-Y_UNIT_TEST_SUITE(I128DivisionWithDifferentSigns) {
- Y_UNIT_TEST(DivisionPositiveByNegative) {
- i128 dividend = i128{100};
- i128 divider = i128{-33};
- i128 expectedQuotient = -3;
- i128 expectedRemainder = 1;
- i128 quotient = dividend / divider;
- i128 remainder = dividend % divider;
-
- UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
- UNIT_ASSERT_EQUAL(remainder, expectedRemainder);
- }
-
- Y_UNIT_TEST(DivisionNegativeByPositive) {
- i128 dividend = i128{-100};
- i128 divider = i128{33};
- i128 expectedQuotient = -3;
- i128 expectedRemainder = -1;
- i128 quotient = dividend / divider;
- i128 remainder = dividend % divider;
-
- UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
- UNIT_ASSERT_EQUAL(remainder, expectedRemainder);
- }
-
- Y_UNIT_TEST(DivisionNegativeByNegative) {
- i128 dividend = i128{-100};
- i128 divider = i128{-33};
- i128 expectedQuotient = 3;
- i128 expectedRemainder = -1;
- i128 quotient = dividend / divider;
- i128 remainder = dividend % divider;
-
- UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
- UNIT_ASSERT_EQUAL(remainder, expectedRemainder);
- }
-}
-
-Y_UNIT_TEST_SUITE(i128DivisionBigByBigSuite) {
- Y_UNIT_TEST(i128DivideBigByBig1) {
- i128 dividend = {64, 0};
- i128 divider = {1, 0};
- i128 expectedQuotient = 64;
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(i128DivideBigByBig1_PosByNeg) {
- i128 dividend = i128{64, 0};
- i128 divider = -i128{1, 0};
- i128 expectedQuotient = -i128{64};
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(i128DivideBigByBig1_NegByPos) {
- i128 dividend = -i128{64, 0};
- i128 divider = i128{1, 0};
- i128 expectedQuotient = -i128{64};
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(i128DivideBigByBig1_NegByNeg) {
- i128 dividend = -i128{64, 0};
- i128 divider = -i128{1, 0};
- i128 expectedQuotient = i128{64};
- i128 expectedRemainder = 0;
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(i128DivideBigByBig2) {
- i128 dividend = {64, 0};
- i128 divider = {12, 5};
- i128 expectedQuotient = 5;
- i128 expectedRemainder = i128{3, 18446744073709551591ull}; // plz don't ask
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(i128DivideBigByBig2_PosByNeg) {
- i128 dividend = i128{64, 0};
- i128 divider = -i128{12, 5};
- i128 expectedQuotient = -5;
- i128 expectedRemainder = i128{3, 18446744073709551591ull};
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(i128DivideBigByBig2_NegByPos) {
- i128 dividend = -i128{64, 0};
- i128 divider = i128{12, 5};
- i128 expectedQuotient = -5;
- i128 expectedRemainder = -i128{3, 18446744073709551591ull};
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
- Y_UNIT_TEST(i128DivideBigByBig2_NegByNeg) {
- i128 dividend = -i128{64, 0};
- i128 divider = -i128{12, 5};
- i128 expectedQuotient = 5;
- i128 expectedRemainder = -i128{3, 18446744073709551591ull};
-
- UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
- UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
- }
-
-}
-
-Y_UNIT_TEST_SUITE(i128DivisionAlgo) {
- Y_UNIT_TEST(ii128DivideAlgoCheck_PosByPos) {
- /*
- 49672666804009505000000 / 10000000 == 4967266680400950
- 49672666804009505000000 % 10000000 == 5000000
- */
- i128 dividend = {2692ull, 14031757583392049728ull};
- i64 divider = 10000000;
- i128 expectedQuotient = {0, 4967266680400950ull};
- i128 expectedRemainder = {0, 5000000ull};
-
- i128 quotient = dividend / divider;
- i128 reminder = dividend % divider;
-
- UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
- UNIT_ASSERT_EQUAL(reminder, expectedRemainder);
- }
-
- Y_UNIT_TEST(ii128DivideAlgoCheck_PosByNeg) {
- /*
- 49672666804009505000000 / -10000000 == -4967266680400950
- 49672666804009505000000 % -10000000 == 5000000
- */
- i128 dividend = {2692ull, 14031757583392049728ull};
- i64 divider = -10000000;
- i128 expectedQuotient = -i128{0, 4967266680400950ull};
- i128 expectedRemainder = {0, 5000000ull};
-
- i128 quotient = dividend / divider;
- i128 reminder = dividend % divider;
-
- UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
- UNIT_ASSERT_EQUAL(reminder, expectedRemainder);
- }
-
- Y_UNIT_TEST(ii128DivideAlgoCheck_NegByPos) {
- /*
- -49672666804009505000000 / 10000000 == -4967266680400950
- -49672666804009505000000 % 10000000 == -5000000
- */
- i128 dividend = -i128{2692ull, 14031757583392049728ull};
- i64 divider = 10000000;
- i128 expectedQuotient = -i128{0, 4967266680400950ull};
- i128 expectedRemainder = -i128{0, 5000000ull};
-
- i128 quotient = dividend / divider;
- i128 reminder = dividend % divider;
-
- UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
- UNIT_ASSERT_EQUAL(reminder, expectedRemainder);
- }
-
- Y_UNIT_TEST(ii128DivideAlgoCheck_NegByNeg) {
- /*
- -49672666804009505000000 / -10000000 == 4967266680400950
- -49672666804009505000000 % -10000000 == -5000000
- */
- i128 dividend = -i128{2692ull, 14031757583392049728ull};
- i64 divider = -10000000;
- i128 expectedQuotient = {0, 4967266680400950ull};
- i128 expectedRemainder = -i128{0, 5000000ull};
-
- i128 quotient = dividend / divider;
- i128 reminder = dividend % divider;
-
- UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
- UNIT_ASSERT_EQUAL(reminder, expectedRemainder);
- }
-
-}
+
+#include <util/generic/cast.h>
+
+Y_UNIT_TEST_SUITE(I128DivisionBy1Suite) {
+ Y_UNIT_TEST(I128Divide0By1) {
+ i128 dividend = 0;
+ i128 divider = 1;
+ i128 expectedQuotient = 0;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128Divide1By1) {
+ i128 dividend = 1;
+ i128 divider = 1;
+ i128 expectedQuotient = 1;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128Divide2By1) {
+ i128 dividend = 2;
+ i128 divider = 1;
+ i128 expectedQuotient = 2;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128Divide42By1) {
+ i128 dividend = 42;
+ i128 divider = 1;
+ i128 expectedQuotient = 42;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128DivideMaxUi64By1) {
+ i128 dividend = std::numeric_limits<ui64>::max();
+ i128 divider = 1;
+ i128 expectedQuotient = std::numeric_limits<ui64>::max();
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128DivideMaxUi64Plus1By1) {
+ i128 dividend = i128{std::numeric_limits<ui64>::max()} + i128{1};
+ i128 divider = 1;
+ i128 expectedQuotient = i128{std::numeric_limits<ui64>::max()} + i128{1};
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128DivideMaxUi64Plus42By1) {
+ i128 dividend = i128{std::numeric_limits<ui64>::max()} + i128{42};
+ i128 divider = 1;
+ i128 expectedQuotient = i128{std::numeric_limits<ui64>::max()} + i128{42};
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128DivideMaxI128By1) {
+ i128 dividend = std::numeric_limits<i128>::max();
+ i128 divider = 1;
+ i128 expectedQuotient = std::numeric_limits<i128>::max();
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128DivideMaxI128Minus1By1) {
+ i128 dividend = std::numeric_limits<i128>::max() - 1;
+ i128 divider = 1;
+ i128 expectedQuotient = std::numeric_limits<i128>::max() - 1;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+}
+
+Y_UNIT_TEST_SUITE(I128DivisionByEqualSuite) {
+ Y_UNIT_TEST(I128Divide1ByEqual) {
+ i128 dividend = 1;
+ i128 divider = dividend;
+ i128 expectedQuotient = 1;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128Divide2ByEqual) {
+ i128 dividend = 2;
+ i128 divider = dividend;
+ i128 expectedQuotient = 1;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128Divide42ByEqual) {
+ i128 dividend = 42;
+ i128 divider = dividend;
+ i128 expectedQuotient = 1;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128DivideMaxUi64ByEqual) {
+ i128 dividend = std::numeric_limits<ui64>::max();
+ i128 divider = dividend;
+ i128 expectedQuotient = 1;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128DivideMaxUi64Plus1ByEqual) {
+ i128 dividend = i128{std::numeric_limits<ui64>::max()} + i128{1};
+ i128 divider = dividend;
+ i128 expectedQuotient = 1;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128DivideMaxUi64Plus42ByEqual) {
+ i128 dividend = i128{std::numeric_limits<ui64>::max()} + i128{42};
+ i128 divider = dividend;
+ i128 expectedQuotient = 1;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128DivideMaxI128ByEqual) {
+ i128 dividend = std::numeric_limits<i128>::max();
+ i128 divider = dividend;
+ i128 expectedQuotient = 1;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128DivideMaxI128Minus1ByEqual) {
+ i128 dividend = std::numeric_limits<i128>::max() - 1;
+ i128 divider = dividend;
+ i128 expectedQuotient = 1;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+}
+
+Y_UNIT_TEST_SUITE(I128DivisionLessByHigherSuite) {
+ Y_UNIT_TEST(I128Divide42By84) {
+ i128 dividend = 42;
+ i128 divider = 84;
+ i128 expectedQuotient = 0;
+ i128 expectedRemainder = 42;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128Divide42ByMaxUi64) {
+ i128 dividend = 42;
+ i128 divider = std::numeric_limits<ui64>::max();
+ i128 expectedQuotient = 0;
+ i128 expectedRemainder = 42;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128Divide42ByMaxUi64Plus1) {
+ i128 dividend = 42;
+ i128 divider = i128{std::numeric_limits<ui64>::max()} + i128{1};
+ i128 expectedQuotient = 0;
+ i128 expectedRemainder = 42;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(I128DivideMaxUi64ByMaxUi64Plus1) {
+ i128 dividend = i128{std::numeric_limits<ui64>::max()};
+ i128 divider = i128{std::numeric_limits<ui64>::max()} + i128{1};
+ i128 expectedQuotient = 0;
+ i128 expectedRemainder = i128{std::numeric_limits<ui64>::max()};
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+}
+
+Y_UNIT_TEST_SUITE(I128DivisionWithDifferentSigns) {
+ Y_UNIT_TEST(DivisionPositiveByNegative) {
+ i128 dividend = i128{100};
+ i128 divider = i128{-33};
+ i128 expectedQuotient = -3;
+ i128 expectedRemainder = 1;
+ i128 quotient = dividend / divider;
+ i128 remainder = dividend % divider;
+
+ UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
+ UNIT_ASSERT_EQUAL(remainder, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(DivisionNegativeByPositive) {
+ i128 dividend = i128{-100};
+ i128 divider = i128{33};
+ i128 expectedQuotient = -3;
+ i128 expectedRemainder = -1;
+ i128 quotient = dividend / divider;
+ i128 remainder = dividend % divider;
+
+ UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
+ UNIT_ASSERT_EQUAL(remainder, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(DivisionNegativeByNegative) {
+ i128 dividend = i128{-100};
+ i128 divider = i128{-33};
+ i128 expectedQuotient = 3;
+ i128 expectedRemainder = -1;
+ i128 quotient = dividend / divider;
+ i128 remainder = dividend % divider;
+
+ UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
+ UNIT_ASSERT_EQUAL(remainder, expectedRemainder);
+ }
+}
+
+Y_UNIT_TEST_SUITE(i128DivisionBigByBigSuite) {
+ Y_UNIT_TEST(i128DivideBigByBig1) {
+ i128 dividend = {64, 0};
+ i128 divider = {1, 0};
+ i128 expectedQuotient = 64;
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(i128DivideBigByBig1_PosByNeg) {
+ i128 dividend = i128{64, 0};
+ i128 divider = -i128{1, 0};
+ i128 expectedQuotient = -i128{64};
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(i128DivideBigByBig1_NegByPos) {
+ i128 dividend = -i128{64, 0};
+ i128 divider = i128{1, 0};
+ i128 expectedQuotient = -i128{64};
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(i128DivideBigByBig1_NegByNeg) {
+ i128 dividend = -i128{64, 0};
+ i128 divider = -i128{1, 0};
+ i128 expectedQuotient = i128{64};
+ i128 expectedRemainder = 0;
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(i128DivideBigByBig2) {
+ i128 dividend = {64, 0};
+ i128 divider = {12, 5};
+ i128 expectedQuotient = 5;
+ i128 expectedRemainder = i128{3, 18446744073709551591ull}; // plz don't ask
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(i128DivideBigByBig2_PosByNeg) {
+ i128 dividend = i128{64, 0};
+ i128 divider = -i128{12, 5};
+ i128 expectedQuotient = -5;
+ i128 expectedRemainder = i128{3, 18446744073709551591ull};
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(i128DivideBigByBig2_NegByPos) {
+ i128 dividend = -i128{64, 0};
+ i128 divider = i128{12, 5};
+ i128 expectedQuotient = -5;
+ i128 expectedRemainder = -i128{3, 18446744073709551591ull};
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(i128DivideBigByBig2_NegByNeg) {
+ i128 dividend = -i128{64, 0};
+ i128 divider = -i128{12, 5};
+ i128 expectedQuotient = 5;
+ i128 expectedRemainder = -i128{3, 18446744073709551591ull};
+
+ UNIT_ASSERT_EQUAL(dividend / divider, expectedQuotient);
+ UNIT_ASSERT_EQUAL(dividend % divider, expectedRemainder);
+ }
+
+}
+
+Y_UNIT_TEST_SUITE(i128DivisionAlgo) {
+ Y_UNIT_TEST(ii128DivideAlgoCheck_PosByPos) {
+ /*
+ 49672666804009505000000 / 10000000 == 4967266680400950
+ 49672666804009505000000 % 10000000 == 5000000
+ */
+ i128 dividend = {2692ull, 14031757583392049728ull};
+ i64 divider = 10000000;
+ i128 expectedQuotient = {0, 4967266680400950ull};
+ i128 expectedRemainder = {0, 5000000ull};
+
+ i128 quotient = dividend / divider;
+ i128 reminder = dividend % divider;
+
+ UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
+ UNIT_ASSERT_EQUAL(reminder, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(ii128DivideAlgoCheck_PosByNeg) {
+ /*
+ 49672666804009505000000 / -10000000 == -4967266680400950
+ 49672666804009505000000 % -10000000 == 5000000
+ */
+ i128 dividend = {2692ull, 14031757583392049728ull};
+ i64 divider = -10000000;
+ i128 expectedQuotient = -i128{0, 4967266680400950ull};
+ i128 expectedRemainder = {0, 5000000ull};
+
+ i128 quotient = dividend / divider;
+ i128 reminder = dividend % divider;
+
+ UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
+ UNIT_ASSERT_EQUAL(reminder, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(ii128DivideAlgoCheck_NegByPos) {
+ /*
+ -49672666804009505000000 / 10000000 == -4967266680400950
+ -49672666804009505000000 % 10000000 == -5000000
+ */
+ i128 dividend = -i128{2692ull, 14031757583392049728ull};
+ i64 divider = 10000000;
+ i128 expectedQuotient = -i128{0, 4967266680400950ull};
+ i128 expectedRemainder = -i128{0, 5000000ull};
+
+ i128 quotient = dividend / divider;
+ i128 reminder = dividend % divider;
+
+ UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
+ UNIT_ASSERT_EQUAL(reminder, expectedRemainder);
+ }
+
+ Y_UNIT_TEST(ii128DivideAlgoCheck_NegByNeg) {
+ /*
+ -49672666804009505000000 / -10000000 == 4967266680400950
+ -49672666804009505000000 % -10000000 == -5000000
+ */
+ i128 dividend = -i128{2692ull, 14031757583392049728ull};
+ i64 divider = -10000000;
+ i128 expectedQuotient = {0, 4967266680400950ull};
+ i128 expectedRemainder = -i128{0, 5000000ull};
+
+ i128 quotient = dividend / divider;
+ i128 reminder = dividend % divider;
+
+ UNIT_ASSERT_EQUAL(quotient, expectedQuotient);
+ UNIT_ASSERT_EQUAL(reminder, expectedRemainder);
+ }
+
+}