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"
|
2014-08-13 14:14:36 +00:00
|
|
|
#include "SkRandom.h"
|
2010-12-02 22:55:33 +00:00
|
|
|
#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 14:14:36 +00:00
|
|
|
// Returns a deterministic data of the given size.
|
|
|
|
static SkData* new_test_data(size_t dataSize) {
|
|
|
|
SkAutoTMalloc<uint8_t> testBuffer(dataSize);
|
|
|
|
SkRandom random(0);
|
|
|
|
for (size_t i = 0; i < dataSize; ++i) {
|
|
|
|
testBuffer[i] = random.nextU() & 0xFF;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
// Assume there are two test sizes, big and small.
|
2014-08-13 14:14:36 +00:00
|
|
|
if (dataSize < 1024) {
|
|
|
|
REPORTER_ASSERT(reporter, compressed.getOffset() < 1024);
|
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(reporter, compressed.getOffset() > 1024);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, compressedData->size() == inputSize);
|
|
|
|
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);
|
|
|
|
}
|
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
|
|
|
}
|