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 13 #include "llvm/Support/Compression.h" 14 #include "llvm/ADT/SmallString.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/Config/config.h" 17 #include "llvm/Support/Error.h" 18 #include "gtest/gtest.h" 19 20 using namespace llvm; 21 using namespace llvm::compression; 22 23 namespace { 24 25 #if LLVM_ENABLE_ZLIB 26 27 void TestZlibCompression(StringRef Input, int Level) { 28 SmallString<32> Compressed; 29 SmallString<32> Uncompressed; 30 31 zlib::compress(Input, Compressed, Level); 32 33 // Check that uncompressed buffer is the same as original. 34 Error E = zlib::uncompress(Compressed, Uncompressed, Input.size()); 35 consumeError(std::move(E)); 36 37 EXPECT_EQ(Input, Uncompressed); 38 if (Input.size() > 0) { 39 // Uncompression fails if expected length is too short. 40 E = zlib::uncompress(Compressed, Uncompressed, Input.size() - 1); 41 EXPECT_EQ("zlib error: Z_BUF_ERROR", llvm::toString(std::move(E))); 42 } 43 } 44 45 TEST(CompressionTest, Zlib) { 46 TestZlibCompression("", zlib::DefaultCompression); 47 48 TestZlibCompression("hello, world!", zlib::NoCompression); 49 TestZlibCompression("hello, world!", zlib::BestSizeCompression); 50 TestZlibCompression("hello, world!", zlib::BestSpeedCompression); 51 TestZlibCompression("hello, world!", zlib::DefaultCompression); 52 53 const size_t kSize = 1024; 54 char BinaryData[kSize]; 55 for (size_t i = 0; i < kSize; ++i) { 56 BinaryData[i] = i & 255; 57 } 58 StringRef BinaryDataStr(BinaryData, kSize); 59 60 TestZlibCompression(BinaryDataStr, zlib::NoCompression); 61 TestZlibCompression(BinaryDataStr, zlib::BestSizeCompression); 62 TestZlibCompression(BinaryDataStr, zlib::BestSpeedCompression); 63 TestZlibCompression(BinaryDataStr, zlib::DefaultCompression); 64 } 65 66 TEST(CompressionTest, ZlibCRC32) { 67 EXPECT_EQ( 68 0x414FA339U, 69 zlib::crc32(StringRef("The quick brown fox jumps over the lazy dog"))); 70 } 71 72 #endif 73 74 } 75