CompressionTest.cpp revision ee03c949b85036b68c261dcc27dca064ee7e525d
1//===- llvm/unittest/Support/CompressionTest.cpp - Compression tests ------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements unit tests for the Compression functions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/Compression.h"
15#include "llvm/ADT/OwningPtr.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Config/config.h"
18#include "llvm/Support/MemoryBuffer.h"
19#include "gtest/gtest.h"
20
21using namespace llvm;
22
23namespace {
24
25#if LLVM_ENABLE_ZLIB == 1
26
27void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) {
28  OwningPtr<MemoryBuffer> Compressed;
29  OwningPtr<MemoryBuffer> Uncompressed;
30  EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level));
31  // Check that uncompressed buffer is the same as original.
32  EXPECT_EQ(zlib::StatusOK, zlib::uncompress(Compressed->getBuffer(),
33                                             Uncompressed, Input.size()));
34  EXPECT_EQ(Input.size(), Uncompressed->getBufferSize());
35  EXPECT_EQ(0,
36            memcmp(Input.data(), Uncompressed->getBufferStart(), Input.size()));
37  if (Input.size() > 0) {
38    // Uncompression fails if expected length is too short.
39    EXPECT_EQ(zlib::StatusBufferTooShort,
40              zlib::uncompress(Compressed->getBuffer(), Uncompressed,
41                               Input.size() - 1));
42  }
43}
44
45TEST(CompressionTest, Zlib) {
46  TestZlibCompression("", zlib::DefaultCompression);
47
48  TestZlibCompression("hello, world!", zlib::NoCompression);
49  TestZlibCompression("hello, world!", zlib::BestSizeCompression);
50  TestZlibCompression("hello, world!", zlib::BestSpeedCompression);
51  TestZlibCompression("hello, world!", zlib::DefaultCompression);
52
53  const size_t kSize = 1024;
54  char BinaryData[kSize];
55  for (size_t i = 0; i < kSize; ++i) {
56    BinaryData[i] = i & 255;
57  }
58  StringRef BinaryDataStr(BinaryData, kSize);
59
60  TestZlibCompression(BinaryDataStr, zlib::NoCompression);
61  TestZlibCompression(BinaryDataStr, zlib::BestSizeCompression);
62  TestZlibCompression(BinaryDataStr, zlib::BestSpeedCompression);
63  TestZlibCompression(BinaryDataStr, zlib::DefaultCompression);
64}
65
66#endif
67
68}
69