123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- #include "int128_ut_helpers.h"
- #include <library/cpp/testing/unittest/registar.h>
- #include <library/cpp/int128/int128.h>
- #include <util/generic/cast.h>
- #include <array>
- #include <type_traits>
- #if defined(Y_HAVE_INT128)
- bool IsIdentical(const i128 a, const signed __int128 b) {
- const std::array<ui8, 16> arrayA = NInt128Private::GetAsArray(a);
- const std::array<ui8, 16> arrayB = NInt128Private::GetAsArray(b);
- return arrayA == arrayB;
- }
- Y_UNIT_TEST_SUITE(i128_And_i8_BitwiseIdentity) {
- Y_UNIT_TEST(i128_from_i8_Zero) {
- i8 n = 0;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i8_Minus1) {
- i8 n = -1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i8_Plus1) {
- i8 n = 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i8_Minus42) {
- i8 n = -42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i8_Plus42) {
- i8 n = 42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i8_Min) {
- i8 n = std::numeric_limits<i8>::min();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i8_Max) {
- i8 n = std::numeric_limits<i8>::max();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i8_MinPlus1) {
- i8 n = std::numeric_limits<i8>::min() + 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i8_MaxMinus1) {
- i8 n = std::numeric_limits<i8>::max() - 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- }
- Y_UNIT_TEST_SUITE(i128_And_i16_BitwiseIdentity) {
- Y_UNIT_TEST(i128_from_i16_Zero) {
- i16 n = 0;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i16_Minus1) {
- i16 n = -1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i16_Plus1) {
- i16 n = 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i16_Minus42) {
- i16 n = -42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i16_Plus42) {
- i16 n = 42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i16_Min) {
- i16 n = std::numeric_limits<i16>::min();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i16_Max) {
- i16 n = std::numeric_limits<i16>::max();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i16_MinPlus1) {
- i16 n = std::numeric_limits<i16>::min() + 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i16_MaxMinus1) {
- i16 n = std::numeric_limits<i16>::max() - 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- }
- Y_UNIT_TEST_SUITE(i128_And_i32_BitwiseIdentity) {
- Y_UNIT_TEST(i128_from_i32_Zero) {
- i32 n = 0;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i32_Minus1) {
- i32 n = -1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i32_Plus1) {
- i32 n = 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i32_Minus42) {
- i32 n = -42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i32_Plus42) {
- i32 n = 42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i32_Min) {
- i32 n = std::numeric_limits<i32>::min();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i32_Max) {
- i32 n = std::numeric_limits<i32>::max();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i32_MinPlus1) {
- i32 n = std::numeric_limits<i32>::min() + 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i32_MaxMinus1) {
- i32 n = std::numeric_limits<i32>::max() - 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- }
- Y_UNIT_TEST_SUITE(i128_And_i64_BitwiseIdentity) {
- Y_UNIT_TEST(i128_from_i64_Zero) {
- i64 n = 0;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i64_Minus1) {
- i64 n = -1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i64_Plus1) {
- i64 n = 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i64_Minus42) {
- i64 n = -42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i64_Plus42) {
- i64 n = 42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i64_Min) {
- i64 n = std::numeric_limits<i64>::min();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i64_Max) {
- i64 n = std::numeric_limits<i64>::max();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i64_MinPlus1) {
- i64 n = std::numeric_limits<i64>::min() + 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_i64_MaxMinus1) {
- i64 n = std::numeric_limits<i64>::max() - 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- }
- Y_UNIT_TEST_SUITE(i128_And_signed_int128_BitwiseIdentity) {
- Y_UNIT_TEST(i128_from_signed_int128_Zero) {
- signed __int128 n = 0;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_signed_int128_Minus1) {
- signed __int128 n = -1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_signed_int128_Plus1) {
- signed __int128 n = 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_signed_int128_Minus42) {
- signed __int128 n = -42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_signed_int128_Plus42) {
- signed __int128 n = 42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_signed_int128_Min) {
- signed __int128 n = std::numeric_limits<signed __int128>::min();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_signed_int128_Max) {
- signed __int128 n = std::numeric_limits<signed __int128>::max();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_signed_int128_MinPlus1) {
- signed __int128 n = std::numeric_limits<signed __int128>::min() + 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_signed_int128_MaxMinus1) {
- signed __int128 n = std::numeric_limits<signed __int128>::max() - 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- }
- Y_UNIT_TEST_SUITE(i128_And_ui8_BitwiseIdentity) {
- Y_UNIT_TEST(i128_from_ui8_Zero) {
- ui8 n = 0;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui8_Plus1) {
- ui8 n = 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui8_Plus42) {
- ui8 n = 42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui8_Min) {
- ui8 n = std::numeric_limits<i8>::min();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui8_Max) {
- ui8 n = std::numeric_limits<i8>::max();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui8_MinPlus1) {
- ui8 n = std::numeric_limits<i8>::min() + 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui8_MaxMinus1) {
- ui8 n = std::numeric_limits<i8>::max() - 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- }
- Y_UNIT_TEST_SUITE(i128_And_ui16_BitwiseIdentity) {
- Y_UNIT_TEST(i128_from_ui16_Zero) {
- ui16 n = 0;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui16_Plus1) {
- ui16 n = 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui16_Plus42) {
- ui16 n = 42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui16_Min) {
- ui16 n = std::numeric_limits<i8>::min();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui16_Max) {
- ui16 n = std::numeric_limits<i8>::max();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui16_MinPlus1) {
- ui16 n = std::numeric_limits<i8>::min() + 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui16_MaxMinus1) {
- ui16 n = std::numeric_limits<i8>::max() - 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- }
- Y_UNIT_TEST_SUITE(i128_And_ui32_BitwiseIdentity) {
- Y_UNIT_TEST(i128_from_ui32_Zero) {
- ui32 n = 0;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui32_Plus1) {
- ui32 n = 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui32_Plus42) {
- ui32 n = 42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui32_Min) {
- ui32 n = std::numeric_limits<i8>::min();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui32_Max) {
- ui32 n = std::numeric_limits<i8>::max();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui32_MinPlus1) {
- ui32 n = std::numeric_limits<i8>::min() + 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui32_MaxMinus1) {
- ui32 n = std::numeric_limits<i8>::max() - 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- }
- Y_UNIT_TEST_SUITE(i128_And_ui64_BitwiseIdentity) {
- Y_UNIT_TEST(i128_from_ui64_Zero) {
- ui64 n = 0;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui64_Plus1) {
- ui64 n = 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui64_Plus42) {
- ui64 n = 42;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui64_Min) {
- ui64 n = std::numeric_limits<i8>::min();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui64_Max) {
- ui64 n = std::numeric_limits<i8>::max();
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui64_MinPlus1) {
- ui64 n = std::numeric_limits<i8>::min() + 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_ui64_MaxMinus1) {
- ui64 n = std::numeric_limits<i8>::max() - 1;
- i128 t1{n};
- signed __int128 t2{n};
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- }
- Y_UNIT_TEST_SUITE(i128_And_unsigned_int128_BitwiseIdentity) {
- Y_UNIT_TEST(i128_from_unsigned_int128_Zero) {
- unsigned __int128 n = 0;
- i128 t1{n};
- signed __int128 t2 = static_cast<signed __int128>(n);
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_unsigned_int128_Plus1) {
- unsigned __int128 n = 1;
- i128 t1{n};
- signed __int128 t2 = static_cast<signed __int128>(n);
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_unsigned_int128_Plus42) {
- unsigned __int128 n = 42;
- i128 t1{n};
- signed __int128 t2 = static_cast<signed __int128>(n);
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_unsigned_int128_Min) {
- unsigned __int128 n = std::numeric_limits<i8>::min();
- i128 t1{n};
- signed __int128 t2 = static_cast<signed __int128>(n);
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_unsigned_int128_Max) {
- unsigned __int128 n = std::numeric_limits<i8>::max();
- i128 t1{n};
- signed __int128 t2 = static_cast<signed __int128>(n);
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_unsigned_int128_MinPlus1) {
- unsigned __int128 n = std::numeric_limits<i8>::min() + 1;
- i128 t1{n};
- signed __int128 t2 = static_cast<signed __int128>(n);
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- Y_UNIT_TEST(i128_from_unsigned_int128_MaxMinus1) {
- unsigned __int128 n = std::numeric_limits<i8>::max() - 1;
- i128 t1{n};
- signed __int128 t2 = static_cast<signed __int128>(n);
- UNIT_ASSERT(IsIdentical(t1, t2));
- }
- }
- #endif
|