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 "include/core/SkData.h"
9 #include "include/core/SkRect.h"
10 #include "include/core/SkRefCnt.h"
11 #include "include/core/SkScalar.h"
12 #include "include/core/SkTypes.h"
13 #include "include/private/base/SkAlign.h"
14 #include "include/private/base/SkTemplates.h"
15 #include "src/base/SkAutoMalloc.h"
16 #include "src/base/SkRandom.h"
17 #include "src/core/SkReadBuffer.h"
18 #include "src/core/SkWriter32.h"
19 #include "tests/Test.h"
20
21 #include <array>
22 #include <cstdint>
23 #include <cstring>
24
25 using namespace skia_private;
26
check_contents(skiatest::Reporter * reporter,const SkWriter32 & writer,const void * expected,size_t size)27 static void check_contents(skiatest::Reporter* reporter, const SkWriter32& writer,
28 const void* expected, size_t size) {
29 SkAutoSMalloc<256> storage(size);
30 REPORTER_ASSERT(reporter, writer.bytesWritten() == size);
31 writer.flatten(storage.get());
32 REPORTER_ASSERT(reporter, !memcmp(storage.get(), expected, size));
33 }
34
35
test_reserve(skiatest::Reporter * reporter)36 static void test_reserve(skiatest::Reporter* reporter) {
37 // There used to be a bug where we'd assert your first reservation had to
38 // fit in external storage if you used it. This would crash in debug mode.
39 uint8_t storage[4];
40 SkWriter32 writer(storage, sizeof(storage));
41 writer.reserve(40);
42 }
43
test_string_null(skiatest::Reporter * reporter)44 static void test_string_null(skiatest::Reporter* reporter) {
45 uint8_t storage[8];
46 SkWriter32 writer(storage, sizeof(storage));
47
48 // Can we write nullptr?
49 writer.writeString(nullptr);
50 const int32_t expected[] = { 0x0, 0x0 };
51 check_contents(reporter, writer, expected, sizeof(expected));
52 }
53
test_rewind(skiatest::Reporter * reporter)54 static void test_rewind(skiatest::Reporter* reporter) {
55 SkSWriter32<32> swriter;
56 int32_t array[3] = { 1, 2, 4 };
57
58 REPORTER_ASSERT(reporter, 0 == swriter.bytesWritten());
59 for (size_t i = 0; i < std::size(array); ++i) {
60 swriter.writeInt(array[i]);
61 }
62 check_contents(reporter, swriter, array, sizeof(array));
63
64 swriter.rewindToOffset(2*sizeof(int32_t));
65 REPORTER_ASSERT(reporter, sizeof(array) - 4 == swriter.bytesWritten());
66 swriter.writeInt(3);
67 REPORTER_ASSERT(reporter, sizeof(array) == swriter.bytesWritten());
68 array[2] = 3;
69 check_contents(reporter, swriter, array, sizeof(array));
70
71 // test rewinding past allocated chunks. This used to crash because we
72 // didn't truncate our link-list after freeing trailing blocks
73 SkWriter32 writer;
74 for (int i = 0; i < 100; ++i) {
75 writer.writeInt(i);
76 }
77 REPORTER_ASSERT(reporter, 100*4 == writer.bytesWritten());
78 for (int j = 100*4; j >= 0; j -= 16) {
79 writer.rewindToOffset(j);
80 }
81 REPORTER_ASSERT(reporter, writer.bytesWritten() < 16);
82 }
83
test1(skiatest::Reporter * reporter,SkWriter32 * writer)84 static void test1(skiatest::Reporter* reporter, SkWriter32* writer) {
85 const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
86 for (size_t i = 0; i < std::size(data); ++i) {
87 REPORTER_ASSERT(reporter, i*4 == writer->bytesWritten());
88 writer->write32(data[i]);
89 REPORTER_ASSERT(reporter, data[i] == writer->readTAt<uint32_t>(i * 4));
90 }
91
92 char buffer[sizeof(data)];
93 REPORTER_ASSERT(reporter, sizeof(buffer) == writer->bytesWritten());
94 writer->flatten(buffer);
95 REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer)));
96 }
97
testWritePad(skiatest::Reporter * reporter,SkWriter32 * writer)98 static void testWritePad(skiatest::Reporter* reporter, SkWriter32* writer) {
99 // Create some random data to write.
100 const size_t dataSize = 10;
101
102 AutoTMalloc<uint32_t> originalData(dataSize);
103 {
104 SkRandom rand(0);
105 for (size_t i = 0; i < dataSize; i++) {
106 originalData[(int) i] = rand.nextU();
107 }
108
109 // Write the random data to the writer at different lengths for
110 // different alignments.
111 for (size_t len = 0; len < dataSize; len++) {
112 writer->writePad(originalData.get(), len);
113 }
114 }
115
116 size_t totalBytes = writer->bytesWritten();
117
118 SkAutoMalloc readStorage(totalBytes);
119 writer->flatten(readStorage.get());
120
121 SkReadBuffer reader(readStorage.get(), totalBytes);
122
123 for (size_t len = 0; len < dataSize; len++) {
124 const char* readPtr = static_cast<const char*>(reader.skip(len));
125 // Ensure that the data read is the same as what was written.
126 REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0);
127 // Ensure that the rest is padded with zeroes.
128 const char* stop = readPtr + SkAlign4(len);
129 readPtr += len;
130 while (readPtr < stop) {
131 REPORTER_ASSERT(reporter, *readPtr++ == 0);
132 }
133 }
134 }
135
testOverwriteT(skiatest::Reporter * reporter,SkWriter32 * writer)136 static void testOverwriteT(skiatest::Reporter* reporter, SkWriter32* writer) {
137 const size_t padding = 64;
138
139 const uint32_t uint1 = 0x12345678;
140 const uint32_t uint2 = 0x98765432;
141 const SkScalar scalar1 = 1234.5678f;
142 const SkScalar scalar2 = 9876.5432f;
143 const SkRect rect1 = SkRect::MakeXYWH(1, 2, 3, 4);
144 const SkRect rect2 = SkRect::MakeXYWH(5, 6, 7, 8);
145
146 for (size_t i = 0; i < (padding / 4); ++i) {
147 writer->write32(0);
148 }
149
150 writer->write32(uint1);
151 writer->writeRect(rect1);
152 writer->writeScalar(scalar1);
153
154 for (size_t i = 0; i < (padding / 4); ++i) {
155 writer->write32(0);
156 }
157
158 REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint1);
159 REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)) == rect1);
160 REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>(
161 padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar1);
162
163 writer->overwriteTAt(padding, uint2);
164 writer->overwriteTAt(padding + sizeof(uint32_t), rect2);
165 writer->overwriteTAt(padding + sizeof(uint32_t) + sizeof(SkRect), scalar2);
166
167 REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint2);
168 REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)) == rect2);
169 REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>(
170 padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar2);
171 }
172
DEF_TEST(Writer32_dynamic,reporter)173 DEF_TEST(Writer32_dynamic, reporter) {
174 SkWriter32 writer;
175 test1(reporter, &writer);
176
177 writer.reset();
178 testWritePad(reporter, &writer);
179
180 writer.reset();
181 testOverwriteT(reporter, &writer);
182 }
183
DEF_TEST(Writer32_small,reporter)184 DEF_TEST(Writer32_small, reporter) {
185 SkSWriter32<8 * sizeof(intptr_t)> writer;
186 test1(reporter, &writer);
187
188 writer.reset(); // should just rewind our storage
189 testWritePad(reporter, &writer);
190
191 writer.reset();
192 testOverwriteT(reporter, &writer);
193 }
194
DEF_TEST(Writer32_large,reporter)195 DEF_TEST(Writer32_large, reporter) {
196 SkSWriter32<1024 * sizeof(intptr_t)> writer;
197 test1(reporter, &writer);
198
199 writer.reset(); // should just rewind our storage
200 testWritePad(reporter, &writer);
201
202 writer.reset();
203 testOverwriteT(reporter, &writer);
204 }
205
DEF_TEST(Writer32_misc,reporter)206 DEF_TEST(Writer32_misc, reporter) {
207 test_reserve(reporter);
208 test_string_null(reporter);
209 test_rewind(reporter);
210 }
211
DEF_TEST(Writer32_data,reporter)212 DEF_TEST(Writer32_data, reporter) {
213 const char* str = "0123456789";
214 sk_sp<SkData> data0(SkData::MakeWithCString(str));
215 sk_sp<SkData> data1(SkData::MakeEmpty());
216
217 const size_t sizes[] = {
218 SkWriter32::WriteDataSize(nullptr),
219 SkWriter32::WriteDataSize(data0.get()),
220 SkWriter32::WriteDataSize(data1.get()),
221 };
222
223 SkSWriter32<1000> writer;
224 size_t sizeWritten = 0;
225
226 writer.writeData(nullptr);
227 sizeWritten += sizes[0];
228 REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
229
230 writer.writeData(data0.get());
231 sizeWritten += sizes[1];
232 REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
233
234 writer.writeData(data1.get());
235 sizeWritten += sizes[2];
236 REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
237
238 auto result(writer.snapshotAsData());
239
240 SkReadBuffer reader(result->data(), result->size());
241 auto d0(reader.readByteArrayAsData()),
242 d1(reader.readByteArrayAsData()),
243 d2(reader.readByteArrayAsData());
244
245 REPORTER_ASSERT(reporter, 0 == d0->size());
246 REPORTER_ASSERT(reporter, strlen(str)+1 == d1->size());
247 REPORTER_ASSERT(reporter, !memcmp(str, d1->data(), strlen(str)+1));
248 REPORTER_ASSERT(reporter, 0 == d2->size());
249
250 REPORTER_ASSERT(reporter, reader.offset() == sizeWritten);
251 REPORTER_ASSERT(reporter, reader.eof());
252 }
253