int128_ut.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <library/cpp/int128/int128.h>
  3. #include <util/generic/cast.h>
  4. #include <type_traits>
  5. Y_UNIT_TEST_SUITE(Uint128Suite) {
  6. Y_UNIT_TEST(Uint128DefaultCtor) {
  7. const ui128 value{};
  8. UNIT_ASSERT_EQUAL(GetLow(value), 0);
  9. UNIT_ASSERT_EQUAL(GetHigh(value), 0);
  10. }
  11. Y_UNIT_TEST(Uint128NumericLimits) {
  12. UNIT_ASSERT_EQUAL(std::numeric_limits<ui128>::digits, 128);
  13. UNIT_ASSERT_EQUAL(std::numeric_limits<ui128>::max() + 1, ui128{0});
  14. }
  15. Y_UNIT_TEST(Uint128Sizeof) {
  16. UNIT_ASSERT_EQUAL(sizeof(ui128), sizeof(ui64) * 2);
  17. }
  18. Y_UNIT_TEST(Uint128Cast) {
  19. // see util/generic/cast.h
  20. const auto underlyingTypeIsSelf = std::is_same<::NPrivate::TUnderlyingTypeOrSelf<ui128>, ui128>::value;
  21. UNIT_ASSERT_EQUAL(underlyingTypeIsSelf, true);
  22. const auto convertibleUi128Ui128 = ::NPrivate::TSafelyConvertible<ui128, ui128>::Result;
  23. const auto convertibleUi64Ui128 = ::NPrivate::TSafelyConvertible<ui64, ui128>::Result;
  24. const auto convertibleUi128Ui64 = ::NPrivate::TSafelyConvertible<ui128, ui64>::Result;
  25. UNIT_ASSERT_EQUAL(convertibleUi128Ui128, true); // from ui128 to ui128 => safe
  26. UNIT_ASSERT_EQUAL(convertibleUi64Ui128, false); // from ui128 to ui64 => not safe
  27. UNIT_ASSERT_EQUAL(convertibleUi128Ui64, true); // from ui64 to ui128 => safe
  28. }
  29. Y_UNIT_TEST(SafeIntegerCastTest) {
  30. ui128 narrowNumber = 1;
  31. UNIT_ASSERT_NO_EXCEPTION(SafeIntegerCast<ui64>(narrowNumber));
  32. ui128 wideNumber{0};
  33. wideNumber -= 1;
  34. UNIT_ASSERT_EXCEPTION(SafeIntegerCast<ui64>(wideNumber), yexception);
  35. }
  36. Y_UNIT_TEST(SignbitTest) {
  37. UNIT_ASSERT(!std::signbit(ui128{0}));
  38. UNIT_ASSERT(!std::signbit(ui128{-1}));
  39. UNIT_ASSERT(!std::signbit(i128{0}));
  40. UNIT_ASSERT(std::signbit(i128{-1}));
  41. }
  42. Y_UNIT_TEST(ToStringTest) {
  43. // int128
  44. UNIT_ASSERT_VALUES_EQUAL(ToString(i128(0)), "0");
  45. UNIT_ASSERT_VALUES_EQUAL(ToString(i128(42)), "42");
  46. UNIT_ASSERT_VALUES_EQUAL(ToString(i128(-142)), "-142");
  47. UNIT_ASSERT_VALUES_EQUAL(ToString(std::numeric_limits<i128>::min()), "-170141183460469231731687303715884105728");
  48. UNIT_ASSERT_VALUES_EQUAL(ToString(std::numeric_limits<i128>::max()), "170141183460469231731687303715884105727");
  49. // Just random number
  50. UNIT_ASSERT_VALUES_EQUAL(
  51. ToString(
  52. - ((i128(8741349088318632894ul) << 64) | i128(1258331728153556511ul))
  53. ),
  54. "-161249429491168133245752281683002013215");
  55. // uint128
  56. UNIT_ASSERT_VALUES_EQUAL(ToString(ui128(0)), "0");
  57. UNIT_ASSERT_VALUES_EQUAL(ToString(ui128(42)), "42");
  58. UNIT_ASSERT_VALUES_EQUAL(ToString(std::numeric_limits<ui128>::min()), "0");
  59. UNIT_ASSERT_VALUES_EQUAL(ToString(std::numeric_limits<ui128>::max()), "340282366920938463463374607431768211455");
  60. // Just random number
  61. UNIT_ASSERT_VALUES_EQUAL(
  62. ToString(
  63. ((ui128(12745260439834612983ul) << 64) | ui128(10970669179777569799ul))
  64. ),
  65. "235108557486403940296800289353599800327");
  66. }
  67. }