blob: 30df0509cce0ef4c48015c9678969a11264a1499 [file] [log] [blame]
Alexey Samsonov2fb337e2013-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"
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000015#include "llvm/ADT/StringRef.h"
16#include "llvm/Config/config.h"
17#include "llvm/Support/MemoryBuffer.h"
18#include "gtest/gtest.h"
19
20using namespace llvm;
21
22namespace {
23
Alexey Samsonov28acf052013-04-23 08:57:30 +000024#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000025
26void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000027 std::unique_ptr<MemoryBuffer> Compressed;
David Blaikiea505f242014-04-05 21:26:44 +000028 SmallString<32> Uncompressed;
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000029 EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level));
30 // Check that uncompressed buffer is the same as original.
31 EXPECT_EQ(zlib::StatusOK, zlib::uncompress(Compressed->getBuffer(),
32 Uncompressed, Input.size()));
David Blaikiea505f242014-04-05 21:26:44 +000033 EXPECT_EQ(Input.size(), Uncompressed.size());
34 EXPECT_EQ(0, memcmp(Input.data(), Uncompressed.data(), Input.size()));
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000035 if (Input.size() > 0) {
36 // Uncompression fails if expected length is too short.
37 EXPECT_EQ(zlib::StatusBufferTooShort,
38 zlib::uncompress(Compressed->getBuffer(), Uncompressed,
39 Input.size() - 1));
40 }
41}
42
43TEST(CompressionTest, Zlib) {
44 TestZlibCompression("", zlib::DefaultCompression);
45
46 TestZlibCompression("hello, world!", zlib::NoCompression);
47 TestZlibCompression("hello, world!", zlib::BestSizeCompression);
48 TestZlibCompression("hello, world!", zlib::BestSpeedCompression);
49 TestZlibCompression("hello, world!", zlib::DefaultCompression);
50
51 const size_t kSize = 1024;
52 char BinaryData[kSize];
53 for (size_t i = 0; i < kSize; ++i) {
54 BinaryData[i] = i & 255;
55 }
56 StringRef BinaryDataStr(BinaryData, kSize);
57
58 TestZlibCompression(BinaryDataStr, zlib::NoCompression);
59 TestZlibCompression(BinaryDataStr, zlib::BestSizeCompression);
60 TestZlibCompression(BinaryDataStr, zlib::BestSpeedCompression);
61 TestZlibCompression(BinaryDataStr, zlib::DefaultCompression);
62}
63
Alexey Samsonov6ede7062013-08-14 16:03:29 +000064TEST(CompressionTest, ZlibCRC32) {
65 EXPECT_EQ(
66 0x414FA339U,
67 zlib::crc32(StringRef("The quick brown fox jumps over the lazy dog")));
68}
69
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000070#endif
71
72}