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