1 //===- llvm/unittest/Support/CompressionTest.cpp - Compression tests ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements unit tests for the Compression functions.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Compression.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Config/config.h"
18 #include "gtest/gtest.h"
24 #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
26 void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) {
27 SmallString<32> Compressed;
28 SmallString<32> Uncompressed;
29 EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level));
30 // Check that uncompressed buffer is the same as original.
31 EXPECT_EQ(zlib::StatusOK,
32 zlib::uncompress(Compressed, Uncompressed, Input.size()));
33 EXPECT_EQ(Input, Uncompressed);
34 if (Input.size() > 0) {
35 // Uncompression fails if expected length is too short.
36 EXPECT_EQ(zlib::StatusBufferTooShort,
37 zlib::uncompress(Compressed, Uncompressed, Input.size() - 1));
41 TEST(CompressionTest, Zlib) {
42 TestZlibCompression("", zlib::DefaultCompression);
44 TestZlibCompression("hello, world!", zlib::NoCompression);
45 TestZlibCompression("hello, world!", zlib::BestSizeCompression);
46 TestZlibCompression("hello, world!", zlib::BestSpeedCompression);
47 TestZlibCompression("hello, world!", zlib::DefaultCompression);
49 const size_t kSize = 1024;
50 char BinaryData[kSize];
51 for (size_t i = 0; i < kSize; ++i) {
52 BinaryData[i] = i & 255;
54 StringRef BinaryDataStr(BinaryData, kSize);
56 TestZlibCompression(BinaryDataStr, zlib::NoCompression);
57 TestZlibCompression(BinaryDataStr, zlib::BestSizeCompression);
58 TestZlibCompression(BinaryDataStr, zlib::BestSpeedCompression);
59 TestZlibCompression(BinaryDataStr, zlib::DefaultCompression);
62 TEST(CompressionTest, ZlibCRC32) {
65 zlib::crc32(StringRef("The quick brown fox jumps over the lazy dog")));