packers_ut.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <util/stream/output.h>
  3. #include <utility>
  4. #include <util/charset/wide.h>
  5. #include <util/generic/algorithm.h>
  6. #include <util/generic/buffer.h>
  7. #include <util/generic/map.h>
  8. #include <util/generic/vector.h>
  9. #include <util/generic/ptr.h>
  10. #include <util/generic/ylimits.h>
  11. #include <util/folder/dirut.h>
  12. #include <util/random/random.h>
  13. #include <util/string/hex.h>
  14. #include "packers.h"
  15. #include <array>
  16. #include <iterator>
  17. class TPackersTest: public TTestBase {
  18. private:
  19. UNIT_TEST_SUITE(TPackersTest);
  20. UNIT_TEST(TestPackers);
  21. UNIT_TEST_SUITE_END();
  22. template <class TData, class TPacker>
  23. void TestPacker(const TData& data);
  24. template <class TData, class TPacker>
  25. void TestPacker(const TData* test, size_t size);
  26. public:
  27. void TestPackers();
  28. };
  29. UNIT_TEST_SUITE_REGISTRATION(TPackersTest);
  30. template <class TData, class TPacker>
  31. void TPackersTest::TestPacker(const TData& data) {
  32. size_t len = TPacker().MeasureLeaf(data);
  33. size_t bufLen = len * 3;
  34. TArrayHolder<char> buf(new char[bufLen]);
  35. memset(buf.Get(), -1, bufLen);
  36. TPacker().PackLeaf(buf.Get(), data, len);
  37. UNIT_ASSERT(TPacker().SkipLeaf(buf.Get()) == len);
  38. TData dataTmp;
  39. TPacker().UnpackLeaf(buf.Get(), dataTmp);
  40. UNIT_ASSERT(data == dataTmp);
  41. }
  42. template <class TData, class TPacker>
  43. void TPackersTest::TestPacker(const TData* test, size_t size) {
  44. for (size_t i = 0; i < size; ++i) {
  45. TestPacker<TData, TPacker>(test[i]);
  46. }
  47. }
  48. void TPackersTest::TestPackers() {
  49. {
  50. const TString test[] = {"",
  51. "a", "b", "c", "d",
  52. "aa", "ab", "ac", "ad",
  53. "aaa", "aab", "aac", "aad",
  54. "aba", "abb", "abc", "abd",
  55. "asdfjjmk.gjilsjgilsjilgjildsajgfilsjdfilgjm ldsa8oq43u 583uq4905 -q435 jiores u893q 5oiju fd-KE 89536 9Q2URE 12AI894T3 89 Q*(re43"};
  56. TestPacker<TString, NPackers::TPacker<TString>>(test, Y_ARRAY_SIZE(test));
  57. for (size_t i = 0; i != Y_ARRAY_SIZE(test); ++i) {
  58. TestPacker<TUtf16String, NPackers::TPacker<TUtf16String>>(UTF8ToWide(test[i]));
  59. }
  60. }
  61. {
  62. const ui64 test[] = {
  63. 0, 1, 2, 3, 4, 5, 6, 76, 100000, Max<ui64>()};
  64. TestPacker<ui64, NPackers::TPacker<ui64>>(test, Y_ARRAY_SIZE(test));
  65. }
  66. {
  67. const int test[] = {
  68. 0, 1, 2, 3, 4, 5, 6, 76, 100000, -1, -2, -3, -4, -5, -6, -76, -10000, Min<int>(), Max<int>()};
  69. TestPacker<int, NPackers::TPacker<int>>(test, Y_ARRAY_SIZE(test));
  70. }
  71. {
  72. const float test[] = {
  73. 2.f, 3.f, 4.f, 0.f, -0.f, 1.f, -1.f, 1.1f, -1.1f,
  74. std::numeric_limits<float>::min(), -std::numeric_limits<float>::min(),
  75. std::numeric_limits<float>::max(), -std::numeric_limits<float>::max()};
  76. TestPacker<float, NPackers::TFloatPacker>(test, Y_ARRAY_SIZE(test));
  77. }
  78. {
  79. const double test[] = {
  80. 0., -0., 1., -1., 1.1, -1.1,
  81. std::numeric_limits<double>::min(), -std::numeric_limits<double>::min(),
  82. std::numeric_limits<double>::max(), -std::numeric_limits<double>::max()};
  83. TestPacker<double, NPackers::TDoublePacker>(test, Y_ARRAY_SIZE(test));
  84. }
  85. }