Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/CompressionTest.cpp - Compression tests ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements unit tests for the Compression functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/Compression.h" |
David Blaikie | 857497b | 2014-04-05 21:53:04 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringRef.h" |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 17 | #include "llvm/Config/config.h" |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 18 | #include "gtest/gtest.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | |
Alexey Samsonov | 28acf05 | 2013-04-23 08:57:30 +0000 | [diff] [blame] | 24 | #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 25 | |
| 26 | void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) { |
David Blaikie | 857497b | 2014-04-05 21:53:04 +0000 | [diff] [blame] | 27 | SmallString<32> Compressed; |
David Blaikie | a505f24 | 2014-04-05 21:26:44 +0000 | [diff] [blame] | 28 | SmallString<32> Uncompressed; |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 29 | EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level)); |
| 30 | // Check that uncompressed buffer is the same as original. |
David Blaikie | 857497b | 2014-04-05 21:53:04 +0000 | [diff] [blame] | 31 | EXPECT_EQ(zlib::StatusOK, |
| 32 | zlib::uncompress(Compressed, Uncompressed, Input.size())); |
| 33 | EXPECT_EQ(Input, Uncompressed); |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 34 | if (Input.size() > 0) { |
| 35 | // Uncompression fails if expected length is too short. |
| 36 | EXPECT_EQ(zlib::StatusBufferTooShort, |
David Blaikie | 857497b | 2014-04-05 21:53:04 +0000 | [diff] [blame] | 37 | zlib::uncompress(Compressed, Uncompressed, Input.size() - 1)); |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
| 41 | TEST(CompressionTest, Zlib) { |
| 42 | TestZlibCompression("", zlib::DefaultCompression); |
| 43 | |
| 44 | TestZlibCompression("hello, world!", zlib::NoCompression); |
| 45 | TestZlibCompression("hello, world!", zlib::BestSizeCompression); |
| 46 | TestZlibCompression("hello, world!", zlib::BestSpeedCompression); |
| 47 | TestZlibCompression("hello, world!", zlib::DefaultCompression); |
| 48 | |
| 49 | const size_t kSize = 1024; |
| 50 | char BinaryData[kSize]; |
| 51 | for (size_t i = 0; i < kSize; ++i) { |
| 52 | BinaryData[i] = i & 255; |
| 53 | } |
| 54 | StringRef BinaryDataStr(BinaryData, kSize); |
| 55 | |
| 56 | TestZlibCompression(BinaryDataStr, zlib::NoCompression); |
| 57 | TestZlibCompression(BinaryDataStr, zlib::BestSizeCompression); |
| 58 | TestZlibCompression(BinaryDataStr, zlib::BestSpeedCompression); |
| 59 | TestZlibCompression(BinaryDataStr, zlib::DefaultCompression); |
| 60 | } |
| 61 | |
Alexey Samsonov | 6ede706 | 2013-08-14 16:03:29 +0000 | [diff] [blame] | 62 | TEST(CompressionTest, ZlibCRC32) { |
| 63 | EXPECT_EQ( |
| 64 | 0x414FA339U, |
| 65 | zlib::crc32(StringRef("The quick brown fox jumps over the lazy dog"))); |
| 66 | } |
| 67 | |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
| 70 | } |