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

Side by Side Diff: tests/FlateTest.cpp

Issue 464333002: Fix Flate Test, also cleanup (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Another 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 <stdlib.h>
9 #include <string.h>
10
11 #include "SkData.h" 8 #include "SkData.h"
12 #include "SkFlate.h" 9 #include "SkFlate.h"
10 #include "SkRandom.h"
13 #include "SkStream.h" 11 #include "SkStream.h"
14 #include "Test.h" 12 #include "Test.h"
15 13
16 // A memory stream that reports zero size with the standard call, like 14 // A memory stream that reports zero size with the standard call, like
17 // an unseekable file stream would. 15 // an unseekable file stream would.
18 class SkZeroSizeMemStream : public SkMemoryStream { 16 class SkZeroSizeMemStream : public SkMemoryStream {
19 public: 17 public:
20 virtual size_t read(void* buffer, size_t size) { 18 virtual size_t read(void* buffer, size_t size) {
21 if (buffer == NULL && size == 0) 19 if (buffer == NULL && size == 0)
22 return 0; 20 return 0;
23 if (buffer == NULL && size == kGetSizeKey) 21 if (buffer == NULL && size == kGetSizeKey)
24 size = 0; 22 size = 0;
25 return SkMemoryStream::read(buffer, size); 23 return SkMemoryStream::read(buffer, size);
26 } 24 }
27 25
28 static const size_t kGetSizeKey = 0xDEADBEEF; 26 static const size_t kGetSizeKey = 0xDEADBEEF;
29 }; 27 };
30 28
29 // Returns a deterministic data of the given size.
30 static SkData* new_test_data(size_t dataSize) {
31 SkAutoTMalloc<uint8_t> testBuffer(dataSize);
32 SkRandom random(0);
33 for (size_t i = 0; i < dataSize; ++i) {
34 testBuffer[i] = random.nextU() & 0xFF;
35 }
36 return SkData::NewFromMalloc(testBuffer.detach(), dataSize);
37 }
38
31 static void TestFlate(skiatest::Reporter* reporter, SkMemoryStream* testStream, 39 static void TestFlate(skiatest::Reporter* reporter, SkMemoryStream* testStream,
32 size_t dataSize) { 40 size_t dataSize) {
33 if (testStream == NULL) 41 SkASSERT(testStream != NULL);
34 return;
35 42
36 SkMemoryStream testData(dataSize); 43 SkAutoDataUnref testData(new_test_data(dataSize));
37 uint8_t* data = (uint8_t*)testData.getMemoryBase(); 44 SkASSERT(testData->size() == dataSize);
38 srand(0); // Make data deterministic.
39 for (size_t i = 0; i < dataSize; i++)
40 data[i] = rand() & 0xFF;
41 45
42 testStream->setMemory(testData.getMemoryBase(), dataSize, true); 46 testStream->setMemory(testData->data(), dataSize, /*copyData=*/ true);
43 SkDynamicMemoryWStream compressed; 47 SkDynamicMemoryWStream compressed;
44 bool status = SkFlate::Deflate(testStream, &compressed); 48 bool deflateSuccess = SkFlate::Deflate(testStream, &compressed);
45 REPORTER_ASSERT(reporter, status); 49 REPORTER_ASSERT(reporter, deflateSuccess);
46 50
47 // Check that the input data wasn't changed. 51 // Check that the input data wasn't changed.
48 size_t inputSize = testStream->getLength(); 52 size_t inputSize = testStream->getLength();
49 if (inputSize == 0) 53 if (inputSize == 0) {
50 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey); 54 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
51 REPORTER_ASSERT(reporter, testData.getLength() == inputSize); 55 }
52 REPORTER_ASSERT(reporter, memcmp(testData.getMemoryBase(), 56 REPORTER_ASSERT(reporter, dataSize == inputSize);
53 testStream->getMemoryBase(), 57 if (dataSize == inputSize) {
54 testData.getLength()) == 0); 58 REPORTER_ASSERT(reporter, memcmp(testData->data(),
59 testStream->getMemoryBase(),
60 dataSize) == 0);
61 }
55 62
56 // Assume there are two test sizes, big and small. 63 // Assume there are two test sizes, big and small.
57 if (dataSize < 1024) 64 if (dataSize < 1024) {
58 REPORTER_ASSERT(reporter, compressed.getOffset() < 1024); 65 REPORTER_ASSERT(reporter, compressed.getOffset() < 1024);
59 else 66 } else {
60 REPORTER_ASSERT(reporter, compressed.getOffset() > 1024); 67 REPORTER_ASSERT(reporter, compressed.getOffset() > 1024);
68 }
61 69
62 SkAutoDataUnref data1(compressed.copyToData()); 70 SkAutoDataUnref compressedData(compressed.copyToData());
71 testStream->setData(compressedData.get());
63 72
64 testStream->setData(data1.get())->unref();
65 SkDynamicMemoryWStream uncompressed; 73 SkDynamicMemoryWStream uncompressed;
66 status = SkFlate::Inflate(testStream, &uncompressed); 74 bool inflateSuccess = SkFlate::Inflate(testStream, &uncompressed);
67 REPORTER_ASSERT(reporter, status); 75 REPORTER_ASSERT(reporter, inflateSuccess);
68 76
69 // Check that the input data wasn't changed. 77 // Check that the input data wasn't changed.
70 inputSize = testStream->getLength(); 78 inputSize = testStream->getLength();
71 if (inputSize == 0) 79 if (inputSize == 0) {
72 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey); 80 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
73 REPORTER_ASSERT(reporter, data1->size() == inputSize); 81 }
74 REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(), 82 REPORTER_ASSERT(reporter, compressedData->size() == inputSize);
75 data1->data(), 83 if (compressedData->size() == inputSize) {
76 data1->size()) == 0); 84 REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(),
85 compressedData->data(),
86 compressedData->size()) == 0);
87 }
77 88
78 // Check that the uncompressed data matches the source data. 89 // Check that the uncompressed data matches the source data.
79 SkAutoDataUnref data2(uncompressed.copyToData()); 90 SkAutoDataUnref uncompressedData(uncompressed.copyToData());
80 REPORTER_ASSERT(reporter, testData.getLength() == uncompressed.getOffset()); 91 REPORTER_ASSERT(reporter, dataSize == uncompressedData->size());
81 REPORTER_ASSERT(reporter, memcmp(testData.getMemoryBase(), 92 if (dataSize == uncompressedData->size()) {
82 data2->data(), 93 REPORTER_ASSERT(reporter, memcmp(testData->data(),
83 testData.getLength()) == 0); 94 uncompressedData->data(),
95 dataSize) == 0);
96 }
84 } 97 }
85 98
86 DEF_TEST(Flate, reporter) { 99 DEF_TEST(Flate, reporter) {
87 TestFlate(reporter, NULL, 0); 100 #ifdef SK_HAS_ZLIB
88 #if defined(SK_ZLIB_INCLUDE) && !defined(SK_DEBUG)
89 REPORTER_ASSERT(reporter, SkFlate::HaveFlate()); 101 REPORTER_ASSERT(reporter, SkFlate::HaveFlate());
102 #endif
103 if (SkFlate::HaveFlate()) {
104 SkMemoryStream memStream;
105 TestFlate(reporter, &memStream, 512);
106 TestFlate(reporter, &memStream, 10240);
90 107
91 SkMemoryStream memStream; 108 SkZeroSizeMemStream fileStream;
92 TestFlate(reporter, &memStream, 512); 109 TestFlate(reporter, &fileStream, 512);
93 TestFlate(reporter, &memStream, 10240); 110 TestFlate(reporter, &fileStream, 10240);
94 111 }
95 SkZeroSizeMemStream fileStream;
96 TestFlate(reporter, &fileStream, 512);
97 TestFlate(reporter, &fileStream, 10240);
98 #endif
99 } 112 }
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