proxygen
folly::io::test Namespace Reference

Classes

class  AutomaticCodecTest
 
class  CompressionCorruptionTest
 
class  CompressionTest
 
class  CompressionVarintTest
 
class  ConstantDataHolder
 
class  DataHolder
 
class  RandomDataHolder
 
class  StreamingCompressionTest
 
class  StreamingUnitTest
 
class  TerminalCodecTest
 

Functions

uint64_t hashIOBuf (const IOBuf *buf)
 
static std::vector< CodecTypesupportedCodecs (std::vector< CodecType > const &v)
 
static std::vector< CodecTypeavailableCodecs ()
 
static std::vector< CodecTypeavailableStreamCodecs ()
 
 TEST (CompressionTestNeedsUncompressedLength, Simple)
 
 TEST_P (CompressionTest, RandomData)
 
 TEST_P (CompressionTest, ConstantData)
 
 TEST_P (CompressionTest, RandomDataString)
 
 TEST_P (CompressionTest, ConstantDataString)
 
 INSTANTIATE_TEST_CASE_P (CompressionTest, CompressionTest, testing::Combine(testing::Values(-1, 0, 1, 12, 22, 25, 27), testing::Values(1, 2, 3, 8, 65), testing::ValuesIn(availableCodecs())))
 
uint64_t oneBasedMsbPos (uint64_t number)
 
 TEST_P (CompressionVarintTest, RandomData)
 
 TEST_P (CompressionVarintTest, ConstantData)
 
 INSTANTIATE_TEST_CASE_P (CompressionVarintTest, CompressionVarintTest, testing::Combine(testing::Values(0, 1, 12, 22, 25, 27), testing::ValuesIn(supportedCodecs({CodecType::LZ4_VARINT_SIZE, CodecType::LZMA2_VARINT_SIZE,}))))
 
 TEST (LZMATest, UncompressBadVarint)
 
 TEST_P (CompressionCorruptionTest, RandomData)
 
 TEST_P (CompressionCorruptionTest, ConstantData)
 
 INSTANTIATE_TEST_CASE_P (CompressionCorruptionTest, CompressionCorruptionTest, testing::ValuesIn(supportedCodecs({CodecType::SNAPPY, CodecType::ZLIB, CodecType::LZMA2, CodecType::ZSTD, CodecType::LZ4_FRAME, CodecType::BZIP2,})))
 
static bool codecHasFlush (CodecType type)
 
 TEST (StreamingUnitTest, needsDataLength)
 
 TEST_P (StreamingUnitTest, maxCompressedLength)
 
 TEST_P (StreamingUnitTest, getUncompressedLength)
 
 TEST_P (StreamingUnitTest, emptyData)
 
 TEST_P (StreamingUnitTest, noForwardProgress)
 
 TEST_P (StreamingUnitTest, stateTransitions)
 
 INSTANTIATE_TEST_CASE_P (StreamingUnitTest, StreamingUnitTest, testing::ValuesIn(availableStreamCodecs()))
 
static std::unique_ptr< IOBufcompressSome (StreamCodec *codec, ByteRange data, uint64_t bufferSize, StreamCodec::FlushOp flush)
 
static std::pair< bool, std::unique_ptr< IOBuf > > uncompressSome (StreamCodec *codec, ByteRange &data, uint64_t bufferSize, StreamCodec::FlushOp flush)
 
 TEST_P (StreamingCompressionTest, resetStream)
 
 TEST_P (StreamingCompressionTest, compressStream)
 
 TEST_P (StreamingCompressionTest, uncompressStream)
 
 TEST_P (StreamingCompressionTest, testFlush)
 
 INSTANTIATE_TEST_CASE_P (StreamingCompressionTest, StreamingCompressionTest, testing::Combine(testing::Values(0, 1, 12, 22, 27), testing::Values(12, 17, 20), testing::ValuesIn(availableStreamCodecs())))
 
 TEST_P (AutomaticCodecTest, RandomData)
 
 TEST_P (AutomaticCodecTest, ConstantData)
 
 TEST_P (AutomaticCodecTest, ValidPrefixes)
 
 TEST_P (AutomaticCodecTest, NeedsUncompressedLength)
 
 TEST_P (AutomaticCodecTest, maxUncompressedLength)
 
 TEST_P (AutomaticCodecTest, DefaultCodec)
 
 TEST_P (AutomaticCodecTest, CustomCodec)
 
 TEST_P (AutomaticCodecTest, CustomDefaultCodec)
 
 TEST_P (AutomaticCodecTest, canUncompressOneBytes)
 
 INSTANTIATE_TEST_CASE_P (AutomaticCodecTest, AutomaticCodecTest, testing::ValuesIn(availableCodecs()))
 
 TEST_P (TerminalCodecTest, uncompressIfDefaultThrows)
 
 TEST_P (TerminalCodecTest, terminalOverridesDefaults)
 
 INSTANTIATE_TEST_CASE_P (TerminalCodecTest, TerminalCodecTest, testing::ValuesIn(autoUncompressionCodecTypes))
 
 TEST (ValidPrefixesTest, CustomCodec)
 
 TEST (CheckCompatibleTest, SimplePrefixSecond)
 
 TEST (CheckCompatibleTest, SimplePrefixFirst)
 
 TEST (CheckCompatibleTest, Empty)
 
 TEST (CheckCompatibleTest, ZstdPrefix)
 
 TEST (CheckCompatibleTest, ZstdDuplicate)
 
 TEST (CheckCompatibleTest, ZlibIsPrefix)
 

Variables

constexpr size_t dataSizeLog2 = 27
 
RandomDataHolder randomDataHolder (dataSizeLog2)
 
ConstantDataHolder constantDataHolder (dataSizeLog2)
 

Function Documentation

static std::vector<CodecType> folly::io::test::availableCodecs ( )
static

Definition at line 144 of file CompressionTest.cpp.

References folly::io::hasCodec(), i, folly::io::NUM_CODEC_TYPES, and type.

Referenced by TEST_P().

144  {
145  std::vector<CodecType> codecs;
146 
147  for (size_t i = 0; i < static_cast<size_t>(CodecType::NUM_CODEC_TYPES); ++i) {
148  auto type = static_cast<CodecType>(i);
149  if (hasCodec(type)) {
150  codecs.push_back(type);
151  }
152  }
153 
154  return codecs;
155 }
bool hasCodec(CodecType type)
PskType type
static std::vector<CodecType> folly::io::test::availableStreamCodecs ( )
static

Definition at line 157 of file CompressionTest.cpp.

References folly::io::hasStreamCodec(), i, folly::io::NUM_CODEC_TYPES, and type.

Referenced by TEST_P().

157  {
158  std::vector<CodecType> codecs;
159 
160  for (size_t i = 0; i < static_cast<size_t>(CodecType::NUM_CODEC_TYPES); ++i) {
161  auto type = static_cast<CodecType>(i);
162  if (hasStreamCodec(type)) {
163  codecs.push_back(type);
164  }
165  }
166 
167  return codecs;
168 }
PskType type
bool hasStreamCodec(CodecType type)
static bool folly::io::test::codecHasFlush ( CodecType  type)
static

