xref: /llvm-project/llvm/unittests/Support/CompressionTest.cpp (revision 3b7bfeb483e78d355ec3527aa31786e2545470ea)
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
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::decompress(Compressed, Uncompressed, 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(DebugCompressionType::Zlib, Compressed,
38                               Uncompressed, 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(Compressed, Uncompressed, Input.size() - 1);
45     EXPECT_EQ("zlib error: Z_BUF_ERROR", llvm::toString(std::move(E)));
46   }
47 }
48 
TEST(CompressionTest,Zlib)49 TEST(CompressionTest, Zlib) {
50   testZlibCompression("", zlib::DefaultCompression);
51 
52   testZlibCompression("hello, world!", zlib::NoCompression);
53   testZlibCompression("hello, world!", zlib::BestSizeCompression);
54   testZlibCompression("hello, world!", zlib::BestSpeedCompression);
55   testZlibCompression("hello, world!", 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(BinaryDataStr, zlib::NoCompression);
64   testZlibCompression(BinaryDataStr, zlib::BestSizeCompression);
65   testZlibCompression(BinaryDataStr, zlib::BestSpeedCompression);
66   testZlibCompression(BinaryDataStr, zlib::DefaultCompression);
67 }
68 #endif
69 
70 #if LLVM_ENABLE_ZSTD
testZstdCompression(StringRef Input,int Level)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);
75 
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));
80 
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));
86 
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)));
91   }
92 }
93 
TEST(CompressionTest,Zstd)94 TEST(CompressionTest, Zstd) {
95   testZstdCompression("", zstd::DefaultCompression);
96 
97   testZstdCompression("hello, world!", zstd::NoCompression);
98   testZstdCompression("hello, world!", zstd::BestSizeCompression);
99   testZstdCompression("hello, world!", zstd::BestSpeedCompression);
100   testZstdCompression("hello, world!", 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(BinaryDataStr, zstd::NoCompression);
109   testZstdCompression(BinaryDataStr, zstd::BestSizeCompression);
110   testZstdCompression(BinaryDataStr, zstd::BestSpeedCompression);
111   testZstdCompression(BinaryDataStr, zstd::DefaultCompression);
112 }
113 #endif
114 }
115