blob: f8e0921a1771c03db037beb5bd1f8924d545fd0a [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"
21#include "SkFlate.h"
22#include "SkStream.h"
23
24// A memory stream that reports zero size with the standard call, like
25// an unseekable file stream would.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000026class SkZeroSizeMemStream : public SkMemoryStream {
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000027public:
28 virtual size_t read(void* buffer, size_t size) {
29 if (buffer == NULL && size == 0)
30 return 0;
31 if (buffer == NULL && size == kGetSizeKey)
32 size = 0;
33 return SkMemoryStream::read(buffer, size);
34 }
35
36 static const size_t kGetSizeKey = 0xDEADBEEF;
37};
38
39static void TestFlate(skiatest::Reporter* reporter, SkMemoryStream* testStream,
40 size_t dataSize) {
41 if (testStream == NULL)
42 return;
43
44 SkMemoryStream testData(dataSize);
45 uint8_t* data = (uint8_t*)testData.getMemoryBase();
46 srand(0); // Make data deterministic.
47 for (size_t i = 0; i < dataSize; i++)
48 data[i] = rand() & 0xFF;
49
50 testStream->setMemory(testData.getMemoryBase(), dataSize, true);
51 SkDynamicMemoryWStream compressed;
52 bool status = SkFlate::Deflate(testStream, &compressed);
53 REPORTER_ASSERT(reporter, status);
54
55 // Check that the input data wasn't changed.
56 size_t inputSize = testStream->getLength();
57 if (inputSize == 0)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000058 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000059 REPORTER_ASSERT(reporter, testData.getLength() == inputSize);
60 REPORTER_ASSERT(reporter, memcmp(testData.getMemoryBase(),
61 testStream->getMemoryBase(),
62 testData.getLength()) == 0);
63
64 // Assume there are two test sizes, big and small.
65 if (dataSize < 1024)
66 REPORTER_ASSERT(reporter, compressed.getOffset() < 1024);
67 else
68 REPORTER_ASSERT(reporter, compressed.getOffset() > 1024);
69
70 testStream->setMemory(compressed.getStream(), compressed.getOffset(), true);
71 SkDynamicMemoryWStream uncompressed;
72 status = SkFlate::Inflate(testStream, &uncompressed);
73 REPORTER_ASSERT(reporter, status);
74
75 // Check that the input data wasn't changed.
76 inputSize = testStream->getLength();
77 if (inputSize == 0)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000078 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000079 REPORTER_ASSERT(reporter, compressed.getOffset() == inputSize);
80 REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(),
81 compressed.getStream(),
82 compressed.getOffset()) == 0);
83
84 // Check that the uncompressed data matches the source data.
85 REPORTER_ASSERT(reporter, testData.getLength() == uncompressed.getOffset());
86 REPORTER_ASSERT(reporter, memcmp(testData.getMemoryBase(),
87 uncompressed.getStream(),
88 testData.getLength()) == 0);
89}
90
91static void TestFlateCompression(skiatest::Reporter* reporter) {
92 TestFlate(reporter, NULL, 0);
93#ifdef SK_ZLIB_INCLUDE
94 REPORTER_ASSERT(reporter, SkFlate::HaveFlate());
95
96 SkMemoryStream memStream;
97 TestFlate(reporter, &memStream, 512);
98 TestFlate(reporter, &memStream, 10240);
99
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000100 SkZeroSizeMemStream fileStream;
vandebo@chromium.orgee34e352010-12-02 22:55:33 +0000101 TestFlate(reporter, &fileStream, 512);
102 TestFlate(reporter, &fileStream, 10240);
103#endif
104}
105
106#include "TestClassDef.h"
107DEFINE_TESTCLASS("Flate", FlateTestClass, TestFlateCompression)