diff options
author | Vlad Yaroslavlev <vladon@vladon.com> | 2022-02-10 16:46:23 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:23 +0300 |
commit | 706b83ed7de5a473436620367af31fc0ceecde07 (patch) | |
tree | 103305d30dec77e8f6367753367f59b3cd68f9f1 /library/cpp/int128/ut/int128_old_ut.cpp | |
parent | 918e8a1574070d0ec733f0b76cfad8f8892ad2e5 (diff) | |
download | ydb-706b83ed7de5a473436620367af31fc0ceecde07.tar.gz |
Restoring authorship annotation for Vlad Yaroslavlev <vladon@vladon.com>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/int128/ut/int128_old_ut.cpp')
-rw-r--r-- | library/cpp/int128/ut/int128_old_ut.cpp | 202 |
1 files changed, 101 insertions, 101 deletions
diff --git a/library/cpp/int128/ut/int128_old_ut.cpp b/library/cpp/int128/ut/int128_old_ut.cpp index 2c5b9e9610..e8dfbff5c2 100644 --- a/library/cpp/int128/ut/int128_old_ut.cpp +++ b/library/cpp/int128/ut/int128_old_ut.cpp @@ -1,8 +1,8 @@ #include <library/cpp/testing/unittest/registar.h> #include <library/cpp/int128/int128.h> -#include "int128_ut_helpers.h" - +#include "int128_ut_helpers.h" + class TUInt128Test: public TTestBase { UNIT_TEST_SUITE(TUInt128Test); UNIT_TEST(Create); @@ -13,9 +13,9 @@ class TUInt128Test: public TTestBase { UNIT_TEST(Underflow); UNIT_TEST(ToStringTest); UNIT_TEST(FromStringTest); -#if defined(Y_HAVE_INT128) +#if defined(Y_HAVE_INT128) UNIT_TEST(FromSystemUint128); -#endif +#endif UNIT_TEST_SUITE_END(); private: @@ -25,30 +25,30 @@ private: void Shift(); void Overflow(); void Underflow(); - void ToStringTest(); - void FromStringTest(); -#if defined(Y_HAVE_INT128) - void FromSystemUint128(); -#endif + void ToStringTest(); + void FromStringTest(); +#if defined(Y_HAVE_INT128) + void FromSystemUint128(); +#endif }; UNIT_TEST_SUITE_REGISTRATION(TUInt128Test); void TUInt128Test::Create() { - const ui128 n1 = 10; + const ui128 n1 = 10; UNIT_ASSERT_EQUAL(n1, 10); - const ui128 n2 = n1; + const ui128 n2 = n1; UNIT_ASSERT_EQUAL(n2, 10); - const ui128 n3(10); + const ui128 n3(10); UNIT_ASSERT_EQUAL(n3, 10); } void TUInt128Test::Minus() { - const ui128 n2 = 20; - const ui128 n3 = 30; + const ui128 n2 = 20; + const ui128 n3 = 30; - ui128 n4 = n3 - n2; + ui128 n4 = n3 - n2; UNIT_ASSERT_EQUAL(n4, 10); n4 = n4 - 2; @@ -61,10 +61,10 @@ void TUInt128Test::Minus() { UNIT_ASSERT_EQUAL(n4, 4); } void TUInt128Test::Plus() { - const ui128 n2 = 20; - const ui128 n3 = 30; + const ui128 n2 = 20; + const ui128 n3 = 30; - ui128 n4 = n3 + n2; + ui128 n4 = n3 + n2; UNIT_ASSERT_EQUAL(n4, 50); n4 = n4 + 2; @@ -77,103 +77,103 @@ void TUInt128Test::Plus() { UNIT_ASSERT_EQUAL(n4, 64); } void TUInt128Test::Shift() { - ui128 n = 1; + ui128 n = 1; - const ui128 n4 = n << 4; - UNIT_ASSERT_EQUAL(n4, ui128(0x0, 0x0000000000000010)); + const ui128 n4 = n << 4; + UNIT_ASSERT_EQUAL(n4, ui128(0x0, 0x0000000000000010)); UNIT_ASSERT_EQUAL(n4 >> 4, 1); - const ui128 n8 = n << 8; - UNIT_ASSERT_EQUAL(n8, ui128(0x0, 0x0000000000000100)); + const ui128 n8 = n << 8; + UNIT_ASSERT_EQUAL(n8, ui128(0x0, 0x0000000000000100)); UNIT_ASSERT_EQUAL(n8 >> 8, 1); - const ui128 n60 = n << 60; - UNIT_ASSERT_EQUAL(n60, ui128(0x0, 0x1000000000000000)); + const ui128 n60 = n << 60; + UNIT_ASSERT_EQUAL(n60, ui128(0x0, 0x1000000000000000)); UNIT_ASSERT_EQUAL(n60 >> 60, 1); - const ui128 n64 = n << 64; - UNIT_ASSERT_EQUAL(n64, ui128(0x1, 0x0000000000000000)); + const ui128 n64 = n << 64; + UNIT_ASSERT_EQUAL(n64, ui128(0x1, 0x0000000000000000)); UNIT_ASSERT_EQUAL(n64 >> 64, 1); - const ui128 n124 = n << 124; - UNIT_ASSERT_EQUAL(n124, ui128(0x1000000000000000, 0x0000000000000000)); + const ui128 n124 = n << 124; + UNIT_ASSERT_EQUAL(n124, ui128(0x1000000000000000, 0x0000000000000000)); UNIT_ASSERT_EQUAL(n124 >> 124, 1); } void TUInt128Test::Overflow() { - ui128 n = ui128(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF); - const ui128 n2 = n + 2; + ui128 n = ui128(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF); + const ui128 n2 = n + 2; UNIT_ASSERT_EQUAL(n2, 1); } void TUInt128Test::Underflow() { - ui128 n = 1; - const ui128 n128 = n - 2; - UNIT_ASSERT_EQUAL(n128, ui128(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF)); -} - -void TUInt128Test::ToStringTest() { - ui128 n(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF); - TString correct = "340282366920938463463374607431768211455"; - UNIT_ASSERT_EQUAL(correct, ::ToString(n)); -} - -void TUInt128Test::FromStringTest() { - { - const TString originalString = "37778931862957161709568"; - const ui128 number = FromString<ui128>(originalString); - UNIT_ASSERT_EQUAL(ToString(number), originalString); - } - - { - const TString originalString = "1024"; - const ui128 number = FromString<ui128>(originalString); - UNIT_ASSERT_EQUAL(ToString(number), originalString); - UNIT_ASSERT_EQUAL(GetHigh(number), 0); - UNIT_ASSERT_EQUAL(GetLow(number), 1024); - } - - { - const TString originalString = "18446744073709551616"; // 2^64, i.e. UINT64_MAX + 1 - const ui128 number = FromString<ui128>(originalString); - UNIT_ASSERT_EQUAL(ToString(number), originalString); - UNIT_ASSERT_EQUAL(GetHigh(number), 1); - UNIT_ASSERT_EQUAL(GetLow(number), 0); - } - - { - const TString originalString = "340282366920938463463374607431768211455"; // 2^128-1, i.e. UINT128_MAX - const ui128 number = FromString<ui128>(originalString); - UNIT_ASSERT_EQUAL(ToString(number), originalString); - UNIT_ASSERT_EQUAL(GetHigh(number), 0xFFFFFFFFFFFFFFFF); - UNIT_ASSERT_EQUAL(GetLow(number), 0xFFFFFFFFFFFFFFFF); - } -} - -#if defined(Y_HAVE_INT128) -void TUInt128Test::FromSystemUint128() { - unsigned __int128 n = 1; - ui128 number{n}; - - UNIT_ASSERT_EQUAL(GetLow(number), 1); - UNIT_ASSERT_EQUAL(GetHigh(number), 0); - - auto byteArray = NInt128Private::GetAsArray(number); -#ifdef _little_endian_ - UNIT_ASSERT_EQUAL(byteArray[0], 1); - for (size_t i = 1; i < 16; i++) { - UNIT_ASSERT_EQUAL(byteArray[i], 0); - } -#elif defined(_big_endian_) - UNIT_ASSERT_EQUAL(byteArray[15], 1); - for (size_t i = 0; i < 15; i++) { - UNIT_ASSERT_EQUAL(byteArray[i], 0); - } -#endif - - UNIT_ASSERT_EQUAL(std::memcmp((void*)&n, (void*)&number, 16), 0); - - UNIT_ASSERT_EQUAL(ToString(n), "1"); - - UNIT_ASSERT_EQUAL(FromString<unsigned __int128>(ToString(n)), n); + ui128 n = 1; + const ui128 n128 = n - 2; + UNIT_ASSERT_EQUAL(n128, ui128(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF)); } -#endif + +void TUInt128Test::ToStringTest() { + ui128 n(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF); + TString correct = "340282366920938463463374607431768211455"; + UNIT_ASSERT_EQUAL(correct, ::ToString(n)); +} + +void TUInt128Test::FromStringTest() { + { + const TString originalString = "37778931862957161709568"; + const ui128 number = FromString<ui128>(originalString); + UNIT_ASSERT_EQUAL(ToString(number), originalString); + } + + { + const TString originalString = "1024"; + const ui128 number = FromString<ui128>(originalString); + UNIT_ASSERT_EQUAL(ToString(number), originalString); + UNIT_ASSERT_EQUAL(GetHigh(number), 0); + UNIT_ASSERT_EQUAL(GetLow(number), 1024); + } + + { + const TString originalString = "18446744073709551616"; // 2^64, i.e. UINT64_MAX + 1 + const ui128 number = FromString<ui128>(originalString); + UNIT_ASSERT_EQUAL(ToString(number), originalString); + UNIT_ASSERT_EQUAL(GetHigh(number), 1); + UNIT_ASSERT_EQUAL(GetLow(number), 0); + } + + { + const TString originalString = "340282366920938463463374607431768211455"; // 2^128-1, i.e. UINT128_MAX + const ui128 number = FromString<ui128>(originalString); + UNIT_ASSERT_EQUAL(ToString(number), originalString); + UNIT_ASSERT_EQUAL(GetHigh(number), 0xFFFFFFFFFFFFFFFF); + UNIT_ASSERT_EQUAL(GetLow(number), 0xFFFFFFFFFFFFFFFF); + } +} + +#if defined(Y_HAVE_INT128) +void TUInt128Test::FromSystemUint128() { + unsigned __int128 n = 1; + ui128 number{n}; + + UNIT_ASSERT_EQUAL(GetLow(number), 1); + UNIT_ASSERT_EQUAL(GetHigh(number), 0); + + auto byteArray = NInt128Private::GetAsArray(number); +#ifdef _little_endian_ + UNIT_ASSERT_EQUAL(byteArray[0], 1); + for (size_t i = 1; i < 16; i++) { + UNIT_ASSERT_EQUAL(byteArray[i], 0); + } +#elif defined(_big_endian_) + UNIT_ASSERT_EQUAL(byteArray[15], 1); + for (size_t i = 0; i < 15; i++) { + UNIT_ASSERT_EQUAL(byteArray[i], 0); + } +#endif + + UNIT_ASSERT_EQUAL(std::memcmp((void*)&n, (void*)&number, 16), 0); + + UNIT_ASSERT_EQUAL(ToString(n), "1"); + + UNIT_ASSERT_EQUAL(FromString<unsigned __int128>(ToString(n)), n); +} +#endif |