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/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"
19
20 using namespace llvm;
21 using namespace llvm::compression;
22
23 namespace {
24
25 #if LLVM_ENABLE_ZLIB
testZlibCompression(StringRef Input,int Level)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);
30
31 // Check that uncompressed buffer is the same as original.
32 Error E = zlib::uncompress(Compressed, Uncompressed, Input.size());
33 consumeError(std::move(E));
34
35 EXPECT_EQ(Input, toStringRef(Uncompressed));
36 if (Input.size() > 0) {
37 // Uncompression fails if expected length is too short.
38 E = zlib::uncompress(Compressed, Uncompressed, Input.size() - 1);
39 EXPECT_EQ("zlib error: Z_BUF_ERROR", llvm::toString(std::move(E)));
40 }
41 }
42
TEST(CompressionTest,Zlib)43 TEST(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 StringRef BinaryDataStr(BinaryData, kSize);
56
57 testZlibCompression(BinaryDataStr, zlib::NoCompression);
58 testZlibCompression(BinaryDataStr, zlib::BestSizeCompression);
59 testZlibCompression(BinaryDataStr, zlib::BestSpeedCompression);
60 testZlibCompression(BinaryDataStr, zlib::DefaultCompression);
61 }
62 #endif
63
64 #if LLVM_ENABLE_ZSTD
testZstdCompression(StringRef Input,int Level)65 static void testZstdCompression(StringRef Input, int Level) {
66 SmallVector<uint8_t, 0> Compressed;
67 SmallVector<uint8_t, 0> Uncompressed;
68 zstd::compress(arrayRefFromStringRef(Input), Compressed, Level);
69
70 // Check that uncompressed buffer is the same as original.
71 Error E = zstd::uncompress(Compressed, Uncompressed, Input.size());
72 consumeError(std::move(E));
73
74 EXPECT_EQ(Input, toStringRef(Uncompressed));
75 if (Input.size() > 0) {
76 // Uncompression fails if expected length is too short.
77 E = zstd::uncompress(Compressed, Uncompressed, Input.size() - 1);
78 EXPECT_EQ("Destination buffer is too small", llvm::toString(std::move(E)));
79 }
80 }
81
TEST(CompressionTest,Zstd)82 TEST(CompressionTest, Zstd) {
83 testZstdCompression("", zstd::DefaultCompression);
84
85 testZstdCompression("hello, world!", zstd::NoCompression);
86 testZstdCompression("hello, world!", zstd::BestSizeCompression);
87 testZstdCompression("hello, world!", zstd::BestSpeedCompression);
88 testZstdCompression("hello, world!", zstd::DefaultCompression);
89
90 const size_t kSize = 1024;
91 char BinaryData[kSize];
92 for (size_t i = 0; i < kSize; ++i)
93 BinaryData[i] = i & 255;
94 StringRef BinaryDataStr(BinaryData, kSize);
95
96 testZstdCompression(BinaryDataStr, zstd::NoCompression);
97 testZstdCompression(BinaryDataStr, zstd::BestSizeCompression);
98 testZstdCompression(BinaryDataStr, zstd::BestSpeedCompression);
99 testZstdCompression(BinaryDataStr, zstd::DefaultCompression);
100 }
101 #endif
102 }
103