2010-12-02 22:55:33 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2011 Google Inc.
|
2010-12-02 22:55:33 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2010-12-02 22:55:33 +00:00
|
|
|
*/
|
|
|
|
|
2011-06-24 19:12:12 +00:00
|
|
|
#include "SkData.h"
|
2010-12-02 22:55:33 +00:00
|
|
|
#include "SkFlate.h"
|
|
|
|
#include "SkStream.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2010-12-02 22:55:33 +00:00
|
|
|
|
|
|
|
// A memory stream that reports zero size with the standard call, like
|
|
|
|
// an unseekable file stream would.
|
2011-01-25 21:01:34 +00:00
|
|
|
class SkZeroSizeMemStream : public SkMemoryStream {
|
2010-12-02 22:55:33 +00:00
|
|
|
public:
|
|
|
|
virtual size_t read(void* buffer, size_t size) {
|
|
|
|
if (buffer == NULL && size == 0)
|
|
|
|
return 0;
|
|
|
|
if (buffer == NULL && size == kGetSizeKey)
|
|
|
|
size = 0;
|
|
|
|
return SkMemoryStream::read(buffer, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const size_t kGetSizeKey = 0xDEADBEEF;
|
|
|
|
};
|
|
|
|
|
2014-08-13 15:07:38 +00:00
|
|
|
// Returns a deterministic data of the given size that should be
|
|
|
|
// very compressible.
|
2014-08-13 14:14:36 +00:00
|
|
|
static SkData* new_test_data(size_t dataSize) {
|
|
|
|
SkAutoTMalloc<uint8_t> testBuffer(dataSize);
|
|
|
|
for (size_t i = 0; i < dataSize; ++i) {
|
2014-08-13 15:07:38 +00:00
|
|
|
testBuffer[i] = i % 64;
|
2014-08-13 14:14:36 +00:00
|
|
|
}
|
|
|
|
return SkData::NewFromMalloc(testBuffer.detach(), dataSize);
|
|
|
|
}
|
|
|
|
|
2010-12-02 22:55:33 +00:00
|
|
|
static void TestFlate(skiatest::Reporter* reporter, SkMemoryStream* testStream,
|
|
|
|
size_t dataSize) {
|
2014-08-13 14:14:36 +00:00
|
|
|
SkASSERT(testStream != NULL);
|
2010-12-02 22:55:33 +00:00
|
|
|
|
2014-08-13 14:14:36 +00:00
|
|
|
SkAutoDataUnref testData(new_test_data(dataSize));
|
|
|
|
SkASSERT(testData->size() == dataSize);
|
2010-12-02 22:55:33 +00:00
|
|
|
|
2014-08-13 14:14:36 +00:00
|
|
|
testStream->setMemory(testData->data(), dataSize, /*copyData=*/ true);
|
2010-12-02 22:55:33 +00:00
|
|
|
SkDynamicMemoryWStream compressed;
|
2014-08-13 14:14:36 +00:00
|
|
|
bool deflateSuccess = SkFlate::Deflate(testStream, &compressed);
|
|
|
|
REPORTER_ASSERT(reporter, deflateSuccess);
|
2010-12-02 22:55:33 +00:00
|
|
|
|
|
|
|
// Check that the input data wasn't changed.
|
|
|
|
size_t inputSize = testStream->getLength();
|
2014-08-13 14:14:36 +00:00
|
|
|
if (inputSize == 0) {
|
2011-01-25 21:01:34 +00:00
|
|
|
inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
|
2014-08-13 14:14:36 +00:00
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, dataSize == inputSize);
|
|
|
|
if (dataSize == inputSize) {
|
|
|
|
REPORTER_ASSERT(reporter, memcmp(testData->data(),
|
|
|
|
testStream->getMemoryBase(),
|
|
|
|
dataSize) == 0);
|
|
|
|
}
|
2010-12-02 22:55:33 +00:00
|
|
|
|
2014-08-13 15:07:38 +00:00
|
|
|
size_t compressedSize = compressed.getOffset();
|
2010-12-02 22:55:33 +00:00
|
|
|
|
2014-08-13 14:14:36 +00:00
|
|
|
SkAutoDataUnref compressedData(compressed.copyToData());
|
|
|
|
testStream->setData(compressedData.get());
|
2011-06-24 19:12:12 +00:00
|
|
|
|
2010-12-02 22:55:33 +00:00
|
|
|
SkDynamicMemoryWStream uncompressed;
|
2014-08-13 14:14:36 +00:00
|
|
|
bool inflateSuccess = SkFlate::Inflate(testStream, &uncompressed);
|
|
|
|
REPORTER_ASSERT(reporter, inflateSuccess);
|
2010-12-02 22:55:33 +00:00
|
|
|
|
|
|
|
// Check that the input data wasn't changed.
|
|
|
|
inputSize = testStream->getLength();
|
2014-08-13 14:14:36 +00:00
|
|
|
if (inputSize == 0) {
|
2011-01-25 21:01:34 +00:00
|
|
|
inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
|
2014-08-13 14:14:36 +00:00
|
|
|
}
|
2014-08-13 15:07:38 +00:00
|
|
|
REPORTER_ASSERT(reporter, compressedSize == inputSize);
|
2014-08-13 14:14:36 +00:00
|
|
|
if (compressedData->size() == inputSize) {
|
|
|
|
REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(),
|
|
|
|
compressedData->data(),
|
|
|
|
compressedData->size()) == 0);
|
|
|
|
}
|
2010-12-02 22:55:33 +00:00
|
|
|
|
|
|
|
// Check that the uncompressed data matches the source data.
|
2014-08-13 14:14:36 +00:00
|
|
|
SkAutoDataUnref uncompressedData(uncompressed.copyToData());
|
|
|
|
REPORTER_ASSERT(reporter, dataSize == uncompressedData->size());
|
|
|
|
if (dataSize == uncompressedData->size()) {
|
|
|
|
REPORTER_ASSERT(reporter, memcmp(testData->data(),
|
|
|
|
uncompressedData->data(),
|
|
|
|
dataSize) == 0);
|
|
|
|
}
|
2014-08-13 15:07:38 +00:00
|
|
|
|
2014-08-13 16:20:08 +00:00
|
|
|
if (compressedSize < 1) { return; }
|
|
|
|
|
2014-08-13 15:07:38 +00:00
|
|
|
double compressionRatio = static_cast<double>(dataSize) / compressedSize;
|
|
|
|
// Assert that some compression took place.
|
|
|
|
REPORTER_ASSERT(reporter, compressionRatio > 1.2);
|
|
|
|
|
|
|
|
if (reporter->verbose()) {
|
|
|
|
SkDebugf("Flate Test: \t input size: " SK_SIZE_T_SPECIFIER
|
|
|
|
"\tcompressed size: " SK_SIZE_T_SPECIFIER
|
|
|
|
"\tratio: %.4g\n",
|
|
|
|
dataSize, compressedSize, compressionRatio);
|
|
|
|
}
|
2010-12-02 22:55:33 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Flate, reporter) {
|
2014-08-13 14:14:36 +00:00
|
|
|
#ifdef SK_HAS_ZLIB
|
2010-12-02 22:55:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkFlate::HaveFlate());
|
|
|
|
#endif
|
2014-08-13 14:14:36 +00:00
|
|
|
if (SkFlate::HaveFlate()) {
|
|
|
|
SkMemoryStream memStream;
|
|
|
|
TestFlate(reporter, &memStream, 512);
|
|
|
|
TestFlate(reporter, &memStream, 10240);
|
|
|
|
|
|
|
|
SkZeroSizeMemStream fileStream;
|
|
|
|
TestFlate(reporter, &fileStream, 512);
|
|
|
|
TestFlate(reporter, &fileStream, 10240);
|
|
|
|
}
|
2010-12-02 22:55:33 +00:00
|
|
|
}
|