summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorAlexey Samsonov <samsonov@google.com>2013-04-23 08:28:39 +0000
committerAlexey Samsonov <samsonov@google.com>2013-04-23 08:28:39 +0000
commitee03c949b85036b68c261dcc27dca064ee7e525d (patch)
tree14f060f70c0c6e5346a0f120c196f6718b060cd4 /unittests
parenta8958769ea5583d58ae51931d8b1271c23fd20ea (diff)
downloadllvm-ee03c949b85036b68c261dcc27dca064ee7e525d.tar.gz
llvm-ee03c949b85036b68c261dcc27dca064ee7e525d.tar.bz2
llvm-ee03c949b85036b68c261dcc27dca064ee7e525d.tar.xz
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
Diffstat (limited to 'unittests')
-rw-r--r--unittests/Support/CMakeLists.txt1
-rw-r--r--unittests/Support/CompressionTest.cpp68
2 files changed, 69 insertions, 0 deletions
diff --git a/unittests/Support/CMakeLists.txt b/unittests/Support/CMakeLists.txt
index a5bf3aef62..e6cafbcaf1 100644
--- a/unittests/Support/CMakeLists.txt
+++ b/unittests/Support/CMakeLists.txt
@@ -10,6 +10,7 @@ add_llvm_unittest(SupportTests
BlockFrequencyTest.cpp
Casting.cpp
CommandLineTest.cpp
+ CompressionTest.cpp
ConstantRangeTest.cpp
DataExtractorTest.cpp
EndianTest.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<MemoryBuffer> Compressed;
+ OwningPtr<MemoryBuffer> 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
+
+}