blob: 879973fc903cb84eb6826b497e9a2fe6a50b8ace [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
vandebo@chromium.orgee34e352010-12-02 22:55:33 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
vandebo@chromium.orgee34e352010-12-02 22:55:33 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
vandebo@chromium.orgee34e352010-12-02 22:55:33 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000010#include <stdlib.h>
11#include <string.h>
12
13#include "Test.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000014#include "SkData.h"
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000015#include "SkFlate.h"
16#include "SkStream.h"
17
18// A memory stream that reports zero size with the standard call, like
19// an unseekable file stream would.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000020class SkZeroSizeMemStream : public SkMemoryStream {
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000021public:
22 virtual size_t read(void* buffer, size_t size) {
23 if (buffer == NULL && size == 0)
24 return 0;
25 if (buffer == NULL && size == kGetSizeKey)
26 size = 0;
27 return SkMemoryStream::read(buffer, size);
28 }
29
30 static const size_t kGetSizeKey = 0xDEADBEEF;
31};
32
33static void TestFlate(skiatest::Reporter* reporter, SkMemoryStream* testStream,
34 size_t dataSize) {
35 if (testStream == NULL)
36 return;
37
38 SkMemoryStream testData(dataSize);
39 uint8_t* data = (uint8_t*)testData.getMemoryBase();
40 srand(0); // Make data deterministic.
41 for (size_t i = 0; i < dataSize; i++)
42 data[i] = rand() & 0xFF;
43
44 testStream->setMemory(testData.getMemoryBase(), dataSize, true);
45 SkDynamicMemoryWStream compressed;
46 bool status = SkFlate::Deflate(testStream, &compressed);
47 REPORTER_ASSERT(reporter, status);
48
49 // Check that the input data wasn't changed.
50 size_t inputSize = testStream->getLength();
51 if (inputSize == 0)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000052 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000053 REPORTER_ASSERT(reporter, testData.getLength() == inputSize);
54 REPORTER_ASSERT(reporter, memcmp(testData.getMemoryBase(),
55 testStream->getMemoryBase(),
56 testData.getLength()) == 0);
57
58 // Assume there are two test sizes, big and small.
59 if (dataSize < 1024)
60 REPORTER_ASSERT(reporter, compressed.getOffset() < 1024);
61 else
62 REPORTER_ASSERT(reporter, compressed.getOffset() > 1024);
63
reed@google.com8a85d0c2011-06-24 19:12:12 +000064 SkAutoDataUnref data1(compressed.copyToData());
65
66 testStream->setData(data1.get())->unref();
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000067 SkDynamicMemoryWStream uncompressed;
68 status = SkFlate::Inflate(testStream, &uncompressed);
69 REPORTER_ASSERT(reporter, status);
70
71 // Check that the input data wasn't changed.
72 inputSize = testStream->getLength();
73 if (inputSize == 0)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000074 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
robertphillips@google.com59f46b82012-07-10 17:30:58 +000075 REPORTER_ASSERT(reporter, data1->size() == inputSize);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000076 REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(),
rmistry@google.comd6176b02012-08-23 18:14:13 +000077 data1->data(),
robertphillips@google.com59f46b82012-07-10 17:30:58 +000078 data1->size()) == 0);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000079
80 // Check that the uncompressed data matches the source data.
reed@google.com8a85d0c2011-06-24 19:12:12 +000081 SkAutoDataUnref data2(uncompressed.copyToData());
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000082 REPORTER_ASSERT(reporter, testData.getLength() == uncompressed.getOffset());
rmistry@google.comd6176b02012-08-23 18:14:13 +000083 REPORTER_ASSERT(reporter, memcmp(testData.getMemoryBase(),
robertphillips@google.com59f46b82012-07-10 17:30:58 +000084 data2->data(),
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000085 testData.getLength()) == 0);
86}
87
88static void TestFlateCompression(skiatest::Reporter* reporter) {
89 TestFlate(reporter, NULL, 0);
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +000090#if defined(SK_ZLIB_INCLUDE) && !defined(SK_DEBUG)
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000091 REPORTER_ASSERT(reporter, SkFlate::HaveFlate());
92
93 SkMemoryStream memStream;
94 TestFlate(reporter, &memStream, 512);
95 TestFlate(reporter, &memStream, 10240);
96
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000097 SkZeroSizeMemStream fileStream;
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000098 TestFlate(reporter, &fileStream, 512);
99 TestFlate(reporter, &fileStream, 10240);
100#endif
101}
102
103#include "TestClassDef.h"
104DEFINE_TESTCLASS("Flate", FlateTestClass, TestFlateCompression)