2013-04-23 16:28:39 +08:00
|
|
|
//===- llvm/unittest/Support/CompressionTest.cpp - Compression tests ------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-04-23 16:28:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements unit tests for the Compression functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/Compression.h"
|
2014-04-06 05:53:04 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2013-04-23 16:28:39 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2017-06-06 19:06:56 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2013-04-23 16:28:39 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-04-23 16:57:30 +08:00
|
|
|
#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
|
2013-04-23 16:28:39 +08:00
|
|
|
|
2018-08-04 08:13:13 +08:00
|
|
|
void TestZlibCompression(StringRef Input, int Level) {
|
2014-04-06 05:53:04 +08:00
|
|
|
SmallString<32> Compressed;
|
2014-04-06 05:26:44 +08:00
|
|
|
SmallString<32> Uncompressed;
|
2017-01-17 23:45:07 +08:00
|
|
|
|
|
|
|
Error E = zlib::compress(Input, Compressed, Level);
|
|
|
|
EXPECT_FALSE(E);
|
|
|
|
consumeError(std::move(E));
|
|
|
|
|
2013-04-23 16:28:39 +08:00
|
|
|
// Check that uncompressed buffer is the same as original.
|
2017-01-17 23:45:07 +08:00
|
|
|
E = zlib::uncompress(Compressed, Uncompressed, Input.size());
|
|
|
|
EXPECT_FALSE(E);
|
|
|
|
consumeError(std::move(E));
|
|
|
|
|
2014-04-06 05:53:04 +08:00
|
|
|
EXPECT_EQ(Input, Uncompressed);
|
2013-04-23 16:28:39 +08:00
|
|
|
if (Input.size() > 0) {
|
|
|
|
// Uncompression fails if expected length is too short.
|
2017-01-17 23:45:07 +08:00
|
|
|
E = zlib::uncompress(Compressed, Uncompressed, Input.size() - 1);
|
|
|
|
EXPECT_EQ("zlib error: Z_BUF_ERROR", llvm::toString(std::move(E)));
|
2013-04-23 16:28:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-08-15 00:03:29 +08:00
|
|
|
TEST(CompressionTest, ZlibCRC32) {
|
|
|
|
EXPECT_EQ(
|
|
|
|
0x414FA339U,
|
|
|
|
zlib::crc32(StringRef("The quick brown fox jumps over the lazy dog")));
|
|
|
|
}
|
|
|
|
|
2013-04-23 16:28:39 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|