blob: 8b85fdf49deb084077cf63cb2769ecd2688d29e6 [file] [log] [blame]
vandebo@chromium.orgee34e352010-12-02 22:55:33 +00001/*
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00002 * Copyright (C) 2011 Google Inc.
vandebo@chromium.orgee34e352010-12-02 22:55:33 +00003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18#include <string.h>
19
20#include "Test.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000021#include "SkData.h"
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000022#include "SkFlate.h"
23#include "SkStream.h"
24
25// A memory stream that reports zero size with the standard call, like
26// an unseekable file stream would.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000027class SkZeroSizeMemStream : public SkMemoryStream {
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000028public:
29 virtual size_t read(void* buffer, size_t size) {
30 if (buffer == NULL && size == 0)
31 return 0;
32 if (buffer == NULL && size == kGetSizeKey)
33 size = 0;
34 return SkMemoryStream::read(buffer, size);
35 }
36
37 static const size_t kGetSizeKey = 0xDEADBEEF;
38};
39
40static void TestFlate(skiatest::Reporter* reporter, SkMemoryStream* testStream,
41 size_t dataSize) {
42 if (testStream == NULL)
43 return;
44
45 SkMemoryStream testData(dataSize);
46 uint8_t* data = (uint8_t*)testData.getMemoryBase();
47 srand(0); // Make data deterministic.
48 for (size_t i = 0; i < dataSize; i++)
49 data[i] = rand() & 0xFF;
50
51 testStream->setMemory(testData.getMemoryBase(), dataSize, true);
52 SkDynamicMemoryWStream compressed;
53 bool status = SkFlate::Deflate(testStream, &compressed);
54 REPORTER_ASSERT(reporter, status);
55
56 // Check that the input data wasn't changed.
57 size_t inputSize = testStream->getLength();
58 if (inputSize == 0)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000059 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000060 REPORTER_ASSERT(reporter, testData.getLength() == inputSize);
61 REPORTER_ASSERT(reporter, memcmp(testData.getMemoryBase(),
62 testStream->getMemoryBase(),
63 testData.getLength()) == 0);
64
65 // Assume there are two test sizes, big and small.
66 if (dataSize < 1024)
67 REPORTER_ASSERT(reporter, compressed.getOffset() < 1024);
68 else
69 REPORTER_ASSERT(reporter, compressed.getOffset() > 1024);
70
reed@google.com8a85d0c2011-06-24 19:12:12 +000071 SkAutoDataUnref data1(compressed.copyToData());
72
73 testStream->setData(data1.get())->unref();
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000074 SkDynamicMemoryWStream uncompressed;
75 status = SkFlate::Inflate(testStream, &uncompressed);
76 REPORTER_ASSERT(reporter, status);
77
78 // Check that the input data wasn't changed.
79 inputSize = testStream->getLength();
80 if (inputSize == 0)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000081 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
reed@google.com8a85d0c2011-06-24 19:12:12 +000082 REPORTER_ASSERT(reporter, data1.size() == inputSize);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000083 REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(),
reed@google.com8a85d0c2011-06-24 19:12:12 +000084 data1.data(), data1.size()) == 0);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000085
86 // Check that the uncompressed data matches the source data.
reed@google.com8a85d0c2011-06-24 19:12:12 +000087 SkAutoDataUnref data2(uncompressed.copyToData());
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000088 REPORTER_ASSERT(reporter, testData.getLength() == uncompressed.getOffset());
reed@google.com8a85d0c2011-06-24 19:12:12 +000089 REPORTER_ASSERT(reporter, memcmp(testData.getMemoryBase(), data2.data(),
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000090 testData.getLength()) == 0);
91}
92
93static void TestFlateCompression(skiatest::Reporter* reporter) {
94 TestFlate(reporter, NULL, 0);
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +000095#if defined(SK_ZLIB_INCLUDE) && !defined(SK_DEBUG)
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000096 REPORTER_ASSERT(reporter, SkFlate::HaveFlate());
97
98 SkMemoryStream memStream;
99 TestFlate(reporter, &memStream, 512);
100 TestFlate(reporter, &memStream, 10240);
101
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000102 SkZeroSizeMemStream fileStream;
vandebo@chromium.orgee34e352010-12-02 22:55:33 +0000103 TestFlate(reporter, &fileStream, 512);
104 TestFlate(reporter, &fileStream, 10240);
105#endif
106}
107
108#include "TestClassDef.h"
109DEFINE_TESTCLASS("Flate", FlateTestClass, TestFlateCompression)