Definition at line 462 of file CompressionTest.cpp.

References folly::io::BZIP2.

Referenced by folly::io::test::StreamingUnitTest::hasFlush(), and folly::io::test::StreamingCompressionTest::hasFlush().

462  {
463  return type != CodecType::BZIP2;
464 }
PskType type
static std::unique_ptr<IOBuf> folly::io::test::compressSome ( StreamCodec codec,
ByteRange  data,
uint64_t  bufferSize,
StreamCodec::FlushOp  flush 
)
static

Definition at line 819 of file CompressionTest.cpp.

References folly::IOBufQueue::append(), buffer(), folly::io::StreamCodec::compressStream(), folly::IOBuf::create(), folly::Range< Iter >::empty(), EXPECT_TRUE, folly::IOBufQueue::move(), folly::gen::move, folly::io::StreamCodec::NONE, and gmock_output_test::output.

Referenced by folly::io::test::StreamingCompressionTest::runCompressStreamTest(), folly::io::test::StreamingCompressionTest::runFlushTest(), and folly::io::test::StreamingCompressionTest::runResetStreamTest().

823  {
824  bool result;
825  IOBufQueue queue;
826  do {
827  auto buffer = IOBuf::create(bufferSize);
828  buffer->append(buffer->capacity());
829  MutableByteRange output{buffer->writableData(), buffer->length()};
830 
831  result = codec->compressStream(data, output, flush);
832  buffer->trimEnd(output.size());
833  queue.append(std::move(buffer));
834 
835  } while (!(flush == StreamCodec::FlushOp::NONE && data.empty()) && !result);
836  EXPECT_TRUE(data.empty());
837  return queue.move();
838 }
std::vector< uint8_t > buffer(kBufferSize+16)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
CodecFactory codec
Range< unsigned char * > MutableByteRange
Definition: Range.h:1164
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
folly::io::test::INSTANTIATE_TEST_CASE_P ( CompressionTest  ,
CompressionTest  ,
testing::Combine(testing::Values(-1, 0, 1, 12, 22, 25, 27), testing::Values(1, 2, 3, 8, 65), testing::ValuesIn(availableCodecs()))   
)

Referenced by TEST(), and TEST_P().

folly::io::test::INSTANTIATE_TEST_CASE_P ( CompressionVarintTest  ,
CompressionVarintTest  ,
testing::Combine(testing::Values(0, 1, 12, 22, 25, 27), testing::ValuesIn(supportedCodecs({CodecType::LZ4_VARINT_SIZE, CodecType::LZMA2_VARINT_SIZE,})))   
)
folly::io::test::INSTANTIATE_TEST_CASE_P ( StreamingUnitTest  ,
StreamingUnitTest  ,
testing::ValuesIn(availableStreamCodecs())   
)
folly::io::test::INSTANTIATE_TEST_CASE_P ( StreamingCompressionTest  ,
StreamingCompressionTest  ,
testing::Combine(testing::Values(0, 1, 12, 22, 27), testing::Values(12, 17, 20), testing::ValuesIn(availableStreamCodecs()))   
)
folly::io::test::INSTANTIATE_TEST_CASE_P ( AutomaticCodecTest  ,
AutomaticCodecTest  ,
testing::ValuesIn(availableCodecs())   
)
folly::io::test::INSTANTIATE_TEST_CASE_P ( TerminalCodecTest  ,
TerminalCodecTest  ,
testing::ValuesIn(autoUncompressionCodecTypes)   
)
uint64_t folly::io::test::oneBasedMsbPos ( uint64_t  number)
inline

Definition at line 325 of file CompressionTest.cpp.

References uint64_t.

Referenced by folly::io::test::CompressionVarintTest::runSimpleTest().

325  {
326  uint64_t pos = 0;
327  for (; number > 0; ++pos, number >>= 1) {
328  }
329  return pos;
330 }
int number
static std::vector<CodecType> folly::io::test::supportedCodecs ( std::vector< CodecType > const &  v)
static

Definition at line 134 of file CompressionTest.cpp.

References folly::test::begin(), folly::test::end(), and folly::io::hasCodec().

Referenced by TEST_P().

134  {
135  std::vector<CodecType> supported;
136 
137  std::copy_if(
138  std::begin(v), std::end(v), std::back_inserter(supported), hasCodec);
139 
140  return supported;
141 }
bool hasCodec(CodecType type)
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
folly::io::test::TEST ( CompressionTestNeedsUncompressedLength  ,
Simple   
)

Definition at line 170 of file CompressionTest.cpp.

References folly::io::BZIP2, EXPECT_EQ, folly::io::getCodec(), folly::io::GZIP, folly::io::hasCodec(), folly::io::LZ4, folly::io::LZ4_FRAME, folly::io::LZ4_VARINT_SIZE, folly::io::LZMA2, folly::io::LZMA2_VARINT_SIZE, folly::io::NO_COMPRESSION, folly::io::SNAPPY, type, folly::io::ZLIB, and folly::io::ZSTD.

Referenced by TEST().

170  {
171  static const struct {
172  CodecType type;
173  bool needsUncompressedLength;
174  } expectations[] = {
175  {CodecType::NO_COMPRESSION, false},
176  {CodecType::LZ4, true},
177  {CodecType::SNAPPY, false},
178  {CodecType::ZLIB, false},
179  {CodecType::LZ4_VARINT_SIZE, false},
180  {CodecType::LZMA2, false},
181  {CodecType::LZMA2_VARINT_SIZE, false},
182  {CodecType::ZSTD, false},
183  {CodecType::GZIP, false},
184  {CodecType::LZ4_FRAME, false},
185  {CodecType::BZIP2, false},
186  };
187 
188  for (auto const& test : expectations) {
189  if (hasCodec(test.type)) {
190  EXPECT_EQ(
191  getCodec(test.type)->needsUncompressedLength(),
192  test.needsUncompressedLength);
193  }
194  }
195 }
bool hasCodec(CodecType type)
PskType type
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::unique_ptr< Codec > getCodec(CodecType type, int level)
folly::io::test::TEST ( LZMATest  ,
UncompressBadVarint   
)

Definition at line 369 of file CompressionTest.cpp.

References buffer(), codec, folly::IOBuf::create(), EXPECT_THROW, folly::io::getStreamCodec(), folly::io::hasStreamCodec(), folly::kMaxVarintLength64, folly::io::LZMA2_VARINT_SIZE, gmock_output_test::output, and string.

369  {
370  if (hasStreamCodec(CodecType::LZMA2_VARINT_SIZE)) {
371  std::string const str(kMaxVarintLength64 * 2, '\xff');
372  ByteRange input((folly::StringPiece(str)));
373  auto codec = getStreamCodec(CodecType::LZMA2_VARINT_SIZE);
374  auto buffer = IOBuf::create(16);
375  buffer->append(buffer->capacity());
376  MutableByteRange output{buffer->writableData(), buffer->length()};
377  EXPECT_THROW(codec->uncompressStream(input, output), std::runtime_error);
378  }
379 }
std::vector< uint8_t > buffer(kBufferSize+16)
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
bool hasStreamCodec(CodecType type)
constexpr size_t kMaxVarintLength64
Definition: Varint.h:50
CodecFactory codec
Range< unsigned char * > MutableByteRange
Definition: Range.h:1164
std::unique_ptr< StreamCodec > getStreamCodec(CodecType type, int level)
const char * string
Definition: Conv.cpp:212
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
folly::io::test::TEST ( StreamingUnitTest  ,
needsDataLength   
)

