1 //===- llvm/unittest/Support/CompressionTest.cpp - Compression tests ------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements unit tests for the Compression functions.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Compression.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Error.h"
18 #include "gtest/gtest.h"
24 #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
26 void TestZlibCompression(StringRef Input
, int Level
) {
27 SmallString
<32> Compressed
;
28 SmallString
<32> Uncompressed
;
30 Error E
= zlib::compress(Input
, Compressed
, Level
);
32 consumeError(std::move(E
));
34 // Check that uncompressed buffer is the same as original.
35 E
= zlib::uncompress(Compressed
, Uncompressed
, Input
.size());
37 consumeError(std::move(E
));
39 EXPECT_EQ(Input
, Uncompressed
);
40 if (Input
.size() > 0) {
41 // Uncompression fails if expected length is too short.
42 E
= zlib::uncompress(Compressed
, Uncompressed
, Input
.size() - 1);
43 EXPECT_EQ("zlib error: Z_BUF_ERROR", llvm::toString(std::move(E
)));
47 TEST(CompressionTest
, Zlib
) {
48 TestZlibCompression("", zlib::DefaultCompression
);
50 TestZlibCompression("hello, world!", zlib::NoCompression
);
51 TestZlibCompression("hello, world!", zlib::BestSizeCompression
);
52 TestZlibCompression("hello, world!", zlib::BestSpeedCompression
);
53 TestZlibCompression("hello, world!", zlib::DefaultCompression
);
55 const size_t kSize
= 1024;
56 char BinaryData
[kSize
];
57 for (size_t i
= 0; i
< kSize
; ++i
) {
58 BinaryData
[i
] = i
& 255;
60 StringRef
BinaryDataStr(BinaryData
, kSize
);
62 TestZlibCompression(BinaryDataStr
, zlib::NoCompression
);
63 TestZlibCompression(BinaryDataStr
, zlib::BestSizeCompression
);
64 TestZlibCompression(BinaryDataStr
, zlib::BestSpeedCompression
);
65 TestZlibCompression(BinaryDataStr
, zlib::DefaultCompression
);
68 TEST(CompressionTest
, ZlibCRC32
) {
71 zlib::crc32(StringRef("The quick brown fox jumps over the lazy dog")));