epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
| 13 | #include "Test.h" |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 14 | #include "SkData.h" |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 15 | #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.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 20 | class SkZeroSizeMemStream : public SkMemoryStream { |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 21 | public: |
| 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 | |
| 33 | static 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.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 52 | inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 53 | 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.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 64 | SkAutoDataUnref data1(compressed.copyToData()); |
| 65 | |
| 66 | testStream->setData(data1.get())->unref(); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 67 | 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.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 74 | inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey); |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 75 | REPORTER_ASSERT(reporter, data1->size() == inputSize); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 76 | REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(), |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 77 | data1->data(), |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 78 | data1->size()) == 0); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 79 | |
| 80 | // Check that the uncompressed data matches the source data. |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 81 | SkAutoDataUnref data2(uncompressed.copyToData()); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 82 | REPORTER_ASSERT(reporter, testData.getLength() == uncompressed.getOffset()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 83 | REPORTER_ASSERT(reporter, memcmp(testData.getMemoryBase(), |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 84 | data2->data(), |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 85 | testData.getLength()) == 0); |
| 86 | } |
| 87 | |
| 88 | static void TestFlateCompression(skiatest::Reporter* reporter) { |
| 89 | TestFlate(reporter, NULL, 0); |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 90 | #if defined(SK_ZLIB_INCLUDE) && !defined(SK_DEBUG) |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 91 | REPORTER_ASSERT(reporter, SkFlate::HaveFlate()); |
| 92 | |
| 93 | SkMemoryStream memStream; |
| 94 | TestFlate(reporter, &memStream, 512); |
| 95 | TestFlate(reporter, &memStream, 10240); |
| 96 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 97 | SkZeroSizeMemStream fileStream; |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 98 | TestFlate(reporter, &fileStream, 512); |
| 99 | TestFlate(reporter, &fileStream, 10240); |
| 100 | #endif |
| 101 | } |
| 102 | |
| 103 | #include "TestClassDef.h" |
| 104 | DEFINE_TESTCLASS("Flate", FlateTestClass, TestFlateCompression) |