zstd_ut.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "zstd.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/random/fast.h>
  4. #include <util/stream/null.h>
  5. #include <util/stream/str.h>
  6. Y_UNIT_TEST_SUITE(TZstdTestSuite) {
  7. TString Compress(TString data, int quality = -1) {
  8. TString compressed;
  9. TStringOutput output(compressed);
  10. TZstdCompress compressStream(&output, quality);
  11. compressStream.Write(data.data(), data.size());
  12. compressStream.Finish();
  13. output.Finish();
  14. return compressed;
  15. }
  16. TString Decompress(TString data) {
  17. TStringInput input(data);
  18. TZstdDecompress decompressStream(&input);
  19. return decompressStream.ReadAll();
  20. }
  21. void TestCase(const TString& s) {
  22. UNIT_ASSERT_VALUES_EQUAL(s, Decompress(Compress(s, -1)));
  23. UNIT_ASSERT_VALUES_EQUAL(s, Decompress(Compress(s, 0)));
  24. UNIT_ASSERT_VALUES_EQUAL(s, Decompress(Compress(s, 22)));
  25. UNIT_ASSERT_VALUES_EQUAL(s, Decompress(Compress(s, 11)));
  26. UNIT_ASSERT_VALUES_EQUAL(s, Decompress(Compress(s, 100500)));
  27. }
  28. TString GenerateRandomString(size_t size) {
  29. TReallyFastRng32 rng(42);
  30. TString result;
  31. result.reserve(size + sizeof(ui64));
  32. while (result.size() < size) {
  33. ui64 value = rng.GenRand64();
  34. result += TStringBuf(reinterpret_cast<const char*>(&value), sizeof(value));
  35. }
  36. result.resize(size);
  37. return result;
  38. }
  39. Y_UNIT_TEST(TestHelloWorld) {
  40. TestCase("hello world");
  41. }
  42. Y_UNIT_TEST(TestSeveralStreamsWithSameQuality) {
  43. auto s1 = GenerateRandomString(1 << 15);
  44. auto s2 = GenerateRandomString(1 << 15);
  45. auto c1 = Compress(s1);
  46. auto c2 = Compress(s2);
  47. UNIT_ASSERT_VALUES_EQUAL(s1 + s2, Decompress(c1 + c2));
  48. }
  49. Y_UNIT_TEST(TestSeveralStreamsWithDifferentQuality) {
  50. auto s1 = GenerateRandomString(1 << 15);
  51. auto s2 = GenerateRandomString(1 << 15);
  52. auto c1 = Compress(s1, 1);
  53. auto c2 = Compress(s2, 2);
  54. UNIT_ASSERT_VALUES_EQUAL(s1 + s2, Decompress(c1 + c2));
  55. }
  56. Y_UNIT_TEST(TestIncompleteStream) {
  57. TString manyAs(64 * 1024, 'a');
  58. auto compressed = Compress(manyAs);
  59. TString truncated(compressed.data(), compressed.size() - 1);
  60. UNIT_CHECK_GENERATED_EXCEPTION(Decompress(truncated), std::exception);
  61. }
  62. Y_UNIT_TEST(Test64KB) {
  63. auto manyAs = TString(64 * 1024, 'a');
  64. TString str("Hello from the Matrix!@#% How are you?}{\n\t\a");
  65. TestCase(manyAs + str + manyAs);
  66. }
  67. Y_UNIT_TEST(Test1MB) {
  68. TestCase(GenerateRandomString(1 * 1024 * 1024));
  69. }
  70. Y_UNIT_TEST(TestEmpty) {
  71. TestCase("");
  72. }
  73. Y_UNIT_TEST(TestWriteAfterFinish) {
  74. TNullOutput output;
  75. TZstdCompress compressStream(&output);
  76. compressStream.Finish();
  77. UNIT_ASSERT_EXCEPTION_CONTAINS(compressStream.Write("a", 1), std::exception, "Cannot use stream after finish.");
  78. UNIT_ASSERT_EXCEPTION_CONTAINS(compressStream.Flush(), std::exception, "Cannot use stream after finish.");
  79. }
  80. }