Definition at line 479 of file CompressionTest.cpp.

References EXPECT_EQ, folly::io::getStreamCodec(), folly::io::GZIP, folly::io::hasStreamCodec(), folly::io::LZMA2, folly::io::LZMA2_VARINT_SIZE, type, folly::io::ZLIB, and folly::io::ZSTD.

479  {
480  static const struct {
481  CodecType type;
482  bool needsDataLength;
483  } expectations[] = {
484  {CodecType::ZLIB, false},
485  {CodecType::GZIP, false},
486  {CodecType::LZMA2, false},
487  {CodecType::LZMA2_VARINT_SIZE, true},
488  {CodecType::ZSTD, false},
489  };
490 
491  for (auto const& test : expectations) {
492  if (hasStreamCodec(test.type)) {
493  EXPECT_EQ(
494  getStreamCodec(test.type)->needsDataLength(), test.needsDataLength);
495  }
496  }
497 }
PskType type
bool hasStreamCodec(CodecType type)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::unique_ptr< StreamCodec > getStreamCodec(CodecType type, int level)
folly::io::test::TEST ( ValidPrefixesTest  ,
CustomCodec   
)

Definition at line 1325 of file CompressionTest.cpp.

References EXPECT_TRUE, folly::io::getAutoUncompressionCodec(), folly::gen::move, folly::io::NO_COMPRESSION, and folly::none.

