blob: 6cb79f82707ad7d386a049eb9edc584d6f46b50c [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
Hal Canary95e3c052017-01-11 12:44:43 -05008#include "SkAutoMalloc.h"
scroggo@google.comdd394882012-07-24 20:47:55 +00009#include "SkRandom.h"
reed@google.comdde09562011-05-23 12:21:05 +000010#include "SkReader32.h"
11#include "SkWriter32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "Test.h"
reed@google.comdde09562011-05-23 12:21:05 +000013
reed@google.com1cf58d02012-08-30 14:03:21 +000014static void check_contents(skiatest::Reporter* reporter, const SkWriter32& writer,
15 const void* expected, size_t size) {
16 SkAutoSMalloc<256> storage(size);
17 REPORTER_ASSERT(reporter, writer.bytesWritten() == size);
18 writer.flatten(storage.get());
19 REPORTER_ASSERT(reporter, !memcmp(storage.get(), expected, size));
20}
21
mtklein@google.com8b4ba632013-09-03 18:07:01 +000022
23static void test_reserve(skiatest::Reporter* reporter) {
24 // There used to be a bug where we'd assert your first reservation had to
25 // fit in external storage if you used it. This would crash in debug mode.
26 uint8_t storage[4];
commit-bot@chromium.org19382422014-01-14 20:51:26 +000027 SkWriter32 writer(storage, sizeof(storage));
mtklein@google.com8b4ba632013-09-03 18:07:01 +000028 writer.reserve(40);
29}
30
commit-bot@chromium.org47fa1362013-08-09 16:03:05 +000031static void test_string_null(skiatest::Reporter* reporter) {
32 uint8_t storage[8];
commit-bot@chromium.org19382422014-01-14 20:51:26 +000033 SkWriter32 writer(storage, sizeof(storage));
commit-bot@chromium.org47fa1362013-08-09 16:03:05 +000034
halcanary96fcdcc2015-08-27 07:41:13 -070035 // Can we write nullptr?
36 writer.writeString(nullptr);
mtklein@google.com0038c122013-08-15 21:01:32 +000037 const int32_t expected[] = { 0x0, 0x0 };
38 check_contents(reporter, writer, expected, sizeof(expected));
commit-bot@chromium.org47fa1362013-08-09 16:03:05 +000039}
40
reed@google.com1cf58d02012-08-30 14:03:21 +000041static void test_rewind(skiatest::Reporter* reporter) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +000042 SkSWriter32<32> writer;
reed@google.com1cf58d02012-08-30 14:03:21 +000043 int32_t array[3] = { 1, 2, 4 };
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +000044
reed@google.com1cf58d02012-08-30 14:03:21 +000045 REPORTER_ASSERT(reporter, 0 == writer.bytesWritten());
46 for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) {
47 writer.writeInt(array[i]);
48 }
49 check_contents(reporter, writer, array, sizeof(array));
50
51 writer.rewindToOffset(2*sizeof(int32_t));
52 REPORTER_ASSERT(reporter, sizeof(array) - 4 == writer.bytesWritten());
53 writer.writeInt(3);
54 REPORTER_ASSERT(reporter, sizeof(array) == writer.bytesWritten());
55 array[2] = 3;
56 check_contents(reporter, writer, array, sizeof(array));
reed@google.comd7e27822012-08-31 20:17:56 +000057
58 // test rewinding past allocated chunks. This used to crash because we
59 // didn't truncate our link-list after freeing trailing blocks
60 {
commit-bot@chromium.org19382422014-01-14 20:51:26 +000061 SkWriter32 writer;
reed@google.comd7e27822012-08-31 20:17:56 +000062 for (int i = 0; i < 100; ++i) {
63 writer.writeInt(i);
64 }
65 REPORTER_ASSERT(reporter, 100*4 == writer.bytesWritten());
66 for (int j = 100*4; j >= 0; j -= 16) {
67 writer.rewindToOffset(j);
68 }
69 REPORTER_ASSERT(reporter, writer.bytesWritten() < 16);
70 }
reed@google.com1cf58d02012-08-30 14:03:21 +000071}
72
reed@google.com51c62a62012-06-12 20:47:53 +000073static void test_ptr(skiatest::Reporter* reporter) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +000074 SkSWriter32<32> writer;
rmistry@google.comd6176b02012-08-23 18:14:13 +000075
reed@google.com51c62a62012-06-12 20:47:53 +000076 void* p0 = reporter;
77 void* p1 = &writer;
78
79 // try writing ptrs where at least one of them may be at a non-multiple of
80 // 8 boundary, to confirm this works on 64bit machines.
81
82 writer.writePtr(p0);
83 writer.write8(0x33);
84 writer.writePtr(p1);
85 writer.write8(0x66);
86
reed@google.com44699382013-10-31 17:28:30 +000087 size_t size = writer.bytesWritten();
reed@google.com51c62a62012-06-12 20:47:53 +000088 REPORTER_ASSERT(reporter, 2 * sizeof(void*) + 2 * sizeof(int32_t));
89
90 char buffer[32];
91 SkASSERT(sizeof(buffer) >= size);
92 writer.flatten(buffer);
93
94 SkReader32 reader(buffer, size);
95 REPORTER_ASSERT(reporter, reader.readPtr() == p0);
96 REPORTER_ASSERT(reporter, reader.readInt() == 0x33);
97 REPORTER_ASSERT(reporter, reader.readPtr() == p1);
98 REPORTER_ASSERT(reporter, reader.readInt() == 0x66);
99}
100
reed@google.comdde09562011-05-23 12:21:05 +0000101static void test1(skiatest::Reporter* reporter, SkWriter32* writer) {
102 const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
103 for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) {
reed@google.com44699382013-10-31 17:28:30 +0000104 REPORTER_ASSERT(reporter, i*4 == writer->bytesWritten());
reed@google.comdde09562011-05-23 12:21:05 +0000105 writer->write32(data[i]);
commit-bot@chromium.org536ac5e2014-02-11 22:38:51 +0000106 REPORTER_ASSERT(reporter, data[i] == writer->readTAt<uint32_t>(i * 4));
reed@google.comdde09562011-05-23 12:21:05 +0000107 }
108
109 char buffer[sizeof(data)];
reed@google.com44699382013-10-31 17:28:30 +0000110 REPORTER_ASSERT(reporter, sizeof(buffer) == writer->bytesWritten());
reed@google.comdde09562011-05-23 12:21:05 +0000111 writer->flatten(buffer);
112 REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer)));
113}
114
115static void test2(skiatest::Reporter* reporter, SkWriter32* writer) {
116 static const char gStr[] = "abcdefghimjklmnopqrstuvwxyz";
117 size_t i;
118
119 size_t len = 0;
120 for (i = 0; i <= 26; ++i) {
121 len += SkWriter32::WriteStringSize(gStr, i);
122 writer->writeString(gStr, i);
123 }
reed@google.com44699382013-10-31 17:28:30 +0000124 REPORTER_ASSERT(reporter, writer->bytesWritten() == len);
reed@google.comdde09562011-05-23 12:21:05 +0000125
126 SkAutoMalloc storage(len);
127 writer->flatten(storage.get());
128
129 SkReader32 reader;
130 reader.setMemory(storage.get(), len);
131 for (i = 0; i <= 26; ++i) {
132 REPORTER_ASSERT(reporter, !reader.eof());
133 const char* str = reader.readString(&len);
134 REPORTER_ASSERT(reporter, i == len);
135 REPORTER_ASSERT(reporter, strlen(str) == len);
136 REPORTER_ASSERT(reporter, !memcmp(str, gStr, len));
scroggo@google.come9617eb2012-07-23 13:44:10 +0000137 // Ensure that the align4 of the string is padded with zeroes.
138 size_t alignedSize = SkAlign4(len + 1);
139 for (size_t j = len; j < alignedSize; j++) {
140 REPORTER_ASSERT(reporter, 0 == str[j]);
141 }
reed@google.comdde09562011-05-23 12:21:05 +0000142 }
143 REPORTER_ASSERT(reporter, reader.eof());
144}
145
scroggo@google.comdd394882012-07-24 20:47:55 +0000146static void testWritePad(skiatest::Reporter* reporter, SkWriter32* writer) {
147 // Create some random data to write.
scroggo565901d2015-12-10 10:44:13 -0800148 const size_t dataSize = 10;
scroggo@google.comdd394882012-07-24 20:47:55 +0000149
scroggo565901d2015-12-10 10:44:13 -0800150 SkAutoTMalloc<uint32_t> originalData(dataSize);
scroggo@google.comdd394882012-07-24 20:47:55 +0000151 {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000152 SkRandom rand(0);
scroggo565901d2015-12-10 10:44:13 -0800153 for (size_t i = 0; i < dataSize; i++) {
154 originalData[(int) i] = rand.nextU();
scroggo@google.comdd394882012-07-24 20:47:55 +0000155 }
156
157 // Write the random data to the writer at different lengths for
158 // different alignments.
159 for (size_t len = 0; len < dataSize; len++) {
160 writer->writePad(originalData.get(), len);
161 }
162 }
163
bsalomonfbaace02014-12-12 16:41:46 -0800164 size_t totalBytes = writer->bytesWritten();
scroggo@google.comdd394882012-07-24 20:47:55 +0000165
166 SkAutoMalloc readStorage(totalBytes);
167 writer->flatten(readStorage.get());
168
169 SkReader32 reader;
170 reader.setMemory(readStorage.get(), totalBytes);
171
172 for (size_t len = 0; len < dataSize; len++) {
173 const char* readPtr = static_cast<const char*>(reader.skip(len));
174 // Ensure that the data read is the same as what was written.
175 REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0);
176 // Ensure that the rest is padded with zeroes.
177 const char* stop = readPtr + SkAlign4(len);
178 readPtr += len;
179 while (readPtr < stop) {
180 REPORTER_ASSERT(reporter, *readPtr++ == 0);
181 }
182 }
183}
184
commit-bot@chromium.org536ac5e2014-02-11 22:38:51 +0000185static void testOverwriteT(skiatest::Reporter* reporter, SkWriter32* writer) {
186 const size_t padding = 64;
187
188 const uint32_t uint1 = 0x12345678;
189 const uint32_t uint2 = 0x98765432;
190 const SkScalar scalar1 = 1234.5678f;
191 const SkScalar scalar2 = 9876.5432f;
192 const SkRect rect1 = SkRect::MakeXYWH(1, 2, 3, 4);
193 const SkRect rect2 = SkRect::MakeXYWH(5, 6, 7, 8);
194
195 for (size_t i = 0; i < (padding / 4); ++i) {
196 writer->write32(0);
197 }
198
199 writer->write32(uint1);
200 writer->writeRect(rect1);
201 writer->writeScalar(scalar1);
202
203 for (size_t i = 0; i < (padding / 4); ++i) {
204 writer->write32(0);
205 }
206
207 REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint1);
208 REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)) == rect1);
209 REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>(
210 padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar1);
211
212 writer->overwriteTAt(padding, uint2);
213 writer->overwriteTAt(padding + sizeof(uint32_t), rect2);
214 writer->overwriteTAt(padding + sizeof(uint32_t) + sizeof(SkRect), scalar2);
215
216 REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint2);
217 REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)) == rect2);
218 REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>(
219 padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar2);
220}
221
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000222DEF_TEST(Writer32_dynamic, reporter) {
223 SkWriter32 writer;
224 test1(reporter, &writer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000225
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000226 writer.reset();
227 test2(reporter, &writer);
scroggo@google.comdd394882012-07-24 20:47:55 +0000228
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000229 writer.reset();
230 testWritePad(reporter, &writer);
commit-bot@chromium.org536ac5e2014-02-11 22:38:51 +0000231
232 writer.reset();
233 testOverwriteT(reporter, &writer);
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000234}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000235
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000236DEF_TEST(Writer32_small, reporter) {
237 SkSWriter32<8 * sizeof(intptr_t)> writer;
238 test1(reporter, &writer);
239 writer.reset(); // should just rewind our storage
240 test2(reporter, &writer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000241
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000242 writer.reset();
243 testWritePad(reporter, &writer);
commit-bot@chromium.org536ac5e2014-02-11 22:38:51 +0000244
245 writer.reset();
246 testOverwriteT(reporter, &writer);
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000247}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000248
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000249DEF_TEST(Writer32_large, reporter) {
250 SkSWriter32<1024 * sizeof(intptr_t)> writer;
251 test1(reporter, &writer);
252 writer.reset(); // should just rewind our storage
253 test2(reporter, &writer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000254
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000255 writer.reset();
256 testWritePad(reporter, &writer);
commit-bot@chromium.org536ac5e2014-02-11 22:38:51 +0000257
258 writer.reset();
259 testOverwriteT(reporter, &writer);
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000260}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000261
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000262DEF_TEST(Writer32_misc, reporter) {
mtklein@google.com8b4ba632013-09-03 18:07:01 +0000263 test_reserve(reporter);
commit-bot@chromium.org47fa1362013-08-09 16:03:05 +0000264 test_string_null(reporter);
reed@google.com51c62a62012-06-12 20:47:53 +0000265 test_ptr(reporter);
reed@google.com1cf58d02012-08-30 14:03:21 +0000266 test_rewind(reporter);
reed@google.comdde09562011-05-23 12:21:05 +0000267}
commit-bot@chromium.orgc30dcb92014-03-12 17:04:28 +0000268
reedf70b5312016-03-04 16:36:20 -0800269DEF_TEST(Writer32_data, reporter) {
270 const char* str = "0123456789";
bungeman38d909e2016-08-02 14:40:46 -0700271 sk_sp<SkData> data0(SkData::MakeWithCString(str));
272 sk_sp<SkData> data1(SkData::MakeEmpty());
reedf70b5312016-03-04 16:36:20 -0800273
274 const size_t sizes[] = {
275 SkWriter32::WriteDataSize(nullptr),
bungeman38d909e2016-08-02 14:40:46 -0700276 SkWriter32::WriteDataSize(data0.get()),
277 SkWriter32::WriteDataSize(data1.get()),
reedf70b5312016-03-04 16:36:20 -0800278 };
halcanary9d524f22016-03-29 09:03:52 -0700279
reedf70b5312016-03-04 16:36:20 -0800280 SkSWriter32<1000> writer;
281 size_t sizeWritten = 0;
282
283 writer.writeData(nullptr);
284 sizeWritten += sizes[0];
285 REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
halcanary9d524f22016-03-29 09:03:52 -0700286
bungeman38d909e2016-08-02 14:40:46 -0700287 writer.writeData(data0.get());
reedf70b5312016-03-04 16:36:20 -0800288 sizeWritten += sizes[1];
289 REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
290
bungeman38d909e2016-08-02 14:40:46 -0700291 writer.writeData(data1.get());
reedf70b5312016-03-04 16:36:20 -0800292 sizeWritten += sizes[2];
293 REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
294
reedfde05112016-03-11 13:02:28 -0800295 auto result(writer.snapshotAsData());
reedf70b5312016-03-04 16:36:20 -0800296
297 SkReader32 reader(result->data(), result->size());
reedfde05112016-03-11 13:02:28 -0800298 auto d0(reader.readData()),
299 d1(reader.readData()),
300 d2(reader.readData());
reedf70b5312016-03-04 16:36:20 -0800301
302 REPORTER_ASSERT(reporter, 0 == d0->size());
303 REPORTER_ASSERT(reporter, strlen(str)+1 == d1->size());
304 REPORTER_ASSERT(reporter, !memcmp(str, d1->data(), strlen(str)+1));
305 REPORTER_ASSERT(reporter, 0 == d2->size());
halcanary9d524f22016-03-29 09:03:52 -0700306
reedf70b5312016-03-04 16:36:20 -0800307 REPORTER_ASSERT(reporter, reader.offset() == sizeWritten);
308 REPORTER_ASSERT(reporter, reader.eof());
309}