blob: c0a9adadb78872b2891b6d16ea2c08e16ddb1ea9 [file] [log] [blame]
Alexey Samsonovee03c942013-04-23 08:28:39 +00001//===- 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"
15#include "llvm/ADT/OwningPtr.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Config/config.h"
18#include "llvm/Support/MemoryBuffer.h"
19#include "gtest/gtest.h"
20
21using namespace llvm;
22
23namespace {
24
Alexey Samsonova0bd5df2013-04-23 08:57:30 +000025#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
Alexey Samsonovee03c942013-04-23 08:28:39 +000026
27void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) {
28 OwningPtr<MemoryBuffer> Compressed;
29 OwningPtr<MemoryBuffer> Uncompressed;
30 EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level));
31 // Check that uncompressed buffer is the same as original.
32 EXPECT_EQ(zlib::StatusOK, zlib::uncompress(Compressed->getBuffer(),
33 Uncompressed, Input.size()));
34 EXPECT_EQ(Input.size(), Uncompressed->getBufferSize());
35 EXPECT_EQ(0,
36 memcmp(Input.data(), Uncompressed->getBufferStart(), Input.size()));
37 if (Input.size() > 0) {
38 // Uncompression fails if expected length is too short.
39 EXPECT_EQ(zlib::StatusBufferTooShort,
40 zlib::uncompress(Compressed->getBuffer(), Uncompressed,
41 Input.size() - 1));
42 }
43}
44
45TEST(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
Alexey Samsonovef7aefc2013-08-14 16:03:29 +000066TEST(CompressionTest, ZlibCRC32) {
67 EXPECT_EQ(
68 0x414FA339U,
69 zlib::crc32(StringRef("The quick brown fox jumps over the lazy dog")));
70}
71
Alexey Samsonovee03c942013-04-23 08:28:39 +000072#endif
73
74}