1325  {
1326  std::vector<std::unique_ptr<Codec>> codecs;
1327  codecs.push_back(CustomCodec::create("none", CodecType::NO_COMPRESSION));
1328  const auto none = getAutoUncompressionCodec(std::move(codecs));
1329  const auto prefixes = none->validPrefixes();
1330  const auto it = std::find(prefixes.begin(), prefixes.end(), "none");
1331  EXPECT_TRUE(it != prefixes.end());
1332 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::unique_ptr< Codec > getAutoUncompressionCodec(std::vector< std::unique_ptr< Codec >> customCodecs, std::unique_ptr< Codec > terminalCodec)
constexpr None none
Definition: Optional.h:87
folly::io::test::TEST ( CheckCompatibleTest  ,
SimplePrefixSecond   
)

Definition at line 1343 of file CompressionTest.cpp.

References EXPECT_THROW_IF_DEBUG, folly::io::getAutoUncompressionCodec(), folly::gen::move, and folly::io::NO_COMPRESSION.

1343  {
1344  std::vector<std::unique_ptr<Codec>> codecs;
1345  codecs.push_back(CustomCodec::create("abc", CodecType::NO_COMPRESSION));
1346  codecs.push_back(CustomCodec::create("ab", CodecType::NO_COMPRESSION));
1348  getAutoUncompressionCodec(std::move(codecs)), std::invalid_argument);
1349 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_THROW_IF_DEBUG(statement, expected_exception)
std::unique_ptr< Codec > getAutoUncompressionCodec(std::vector< std::unique_ptr< Codec >> customCodecs, std::unique_ptr< Codec > terminalCodec)
folly::io::test::TEST ( CheckCompatibleTest  ,
SimplePrefixFirst   
)

Definition at line 1351 of file CompressionTest.cpp.

References EXPECT_THROW_IF_DEBUG, folly::io::getAutoUncompressionCodec(), folly::gen::move, and folly::io::NO_COMPRESSION.

1351  {
1352  std::vector<std::unique_ptr<Codec>> codecs;
1353  codecs.push_back(CustomCodec::create("ab", CodecType::NO_COMPRESSION));
1354  codecs.push_back(CustomCodec::create("abc", CodecType::NO_COMPRESSION));
1356  getAutoUncompressionCodec(std::move(codecs)), std::invalid_argument);
1357 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_THROW_IF_DEBUG(statement, expected_exception)
std::unique_ptr< Codec > getAutoUncompressionCodec(std::vector< std::unique_ptr< Codec >> customCodecs, std::unique_ptr< Codec > terminalCodec)
folly::io::test::TEST ( CheckCompatibleTest  ,
Empty   
)

Definition at line 1359 of file CompressionTest.cpp.

References EXPECT_THROW_IF_DEBUG, folly::io::getAutoUncompressionCodec(), folly::gen::move, and folly::io::NO_COMPRESSION.

1359  {
1360  std::vector<std::unique_ptr<Codec>> codecs;
1361  codecs.push_back(CustomCodec::create("", CodecType::NO_COMPRESSION));
1363  getAutoUncompressionCodec(std::move(codecs)), std::invalid_argument);
1364 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_THROW_IF_DEBUG(statement, expected_exception)
std::unique_ptr< Codec > getAutoUncompressionCodec(std::vector< std::unique_ptr< Codec >> customCodecs, std::unique_ptr< Codec > terminalCodec)
folly::io::test::TEST ( CheckCompatibleTest  ,
ZstdPrefix   
)

Definition at line 1366 of file CompressionTest.cpp.

References EXPECT_THROW_IF_DEBUG, folly::io::getAutoUncompressionCodec(), folly::gen::move, and folly::io::ZSTD.

1366  {
1367  std::vector<std::unique_ptr<Codec>> codecs;
1368  codecs.push_back(CustomCodec::create("\x28\xB5\x2F", CodecType::ZSTD));
1370  getAutoUncompressionCodec(std::move(codecs)), std::invalid_argument);
1371 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_THROW_IF_DEBUG(statement, expected_exception)
std::unique_ptr< Codec > getAutoUncompressionCodec(std::vector< std::unique_ptr< Codec >> customCodecs, std::unique_ptr< Codec > terminalCodec)
folly::io::test::TEST ( CheckCompatibleTest  ,
ZstdDuplicate   
)

Definition at line 1373 of file CompressionTest.cpp.

References EXPECT_THROW_IF_DEBUG, folly::io::getAutoUncompressionCodec(), folly::gen::move, and folly::io::ZSTD.

1373  {
1374  std::vector<std::unique_ptr<Codec>> codecs;
1375  codecs.push_back(CustomCodec::create("\x28\xB5\x2F\xFD", CodecType::ZSTD));
1377  getAutoUncompressionCodec(std::move(codecs)), std::invalid_argument);
1378 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_THROW_IF_DEBUG(statement, expected_exception)
std::unique_ptr< Codec > getAutoUncompressionCodec(std::vector< std::unique_ptr< Codec >> customCodecs, std::unique_ptr< Codec > terminalCodec)
folly::io::test::TEST ( CheckCompatibleTest  ,
ZlibIsPrefix   
)

Definition at line 1380 of file CompressionTest.cpp.

References codec, codec_, constantDataHolder, folly::io::test::DataHolder::data(), folly::Range< Iter >::data(), EXPECT_EQ, EXPECT_GT, EXPECT_THROW_IF_DEBUG, folly::io::getAutoUncompressionCodec(), folly::io::getCodec(), folly::io::getStreamCodec(), folly::io::GZIP, INSTANTIATE_TEST_CASE_P(), folly::gen::move, randomDataHolder, string, TEST(), test(), TEST_P(), folly::IOBuf::wrapBuffer(), folly::io::ZLIB, and folly::io::ZSTD.

1380  {
1381  std::vector<std::unique_ptr<Codec>> codecs;
1382  codecs.push_back(CustomCodec::create("\x18\x76zzasdf", CodecType::ZSTD));
1384  getAutoUncompressionCodec(std::move(codecs)), std::invalid_argument);
1385 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_THROW_IF_DEBUG(statement, expected_exception)
std::unique_ptr< Codec > getAutoUncompressionCodec(std::vector< std::unique_ptr< Codec >> customCodecs, std::unique_ptr< Codec > terminalCodec)
folly::io::test::TEST_P ( CompressionTest  ,
RandomData   
)

Definition at line 286 of file CompressionTest.cpp.

References randomDataHolder.

Referenced by TEST().

286  {
287  runSimpleIOBufTest(randomDataHolder);
288 }
RandomDataHolder randomDataHolder(dataSizeLog2)
folly::io::test::TEST_P ( CompressionTest  ,
ConstantData   
)

Definition at line 290 of file CompressionTest.cpp.

References constantDataHolder.

290  {
291  runSimpleIOBufTest(constantDataHolder);
292 }
ConstantDataHolder constantDataHolder(dataSizeLog2)
folly::io::test::TEST_P ( CompressionTest  ,
RandomDataString   
)

Definition at line 294 of file CompressionTest.cpp.

References randomDataHolder.

294  {
295  runSimpleStringTest(randomDataHolder);
296 }
RandomDataHolder randomDataHolder(dataSizeLog2)
folly::io::test::TEST_P ( CompressionTest  ,
ConstantDataString   
)

Definition at line 298 of file CompressionTest.cpp.

References availableCodecs(), constantDataHolder, and INSTANTIATE_TEST_CASE_P().

298  {
299  runSimpleStringTest(constantDataHolder);
300 }
ConstantDataHolder constantDataHolder(dataSizeLog2)
folly::io::test::TEST_P ( CompressionVarintTest  ,
RandomData   
)

Definition at line 351 of file CompressionTest.cpp.

References randomDataHolder.

351  {
352  runSimpleTest(randomDataHolder);
353 }
RandomDataHolder randomDataHolder(dataSizeLog2)
folly::io::test::TEST_P ( CompressionVarintTest  ,
ConstantData   
)

Definition at line 355 of file CompressionTest.cpp.

References constantDataHolder, INSTANTIATE_TEST_CASE_P(), folly::io::LZ4_VARINT_SIZE, folly::io::LZMA2_VARINT_SIZE, and supportedCodecs().

355  {
356  runSimpleTest(constantDataHolder);
357 }
ConstantDataHolder constantDataHolder(dataSizeLog2)
folly::io::test::TEST_P ( CompressionCorruptionTest  ,
RandomData   
)

Definition at line 439 of file CompressionTest.cpp.

References randomDataHolder.

439  {
440  runSimpleTest(randomDataHolder);
441 }
RandomDataHolder randomDataHolder(dataSizeLog2)
folly::io::test::TEST_P ( CompressionCorruptionTest  ,
ConstantData   
)
folly::io::test::TEST_P ( StreamingUnitTest  ,
maxCompressedLength   
)

Definition at line 499 of file CompressionTest.cpp.

References codec_, EXPECT_GE, and uint64_t.

499  {
500  for (uint64_t const length : {1, 10, 100, 1000, 10000, 100000, 1000000}) {
501  EXPECT_GE(codec_->maxCompressedLength(length), length);
502  }
503 }
std::unique_ptr< Codec > codec_
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
folly::io::test::TEST_P ( StreamingUnitTest  ,
getUncompressedLength   
)

Definition at line 505 of file CompressionTest.cpp.

References codec_, folly::IOBuf::create(), folly::io::test::DataHolder::data(), folly::empty(), EXPECT_ANY_THROW, EXPECT_EQ, randomDataHolder, uint64_t, and folly::IOBuf::wrapBuffer().

505  {
506  auto const empty = IOBuf::create(0);
507  EXPECT_EQ(uint64_t(0), codec_->getUncompressedLength(empty.get()));
508  EXPECT_EQ(uint64_t(0), codec_->getUncompressedLength(empty.get(), 0));
509  EXPECT_ANY_THROW(codec_->getUncompressedLength(empty.get(), 1));
510 
511  auto const data = IOBuf::wrapBuffer(randomDataHolder.data(100));
512  auto const compressed = codec_->compress(data.get());
513 
514  if (auto const length = codec_->getUncompressedLength(data.get())) {
515  EXPECT_EQ(100, *length);
516  }
517  EXPECT_EQ(uint64_t(100), codec_->getUncompressedLength(data.get(), 100));
518  // If the uncompressed length is stored in the frame, then make sure it throws
519  // when it is given the wrong length.
520  if (codec_->getUncompressedLength(data.get()) == uint64_t(100)) {
521  EXPECT_ANY_THROW(codec_->getUncompressedLength(data.get(), 200));
522  }
523 }
#define EXPECT_ANY_THROW(statement)
Definition: gtest.h:1847
RandomDataHolder randomDataHolder(dataSizeLog2)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::unique_ptr< Codec > codec_
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
ByteRange data(size_t size) const
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
folly::io::test::TEST_P ( StreamingUnitTest  ,
emptyData   
)

Definition at line 525 of file CompressionTest.cpp.

References buffer(), codec_, folly::IOBuf::copyBuffer(), folly::IOBuf::create(), folly::io::StreamCodec::END, EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, folly::io::StreamCodec::FLUSH, gmock_output_test::output, and folly::Range< Iter >::size().

525  {
526  ByteRange input{};
527  auto buffer = IOBuf::create(codec_->maxCompressedLength(0));
528  buffer->append(buffer->capacity());
530 
531  // Test compressing empty data in one pass
532  if (!codec_->needsDataLength()) {
533  output = {buffer->writableData(), buffer->length()};
534  EXPECT_TRUE(
535  codec_->compressStream(input, output, StreamCodec::FlushOp::END));
536  }
537  codec_->resetStream(0);
538  output = {buffer->writableData(), buffer->length()};
539  EXPECT_TRUE(codec_->compressStream(input, output, StreamCodec::FlushOp::END));
540 
541  // Test uncompressing the compressed empty data is equivalent to the empty
542  // string
543  {
544  size_t compressedSize = buffer->length() - output.size();
545  auto const compressed =
546  IOBuf::copyBuffer(buffer->writableData(), compressedSize);
547  auto inputRange = compressed->coalesce();
548  codec_->resetStream(0);
549  output = {buffer->writableData(), buffer->length()};
550  EXPECT_TRUE(codec_->uncompressStream(
551  inputRange, output, StreamCodec::FlushOp::END));
552  EXPECT_EQ(output.size(), buffer->length());
553  }
554 
555  // Test compressing empty data with multiple calls to compressStream()
556  {
557  auto largeBuffer = IOBuf::create(codec_->maxCompressedLength(0) * 2);
558  largeBuffer->append(largeBuffer->capacity());
559  codec_->resetStream(0);
560  output = {largeBuffer->writableData(), largeBuffer->length()};
561  EXPECT_FALSE(codec_->compressStream(input, output));
562  if (hasFlush()) {
563  EXPECT_TRUE(
564  codec_->compressStream(input, output, StreamCodec::FlushOp::FLUSH));
565  }
566  EXPECT_TRUE(
567  codec_->compressStream(input, output, StreamCodec::FlushOp::END));
568  }
569 
570  // Test uncompressing empty data
571  output = {};
572  codec_->resetStream();
573  EXPECT_TRUE(codec_->uncompressStream(input, output));
574  if (hasFlush()) {
575  codec_->resetStream();
576  EXPECT_TRUE(
577  codec_->uncompressStream(input, output, StreamCodec::FlushOp::FLUSH));
578  }
579  codec_->resetStream();
580  EXPECT_TRUE(
581  codec_->uncompressStream(input, output, StreamCodec::FlushOp::END));
582  codec_->resetStream(0);
583  EXPECT_TRUE(codec_->uncompressStream(input, output));
584  if (hasFlush()) {
585  codec_->resetStream(0);
586  EXPECT_TRUE(
587  codec_->uncompressStream(input, output, StreamCodec::FlushOp::FLUSH));
588  }
589  codec_->resetStream(0);
590  EXPECT_TRUE(
591  codec_->uncompressStream(input, output, StreamCodec::FlushOp::END));
592 }
std::vector< uint8_t > buffer(kBufferSize+16)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::unique_ptr< Codec > codec_
Range< unsigned char * > MutableByteRange
Definition: Range.h:1164
std::unique_ptr< IOBuf > copyBuffer(const folly::IOBuf &buf)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
folly::io::test::TEST_P ( StreamingUnitTest  ,
noForwardProgress   
)

Definition at line 594 of file CompressionTest.cpp.

References codec_, folly::IOBuf::create(), folly::io::StreamCodec::END, EXPECT_FALSE, EXPECT_THROW, folly::io::StreamCodec::FLUSH, folly::io::StreamCodec::NONE, and gmock_output_test::output.

594  {
595  auto inBuffer = IOBuf::create(2);
596  inBuffer->writableData()[0] = 'a';
597  inBuffer->writableData()[1] = 'a';
598  inBuffer->append(2);
599  const auto compressed = codec_->compress(inBuffer.get());
600  auto outBuffer = IOBuf::create(codec_->maxCompressedLength(2));
601 
602  ByteRange emptyInput;
603  MutableByteRange emptyOutput;
604 
605  const std::array<StreamCodec::FlushOp, 3> flushOps = {{
606  StreamCodec::FlushOp::NONE,
607  StreamCodec::FlushOp::FLUSH,
608  StreamCodec::FlushOp::END,
609  }};
610 
611  // No progress is not okay twice in a row for all flush operations when
612  // compressing
613  for (const auto flushOp : flushOps) {
614  if (flushOp == StreamCodec::FlushOp::FLUSH && !hasFlush()) {
615  continue;
616  }
617  if (codec_->needsDataLength()) {
618  codec_->resetStream(inBuffer->computeChainDataLength());
619  } else {
620  codec_->resetStream();
621  }
622  auto input = inBuffer->coalesce();
623  MutableByteRange output = {outBuffer->writableTail(),
624  outBuffer->tailroom()};
625  // Compress some data to avoid empty data special casing
626  while (!input.empty()) {
627  codec_->compressStream(input, output);
628  }
629  EXPECT_FALSE(codec_->compressStream(emptyInput, emptyOutput, flushOp));
630  EXPECT_THROW(
631  codec_->compressStream(emptyInput, emptyOutput, flushOp),
632  std::runtime_error);
633  }
634 
635  // No progress is not okay twice in a row for all flush operations when
636  // uncompressing
637  for (const auto flushOp : flushOps) {
638  if (flushOp == StreamCodec::FlushOp::FLUSH && !hasFlush()) {
639  continue;
640  }
641  codec_->resetStream();
642  auto input = compressed->coalesce();
643  // Remove the last byte so the operation is incomplete
644  input.uncheckedSubtract(1);
645  MutableByteRange output = {inBuffer->writableData(), inBuffer->length()};
646  // Uncompress some data to avoid empty data special casing
647  while (!input.empty()) {
648  EXPECT_FALSE(codec_->uncompressStream(input, output));
649  }
650  EXPECT_FALSE(codec_->uncompressStream(emptyInput, emptyOutput, flushOp));
651  EXPECT_THROW(
652  codec_->uncompressStream(emptyInput, emptyOutput, flushOp),
653  std::runtime_error);
654  }
655 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
std::unique_ptr< Codec > codec_
Range< unsigned char * > MutableByteRange
Definition: Range.h:1164
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
folly::io::test::TEST_P ( StreamingUnitTest  ,
stateTransitions   
)

Definition at line 657 of file CompressionTest.cpp.

References availableStreamCodecs(), codec_, folly::IOBuf::create(), folly::empty(), folly::io::StreamCodec::END, folly::symbolizer::test::expect(), EXPECT_FALSE, EXPECT_THROW, EXPECT_TRUE, folly::io::StreamCodec::FLUSH, INSTANTIATE_TEST_CASE_P(), folly::io::StreamCodec::NONE, gmock_output_test::output, and folly::Range< Iter >::size().

657  {
658  auto inBuffer = IOBuf::create(2);
659  inBuffer->writableData()[0] = 'a';
660  inBuffer->writableData()[1] = 'a';
661  inBuffer->append(2);
662  auto compressed = codec_->compress(inBuffer.get());
663  ByteRange const in = compressed->coalesce();
664  auto outBuffer = IOBuf::create(codec_->maxCompressedLength(in.size()));
665  MutableByteRange const out{outBuffer->writableTail(), outBuffer->tailroom()};
666 
667  auto compress = [&](StreamCodec::FlushOp flushOp = StreamCodec::FlushOp::NONE,
668  bool empty = false) {
669  auto input = in;
670  auto output = empty ? MutableByteRange{} : out;
671  return codec_->compressStream(input, output, flushOp);
672  };
673  auto compress_all = [&](bool expect,
674  StreamCodec::FlushOp flushOp =
675  StreamCodec::FlushOp::NONE,
676  bool empty = false) {
677  auto input = in;
678  auto output = empty ? MutableByteRange{} : out;
679  while (!input.empty()) {
680  if (expect) {
681  EXPECT_TRUE(codec_->compressStream(input, output, flushOp));
682  } else {
683  EXPECT_FALSE(codec_->compressStream(input, output, flushOp));
684  }
685  }
686  };
687  auto uncompress = [&](StreamCodec::FlushOp flushOp =
688  StreamCodec::FlushOp::NONE,
689  bool empty = false) {
690  auto input = in;
691  auto output = empty ? MutableByteRange{} : out;
692  return codec_->uncompressStream(input, output, flushOp);
693  };
694 
695  // compression flow
696  if (!codec_->needsDataLength()) {
697  codec_->resetStream();
698  EXPECT_FALSE(compress());
699  EXPECT_FALSE(compress());
700  if (hasFlush()) {
701  EXPECT_TRUE(compress(StreamCodec::FlushOp::FLUSH));
702  }
703  EXPECT_FALSE(compress());
704  EXPECT_TRUE(compress(StreamCodec::FlushOp::END));
705  }
706  codec_->resetStream(in.size() * 5);
707  compress_all(false);
708  compress_all(false);
709  if (hasFlush()) {
710  compress_all(true, StreamCodec::FlushOp::FLUSH);
711  }
712  compress_all(false);
713  compress_all(true, StreamCodec::FlushOp::END);
714 
715  // uncompression flow
716  codec_->resetStream();
717  EXPECT_FALSE(uncompress(StreamCodec::FlushOp::NONE, true));
718  if (hasFlush()) {
719  codec_->resetStream();
720  EXPECT_FALSE(uncompress(StreamCodec::FlushOp::FLUSH, true));
721  }
722  codec_->resetStream();
723  EXPECT_FALSE(uncompress(StreamCodec::FlushOp::NONE, true));
724  codec_->resetStream();
725  EXPECT_FALSE(uncompress(StreamCodec::FlushOp::NONE, true));
726  if (hasFlush()) {
727  codec_->resetStream();
728  EXPECT_TRUE(uncompress(StreamCodec::FlushOp::FLUSH));
729  }
730  // compress -> uncompress
731  codec_->resetStream(in.size());
732  EXPECT_FALSE(compress());
733  EXPECT_THROW(uncompress(), std::logic_error);
734  // uncompress -> compress
735  codec_->resetStream(inBuffer->computeChainDataLength());
736  EXPECT_TRUE(uncompress(StreamCodec::FlushOp::NONE));
737  EXPECT_THROW(compress(), std::logic_error);
738  // end -> compress
739  if (!codec_->needsDataLength()) {
740  codec_->resetStream();
741  EXPECT_FALSE(compress());
742  EXPECT_TRUE(compress(StreamCodec::FlushOp::END));
743  EXPECT_THROW(compress(), std::logic_error);
744  }
745  codec_->resetStream(in.size() * 2);
746  compress_all(false);
747  compress_all(true, StreamCodec::FlushOp::END);
748  EXPECT_THROW(compress(), std::logic_error);
749  // end -> uncompress
750  codec_->resetStream();
751  EXPECT_TRUE(uncompress(StreamCodec::FlushOp::END));
752  EXPECT_THROW(uncompress(), std::logic_error);
753  // flush -> compress
754  if (hasFlush()) {
755  codec_->resetStream(in.size());
756  EXPECT_FALSE(compress(StreamCodec::FlushOp::FLUSH, true));
757  EXPECT_THROW(compress(), std::logic_error);
758  }
759  // flush -> end
760  if (hasFlush()) {
761  codec_->resetStream(in.size());
762  EXPECT_FALSE(compress(StreamCodec::FlushOp::FLUSH, true));
763  EXPECT_THROW(compress(StreamCodec::FlushOp::END), std::logic_error);
764  }
765  // undefined -> compress
766  codec_->compress(inBuffer.get());
767  EXPECT_THROW(compress(), std::logic_error);
768  codec_->uncompress(compressed.get(), inBuffer->computeChainDataLength());
769  EXPECT_THROW(compress(), std::logic_error);
770  // undefined -> undefined
771  codec_->uncompress(compressed.get());
772  codec_->compress(inBuffer.get());
773 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
std::unique_ptr< Codec > codec_
Range< unsigned char * > MutableByteRange
Definition: Range.h:1164
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
void expect(LineReader &lr, const char *expected)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
folly::io::test::TEST_P ( StreamingCompressionTest  ,
resetStream   
)

Definition at line 877 of file CompressionTest.cpp.

References constantDataHolder, and randomDataHolder.

877  {
878  runResetStreamTest(constantDataHolder);
879  runResetStreamTest(randomDataHolder);
880 }
RandomDataHolder randomDataHolder(dataSizeLog2)
ConstantDataHolder constantDataHolder(dataSizeLog2)
folly::io::test::TEST_P ( StreamingCompressionTest  ,
compressStream   
)

Definition at line 902 of file CompressionTest.cpp.

References constantDataHolder, and randomDataHolder.

902  {
903  runCompressStreamTest(constantDataHolder);
904  runCompressStreamTest(randomDataHolder);
905 }
RandomDataHolder randomDataHolder(dataSizeLog2)
ConstantDataHolder constantDataHolder(dataSizeLog2)
folly::io::test::TEST_P ( StreamingCompressionTest  ,
uncompressStream   
)

Definition at line 943 of file CompressionTest.cpp.

References constantDataHolder, and randomDataHolder.

943  {
944  runUncompressStreamTest(constantDataHolder);
945  runUncompressStreamTest(randomDataHolder);
946 }
RandomDataHolder randomDataHolder(dataSizeLog2)
ConstantDataHolder constantDataHolder(dataSizeLog2)
folly::io::test::TEST_P ( StreamingCompressionTest  ,
testFlush   
)

Definition at line 979 of file CompressionTest.cpp.

References availableStreamCodecs(), folly::io::BZIP2, constantDataHolder, folly::io::GZIP, INSTANTIATE_TEST_CASE_P(), folly::io::LZ4_FRAME, folly::io::LZMA2, randomDataHolder, folly::io::ZLIB, and folly::io::ZSTD.

979  {
980  if (!hasFlush()) {
981  return;
982  }
983  runFlushTest(constantDataHolder);
984  runFlushTest(randomDataHolder);
985 }
RandomDataHolder randomDataHolder(dataSizeLog2)
ConstantDataHolder constantDataHolder(dataSizeLog2)
folly::io::test::TEST_P ( AutomaticCodecTest  ,
RandomData   
)

Definition at line 1065 of file CompressionTest.cpp.

References randomDataHolder.

1065  {
1066  runSimpleTest(randomDataHolder);
1067 }
RandomDataHolder randomDataHolder(dataSizeLog2)
folly::io::test::TEST_P ( AutomaticCodecTest  ,
ConstantData   
)

Definition at line 1069 of file CompressionTest.cpp.

References constantDataHolder.

1069  {
1070  runSimpleTest(constantDataHolder);
1071 }
ConstantDataHolder constantDataHolder(dataSizeLog2)
folly::io::test::TEST_P ( AutomaticCodecTest  ,
ValidPrefixes   
)

Definition at line 1073 of file CompressionTest.cpp.

References codec_, folly::IOBuf::COPY_BUFFER, folly::io::test::DataHolder::data(), EXPECT_FALSE, EXPECT_TRUE, and prefix().

1073  {
1074  const auto prefixes = codec_->validPrefixes();
1075  for (const auto& prefix : prefixes) {
1076  EXPECT_FALSE(prefix.empty());
1077  // Ensure that all strings are at least 8 bytes for LZMA2.
1078  // The bytes after the prefix should be ignored by `canUncompress()`.
1079  IOBuf data{IOBuf::COPY_BUFFER, prefix, 0, 8};
1080  data.append(8);
1081  EXPECT_TRUE(codec_->canUncompress(&data));
1082  EXPECT_TRUE(auto_->canUncompress(&data));
1083  }
1084 }
std::unique_ptr< Codec > codec_
bool prefix(Cursor &c, uint32_t expected)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
folly::io::test::TEST_P ( AutomaticCodecTest  ,
NeedsUncompressedLength   
)

Definition at line 1086 of file CompressionTest.cpp.

References codec_, and EXPECT_TRUE.

1086  {
1087  if (codec_->needsUncompressedLength()) {
1088  EXPECT_TRUE(auto_->needsUncompressedLength());
1089  }
1090 }
std::unique_ptr< Codec > codec_
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
folly::io::test::TEST_P ( AutomaticCodecTest  ,
maxUncompressedLength   
)

Definition at line 1092 of file CompressionTest.cpp.

References codec_, and EXPECT_LE.

1092  {
1093  EXPECT_LE(codec_->maxUncompressedLength(), auto_->maxUncompressedLength());
1094 }
#define EXPECT_LE(val1, val2)
Definition: gtest.h:1928
std::unique_ptr< Codec > codec_
folly::io::test::TEST_P ( AutomaticCodecTest  ,
DefaultCodec   
)

Definition at line 1096 of file CompressionTest.cpp.

References folly::IOBuf::cloneCoalescedAsValue(), codec_, folly::IOBuf::computeChainDataLength(), constantDataHolder, folly::IOBuf::copyBuffer(), folly::io::test::DataHolder::data(), EXPECT_EQ, EXPECT_TRUE, folly::io::getAutoUncompressionCodec(), folly::io::getCodec(), folly::io::test::DataHolder::hash(), hashIOBuf(), folly::gen::move, prefix(), prefix_, string, folly::IOBuf::trimStart(), type, uint64_t, folly::io::USER_DEFINED, folly::IOBuf::wrapBuffer(), and folly::io::ZSTD.

1096  {
1097  const uint64_t length = 42;
1098  std::vector<std::unique_ptr<Codec>> codecs;
1099  codecs.push_back(getCodec(CodecType::ZSTD));
1100  auto automatic =
1101  getAutoUncompressionCodec(std::move(codecs), getTerminalCodec());
1102  auto original = IOBuf::wrapBuffer(constantDataHolder.data(length));
1103  auto compressed = codec_->compress(original.get());
1104  std::unique_ptr<IOBuf> decompressed;
1105 
1106  if (automatic->needsUncompressedLength()) {
1107  decompressed = automatic->uncompress(compressed.get(), length);
1108  } else {
1109  decompressed = automatic->uncompress(compressed.get());
1110  }
1111 
1112  EXPECT_EQ(constantDataHolder.hash(length), hashIOBuf(decompressed.get()));
1113 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< Codec > codec_
uint64_t hashIOBuf(const IOBuf *buf)
ConstantDataHolder constantDataHolder(dataSizeLog2)
std::unique_ptr< Codec > getAutoUncompressionCodec(std::vector< std::unique_ptr< Codec >> customCodecs, std::unique_ptr< Codec > terminalCodec)
ByteRange data(size_t size) const
std::unique_ptr< Codec > getCodec(CodecType type, int level)
uint64_t hash(size_t size) const
folly::io::test::TEST_P ( AutomaticCodecTest  ,
CustomCodec   
)

Definition at line 1164 of file CompressionTest.cpp.

References codec_, constantDataHolder, folly::io::test::DataHolder::data(), EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, folly::io::getAutoUncompressionCodec(), folly::io::test::DataHolder::hash(), hashIOBuf(), folly::gen::move, uint64_t, folly::IOBuf::wrapBuffer(), and folly::io::ZSTD.

1164  {
1165  const uint64_t length = 42;
1166  auto ab = CustomCodec::create("ab", CodecType::ZSTD);
1167  std::vector<std::unique_ptr<Codec>> codecs;
1168  codecs.push_back(CustomCodec::create("ab", CodecType::ZSTD));
1169  auto automatic =
1170  getAutoUncompressionCodec(std::move(codecs), getTerminalCodec());
1171  auto original = IOBuf::wrapBuffer(constantDataHolder.data(length));
1172 
1173  auto abCompressed = ab->compress(original.get());
1174  std::unique_ptr<IOBuf> abDecompressed;
1175  if (automatic->needsUncompressedLength()) {
1176  abDecompressed = automatic->uncompress(abCompressed.get(), length);
1177  } else {
1178  abDecompressed = automatic->uncompress(abCompressed.get());
1179  }
1180  EXPECT_TRUE(automatic->canUncompress(abCompressed.get()));
1181  EXPECT_FALSE(auto_->canUncompress(abCompressed.get()));
1182  EXPECT_EQ(constantDataHolder.hash(length), hashIOBuf(abDecompressed.get()));
1183 
1184  auto compressed = codec_->compress(original.get());
1185  std::unique_ptr<IOBuf> decompressed;
1186  if (automatic->needsUncompressedLength()) {
1187  decompressed = automatic->uncompress(compressed.get(), length);
1188  } else {
1189  decompressed = automatic->uncompress(compressed.get());
1190  }
1191  EXPECT_EQ(constantDataHolder.hash(length), hashIOBuf(decompressed.get()));
1192 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< Codec > codec_
uint64_t hashIOBuf(const IOBuf *buf)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
ConstantDataHolder constantDataHolder(dataSizeLog2)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
std::unique_ptr< Codec > getAutoUncompressionCodec(std::vector< std::unique_ptr< Codec >> customCodecs, std::unique_ptr< Codec > terminalCodec)
ByteRange data(size_t size) const
uint64_t hash(size_t size) const
folly::io::test::TEST_P ( AutomaticCodecTest  ,
CustomDefaultCodec   
)

Definition at line 1194 of file CompressionTest.cpp.

References codec_, constantDataHolder, folly::io::test::DataHolder::data(), EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, folly::io::getAutoUncompressionCodec(), folly::io::getCodec(), folly::io::test::DataHolder::hash(), hashIOBuf(), folly::io::LZ4_FRAME, folly::gen::move, folly::io::NO_COMPRESSION, folly::none, uint64_t, and folly::IOBuf::wrapBuffer().

1194  {
1195  const uint64_t length = 42;
1196  auto none = CustomCodec::create("none", CodecType::NO_COMPRESSION);
1197  std::vector<std::unique_ptr<Codec>> codecs;
1198  codecs.push_back(CustomCodec::create("none", CodecType::NO_COMPRESSION));
1199  codecs.push_back(getCodec(CodecType::LZ4_FRAME));
1200  auto automatic =
1201  getAutoUncompressionCodec(std::move(codecs), getTerminalCodec());
1202  auto original = IOBuf::wrapBuffer(constantDataHolder.data(length));
1203 
1204  auto noneCompressed = none->compress(original.get());
1205  std::unique_ptr<IOBuf> noneDecompressed;
1206  if (automatic->needsUncompressedLength()) {
1207  noneDecompressed = automatic->uncompress(noneCompressed.get(), length);
1208  } else {
1209  noneDecompressed = automatic->uncompress(noneCompressed.get());
1210  }
1211  EXPECT_TRUE(automatic->canUncompress(noneCompressed.get()));
1212  EXPECT_FALSE(auto_->canUncompress(noneCompressed.get()));
1213  EXPECT_EQ(constantDataHolder.hash(length), hashIOBuf(noneDecompressed.get()));
1214 
1215  auto compressed = codec_->compress(original.get());
1216  std::unique_ptr<IOBuf> decompressed;
1217  if (automatic->needsUncompressedLength()) {
1218  decompressed = automatic->uncompress(compressed.get(), length);
1219  } else {
1220  decompressed = automatic->uncompress(compressed.get());
1221  }
1222  EXPECT_EQ(constantDataHolder.hash(length), hashIOBuf(decompressed.get()));
1223 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< Codec > codec_
uint64_t hashIOBuf(const IOBuf *buf)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
ConstantDataHolder constantDataHolder(dataSizeLog2)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
std::unique_ptr< Codec > getAutoUncompressionCodec(std::vector< std::unique_ptr< Codec >> customCodecs, std::unique_ptr< Codec > terminalCodec)
ByteRange data(size_t size) const
std::unique_ptr< Codec > getCodec(CodecType type, int level)
uint64_t hash(size_t size) const
constexpr None none
Definition: Optional.h:87
folly::io::test::TEST_P ( AutomaticCodecTest  ,
canUncompressOneBytes   
)

Definition at line 1225 of file CompressionTest.cpp.

References availableCodecs(), codec_, folly::IOBuf::copyBuffer(), folly::IOBuf::CREATE, EXPECT_FALSE, INSTANTIATE_TEST_CASE_P(), folly::gen::move, folly::none, string, type, uint64_t, and uncompressed_.

1225  {
1226  // No default codec can uncompress 1 bytes.
1227  IOBuf buf{IOBuf::CREATE, 1};
1228  buf.append(1);
1229  EXPECT_FALSE(codec_->canUncompress(&buf, 1));
1230  EXPECT_FALSE(codec_->canUncompress(&buf, folly::none));
1231  EXPECT_FALSE(auto_->canUncompress(&buf, 1));
1232  EXPECT_FALSE(auto_->canUncompress(&buf, folly::none));
1233 }
std::unique_ptr< Codec > codec_
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
constexpr None none
Definition: Optional.h:87
folly::io::test::TEST_P ( TerminalCodecTest  ,
uncompressIfDefaultThrows   
)

Definition at line 1288 of file CompressionTest.cpp.

References codec_, EXPECT_ANY_THROW, EXPECT_EQ, EXPECT_TRUE, folly::io::getAutoUncompressionCodec(), string, folly::io::USER_DEFINED, and folly::IOBuf::wrapBuffer().

1288  {
1289  std::string const original = "abc";
1290  auto const compressed = codec_->compress(original);
1291 
1292  // Sanity check: the automatic codec can uncompress the original string.
1293  auto const uncompressed = auto_->uncompress(compressed);
1294  EXPECT_EQ(uncompressed, original);
1295 
1296  // Truncate the compressed string.
1297  auto const truncated = compressed.substr(0, compressed.size() - 1);
1298  auto const truncatedBuf =
1299  IOBuf::wrapBuffer(truncated.data(), truncated.size());
1300  EXPECT_TRUE(auto_->canUncompress(truncatedBuf.get()));
1301  EXPECT_ANY_THROW(auto_->uncompress(truncated));
1302 
1303  // Expect the terminal codec to successfully uncompress the string.
1304  std::unique_ptr<Codec> terminal = getAutoUncompressionCodec(
1305  {}, ConstantCodec::create("dummyString", CodecType::USER_DEFINED));
1306  EXPECT_TRUE(terminal->canUncompress(truncatedBuf.get()));
1307  EXPECT_EQ(terminal->uncompress(truncated), "dummyString");
1308 }
#define EXPECT_ANY_THROW(statement)
Definition: gtest.h:1847
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::unique_ptr< Codec > codec_
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
const char * string
Definition: Conv.cpp:212
std::unique_ptr< Codec > getAutoUncompressionCodec(std::vector< std::unique_ptr< Codec >> customCodecs, std::unique_ptr< Codec > terminalCodec)
folly::io::test::TEST_P ( TerminalCodecTest  ,
terminalOverridesDefaults   
)

Definition at line 1312 of file CompressionTest.cpp.

References codec_, EXPECT_EQ, folly::io::getAutoUncompressionCodec(), INSTANTIATE_TEST_CASE_P(), and string.

1312  {
1313  std::unique_ptr<Codec> terminal = getAutoUncompressionCodec(
1314  {}, ConstantCodec::create("dummyString", codecType_));
1315  std::string const original = "abc";
1316  auto const compressed = codec_->compress(original);
1317  EXPECT_EQ(terminal->uncompress(compressed), "dummyString");
1318 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::unique_ptr< Codec > codec_
const char * string
Definition: Conv.cpp:212
std::unique_ptr< Codec > getAutoUncompressionCodec(std::vector< std::unique_ptr< Codec >> customCodecs, std::unique_ptr< Codec > terminalCodec)
static std::pair<bool, std::unique_ptr<IOBuf> > folly::io::test::uncompressSome ( StreamCodec codec,
ByteRange data,
uint64_t  bufferSize,
StreamCodec::FlushOp  flush 
)
static

Definition at line 840 of file CompressionTest.cpp.

References folly::IOBufQueue::append(), buffer(), folly::IOBuf::create(), folly::IOBufQueue::move(), folly::gen::move, gmock_output_test::output, folly::IOBufQueue::tailroom(), and folly::io::StreamCodec::uncompressStream().

Referenced by folly::io::test::StreamingCompressionTest::runFlushTest(), and folly::io::test::StreamingCompressionTest::runUncompressStreamTest().

844  {
845  bool result;
846  IOBufQueue queue;
847  do {
848  auto buffer = IOBuf::create(bufferSize);
849  buffer->append(buffer->capacity());
850  MutableByteRange output{buffer->writableData(), buffer->length()};
851 
852  result = codec->uncompressStream(data, output, flush);
853  buffer->trimEnd(output.size());
854  queue.append(std::move(buffer));
855 
856  } while (queue.tailroom() == 0 && !result);
857  return std::make_pair(result, queue.move());
858 }
std::vector< uint8_t > buffer(kBufferSize+16)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
CodecFactory codec
Range< unsigned char * > MutableByteRange
Definition: Range.h:1164
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43

Variable Documentation

ConstantDataHolder folly::io::test::constantDataHolder(dataSizeLog2)

Referenced by TEST(), and TEST_P().

constexpr size_t folly::io::test::dataSizeLog2 = 27

Definition at line 129 of file CompressionTest.cpp.

RandomDataHolder folly::io::test::randomDataHolder(dataSizeLog2)

Referenced by TEST(), and TEST_P().