OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2011 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "SkData.h" | |
9 #include "SkFlate.h" | |
10 #include "SkStream.h" | |
11 #include "Test.h" | |
12 | |
13 #if SK_SUPPORT_PDF | |
14 | |
15 // A memory stream that reports zero size with the standard call, like | |
16 // an unseekable file stream would. | |
17 class SkZeroSizeMemStream : public SkMemoryStream { | |
18 public: | |
19 virtual size_t read(void* buffer, size_t size) { | |
20 if (buffer == NULL && size == 0) | |
21 return 0; | |
22 if (buffer == NULL && size == kGetSizeKey) | |
23 size = 0; | |
24 return SkMemoryStream::read(buffer, size); | |
25 } | |
26 | |
27 static const size_t kGetSizeKey = 0xDEADBEEF; | |
28 }; | |
29 | |
30 // Returns a deterministic data of the given size that should be | |
31 // very compressible. | |
32 static SkData* new_test_data(size_t dataSize) { | |
33 SkAutoTMalloc<uint8_t> testBuffer(dataSize); | |
34 for (size_t i = 0; i < dataSize; ++i) { | |
35 testBuffer[SkToInt(i)] = i % 64; | |
36 } | |
37 return SkData::NewFromMalloc(testBuffer.detach(), dataSize); | |
38 } | |
39 | |
40 static void TestFlate(skiatest::Reporter* reporter, SkMemoryStream* testStream, | |
41 size_t dataSize) { | |
42 SkASSERT(testStream != NULL); | |
43 | |
44 SkAutoDataUnref testData(new_test_data(dataSize)); | |
45 SkASSERT(testData->size() == dataSize); | |
46 | |
47 testStream->setMemory(testData->data(), dataSize, /*copyData=*/ true); | |
48 SkDynamicMemoryWStream compressed; | |
49 bool deflateSuccess = SkFlate::Deflate(testStream, &compressed); | |
50 REPORTER_ASSERT(reporter, deflateSuccess); | |
51 | |
52 // Check that the input data wasn't changed. | |
53 size_t inputSize = testStream->getLength(); | |
54 if (inputSize == 0) { | |
55 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey); | |
56 } | |
57 REPORTER_ASSERT(reporter, dataSize == inputSize); | |
58 if (dataSize == inputSize) { | |
59 REPORTER_ASSERT(reporter, memcmp(testData->data(), | |
60 testStream->getMemoryBase(), | |
61 dataSize) == 0); | |
62 } | |
63 | |
64 size_t compressedSize = compressed.getOffset(); | |
65 | |
66 SkAutoDataUnref compressedData(compressed.copyToData()); | |
67 testStream->setData(compressedData.get()); | |
68 | |
69 SkDynamicMemoryWStream uncompressed; | |
70 bool inflateSuccess = SkFlate::Inflate(testStream, &uncompressed); | |
71 REPORTER_ASSERT(reporter, inflateSuccess); | |
72 | |
73 // Check that the input data wasn't changed. | |
74 inputSize = testStream->getLength(); | |
75 if (inputSize == 0) { | |
76 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey); | |
77 } | |
78 REPORTER_ASSERT(reporter, compressedSize == inputSize); | |
79 if (compressedData->size() == inputSize) { | |
80 REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(), | |
81 compressedData->data(), | |
82 compressedData->size()) == 0); | |
83 } | |
84 | |
85 // Check that the uncompressed data matches the source data. | |
86 SkAutoDataUnref uncompressedData(uncompressed.copyToData()); | |
87 REPORTER_ASSERT(reporter, dataSize == uncompressedData->size()); | |
88 if (dataSize == uncompressedData->size()) { | |
89 REPORTER_ASSERT(reporter, memcmp(testData->data(), | |
90 uncompressedData->data(), | |
91 dataSize) == 0); | |
92 } | |
93 | |
94 if (compressedSize < 1) { return; } | |
95 | |
96 double compressionRatio = static_cast<double>(dataSize) / compressedSize; | |
97 // Assert that some compression took place. | |
98 REPORTER_ASSERT(reporter, compressionRatio > 1.2); | |
99 | |
100 if (reporter->verbose()) { | |
101 SkDebugf("Flate Test: \t input size: " SK_SIZE_T_SPECIFIER | |
102 "\tcompressed size: " SK_SIZE_T_SPECIFIER | |
103 "\tratio: %.4g\n", | |
104 dataSize, compressedSize, compressionRatio); | |
105 } | |
106 } | |
107 | |
108 DEF_TEST(Flate, reporter) { | |
109 SkMemoryStream memStream; | |
110 TestFlate(reporter, &memStream, 512); | |
111 TestFlate(reporter, &memStream, 10240); | |
112 | |
113 SkZeroSizeMemStream fileStream; | |
114 TestFlate(reporter, &fileStream, 512); | |
115 TestFlate(reporter, &fileStream, 10240); | |
116 } | |
117 #endif // SK_SUPPORT_PDF | |
OLD | NEW |