CompressionTest.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //===- llvm/unittest/Support/CompressionTest.cpp - Compression tests ------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements unit tests for the Compression functions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Support/Compression.h"
  13. #include "llvm/ADT/SmallString.h"
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/Config/config.h"
  16. #include "llvm/Support/Error.h"
  17. #include "gtest/gtest.h"
  18. using namespace llvm;
  19. namespace {
  20. #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
  21. void TestZlibCompression(StringRef Input, int Level) {
  22. SmallString<32> Compressed;
  23. SmallString<32> Uncompressed;
  24. Error E = zlib::compress(Input, Compressed, Level);
  25. EXPECT_FALSE(E);
  26. consumeError(std::move(E));
  27. // Check that uncompressed buffer is the same as original.
  28. E = zlib::uncompress(Compressed, Uncompressed, Input.size());
  29. EXPECT_FALSE(E);
  30. consumeError(std::move(E));
  31. EXPECT_EQ(Input, Uncompressed);
  32. if (Input.size() > 0) {
  33. // Uncompression fails if expected length is too short.
  34. E = zlib::uncompress(Compressed, Uncompressed, Input.size() - 1);
  35. EXPECT_EQ("zlib error: Z_BUF_ERROR", llvm::toString(std::move(E)));
  36. }
  37. }
  38. TEST(CompressionTest, Zlib) {
  39. TestZlibCompression("", zlib::DefaultCompression);
  40. TestZlibCompression("hello, world!", zlib::NoCompression);
  41. TestZlibCompression("hello, world!", zlib::BestSizeCompression);
  42. TestZlibCompression("hello, world!", zlib::BestSpeedCompression);
  43. TestZlibCompression("hello, world!", zlib::DefaultCompression);
  44. const size_t kSize = 1024;
  45. char BinaryData[kSize];
  46. for (size_t i = 0; i < kSize; ++i) {
  47. BinaryData[i] = i & 255;
  48. }
  49. StringRef BinaryDataStr(BinaryData, kSize);
  50. TestZlibCompression(BinaryDataStr, zlib::NoCompression);
  51. TestZlibCompression(BinaryDataStr, zlib::BestSizeCompression);
  52. TestZlibCompression(BinaryDataStr, zlib::BestSpeedCompression);
  53. TestZlibCompression(BinaryDataStr, zlib::DefaultCompression);
  54. }
  55. TEST(CompressionTest, ZlibCRC32) {
  56. EXPECT_EQ(
  57. 0x414FA339U,
  58. zlib::crc32(StringRef("The quick brown fox jumps over the lazy dog")));
  59. }
  60. #endif
  61. }