pack_num_ut.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <yql/essentials/minikql/pack_num.h>
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/generic/ylimits.h>
  4. Y_UNIT_TEST_SUITE(TPackNumTest) {
  5. static ui32 TEST_DATA_UI32[] = {
  6. 0,
  7. 0x80,
  8. 0x4000,
  9. 0x8000,
  10. 0x200000,
  11. 0x800000,
  12. 0x80000000,
  13. Max<ui32>()
  14. };
  15. static ui64 TEST_DATA_UI64[] = {
  16. 0ull,
  17. 0x80ull,
  18. 0x4000ull,
  19. 0x8000ull,
  20. 0x200000ull,
  21. 0x800000ull,
  22. 0x10000000ull,
  23. 0x80000000ull,
  24. 0x800000000ull,
  25. 0x8000000000ull,
  26. 0x40000000000ull,
  27. 0x800000000000ull,
  28. 0x2000000000000ull,
  29. 0x80000000000000ull,
  30. 0x8000000000000000ull,
  31. Max<ui64>()};
  32. static size_t DELTA = 10;
  33. Y_UNIT_TEST(PackUnpack32) {
  34. char buf[10];
  35. for (ui32 num: TEST_DATA_UI32) {
  36. for (ui32 i = num - Min<ui32>(num, DELTA); i < num + Min<ui32>(DELTA, Max<ui32>() - num); ++i) {
  37. size_t packedLen = NKikimr::Pack32(i, buf);
  38. UNIT_ASSERT(packedLen > 0);
  39. UNIT_ASSERT(packedLen <= 5);
  40. UNIT_ASSERT_VALUES_EQUAL(packedLen, NKikimr::GetPack32Length(i));
  41. ui32 unpacked = 0;
  42. size_t unpackedLen = NKikimr::Unpack32(buf, packedLen, unpacked);
  43. UNIT_ASSERT_VALUES_EQUAL(i, unpacked);
  44. UNIT_ASSERT_VALUES_EQUAL(packedLen, unpackedLen);
  45. }
  46. }
  47. }
  48. Y_UNIT_TEST(PackUnpack64) {
  49. char buf[10];
  50. for (ui64 num: TEST_DATA_UI64) {
  51. for (ui64 i = num - Min<ui64>(num, DELTA); i < num + Min<ui64>(DELTA, Max<ui64>() - num); ++i) {
  52. size_t packedLen = NKikimr::Pack64(i, buf);
  53. UNIT_ASSERT(packedLen > 0);
  54. UNIT_ASSERT(packedLen <= 9);
  55. UNIT_ASSERT_VALUES_EQUAL(packedLen, NKikimr::GetPack64Length(i));
  56. ui64 unpacked = 0;
  57. size_t unpackedLen = NKikimr::Unpack64(buf, packedLen, unpacked);
  58. UNIT_ASSERT_VALUES_EQUAL(i, unpacked);
  59. UNIT_ASSERT_VALUES_EQUAL(packedLen, unpackedLen);
  60. }
  61. }
  62. }
  63. }