blob: 3eab94cd2dd3d0403faec92a796bab3240799ecb [file] [log] [blame]
reed@google.comdde09562011-05-23 12:21:05 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
reed@google.comdde09562011-05-23 12:21:05 +00006 */
7
scroggo@google.comdd394882012-07-24 20:47:55 +00008#include "SkRandom.h"
reed@google.comdde09562011-05-23 12:21:05 +00009#include "SkReader32.h"
10#include "SkWriter32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "Test.h"
reed@google.comdde09562011-05-23 12:21:05 +000012
reed@google.com1cf58d02012-08-30 14:03:21 +000013static void check_contents(skiatest::Reporter* reporter, const SkWriter32& writer,
14 const void* expected, size_t size) {
15 SkAutoSMalloc<256> storage(size);
16 REPORTER_ASSERT(reporter, writer.bytesWritten() == size);
17 writer.flatten(storage.get());
18 REPORTER_ASSERT(reporter, !memcmp(storage.get(), expected, size));
19}
20
mtklein@google.com8b4ba632013-09-03 18:07:01 +000021
22static void test_reserve(skiatest::Reporter* reporter) {
23 // There used to be a bug where we'd assert your first reservation had to
24 // fit in external storage if you used it. This would crash in debug mode.
25 uint8_t storage[4];
commit-bot@chromium.org19382422014-01-14 20:51:26 +000026 SkWriter32 writer(storage, sizeof(storage));
mtklein@google.com8b4ba632013-09-03 18:07:01 +000027 writer.reserve(40);
28}
29
commit-bot@chromium.org47fa1362013-08-09 16:03:05 +000030static void test_string_null(skiatest::Reporter* reporter) {
31 uint8_t storage[8];
commit-bot@chromium.org19382422014-01-14 20:51:26 +000032 SkWriter32 writer(storage, sizeof(storage));
commit-bot@chromium.org47fa1362013-08-09 16:03:05 +000033
halcanary96fcdcc2015-08-27 07:41:13 -070034 // Can we write nullptr?
35 writer.writeString(nullptr);
mtklein@google.com0038c122013-08-15 21:01:32 +000036 const int32_t expected[] = { 0x0, 0x0 };
37 check_contents(reporter, writer, expected, sizeof(expected));
commit-bot@chromium.org47fa1362013-08-09 16:03:05 +000038}
39
reed@google.com1cf58d02012-08-30 14:03:21 +000040static void test_rewind(skiatest::Reporter* reporter) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +000041 SkSWriter32<32> writer;
reed@google.com1cf58d02012-08-30 14:03:21 +000042 int32_t array[3] = { 1, 2, 4 };
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +000043
reed@google.com1cf58d02012-08-30 14:03:21 +000044 REPORTER_ASSERT(reporter, 0 == writer.bytesWritten());
45 for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) {
46 writer.writeInt(array[i]);
47 }
48 check_contents(reporter, writer, array, sizeof(array));
49
50 writer.rewindToOffset(2*sizeof(int32_t));
51 REPORTER_ASSERT(reporter, sizeof(array) - 4 == writer.bytesWritten());
52 writer.writeInt(3);
53 REPORTER_ASSERT(reporter, sizeof(array) == writer.bytesWritten());
54 array[2] = 3;
55 check_contents(reporter, writer, array, sizeof(array));
reed@google.comd7e27822012-08-31 20:17:56 +000056
57 // test rewinding past allocated chunks. This used to crash because we
58 // didn't truncate our link-list after freeing trailing blocks
59 {
commit-bot@chromium.org19382422014-01-14 20:51:26 +000060 SkWriter32 writer;
reed@google.comd7e27822012-08-31 20:17:56 +000061 for (int i = 0; i < 100; ++i) {
62 writer.writeInt(i);
63 }
64 REPORTER_ASSERT(reporter, 100*4 == writer.bytesWritten());
65 for (int j = 100*4; j >= 0; j -= 16) {
66 writer.rewindToOffset(j);
67 }
68 REPORTER_ASSERT(reporter, writer.bytesWritten() < 16);
69 }
reed@google.com1cf58d02012-08-30 14:03:21 +000070}
71
reed@google.com51c62a62012-06-12 20:47:53 +000072static void test_ptr(skiatest::Reporter* reporter) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +000073 SkSWriter32<32> writer;
rmistry@google.comd6176b02012-08-23 18:14:13 +000074
reed@google.com51c62a62012-06-12 20:47:53 +000075 void* p0 = reporter;
76 void* p1 = &writer;
77
78 // try writing ptrs where at least one of them may be at a non-multiple of
79 // 8 boundary, to confirm this works on 64bit machines.
80
81 writer.writePtr(p0);
82 writer.write8(0x33);
83 writer.writePtr(p1);
84 writer.write8(0x66);
85
reed@google.com44699382013-10-31 17:28:30 +000086 size_t size = writer.bytesWritten();
reed@google.com51c62a62012-06-12 20:47:53 +000087 REPORTER_ASSERT(reporter, 2 * sizeof(void*) + 2 * sizeof(int32_t));
88
89 char buffer[32];
90 SkASSERT(sizeof(buffer) >= size);
91 writer.flatten(buffer);
92
93 SkReader32 reader(buffer, size);
94 REPORTER_ASSERT(reporter, reader.readPtr() == p0);
95 REPORTER_ASSERT(reporter, reader.readInt() == 0x33);
96 REPORTER_ASSERT(reporter, reader.readPtr() == p1);
97 REPORTER_ASSERT(reporter, reader.readInt() == 0x66);
98}
99
reed@google.comdde09562011-05-23 12:21:05 +0000100static void test1(skiatest::Reporter* reporter, SkWriter32* writer) {
101 const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
102 for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) {
reed@google.com44699382013-10-31 17:28:30 +0000103 REPORTER_ASSERT(reporter, i*4 == writer->bytesWritten());
reed@google.comdde09562011-05-23 12:21:05 +0000104 writer->write32(data[i]);
commit-bot@chromium.org536ac5e2014-02-11 22:38:51 +0000105 REPORTER_ASSERT(reporter, data[i] == writer->readTAt<uint32_t>(i * 4));
reed@google.comdde09562011-05-23 12:21:05 +0000106 }
107
108 char buffer[sizeof(data)];
reed@google.com44699382013-10-31 17:28:30 +0000109 REPORTER_ASSERT(reporter, sizeof(buffer) == writer->bytesWritten());
reed@google.comdde09562011-05-23 12:21:05 +0000110 writer->flatten(buffer);
111 REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer)));
112}
113
114static void test2(skiatest::Reporter* reporter, SkWriter32* writer) {
115 static const char gStr[] = "abcdefghimjklmnopqrstuvwxyz";
116 size_t i;
117
118 size_t len = 0;
119 for (i = 0; i <= 26; ++i) {
120 len += SkWriter32::WriteStringSize(gStr, i);
121 writer->writeString(gStr, i);
122 }
reed@google.com44699382013-10-31 17:28:30 +0000123 REPORTER_ASSERT(reporter, writer->bytesWritten() == len);
reed@google.comdde09562011-05-23 12:21:05 +0000124
125 SkAutoMalloc storage(len);
126 writer->flatten(storage.get());
127
128 SkReader32 reader;
129 reader.setMemory(storage.get(), len);
130 for (i = 0; i <= 26; ++i) {
131 REPORTER_ASSERT(reporter, !reader.eof());
132 const char* str = reader.readString(&len);
133 REPORTER_ASSERT(reporter, i == len);
134 REPORTER_ASSERT(reporter, strlen(str) == len);
135 REPORTER_ASSERT(reporter, !memcmp(str, gStr, len));
scroggo@google.come9617eb2012-07-23 13:44:10 +0000136 // Ensure that the align4 of the string is padded with zeroes.
137 size_t alignedSize = SkAlign4(len + 1);
138 for (size_t j = len; j < alignedSize; j++) {
139 REPORTER_ASSERT(reporter, 0 == str[j]);
140 }
reed@google.comdde09562011-05-23 12:21:05 +0000141 }
142 REPORTER_ASSERT(reporter, reader.eof());
143}
144
scroggo@google.comdd394882012-07-24 20:47:55 +0000145static void testWritePad(skiatest::Reporter* reporter, SkWriter32* writer) {
146 // Create some random data to write.
scroggo565901d2015-12-10 10:44:13 -0800147 const size_t dataSize = 10;
scroggo@google.comdd394882012-07-24 20:47:55 +0000148
scroggo565901d2015-12-10 10:44:13 -0800149 SkAutoTMalloc<uint32_t> originalData(dataSize);
scroggo@google.comdd394882012-07-24 20:47:55 +0000150 {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000151 SkRandom rand(0);
scroggo565901d2015-12-10 10:44:13 -0800152 for (size_t i = 0; i < dataSize; i++) {
153 originalData[(int) i] = rand.nextU();
scroggo@google.comdd394882012-07-24 20:47:55 +0000154 }
155
156 // Write the random data to the writer at different lengths for
157 // different alignments.
158 for (size_t len = 0; len < dataSize; len++) {
159 writer->writePad(originalData.get(), len);
160 }
161 }
162
bsalomonfbaace02014-12-12 16:41:46 -0800163 size_t totalBytes = writer->bytesWritten();
scroggo@google.comdd394882012-07-24 20:47:55 +0000164
165 SkAutoMalloc readStorage(totalBytes);
166 writer->flatten(readStorage.get());
167
168 SkReader32 reader;
169 reader.setMemory(readStorage.get(), totalBytes);
170
171 for (size_t len = 0; len < dataSize; len++) {
172 const char* readPtr = static_cast<const char*>(reader.skip(len));
173 // Ensure that the data read is the same as what was written.
174 REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0);
175 // Ensure that the rest is padded with zeroes.
176 const char* stop = readPtr + SkAlign4(len);
177 readPtr += len;
178 while (readPtr < stop) {
179 REPORTER_ASSERT(reporter, *readPtr++ == 0);
180 }
181 }
182}
183
commit-bot@chromium.org536ac5e2014-02-11 22:38:51 +0000184static void testOverwriteT(skiatest::Reporter* reporter, SkWriter32* writer) {
185 const size_t padding = 64;
186
187 const uint32_t uint1 = 0x12345678;
188 const uint32_t uint2 = 0x98765432;
189 const SkScalar scalar1 = 1234.5678f;
190 const SkScalar scalar2 = 9876.5432f;
191 const SkRect rect1 = SkRect::MakeXYWH(1, 2, 3, 4);
192 const SkRect rect2 = SkRect::MakeXYWH(5, 6, 7, 8);
193
194 for (size_t i = 0; i < (padding / 4); ++i) {
195 writer->write32(0);
196 }
197
198 writer->write32(uint1);
199 writer->writeRect(rect1);
200 writer->writeScalar(scalar1);
201
202 for (size_t i = 0; i < (padding / 4); ++i) {
203 writer->write32(0);
204 }
205
206 REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint1);
207 REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)) == rect1);
208 REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>(
209 padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar1);
210
211 writer->overwriteTAt(padding, uint2);
212 writer->overwriteTAt(padding + sizeof(uint32_t), rect2);
213 writer->overwriteTAt(padding + sizeof(uint32_t) + sizeof(SkRect), scalar2);
214
215 REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint2);
216 REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)) == rect2);
217 REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>(
218 padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar2);
219}
220
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000221DEF_TEST(Writer32_dynamic, reporter) {
222 SkWriter32 writer;
223 test1(reporter, &writer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000224
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000225 writer.reset();
226 test2(reporter, &writer);
scroggo@google.comdd394882012-07-24 20:47:55 +0000227
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000228 writer.reset();
229 testWritePad(reporter, &writer);
commit-bot@chromium.org536ac5e2014-02-11 22:38:51 +0000230
231 writer.reset();
232 testOverwriteT(reporter, &writer);
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000233}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000234
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000235DEF_TEST(Writer32_small, reporter) {
236 SkSWriter32<8 * sizeof(intptr_t)> writer;
237 test1(reporter, &writer);
238 writer.reset(); // should just rewind our storage
239 test2(reporter, &writer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000240
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000241 writer.reset();
242 testWritePad(reporter, &writer);
commit-bot@chromium.org536ac5e2014-02-11 22:38:51 +0000243
244 writer.reset();
245 testOverwriteT(reporter, &writer);
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000246}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000247
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000248DEF_TEST(Writer32_large, reporter) {
249 SkSWriter32<1024 * sizeof(intptr_t)> writer;
250 test1(reporter, &writer);
251 writer.reset(); // should just rewind our storage
252 test2(reporter, &writer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000253
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000254 writer.reset();
255 testWritePad(reporter, &writer);
commit-bot@chromium.org536ac5e2014-02-11 22:38:51 +0000256
257 writer.reset();
258 testOverwriteT(reporter, &writer);
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000259}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000260
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000261DEF_TEST(Writer32_misc, reporter) {
mtklein@google.com8b4ba632013-09-03 18:07:01 +0000262 test_reserve(reporter);
commit-bot@chromium.org47fa1362013-08-09 16:03:05 +0000263 test_string_null(reporter);
reed@google.com51c62a62012-06-12 20:47:53 +0000264 test_ptr(reporter);
reed@google.com1cf58d02012-08-30 14:03:21 +0000265 test_rewind(reporter);
reed@google.comdde09562011-05-23 12:21:05 +0000266}
commit-bot@chromium.orgc30dcb92014-03-12 17:04:28 +0000267
reedf70b5312016-03-04 16:36:20 -0800268DEF_TEST(Writer32_data, reporter) {
269 const char* str = "0123456789";
bungeman38d909e2016-08-02 14:40:46 -0700270 sk_sp<SkData> data0(SkData::MakeWithCString(str));
271 sk_sp<SkData> data1(SkData::MakeEmpty());
reedf70b5312016-03-04 16:36:20 -0800272
273 const size_t sizes[] = {
274 SkWriter32::WriteDataSize(nullptr),
bungeman38d909e2016-08-02 14:40:46 -0700275 SkWriter32::WriteDataSize(data0.get()),
276 SkWriter32::WriteDataSize(data1.get()),
reedf70b5312016-03-04 16:36:20 -0800277 };
halcanary9d524f22016-03-29 09:03:52 -0700278
reedf70b5312016-03-04 16:36:20 -0800279 SkSWriter32<1000> writer;
280 size_t sizeWritten = 0;
281
282 writer.writeData(nullptr);
283 sizeWritten += sizes[0];
284 REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
halcanary9d524f22016-03-29 09:03:52 -0700285
bungeman38d909e2016-08-02 14:40:46 -0700286 writer.writeData(data0.get());
reedf70b5312016-03-04 16:36:20 -0800287 sizeWritten += sizes[1];
288 REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
289
bungeman38d909e2016-08-02 14:40:46 -0700290 writer.writeData(data1.get());
reedf70b5312016-03-04 16:36:20 -0800291 sizeWritten += sizes[2];
292 REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
293
reedfde05112016-03-11 13:02:28 -0800294 auto result(writer.snapshotAsData());
reedf70b5312016-03-04 16:36:20 -0800295
296 SkReader32 reader(result->data(), result->size());
reedfde05112016-03-11 13:02:28 -0800297 auto d0(reader.readData()),
298 d1(reader.readData()),
299 d2(reader.readData());
reedf70b5312016-03-04 16:36:20 -0800300
301 REPORTER_ASSERT(reporter, 0 == d0->size());
302 REPORTER_ASSERT(reporter, strlen(str)+1 == d1->size());
303 REPORTER_ASSERT(reporter, !memcmp(str, d1->data(), strlen(str)+1));
304 REPORTER_ASSERT(reporter, 0 == d2->size());
halcanary9d524f22016-03-29 09:03:52 -0700305
reedf70b5312016-03-04 16:36:20 -0800306 REPORTER_ASSERT(reporter, reader.offset() == sizeWritten);
307 REPORTER_ASSERT(reporter, reader.eof());
308}