yql_wide_int_ut.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #include <yql/essentials/public/decimal/yql_wide_int.h>
  2. #include <library/cpp/testing/unittest/registar.h>
  3. namespace NYql {
  4. Y_UNIT_TEST_SUITE(TYqlWideIntTest) {
  5. template<typename T>
  6. void TestUnary(const T aa) {
  7. using Test = TWide<typename THalfOf<T>::Type>;
  8. const Test at(aa);
  9. static_assert(sizeof(at) == sizeof(aa), "Bad wide int size!");
  10. UNIT_ASSERT_VALUES_EQUAL(static_cast<i8>(aa), static_cast<i8>(at));
  11. UNIT_ASSERT_VALUES_EQUAL(static_cast<ui8>(aa), static_cast<ui8>(at));
  12. UNIT_ASSERT_VALUES_EQUAL(static_cast<i16>(aa), static_cast<i16>(at));
  13. UNIT_ASSERT_VALUES_EQUAL(static_cast<ui16>(aa), static_cast<ui16>(at));
  14. UNIT_ASSERT_VALUES_EQUAL(static_cast<i32>(aa), static_cast<i32>(at));
  15. UNIT_ASSERT_VALUES_EQUAL(static_cast<ui32>(aa), static_cast<ui32>(at));
  16. UNIT_ASSERT_VALUES_EQUAL(static_cast<i64>(aa), static_cast<i64>(at));
  17. UNIT_ASSERT_VALUES_EQUAL(static_cast<ui64>(aa), static_cast<ui64>(at));
  18. #ifndef _win_
  19. UNIT_ASSERT(static_cast<i128_t>(aa) == static_cast<i128_t>(at));
  20. UNIT_ASSERT(static_cast<ui128_t>(aa) == static_cast<ui128_t>(at));
  21. #endif
  22. {
  23. const auto exp = ~aa;
  24. const auto tst = ~at;
  25. UNIT_ASSERT(T(tst) == T(exp));
  26. }
  27. {
  28. const auto exp = +aa;
  29. const auto tst = +at;
  30. UNIT_ASSERT(T(tst) == T(exp));
  31. }
  32. {
  33. const auto exp = -aa;
  34. const auto tst = -at;
  35. UNIT_ASSERT(T(tst) == T(exp));
  36. }
  37. {
  38. auto exp = aa;
  39. auto tst = at;
  40. ++exp;
  41. ++tst;
  42. UNIT_ASSERT(T(tst) == T(exp));
  43. }
  44. {
  45. auto exp = aa;
  46. auto tst = at;
  47. --exp;
  48. --tst;
  49. UNIT_ASSERT(T(tst) == T(exp));
  50. }
  51. {
  52. auto exp = aa;
  53. auto tst = at;
  54. exp++;
  55. tst++;
  56. UNIT_ASSERT(T(tst) == T(exp));
  57. }
  58. {
  59. auto exp = aa;
  60. auto tst = at;
  61. exp--;
  62. tst--;
  63. UNIT_ASSERT(T(tst) == T(exp));
  64. }
  65. }
  66. template<typename T>
  67. void TestBinary(const T ll, const T rr) {
  68. using Test = TWide<typename THalfOf<T>::Type>;
  69. const Test lt(ll), rt(rr);
  70. {
  71. const auto exp = ll & rr;
  72. const auto tst = lt & rt;
  73. UNIT_ASSERT(T(tst) == T(exp));
  74. }
  75. {
  76. const auto exp = ll | rr;
  77. const auto tst = lt | rt;
  78. UNIT_ASSERT(T(tst) == T(exp));
  79. }
  80. {
  81. const auto exp = ll ^ rr;
  82. const auto tst = lt ^ rt;
  83. UNIT_ASSERT(T(tst) == T(exp));
  84. }
  85. {
  86. const auto exp = ll + rr;
  87. const auto tst = lt + rt;
  88. UNIT_ASSERT(T(tst) == T(exp));
  89. }
  90. {
  91. const auto exp = ll - rr;
  92. const auto tst = lt - rt;
  93. UNIT_ASSERT(T(tst) == T(exp));
  94. }
  95. if (rr > 0 && rr < T(sizeof(T) << 3U))
  96. {
  97. const auto exp = ll >> rr;
  98. const auto tst = lt >> rt;
  99. UNIT_ASSERT(T(tst) == T(exp));
  100. }
  101. if (rr > 0 && rr < T(sizeof(T) << 3U))
  102. {
  103. const auto exp = ll << rr;
  104. const auto tst = lt << rt;
  105. UNIT_ASSERT(T(tst) == T(exp));
  106. }
  107. {
  108. const auto exp = ll * rr;
  109. const auto tst = lt * rt;
  110. UNIT_ASSERT(T(tst) == T(exp));
  111. }
  112. if (rr)
  113. {
  114. const auto exp = ll / rr;
  115. const auto tst = lt / rt;
  116. UNIT_ASSERT(T(tst) == T(exp));
  117. }
  118. if (rr)
  119. {
  120. const auto exp = ll % rr;
  121. const auto tst = lt % rt;
  122. UNIT_ASSERT(T(tst) == T(exp));
  123. }
  124. {
  125. const auto exp = ll == rr;
  126. const auto tst = lt == rt;
  127. UNIT_ASSERT_VALUES_EQUAL(tst, exp);
  128. }
  129. {
  130. const auto exp = ll != rr;
  131. const auto tst = lt != rt;
  132. UNIT_ASSERT_VALUES_EQUAL(tst, exp);
  133. }
  134. {
  135. const auto exp = ll > rr;
  136. const auto tst = lt > rt;
  137. UNIT_ASSERT_VALUES_EQUAL(tst, exp);
  138. }
  139. {
  140. const auto exp = ll < rr;
  141. const auto tst = lt < rt;
  142. UNIT_ASSERT_VALUES_EQUAL(tst, exp);
  143. }
  144. {
  145. const auto exp = ll >= rr;
  146. const auto tst = lt >= rt;
  147. UNIT_ASSERT_VALUES_EQUAL(tst, exp);
  148. }
  149. {
  150. const auto exp = ll <= rr;
  151. const auto tst = lt <= rt;
  152. UNIT_ASSERT_VALUES_EQUAL(tst, exp);
  153. }
  154. }
  155. template<typename T>
  156. void TestsForUnsignedType() {
  157. static_assert(std::is_unsigned<T>::value, "Tests for unsigned type.");
  158. TestUnary<T>(2U);
  159. TestUnary<T>(4U);
  160. TestUnary<T>(17U);
  161. TestUnary<T>(42U);
  162. TestUnary<T>(127U);
  163. TestUnary<T>(128U);
  164. TestUnary<T>(129U);
  165. TestUnary<T>(200U);
  166. TestUnary<T>(255U);
  167. TestUnary<T>(256U);
  168. TestUnary<T>(257U);
  169. TestUnary<T>(std::numeric_limits<T>::min());
  170. TestUnary<T>(std::numeric_limits<T>::max());
  171. TestUnary<T>(std::numeric_limits<T>::max() - 1U);
  172. TestUnary<T>(std::numeric_limits<T>::max() >> 1U);
  173. TestUnary<T>(std::numeric_limits<T>::max() >> 3U);
  174. TestUnary<T>(std::numeric_limits<T>::max() >> 7U);
  175. TestUnary<T>(std::numeric_limits<T>::min() + 1U);
  176. TestUnary<T>(std::numeric_limits<T>::max() - 1U);
  177. TestUnary<T>(std::numeric_limits<T>::min() + 3U);
  178. TestUnary<T>(std::numeric_limits<T>::max() - 3U);
  179. TestUnary<T>(std::numeric_limits<T>::min() + 7U);
  180. TestUnary<T>(std::numeric_limits<T>::max() - 7U);
  181. TestBinary<T>(1U, 1U);
  182. TestBinary<T>(7U, 31U);
  183. TestBinary<T>(30000U, 13U);
  184. TestBinary<T>(127U, 13U);
  185. TestBinary<T>(128U, 19U);
  186. TestBinary<T>(129U, 17U);
  187. TestBinary<T>(std::numeric_limits<T>::min(), 7U);
  188. TestBinary<T>(std::numeric_limits<T>::max(), 7U);
  189. TestBinary<T>(std::numeric_limits<T>::min(), 8U);
  190. TestBinary<T>(std::numeric_limits<T>::max(), 8U);
  191. TestBinary<T>(std::numeric_limits<T>::min(), 9U);
  192. TestBinary<T>(std::numeric_limits<T>::max(), 9U);
  193. TestBinary<T>(std::numeric_limits<T>::max() - 1U, std::numeric_limits<T>::min());
  194. TestBinary<T>(std::numeric_limits<T>::max() - 1U, std::numeric_limits<T>::min() + 1U);
  195. TestBinary<T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::max());
  196. TestBinary<T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::min());
  197. TestBinary<T>(std::numeric_limits<T>::min(), std::numeric_limits<T>::min());
  198. TestBinary<T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::min());
  199. TestBinary<T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::min() + 1U);
  200. TestBinary<T>(std::numeric_limits<T>::max() - 1U, std::numeric_limits<T>::min());
  201. TestBinary<T>(std::numeric_limits<T>::max() - 1U, std::numeric_limits<T>::min() + 1U);
  202. TestBinary<T>(std::numeric_limits<T>::max() - 1U, std::numeric_limits<T>::min());
  203. TestBinary<T>(std::numeric_limits<T>::min() + 1U, std::numeric_limits<T>::min());
  204. TestBinary<T>(std::numeric_limits<T>::max(), 1U);
  205. TestBinary<T>(std::numeric_limits<T>::min(), 1U);
  206. TestBinary<T>(std::numeric_limits<T>::max() - 1U, 1U);
  207. TestBinary<T>(std::numeric_limits<T>::min() + 1U, 1U);
  208. TestBinary<T>(std::numeric_limits<T>::max(), 7U);
  209. TestBinary<T>(std::numeric_limits<T>::min(), 7U);
  210. TestBinary<T>(std::numeric_limits<T>::max() - 1U, 7U);
  211. TestBinary<T>(std::numeric_limits<T>::min() + 1U, 7U);
  212. TestBinary<T>(std::numeric_limits<T>::max() >> 1U, std::numeric_limits<T>::min() >> 1U);
  213. TestBinary<T>(std::numeric_limits<T>::max() >> 1U, std::numeric_limits<T>::min() + 1U);
  214. TestBinary<T>(std::numeric_limits<T>::max() - 1U, std::numeric_limits<T>::min() >> 1U);
  215. TestBinary<T>(std::numeric_limits<T>::max() >> 1U, std::numeric_limits<T>::min());
  216. TestBinary<T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::min() >> 1U);
  217. TestBinary<T>(std::numeric_limits<T>::max() >> 3U, std::numeric_limits<T>::min() >> 3U);
  218. TestBinary<T>(std::numeric_limits<T>::max() >> 3U, std::numeric_limits<T>::min() + 3U);
  219. TestBinary<T>(std::numeric_limits<T>::max() - 3U, std::numeric_limits<T>::min() >> 3U);
  220. TestBinary<T>(std::numeric_limits<T>::max() >> 3U, std::numeric_limits<T>::min());
  221. TestBinary<T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::min() >> 3U);
  222. }
  223. template<typename T>
  224. void TestsForSignedType() {
  225. static_assert(std::is_signed<T>::value, "Tests for signed type.");
  226. TestUnary<T>(0);
  227. TestUnary<T>(1);
  228. TestUnary<T>(-1);
  229. TestUnary<T>(2);
  230. TestUnary<T>(-2);
  231. TestUnary<T>(3);
  232. TestUnary<T>(-3);
  233. TestUnary<T>(4);
  234. TestUnary<T>(-4);
  235. TestUnary<T>(17);
  236. TestUnary<T>(-17);
  237. TestUnary<T>(42);
  238. TestUnary<T>(-42);
  239. TestUnary<T>(127);
  240. TestUnary<T>(-127);
  241. TestUnary<T>(128);
  242. TestUnary<T>(-128);
  243. TestUnary<T>(129);
  244. TestUnary<T>(-129);
  245. TestUnary<T>(200);
  246. TestUnary<T>(-200);
  247. TestUnary<T>(255);
  248. TestUnary<T>(-255);
  249. TestUnary<T>(256);
  250. TestUnary<T>(-256);
  251. TestUnary<T>(257);
  252. TestUnary<T>(-257);
  253. TestUnary<T>(258);
  254. TestUnary<T>(-258);
  255. TestUnary<T>(std::numeric_limits<T>::min());
  256. TestUnary<T>(std::numeric_limits<T>::max());
  257. TestUnary<T>(std::numeric_limits<T>::min() + 1);
  258. TestUnary<T>(std::numeric_limits<T>::max() - 1);
  259. TestUnary<T>(std::numeric_limits<T>::min() + 2);
  260. TestUnary<T>(std::numeric_limits<T>::max() - 2);
  261. TestUnary<T>(std::numeric_limits<T>::min() + 3);
  262. TestUnary<T>(std::numeric_limits<T>::max() - 3);
  263. TestUnary<T>(std::numeric_limits<T>::min() + 7);
  264. TestUnary<T>(std::numeric_limits<T>::max() - 7);
  265. TestUnary<T>(std::numeric_limits<T>::min() >> 1);
  266. TestUnary<T>(std::numeric_limits<T>::max() >> 1);
  267. TestUnary<T>(std::numeric_limits<T>::min() >> 3);
  268. TestUnary<T>(std::numeric_limits<T>::max() >> 3);
  269. TestUnary<T>(std::numeric_limits<T>::min() >> 7);
  270. TestUnary<T>(std::numeric_limits<T>::max() >> 7);
  271. TestUnary<T>(std::numeric_limits<T>::min() + 1);
  272. TestUnary<T>(std::numeric_limits<T>::max() - 1);
  273. TestUnary<T>(std::numeric_limits<T>::min() + 3);
  274. TestUnary<T>(std::numeric_limits<T>::max() - 3);
  275. TestUnary<T>(std::numeric_limits<T>::min() + 7);
  276. TestUnary<T>(std::numeric_limits<T>::max() - 7);
  277. TestBinary<T>(0, 0);
  278. TestBinary<T>(1, 0);
  279. TestBinary<T>(0, -1);
  280. TestBinary<T>(1, 1);
  281. TestBinary<T>(1, -1);
  282. TestBinary<T>(-1, 1);
  283. TestBinary<T>(-1, -1);
  284. TestBinary<T>(7, 42);
  285. TestBinary<T>(-7, 42);
  286. TestBinary<T>(0, -43);
  287. TestBinary<T>(-30000, 64);
  288. TestBinary<T>(30000, -64);
  289. TestBinary<T>(30000, 64);
  290. TestBinary<T>(21, 0);
  291. TestBinary<T>(13, -127);
  292. TestBinary<T>(-19, 128);
  293. TestBinary<T>(-77, -129);
  294. TestBinary<T>(13, 127);
  295. TestBinary<T>(19, 128);
  296. TestBinary<T>(77, 129);
  297. TestBinary<T>(std::numeric_limits<T>::max(), -1);
  298. TestBinary<T>(std::numeric_limits<T>::min(), -7);
  299. TestBinary<T>(std::numeric_limits<T>::max(), -7);
  300. TestBinary<T>(std::numeric_limits<T>::min(), -8);
  301. TestBinary<T>(std::numeric_limits<T>::max(), -8);
  302. TestBinary<T>(std::numeric_limits<T>::min(), -9);
  303. TestBinary<T>(std::numeric_limits<T>::max(), -9);
  304. TestBinary<T>(std::numeric_limits<T>::min(), std::numeric_limits<T>::min() >> 5);
  305. TestBinary<T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::min() >> 5);
  306. TestBinary<T>(std::numeric_limits<T>::min(), 7);
  307. TestBinary<T>(std::numeric_limits<T>::max(), 7);
  308. TestBinary<T>(std::numeric_limits<T>::min(), 8);
  309. TestBinary<T>(std::numeric_limits<T>::max(), 8);
  310. TestBinary<T>(std::numeric_limits<T>::min(), 9);
  311. TestBinary<T>(std::numeric_limits<T>::max(), 9);
  312. TestBinary<T>(std::numeric_limits<T>::max() - 1, std::numeric_limits<T>::min());
  313. TestBinary<T>(std::numeric_limits<T>::max() - 1, std::numeric_limits<T>::min() + 1);
  314. TestBinary<T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::max());
  315. TestBinary<T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::min());
  316. TestBinary<T>(std::numeric_limits<T>::min(), std::numeric_limits<T>::min());
  317. TestBinary<T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::min());
  318. TestBinary<T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::min() + 1);
  319. TestBinary<T>(std::numeric_limits<T>::max() - 1, std::numeric_limits<T>::min());
  320. TestBinary<T>(std::numeric_limits<T>::max() - 1, std::numeric_limits<T>::min() + 1);
  321. TestBinary<T>(std::numeric_limits<T>::max(), 0);
  322. TestBinary<T>(std::numeric_limits<T>::min(), 0);
  323. TestBinary<T>(std::numeric_limits<T>::max() - 1, 0);
  324. TestBinary<T>(std::numeric_limits<T>::min() + 1, 0);
  325. TestBinary<T>(std::numeric_limits<T>::max(), 1);
  326. TestBinary<T>(std::numeric_limits<T>::min(), 1);
  327. TestBinary<T>(std::numeric_limits<T>::max() - 1, 1);
  328. TestBinary<T>(std::numeric_limits<T>::min() + 1, 1);
  329. TestBinary<T>(std::numeric_limits<T>::max(), 7);
  330. TestBinary<T>(std::numeric_limits<T>::min(), 7);
  331. TestBinary<T>(std::numeric_limits<T>::max() - 1, 7);
  332. TestBinary<T>(std::numeric_limits<T>::min() + 1, 7);
  333. TestBinary<T>(std::numeric_limits<T>::max() >> 1, std::numeric_limits<T>::min() >> 1);
  334. TestBinary<T>(std::numeric_limits<T>::max() >> 1, std::numeric_limits<T>::min() + 1);
  335. TestBinary<T>(std::numeric_limits<T>::max() - 1, std::numeric_limits<T>::min() >> 1);
  336. TestBinary<T>(std::numeric_limits<T>::max() >> 1, std::numeric_limits<T>::min());
  337. TestBinary<T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::min() >> 1);
  338. TestBinary<T>(std::numeric_limits<T>::max() >> 3, std::numeric_limits<T>::min() >> 3);
  339. TestBinary<T>(std::numeric_limits<T>::max() >> 3, std::numeric_limits<T>::min() + 3);
  340. TestBinary<T>(std::numeric_limits<T>::max() - 3, std::numeric_limits<T>::min() >> 3);
  341. TestBinary<T>(std::numeric_limits<T>::max() >> 3, std::numeric_limits<T>::min());
  342. TestBinary<T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::min() >> 3);
  343. }
  344. Y_UNIT_TEST(CheckUnsignedByCompilerIntegrals) {
  345. TestsForUnsignedType<ui32>();
  346. TestsForUnsignedType<ui64>();
  347. #ifndef _win_
  348. TestsForUnsignedType<ui128_t>();
  349. #endif
  350. }
  351. #ifndef _ubsan_enabled_
  352. #ifndef _msan_enabled_
  353. Y_UNIT_TEST(CheckSignedByCompilerIntegrals) {
  354. TestsForSignedType<i32>();
  355. TestsForSignedType<i64>();
  356. #ifndef _win_
  357. TestsForSignedType<i128_t>();
  358. #endif
  359. }
  360. #endif
  361. #endif
  362. }
  363. }