Home | History | Annotate | Download | only in Support
      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/SmallString.h"
     16 #include "llvm/ADT/StringRef.h"
     17 #include "llvm/Config/config.h"
     18 #include "gtest/gtest.h"
     19 
     20 using namespace llvm;
     21 
     22 namespace {
     23 
     24 #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
     25 
     26 void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) {
     27   SmallString<32> Compressed;
     28   SmallString<32> Uncompressed;
     29   EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level));
     30   // Check that uncompressed buffer is the same as original.
     31   EXPECT_EQ(zlib::StatusOK,
     32             zlib::uncompress(Compressed, Uncompressed, Input.size()));
     33   EXPECT_EQ(Input, Uncompressed);
     34   if (Input.size() > 0) {
     35     // Uncompression fails if expected length is too short.
     36     EXPECT_EQ(zlib::StatusBufferTooShort,
     37               zlib::uncompress(Compressed, Uncompressed, Input.size() - 1));
     38   }
     39 }
     40 
     41 TEST(CompressionTest, Zlib) {
     42   TestZlibCompression("", zlib::DefaultCompression);
     43 
     44   TestZlibCompression("hello, world!", zlib::NoCompression);
     45   TestZlibCompression("hello, world!", zlib::BestSizeCompression);
     46   TestZlibCompression("hello, world!", zlib::BestSpeedCompression);
     47   TestZlibCompression("hello, world!", zlib::DefaultCompression);
     48 
     49   const size_t kSize = 1024;
     50   char BinaryData[kSize];
     51   for (size_t i = 0; i < kSize; ++i) {
     52     BinaryData[i] = i & 255;
     53   }
     54   StringRef BinaryDataStr(BinaryData, kSize);
     55 
     56   TestZlibCompression(BinaryDataStr, zlib::NoCompression);
     57   TestZlibCompression(BinaryDataStr, zlib::BestSizeCompression);
     58   TestZlibCompression(BinaryDataStr, zlib::BestSpeedCompression);
     59   TestZlibCompression(BinaryDataStr, zlib::DefaultCompression);
     60 }
     61 
     62 TEST(CompressionTest, ZlibCRC32) {
     63   EXPECT_EQ(
     64       0x414FA339U,
     65       zlib::crc32(StringRef("The quick brown fox jumps over the lazy dog")));
     66 }
     67 
     68 #endif
     69 
     70 }
     71