From ee03c949b85036b68c261dcc27dca064ee7e525d Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Tue, 23 Apr 2013 08:28:39 +0000 Subject: Add basic zlib support to LLVM. This would allow to use compression/uncompression in selected LLVM tools. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@180083 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/Support/CompressionTest.cpp | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 unittests/Support/CompressionTest.cpp (limited to 'unittests/Support/CompressionTest.cpp') diff --git a/unittests/Support/CompressionTest.cpp b/unittests/Support/CompressionTest.cpp new file mode 100644 index 0000000000..aad7ed0e89 --- /dev/null +++ b/unittests/Support/CompressionTest.cpp @@ -0,0 +1,68 @@ +//===- llvm/unittest/Support/CompressionTest.cpp - Compression tests ------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements unit tests for the Compression functions. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/Compression.h" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Config/config.h" +#include "llvm/Support/MemoryBuffer.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +#if LLVM_ENABLE_ZLIB == 1 + +void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) { + OwningPtr Compressed; + OwningPtr Uncompressed; + EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level)); + // Check that uncompressed buffer is the same as original. + EXPECT_EQ(zlib::StatusOK, zlib::uncompress(Compressed->getBuffer(), + Uncompressed, Input.size())); + EXPECT_EQ(Input.size(), Uncompressed->getBufferSize()); + EXPECT_EQ(0, + memcmp(Input.data(), Uncompressed->getBufferStart(), Input.size())); + if (Input.size() > 0) { + // Uncompression fails if expected length is too short. + EXPECT_EQ(zlib::StatusBufferTooShort, + zlib::uncompress(Compressed->getBuffer(), Uncompressed, + Input.size() - 1)); + } +} + +TEST(CompressionTest, Zlib) { + TestZlibCompression("", zlib::DefaultCompression); + + TestZlibCompression("hello, world!", zlib::NoCompression); + TestZlibCompression("hello, world!", zlib::BestSizeCompression); + TestZlibCompression("hello, world!", zlib::BestSpeedCompression); + TestZlibCompression("hello, world!", zlib::DefaultCompression); + + const size_t kSize = 1024; + char BinaryData[kSize]; + for (size_t i = 0; i < kSize; ++i) { + BinaryData[i] = i & 255; + } + StringRef BinaryDataStr(BinaryData, kSize); + + TestZlibCompression(BinaryDataStr, zlib::NoCompression); + TestZlibCompression(BinaryDataStr, zlib::BestSizeCompression); + TestZlibCompression(BinaryDataStr, zlib::BestSpeedCompression); + TestZlibCompression(BinaryDataStr, zlib::DefaultCompression); +} + +#endif + +} -- cgit v1.2.3-70-g09d2 From a0bd5df0867e054b9740bc734325cdeabfe3825a Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Tue, 23 Apr 2013 08:57:30 +0000 Subject: Add more guards around zlib-dependent code git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@180084 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Config/config.h.cmake | 3 +++ lib/Support/Compression.cpp | 2 +- unittests/Support/CompressionTest.cpp | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) (limited to 'unittests/Support/CompressionTest.cpp') diff --git a/include/llvm/Config/config.h.cmake b/include/llvm/Config/config.h.cmake index 408c590beb..97af6955d9 100644 --- a/include/llvm/Config/config.h.cmake +++ b/include/llvm/Config/config.h.cmake @@ -230,6 +230,9 @@ /* Define to 1 if you have the `udis86' library (-ludis86). */ #undef HAVE_LIBUDIS86 +/* Define to 1 if you have the 'z' library (-lz). */ +#cmakedefine HAVE_LIBZ ${HAVE_LIBZ} + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_LIMITS_H ${HAVE_LIMITS_H} diff --git a/lib/Support/Compression.cpp b/lib/Support/Compression.cpp index d0adf79ec7..497b17ec92 100644 --- a/lib/Support/Compression.cpp +++ b/lib/Support/Compression.cpp @@ -23,7 +23,7 @@ using namespace llvm; -#if LLVM_ENABLE_ZLIB == 1 +#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ static int encodeZlibCompressionLevel(zlib::CompressionLevel Level) { switch (Level) { case zlib::NoCompression: return 0; diff --git a/unittests/Support/CompressionTest.cpp b/unittests/Support/CompressionTest.cpp index aad7ed0e89..c8e2cd9f02 100644 --- a/unittests/Support/CompressionTest.cpp +++ b/unittests/Support/CompressionTest.cpp @@ -22,7 +22,7 @@ using namespace llvm; namespace { -#if LLVM_ENABLE_ZLIB == 1 +#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) { OwningPtr Compressed; -- cgit v1.2.3-70-g09d2