Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: tests/FlateTest.cpp

Issue 464373002: Flate Test tests if compression works (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkData.h" 8 #include "SkData.h"
9 #include "SkFlate.h" 9 #include "SkFlate.h"
10 #include "SkRandom.h"
11 #include "SkStream.h" 10 #include "SkStream.h"
12 #include "Test.h" 11 #include "Test.h"
13 12
14 // A memory stream that reports zero size with the standard call, like 13 // A memory stream that reports zero size with the standard call, like
15 // an unseekable file stream would. 14 // an unseekable file stream would.
16 class SkZeroSizeMemStream : public SkMemoryStream { 15 class SkZeroSizeMemStream : public SkMemoryStream {
17 public: 16 public:
18 virtual size_t read(void* buffer, size_t size) { 17 virtual size_t read(void* buffer, size_t size) {
19 if (buffer == NULL && size == 0) 18 if (buffer == NULL && size == 0)
20 return 0; 19 return 0;
21 if (buffer == NULL && size == kGetSizeKey) 20 if (buffer == NULL && size == kGetSizeKey)
22 size = 0; 21 size = 0;
23 return SkMemoryStream::read(buffer, size); 22 return SkMemoryStream::read(buffer, size);
24 } 23 }
25 24
26 static const size_t kGetSizeKey = 0xDEADBEEF; 25 static const size_t kGetSizeKey = 0xDEADBEEF;
27 }; 26 };
28 27
29 // Returns a deterministic data of the given size. 28 // Returns a deterministic data of the given size that should be
29 // very compressible.
30 static SkData* new_test_data(size_t dataSize) { 30 static SkData* new_test_data(size_t dataSize) {
31 SkAutoTMalloc<uint8_t> testBuffer(dataSize); 31 SkAutoTMalloc<uint8_t> testBuffer(dataSize);
32 SkRandom random(0);
33 for (size_t i = 0; i < dataSize; ++i) { 32 for (size_t i = 0; i < dataSize; ++i) {
34 testBuffer[i] = random.nextU() & 0xFF; 33 testBuffer[i] = i % 64;
35 } 34 }
36 return SkData::NewFromMalloc(testBuffer.detach(), dataSize); 35 return SkData::NewFromMalloc(testBuffer.detach(), dataSize);
37 } 36 }
38 37
39 static void TestFlate(skiatest::Reporter* reporter, SkMemoryStream* testStream, 38 static void TestFlate(skiatest::Reporter* reporter, SkMemoryStream* testStream,
40 size_t dataSize) { 39 size_t dataSize) {
41 SkASSERT(testStream != NULL); 40 SkASSERT(testStream != NULL);
42 41
43 SkAutoDataUnref testData(new_test_data(dataSize)); 42 SkAutoDataUnref testData(new_test_data(dataSize));
44 SkASSERT(testData->size() == dataSize); 43 SkASSERT(testData->size() == dataSize);
45 44
46 testStream->setMemory(testData->data(), dataSize, /*copyData=*/ true); 45 testStream->setMemory(testData->data(), dataSize, /*copyData=*/ true);
47 SkDynamicMemoryWStream compressed; 46 SkDynamicMemoryWStream compressed;
48 bool deflateSuccess = SkFlate::Deflate(testStream, &compressed); 47 bool deflateSuccess = SkFlate::Deflate(testStream, &compressed);
49 REPORTER_ASSERT(reporter, deflateSuccess); 48 REPORTER_ASSERT(reporter, deflateSuccess);
50 49
51 // Check that the input data wasn't changed. 50 // Check that the input data wasn't changed.
52 size_t inputSize = testStream->getLength(); 51 size_t inputSize = testStream->getLength();
53 if (inputSize == 0) { 52 if (inputSize == 0) {
54 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey); 53 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
55 } 54 }
56 REPORTER_ASSERT(reporter, dataSize == inputSize); 55 REPORTER_ASSERT(reporter, dataSize == inputSize);
57 if (dataSize == inputSize) { 56 if (dataSize == inputSize) {
58 REPORTER_ASSERT(reporter, memcmp(testData->data(), 57 REPORTER_ASSERT(reporter, memcmp(testData->data(),
59 testStream->getMemoryBase(), 58 testStream->getMemoryBase(),
60 dataSize) == 0); 59 dataSize) == 0);
61 } 60 }
62 61
63 // Assume there are two test sizes, big and small. 62 size_t compressedSize = compressed.getOffset();
64 if (dataSize < 1024) {
65 REPORTER_ASSERT(reporter, compressed.getOffset() < 1024);
66 } else {
67 REPORTER_ASSERT(reporter, compressed.getOffset() > 1024);
68 }
69 63
70 SkAutoDataUnref compressedData(compressed.copyToData()); 64 SkAutoDataUnref compressedData(compressed.copyToData());
71 testStream->setData(compressedData.get()); 65 testStream->setData(compressedData.get());
72 66
73 SkDynamicMemoryWStream uncompressed; 67 SkDynamicMemoryWStream uncompressed;
74 bool inflateSuccess = SkFlate::Inflate(testStream, &uncompressed); 68 bool inflateSuccess = SkFlate::Inflate(testStream, &uncompressed);
75 REPORTER_ASSERT(reporter, inflateSuccess); 69 REPORTER_ASSERT(reporter, inflateSuccess);
76 70
77 // Check that the input data wasn't changed. 71 // Check that the input data wasn't changed.
78 inputSize = testStream->getLength(); 72 inputSize = testStream->getLength();
79 if (inputSize == 0) { 73 if (inputSize == 0) {
80 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey); 74 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
81 } 75 }
82 REPORTER_ASSERT(reporter, compressedData->size() == inputSize); 76 REPORTER_ASSERT(reporter, compressedSize == inputSize);
83 if (compressedData->size() == inputSize) { 77 if (compressedData->size() == inputSize) {
84 REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(), 78 REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(),
85 compressedData->data(), 79 compressedData->data(),
86 compressedData->size()) == 0); 80 compressedData->size()) == 0);
87 } 81 }
88 82
89 // Check that the uncompressed data matches the source data. 83 // Check that the uncompressed data matches the source data.
90 SkAutoDataUnref uncompressedData(uncompressed.copyToData()); 84 SkAutoDataUnref uncompressedData(uncompressed.copyToData());
91 REPORTER_ASSERT(reporter, dataSize == uncompressedData->size()); 85 REPORTER_ASSERT(reporter, dataSize == uncompressedData->size());
92 if (dataSize == uncompressedData->size()) { 86 if (dataSize == uncompressedData->size()) {
93 REPORTER_ASSERT(reporter, memcmp(testData->data(), 87 REPORTER_ASSERT(reporter, memcmp(testData->data(),
94 uncompressedData->data(), 88 uncompressedData->data(),
95 dataSize) == 0); 89 dataSize) == 0);
96 } 90 }
91
92 double compressionRatio = static_cast<double>(dataSize) / compressedSize;
93 // Assert that some compression took place.
94 REPORTER_ASSERT(reporter, compressionRatio > 1.2);
95
96 if (reporter->verbose()) {
97 SkDebugf("Flate Test: \t input size: " SK_SIZE_T_SPECIFIER
98 "\tcompressed size: " SK_SIZE_T_SPECIFIER
99 "\tratio: %.4g\n",
100 dataSize, compressedSize, compressionRatio);
101 }
97 } 102 }
98 103
99 DEF_TEST(Flate, reporter) { 104 DEF_TEST(Flate, reporter) {
100 #ifdef SK_HAS_ZLIB 105 #ifdef SK_HAS_ZLIB
101 REPORTER_ASSERT(reporter, SkFlate::HaveFlate()); 106 REPORTER_ASSERT(reporter, SkFlate::HaveFlate());
102 #endif 107 #endif
103 if (SkFlate::HaveFlate()) { 108 if (SkFlate::HaveFlate()) {
104 SkMemoryStream memStream; 109 SkMemoryStream memStream;
105 TestFlate(reporter, &memStream, 512); 110 TestFlate(reporter, &memStream, 512);
106 TestFlate(reporter, &memStream, 10240); 111 TestFlate(reporter, &memStream, 10240);
107 112
108 SkZeroSizeMemStream fileStream; 113 SkZeroSizeMemStream fileStream;
109 TestFlate(reporter, &fileStream, 512); 114 TestFlate(reporter, &fileStream, 512);
110 TestFlate(reporter, &fileStream, 10240); 115 TestFlate(reporter, &fileStream, 10240);
111 } 116 }
112 } 117 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698