codecs_ut.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include "codecs.h"
  2. #include "stream.h"
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/stream/str.h>
  5. #include <util/string/join.h>
  6. #include <util/digest/multi.h>
  7. Y_UNIT_TEST_SUITE(TBlockCodecsTest) {
  8. using namespace NBlockCodecs;
  9. TBuffer Buffer(TStringBuf b) {
  10. TBuffer bb;
  11. bb.Assign(b.data(), b.size());
  12. return bb;
  13. }
  14. void TestAllAtOnce(size_t n, size_t m) {
  15. TVector<TBuffer> datas;
  16. datas.emplace_back();
  17. datas.push_back(Buffer("na gorshke sidel korol"));
  18. datas.push_back(Buffer(TStringBuf("", 1)));
  19. datas.push_back(Buffer(" "));
  20. datas.push_back(Buffer(" "));
  21. datas.push_back(Buffer(" "));
  22. datas.push_back(Buffer(" "));
  23. {
  24. TStringStream data;
  25. for (size_t i = 0; i < 1024; ++i) {
  26. data << " " << i;
  27. }
  28. datas.push_back(Buffer(data.Str()));
  29. }
  30. TCodecList lst = ListAllCodecs();
  31. for (size_t i = 0; i < lst.size(); ++i) {
  32. const ICodec* c = Codec(lst[i]);
  33. const auto h = MultiHash(c->Name(), i, 1);
  34. if (h % n == m) {
  35. } else {
  36. continue;
  37. }
  38. for (size_t j = 0; j < datas.size(); ++j) {
  39. const TBuffer& data = datas[j];
  40. TString res;
  41. try {
  42. TBuffer e, d;
  43. c->Encode(data, e);
  44. c->Decode(e, d);
  45. d.AsString(res);
  46. UNIT_ASSERT_EQUAL(NBlockCodecs::TData(res), NBlockCodecs::TData(data));
  47. } catch (...) {
  48. Cerr << c->Name() << "(" << res.Quote() << ")(" << TString{NBlockCodecs::TData(data)}.Quote() << ")" << Endl;
  49. throw;
  50. }
  51. }
  52. }
  53. }
  54. Y_UNIT_TEST(TestAllAtOnce0) {
  55. TestAllAtOnce(20, 0);
  56. }
  57. Y_UNIT_TEST(TestAllAtOnce1) {
  58. TestAllAtOnce(20, 1);
  59. }
  60. Y_UNIT_TEST(TestAllAtOnce2) {
  61. TestAllAtOnce(20, 2);
  62. }
  63. Y_UNIT_TEST(TestAllAtOnce3) {
  64. TestAllAtOnce(20, 3);
  65. }
  66. Y_UNIT_TEST(TestAllAtOnce4) {
  67. TestAllAtOnce(20, 4);
  68. }
  69. Y_UNIT_TEST(TestAllAtOnce5) {
  70. TestAllAtOnce(20, 5);
  71. }
  72. Y_UNIT_TEST(TestAllAtOnce6) {
  73. TestAllAtOnce(20, 6);
  74. }
  75. Y_UNIT_TEST(TestAllAtOnce7) {
  76. TestAllAtOnce(20, 7);
  77. }
  78. Y_UNIT_TEST(TestAllAtOnce8) {
  79. TestAllAtOnce(20, 8);
  80. }
  81. Y_UNIT_TEST(TestAllAtOnce9) {
  82. TestAllAtOnce(20, 9);
  83. }
  84. Y_UNIT_TEST(TestAllAtOnce10) {
  85. TestAllAtOnce(20, 10);
  86. }
  87. Y_UNIT_TEST(TestAllAtOnce12) {
  88. TestAllAtOnce(20, 12);
  89. }
  90. Y_UNIT_TEST(TestAllAtOnce13) {
  91. TestAllAtOnce(20, 13);
  92. }
  93. Y_UNIT_TEST(TestAllAtOnce14) {
  94. TestAllAtOnce(20, 14);
  95. }
  96. Y_UNIT_TEST(TestAllAtOnce15) {
  97. TestAllAtOnce(20, 15);
  98. }
  99. Y_UNIT_TEST(TestAllAtOnce16) {
  100. TestAllAtOnce(20, 16);
  101. }
  102. Y_UNIT_TEST(TestAllAtOnce17) {
  103. TestAllAtOnce(20, 17);
  104. }
  105. Y_UNIT_TEST(TestAllAtOnce18) {
  106. TestAllAtOnce(20, 18);
  107. }
  108. Y_UNIT_TEST(TestAllAtOnce19) {
  109. TestAllAtOnce(20, 19);
  110. }
  111. void TestStreams(size_t n, size_t m) {
  112. TVector<TString> datas;
  113. TString res;
  114. for (size_t i = 0; i < 256; ++i) {
  115. datas.push_back(TString(i, (char)(i % 128)));
  116. }
  117. for (size_t i = 0; i < datas.size(); ++i) {
  118. res += datas[i];
  119. }
  120. TCodecList lst = ListAllCodecs();
  121. for (size_t i = 0; i < lst.size(); ++i) {
  122. TStringStream ss;
  123. const ICodec* c = Codec(lst[i]);
  124. const auto h = MultiHash(c->Name(), i, 2);
  125. if (h % n == m) {
  126. } else {
  127. continue;
  128. }
  129. {
  130. TCodedOutput out(&ss, c, 1234);
  131. for (size_t j = 0; j < datas.size(); ++j) {
  132. out << datas[j];
  133. }
  134. out.Finish();
  135. }
  136. const TString resNew = TDecodedInput(&ss).ReadAll();
  137. try {
  138. UNIT_ASSERT_EQUAL(resNew, res);
  139. } catch (...) {
  140. Cerr << c->Name() << Endl;
  141. throw;
  142. }
  143. }
  144. }
  145. Y_UNIT_TEST(TestStreams0) {
  146. TestStreams(20, 0);
  147. }
  148. Y_UNIT_TEST(TestStreams1) {
  149. TestStreams(20, 1);
  150. }
  151. Y_UNIT_TEST(TestStreams2) {
  152. TestStreams(20, 2);
  153. }
  154. Y_UNIT_TEST(TestStreams3) {
  155. TestStreams(20, 3);
  156. }
  157. Y_UNIT_TEST(TestStreams4) {
  158. TestStreams(20, 4);
  159. }
  160. Y_UNIT_TEST(TestStreams5) {
  161. TestStreams(20, 5);
  162. }
  163. Y_UNIT_TEST(TestStreams6) {
  164. TestStreams(20, 6);
  165. }
  166. Y_UNIT_TEST(TestStreams7) {
  167. TestStreams(20, 7);
  168. }
  169. Y_UNIT_TEST(TestStreams8) {
  170. TestStreams(20, 8);
  171. }
  172. Y_UNIT_TEST(TestStreams9) {
  173. TestStreams(20, 9);
  174. }
  175. Y_UNIT_TEST(TestStreams10) {
  176. TestStreams(20, 10);
  177. }
  178. Y_UNIT_TEST(TestStreams11) {
  179. TestStreams(20, 11);
  180. }
  181. Y_UNIT_TEST(TestStreams12) {
  182. TestStreams(20, 12);
  183. }
  184. Y_UNIT_TEST(TestStreams13) {
  185. TestStreams(20, 13);
  186. }
  187. Y_UNIT_TEST(TestStreams14) {
  188. TestStreams(20, 14);
  189. }
  190. Y_UNIT_TEST(TestStreams15) {
  191. TestStreams(20, 15);
  192. }
  193. Y_UNIT_TEST(TestStreams16) {
  194. TestStreams(20, 16);
  195. }
  196. Y_UNIT_TEST(TestStreams17) {
  197. TestStreams(20, 17);
  198. }
  199. Y_UNIT_TEST(TestStreams18) {
  200. TestStreams(20, 18);
  201. }
  202. Y_UNIT_TEST(TestStreams19) {
  203. TestStreams(20, 19);
  204. }
  205. Y_UNIT_TEST(TestMaxPossibleDecompressedSize) {
  206. UNIT_ASSERT_VALUES_EQUAL(GetMaxPossibleDecompressedLength(), Max<size_t>());
  207. TVector<char> input(10001, ' ');
  208. TCodecList codecs = ListAllCodecs();
  209. SetMaxPossibleDecompressedLength(10000);
  210. for (const auto& codec : codecs) {
  211. const ICodec* c = Codec(codec);
  212. TBuffer inputBuffer(input.data(), input.size());
  213. TBuffer output;
  214. TBuffer decompressed;
  215. c->Encode(inputBuffer, output);
  216. UNIT_ASSERT_EXCEPTION(c->Decode(output, decompressed), yexception);
  217. }
  218. // restore status quo
  219. SetMaxPossibleDecompressedLength(Max<size_t>());
  220. }
  221. Y_UNIT_TEST(TestListAllCodecs) {
  222. static const TString ALL_CODECS =
  223. "brotli_1,brotli_10,brotli_11,brotli_2,brotli_3,brotli_4,brotli_5,brotli_6,brotli_7,brotli_8,brotli_9,"
  224. "bzip2,bzip2-1,bzip2-2,bzip2-3,bzip2-4,bzip2-5,bzip2-6,bzip2-7,bzip2-8,bzip2-9,"
  225. "fastlz,fastlz-0,fastlz-1,fastlz-2,"
  226. "lz4,lz4-fast-fast,lz4-fast-safe,lz4-fast10-fast,lz4-fast10-safe,lz4-fast11-fast,lz4-fast11-safe,"
  227. "lz4-fast12-fast,lz4-fast12-safe,lz4-fast13-fast,lz4-fast13-safe,lz4-fast14-fast,lz4-fast14-safe,"
  228. "lz4-fast15-fast,lz4-fast15-safe,lz4-fast16-fast,lz4-fast16-safe,lz4-fast17-fast,lz4-fast17-safe,"
  229. "lz4-fast18-fast,lz4-fast18-safe,lz4-fast19-fast,lz4-fast19-safe,lz4-fast20-fast,lz4-fast20-safe,"
  230. "lz4-hc-fast,lz4-hc-safe,lz4fast,lz4hc,"
  231. "lzma,lzma-0,lzma-1,lzma-2,lzma-3,lzma-4,lzma-5,lzma-6,lzma-7,lzma-8,lzma-9,"
  232. "null,"
  233. "snappy,"
  234. "zlib,zlib-0,zlib-1,zlib-2,zlib-3,zlib-4,zlib-5,zlib-6,zlib-7,zlib-8,zlib-9,"
  235. "zstd06_1,zstd06_10,zstd06_11,zstd06_12,zstd06_13,zstd06_14,zstd06_15,zstd06_16,zstd06_17,zstd06_18,"
  236. "zstd06_19,zstd06_2,zstd06_20,zstd06_21,zstd06_22,zstd06_3,zstd06_4,zstd06_5,zstd06_6,zstd06_7,zstd06_8,"
  237. "zstd06_9,"
  238. "zstd08_1,zstd08_10,zstd08_11,zstd08_12,zstd08_13,zstd08_14,zstd08_15,zstd08_16,zstd08_17,zstd08_18,"
  239. "zstd08_19,zstd08_2,zstd08_20,zstd08_21,zstd08_22,zstd08_3,zstd08_4,zstd08_5,zstd08_6,zstd08_7,zstd08_8,"
  240. "zstd08_9,zstd_1,zstd_10,zstd_11,zstd_12,zstd_13,zstd_14,zstd_15,zstd_16,zstd_17,zstd_18,zstd_19,zstd_2,"
  241. "zstd_20,zstd_21,zstd_22,zstd_3,zstd_4,zstd_5,zstd_6,zstd_7,zstd_8,zstd_9";
  242. UNIT_ASSERT_VALUES_EQUAL(ALL_CODECS, JoinSeq(",", ListAllCodecs()));
  243. }
  244. Y_UNIT_TEST(TestEncodeDecodeIntoString) {
  245. TStringBuf data = "na gorshke sidel korol";
  246. TCodecList codecs = ListAllCodecs();
  247. for (const auto& codec : codecs) {
  248. const ICodec* c = Codec(codec);
  249. TString encoded = c->Encode(data);
  250. TString decoded = c->Decode(encoded);
  251. UNIT_ASSERT_VALUES_EQUAL(decoded, data);
  252. }
  253. }
  254. }