i128_comparison_ut.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <library/cpp/int128/int128.h>
  3. #include <util/generic/cast.h>
  4. Y_UNIT_TEST_SUITE(I128ComparisonPositiveWithPositiveSuite) {
  5. Y_UNIT_TEST(PositivePositiveGreater) {
  6. UNIT_ASSERT(i128{1} > i128{0});
  7. UNIT_ASSERT(i128{2} > i128{1});
  8. UNIT_ASSERT(i128{42} > i128{0});
  9. UNIT_ASSERT(i128{42} > i128{1});
  10. i128 big = i128{1, 0};
  11. UNIT_ASSERT(big > i128{1});
  12. UNIT_ASSERT(std::numeric_limits<i128>::max() > i128{0});
  13. }
  14. Y_UNIT_TEST(PositivePositiveGreaterOrEqual) {
  15. UNIT_ASSERT(i128{1} >= i128{0});
  16. UNIT_ASSERT(i128{2} >= i128{1});
  17. UNIT_ASSERT(i128{42} >= i128{0});
  18. UNIT_ASSERT(i128{42} >= i128{1});
  19. i128 big = i128{1, 0};
  20. UNIT_ASSERT(big >= i128{1});
  21. UNIT_ASSERT(std::numeric_limits<i128>::max() >= i128{0});
  22. UNIT_ASSERT(i128{0} >= i128{0});
  23. UNIT_ASSERT(i128{1} >= i128{1});
  24. UNIT_ASSERT(i128{2} >= i128{2});
  25. UNIT_ASSERT(i128{42} >= i128{42});
  26. UNIT_ASSERT(big >= big);
  27. UNIT_ASSERT(std::numeric_limits<i128>::max() >= std::numeric_limits<i128>::max());
  28. }
  29. Y_UNIT_TEST(PositivePositiveLess) {
  30. UNIT_ASSERT(i128{0} < i128{1});
  31. UNIT_ASSERT(i128{1} < i128{2});
  32. UNIT_ASSERT(i128{0} < i128{42});
  33. UNIT_ASSERT(i128{1} < i128{42});
  34. i128 big = i128{1, 0};
  35. UNIT_ASSERT(i128{1} < big);
  36. UNIT_ASSERT(i128{0} < std::numeric_limits<i128>::max());
  37. }
  38. Y_UNIT_TEST(PositivePositiveLessOrEqual) {
  39. UNIT_ASSERT(i128{0} <= i128{1});
  40. UNIT_ASSERT(i128{1} <= i128{2});
  41. UNIT_ASSERT(i128{0} <= i128{42});
  42. UNIT_ASSERT(i128{1} <= i128{42});
  43. i128 big = i128{1, 0};
  44. UNIT_ASSERT(i128{1} <= big);
  45. UNIT_ASSERT(i128{0} <= std::numeric_limits<i128>::max());
  46. UNIT_ASSERT(i128{0} <= i128{0});
  47. UNIT_ASSERT(i128{1} <= i128{1});
  48. UNIT_ASSERT(i128{2} <= i128{2});
  49. UNIT_ASSERT(i128{42} <= i128{42});
  50. UNIT_ASSERT(big <= big);
  51. UNIT_ASSERT(std::numeric_limits<i128>::max() <= std::numeric_limits<i128>::max());
  52. }
  53. }
  54. Y_UNIT_TEST_SUITE(I128ComparisonPositiveWithNegativeSuite) {
  55. Y_UNIT_TEST(PositiveNegativeGreater) {
  56. UNIT_ASSERT(i128{0} > i128{-1});
  57. UNIT_ASSERT(i128{2} > i128{-1});
  58. UNIT_ASSERT(i128{0} > i128{-42});
  59. UNIT_ASSERT(i128{42} > i128{-1});
  60. i128 big = i128{1, 0};
  61. UNIT_ASSERT(big > i128{-1});
  62. UNIT_ASSERT(std::numeric_limits<i128>::max() > i128{-1});
  63. }
  64. Y_UNIT_TEST(PositiveNegativeGreaterOrEqual) {
  65. UNIT_ASSERT(i128{0} >= i128{-1});
  66. UNIT_ASSERT(i128{2} >= i128{-1});
  67. UNIT_ASSERT(i128{0} >= i128{-42});
  68. UNIT_ASSERT(i128{42} >= i128{-1});
  69. i128 big = i128{1, 0};
  70. UNIT_ASSERT(big >= i128{-1});
  71. UNIT_ASSERT(std::numeric_limits<i128>::max() >= i128{-1});
  72. }
  73. Y_UNIT_TEST(NegativePositiveLess) {
  74. UNIT_ASSERT(i128{-1} < i128{0});
  75. UNIT_ASSERT(i128{-1} < i128{2});
  76. UNIT_ASSERT(i128{-42} < i128{0});
  77. UNIT_ASSERT(i128{-1} < i128{42});
  78. i128 big = i128{1, 0};
  79. UNIT_ASSERT(i128{-1} < big);
  80. UNIT_ASSERT(i128{-1} < std::numeric_limits<i128>::max());
  81. }
  82. Y_UNIT_TEST(NegativePositiveLessOrEqual) {
  83. UNIT_ASSERT(i128{-1} <= i128{0});
  84. UNIT_ASSERT(i128{-1} <= i128{2});
  85. UNIT_ASSERT(i128{-42} <= i128{0});
  86. UNIT_ASSERT(i128{-1} <= i128{42});
  87. i128 big = i128{1, 0};
  88. UNIT_ASSERT(i128{-1} <= big);
  89. UNIT_ASSERT(i128{-1} <= std::numeric_limits<i128>::max());
  90. }
  91. }
  92. Y_UNIT_TEST_SUITE(I128ComparisonNegativeWithNegativeSuite) {
  93. Y_UNIT_TEST(NegativeNegativeGreater) {
  94. UNIT_ASSERT(i128{-1} > i128{-2});
  95. UNIT_ASSERT(i128{-2} > i128{-3});
  96. UNIT_ASSERT(i128{-1} > i128{-42});
  97. UNIT_ASSERT(i128{-42} > i128{-142});
  98. i128 big = -i128{1, 0};
  99. UNIT_ASSERT(i128{-1} > big);
  100. UNIT_ASSERT(i128{-1} > std::numeric_limits<i128>::min());
  101. }
  102. Y_UNIT_TEST(NegativeNegativeGreaterOrEqual) {
  103. UNIT_ASSERT(i128{-1} >= i128{-2});
  104. UNIT_ASSERT(i128{-2} >= i128{-3});
  105. UNIT_ASSERT(i128{-1} >= i128{-42});
  106. UNIT_ASSERT(i128{-42} >= i128{-142});
  107. i128 big = -i128{1, 0};
  108. UNIT_ASSERT(i128{-1} >= big);
  109. UNIT_ASSERT(i128{-1} >= std::numeric_limits<i128>::min());
  110. }
  111. Y_UNIT_TEST(NegativeNegativeLess) {
  112. UNIT_ASSERT(i128{-2} < i128{-1});
  113. UNIT_ASSERT(i128{-3} < i128{-2});
  114. UNIT_ASSERT(i128{-42} < i128{-1});
  115. UNIT_ASSERT(i128{-142} < i128{42});
  116. i128 big = -i128{1, 0};
  117. UNIT_ASSERT(big < i128{-1});
  118. UNIT_ASSERT(std::numeric_limits<i128>::min() < i128{-1});
  119. }
  120. Y_UNIT_TEST(NegativeNegativeLessOrEqual) {
  121. UNIT_ASSERT(i128{-2} <= i128{-1});
  122. UNIT_ASSERT(i128{-3} <= i128{-2});
  123. UNIT_ASSERT(i128{-42} <= i128{-1});
  124. UNIT_ASSERT(i128{-142} <= i128{42});
  125. i128 big = -i128{1, 0};
  126. UNIT_ASSERT(big <= i128{-1});
  127. UNIT_ASSERT(std::numeric_limits<i128>::min() <= i128{-1});
  128. }
  129. }