blob: 8697df9f5abe9888ebd132ef049f8df2fd1d5857 [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);
reed@google.com8a85d0c2011-06-24 19:12:12 +000075 REPORTER_ASSERT(reporter, data1.size() == inputSize);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000076 REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(),
reed@google.com8a85d0c2011-06-24 19:12:12 +000077 data1.data(), data1.size()) == 0);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000078
79 // Check that the uncompressed data matches the source data.
reed@google.com8a85d0c2011-06-24 19:12:12 +000080 SkAutoDataUnref data2(uncompressed.copyToData());
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000081 REPORTER_ASSERT(reporter, testData.getLength() == uncompressed.getOffset());
reed@google.com8a85d0c2011-06-24 19:12:12 +000082 REPORTER_ASSERT(reporter, memcmp(testData.getMemoryBase(), data2.data(),
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000083 testData.getLength()) == 0);
84}
85
86static void TestFlateCompression(skiatest::Reporter* reporter) {
87 TestFlate(reporter, NULL, 0);
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +000088#if defined(SK_ZLIB_INCLUDE) && !defined(SK_DEBUG)
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000089 REPORTER_ASSERT(reporter, SkFlate::HaveFlate());
90
91 SkMemoryStream memStream;
92 TestFlate(reporter, &memStream, 512);
93 TestFlate(reporter, &memStream, 10240);
94
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000095 SkZeroSizeMemStream fileStream;
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000096 TestFlate(reporter, &fileStream, 512);
97 TestFlate(reporter, &fileStream, 10240);
98#endif
99}
100
101#include "TestClassDef.h"
102DEFINE_TESTCLASS("Flate", FlateTestClass, TestFlateCompression)