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"
21 using namespace llvm::compression
;
26 static void testZlibCompression(StringRef Input
, int Level
) {
27 SmallVector
<uint8_t, 0> Compressed
;
28 SmallVector
<uint8_t, 0> Uncompressed
;
29 zlib::compress(arrayRefFromStringRef(Input
), Compressed
, Level
);
31 // Check that uncompressed buffer is the same as original.
32 Error E
= zlib::decompress(Compressed
, Uncompressed
, Input
.size());
33 EXPECT_FALSE(std::move(E
));
34 EXPECT_EQ(Input
, toStringRef(Uncompressed
));
36 // decompress with Z dispatches to zlib::decompress.
37 E
= compression::decompress(DebugCompressionType::Zlib
, Compressed
,
38 Uncompressed
, Input
.size());
39 EXPECT_FALSE(std::move(E
));
40 EXPECT_EQ(Input
, toStringRef(Uncompressed
));
42 if (Input
.size() > 0) {
43 // Decompression fails if expected length is too short.
44 E
= zlib::decompress(Compressed
, Uncompressed
, Input
.size() - 1);
45 EXPECT_EQ("zlib error: Z_BUF_ERROR", llvm::toString(std::move(E
)));
49 TEST(CompressionTest
, Zlib
) {
50 testZlibCompression("", zlib::DefaultCompression
);
52 testZlibCompression("hello, world!", zlib::NoCompression
);
53 testZlibCompression("hello, world!", zlib::BestSizeCompression
);
54 testZlibCompression("hello, world!", zlib::BestSpeedCompression
);
55 testZlibCompression("hello, world!", zlib::DefaultCompression
);
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
);
63 testZlibCompression(BinaryDataStr
, zlib::NoCompression
);
64 testZlibCompression(BinaryDataStr
, zlib::BestSizeCompression
);
65 testZlibCompression(BinaryDataStr
, zlib::BestSpeedCompression
);
66 testZlibCompression(BinaryDataStr
, zlib::DefaultCompression
);
71 static void testZstdCompression(StringRef Input
, int Level
) {
72 SmallVector
<uint8_t, 0> Compressed
;
73 SmallVector
<uint8_t, 0> Uncompressed
;
74 zstd::compress(arrayRefFromStringRef(Input
), Compressed
, Level
);
76 // Check that uncompressed buffer is the same as original.
77 Error E
= zstd::decompress(Compressed
, Uncompressed
, Input
.size());
78 EXPECT_FALSE(std::move(E
));
79 EXPECT_EQ(Input
, toStringRef(Uncompressed
));
81 // decompress with Zstd dispatches to zstd::decompress.
82 E
= compression::decompress(DebugCompressionType::Zstd
, Compressed
,
83 Uncompressed
, Input
.size());
84 EXPECT_FALSE(std::move(E
));
85 EXPECT_EQ(Input
, toStringRef(Uncompressed
));
87 if (Input
.size() > 0) {
88 // Decompression fails if expected length is too short.
89 E
= zstd::decompress(Compressed
, Uncompressed
, Input
.size() - 1);
90 EXPECT_EQ("Destination buffer is too small", llvm::toString(std::move(E
)));
94 TEST(CompressionTest
, Zstd
) {
95 testZstdCompression("", zstd::DefaultCompression
);
97 testZstdCompression("hello, world!", zstd::NoCompression
);
98 testZstdCompression("hello, world!", zstd::BestSizeCompression
);
99 testZstdCompression("hello, world!", zstd::BestSpeedCompression
);
100 testZstdCompression("hello, world!", zstd::DefaultCompression
);
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
);
108 testZstdCompression(BinaryDataStr
, zstd::NoCompression
);
109 testZstdCompression(BinaryDataStr
, zstd::BestSizeCompression
);
110 testZstdCompression(BinaryDataStr
, zstd::BestSpeedCompression
);
111 testZstdCompression(BinaryDataStr
, zstd::DefaultCompression
);