1 | //===- llvm/unittest/Support/CompressionTest.cpp - Compression tests ------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file implements unit tests for the Compression functions. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "llvm/Support/Compression.h" |
14 | #include "llvm/ADT/StringExtras.h" |
15 | #include "llvm/ADT/StringRef.h" |
16 | #include "llvm/Config/config.h" |
17 | #include "llvm/Support/Error.h" |
18 | #include "gtest/gtest.h" |
19 | |
20 | using namespace llvm; |
21 | using namespace llvm::compression; |
22 | |
23 | namespace { |
24 | |
25 | #if LLVM_ENABLE_ZLIB |
26 | static void testZlibCompression(StringRef Input, int Level) { |
27 | SmallVector<uint8_t, 0> Compressed; |
28 | SmallVector<uint8_t, 0> Uncompressed; |
29 | zlib::compress(Input: arrayRefFromStringRef(Input), CompressedBuffer&: Compressed, Level); |
30 | |
31 | // Check that uncompressed buffer is the same as original. |
32 | Error E = zlib::decompress(Input: Compressed, Output&: Uncompressed, UncompressedSize: Input.size()); |
33 | EXPECT_FALSE(std::move(E)); |
34 | EXPECT_EQ(Input, toStringRef(Uncompressed)); |
35 | |
36 | // decompress with Z dispatches to zlib::decompress. |
37 | E = compression::decompress(T: DebugCompressionType::Zlib, Input: Compressed, |
38 | Output&: Uncompressed, UncompressedSize: Input.size()); |
39 | EXPECT_FALSE(std::move(E)); |
40 | EXPECT_EQ(Input, toStringRef(Uncompressed)); |
41 | |
42 | if (Input.size() > 0) { |
43 | // Decompression fails if expected length is too short. |
44 | E = zlib::decompress(Input: Compressed, Output&: Uncompressed, UncompressedSize: Input.size() - 1); |
45 | EXPECT_EQ("zlib error: Z_BUF_ERROR" , llvm::toString(std::move(E))); |
46 | } |
47 | } |
48 | |
49 | TEST(CompressionTest, Zlib) { |
50 | testZlibCompression(Input: "" , Level: zlib::DefaultCompression); |
51 | |
52 | testZlibCompression(Input: "hello, world!" , Level: zlib::NoCompression); |
53 | testZlibCompression(Input: "hello, world!" , Level: zlib::BestSizeCompression); |
54 | testZlibCompression(Input: "hello, world!" , Level: zlib::BestSpeedCompression); |
55 | testZlibCompression(Input: "hello, world!" , Level: zlib::DefaultCompression); |
56 | |
57 | const size_t kSize = 1024; |
58 | char BinaryData[kSize]; |
59 | for (size_t i = 0; i < kSize; ++i) |
60 | BinaryData[i] = i & 255; |
61 | StringRef BinaryDataStr(BinaryData, kSize); |
62 | |
63 | testZlibCompression(Input: BinaryDataStr, Level: zlib::NoCompression); |
64 | testZlibCompression(Input: BinaryDataStr, Level: zlib::BestSizeCompression); |
65 | testZlibCompression(Input: BinaryDataStr, Level: zlib::BestSpeedCompression); |
66 | testZlibCompression(Input: BinaryDataStr, Level: zlib::DefaultCompression); |
67 | } |
68 | #endif |
69 | |
70 | #if LLVM_ENABLE_ZSTD |
71 | static void testZstdCompression(StringRef Input, int Level) { |
72 | SmallVector<uint8_t, 0> Compressed; |
73 | SmallVector<uint8_t, 0> Uncompressed; |
74 | zstd::compress(Input: arrayRefFromStringRef(Input), CompressedBuffer&: Compressed, Level); |
75 | |
76 | // Check that uncompressed buffer is the same as original. |
77 | Error E = zstd::decompress(Input: Compressed, Output&: Uncompressed, UncompressedSize: Input.size()); |
78 | EXPECT_FALSE(std::move(E)); |
79 | EXPECT_EQ(Input, toStringRef(Uncompressed)); |
80 | |
81 | // decompress with Zstd dispatches to zstd::decompress. |
82 | E = compression::decompress(T: DebugCompressionType::Zstd, Input: Compressed, |
83 | Output&: Uncompressed, UncompressedSize: Input.size()); |
84 | EXPECT_FALSE(std::move(E)); |
85 | EXPECT_EQ(Input, toStringRef(Uncompressed)); |
86 | |
87 | if (Input.size() > 0) { |
88 | // Decompression fails if expected length is too short. |
89 | E = zstd::decompress(Input: Compressed, Output&: Uncompressed, UncompressedSize: Input.size() - 1); |
90 | EXPECT_EQ("Destination buffer is too small" , llvm::toString(std::move(E))); |
91 | } |
92 | } |
93 | |
94 | TEST(CompressionTest, Zstd) { |
95 | testZstdCompression(Input: "" , Level: zstd::DefaultCompression); |
96 | |
97 | testZstdCompression(Input: "hello, world!" , Level: zstd::NoCompression); |
98 | testZstdCompression(Input: "hello, world!" , Level: zstd::BestSizeCompression); |
99 | testZstdCompression(Input: "hello, world!" , Level: zstd::BestSpeedCompression); |
100 | testZstdCompression(Input: "hello, world!" , Level: zstd::DefaultCompression); |
101 | |
102 | const size_t kSize = 1024; |
103 | char BinaryData[kSize]; |
104 | for (size_t i = 0; i < kSize; ++i) |
105 | BinaryData[i] = i & 255; |
106 | StringRef BinaryDataStr(BinaryData, kSize); |
107 | |
108 | testZstdCompression(Input: BinaryDataStr, Level: zstd::NoCompression); |
109 | testZstdCompression(Input: BinaryDataStr, Level: zstd::BestSizeCompression); |
110 | testZstdCompression(Input: BinaryDataStr, Level: zstd::BestSpeedCompression); |
111 | testZstdCompression(Input: BinaryDataStr, Level: zstd::DefaultCompression); |
112 | } |
113 | #endif |
114 | } |
115 | |