blob: 2032fefae75c2fa3264676190a964c1ff448ad44 [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
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00008#include "Test.h"
9#include "TestClassDef.h"
scroggo@google.comdd394882012-07-24 20:47:55 +000010#include "SkRandom.h"
reed@google.comdde09562011-05-23 12:21:05 +000011#include "SkReader32.h"
12#include "SkWriter32.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];
27 SkWriter32 writer(0, storage, sizeof(storage));
28 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];
33 SkWriter32 writer(0, storage, sizeof(storage));
commit-bot@chromium.org47fa1362013-08-09 16:03:05 +000034
35 // Can we write NULL?
36 writer.writeString(NULL);
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) {
42 SkSWriter32<32> writer(32);
43 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 {
61 SkWriter32 writer(64);
62 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) {
74 SkSWriter32<32> writer(32);
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]);
106 uint32_t* addr = writer->peek32(i * 4);
107 REPORTER_ASSERT(reporter, data[i] == *addr);
108 }
109
110 char buffer[sizeof(data)];
reed@google.com44699382013-10-31 17:28:30 +0000111 REPORTER_ASSERT(reporter, sizeof(buffer) == writer->bytesWritten());
reed@google.comdde09562011-05-23 12:21:05 +0000112 writer->flatten(buffer);
113 REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer)));
114}
115
116static void test2(skiatest::Reporter* reporter, SkWriter32* writer) {
117 static const char gStr[] = "abcdefghimjklmnopqrstuvwxyz";
118 size_t i;
119
120 size_t len = 0;
121 for (i = 0; i <= 26; ++i) {
122 len += SkWriter32::WriteStringSize(gStr, i);
123 writer->writeString(gStr, i);
124 }
reed@google.com44699382013-10-31 17:28:30 +0000125 REPORTER_ASSERT(reporter, writer->bytesWritten() == len);
reed@google.comdde09562011-05-23 12:21:05 +0000126
127 SkAutoMalloc storage(len);
128 writer->flatten(storage.get());
129
130 SkReader32 reader;
131 reader.setMemory(storage.get(), len);
132 for (i = 0; i <= 26; ++i) {
133 REPORTER_ASSERT(reporter, !reader.eof());
134 const char* str = reader.readString(&len);
135 REPORTER_ASSERT(reporter, i == len);
136 REPORTER_ASSERT(reporter, strlen(str) == len);
137 REPORTER_ASSERT(reporter, !memcmp(str, gStr, len));
scroggo@google.come9617eb2012-07-23 13:44:10 +0000138 // Ensure that the align4 of the string is padded with zeroes.
139 size_t alignedSize = SkAlign4(len + 1);
140 for (size_t j = len; j < alignedSize; j++) {
141 REPORTER_ASSERT(reporter, 0 == str[j]);
142 }
reed@google.comdde09562011-05-23 12:21:05 +0000143 }
144 REPORTER_ASSERT(reporter, reader.eof());
145}
146
scroggo@google.comdd394882012-07-24 20:47:55 +0000147static void testWritePad(skiatest::Reporter* reporter, SkWriter32* writer) {
148 // Create some random data to write.
149 const size_t dataSize = 10<<2;
150 SkASSERT(SkIsAlign4(dataSize));
151
152 SkAutoMalloc originalData(dataSize);
153 {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000154 SkRandom rand(0);
scroggo@google.comdd394882012-07-24 20:47:55 +0000155 uint32_t* ptr = static_cast<uint32_t*>(originalData.get());
156 uint32_t* stop = ptr + (dataSize>>2);
157 while (ptr < stop) {
158 *ptr++ = rand.nextU();
159 }
160
161 // Write the random data to the writer at different lengths for
162 // different alignments.
163 for (size_t len = 0; len < dataSize; len++) {
164 writer->writePad(originalData.get(), len);
165 }
166 }
167
reed@google.com44699382013-10-31 17:28:30 +0000168 uint32_t totalBytes = writer->bytesWritten();
scroggo@google.comdd394882012-07-24 20:47:55 +0000169
170 SkAutoMalloc readStorage(totalBytes);
171 writer->flatten(readStorage.get());
172
173 SkReader32 reader;
174 reader.setMemory(readStorage.get(), totalBytes);
175
176 for (size_t len = 0; len < dataSize; len++) {
177 const char* readPtr = static_cast<const char*>(reader.skip(len));
178 // Ensure that the data read is the same as what was written.
179 REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0);
180 // Ensure that the rest is padded with zeroes.
181 const char* stop = readPtr + SkAlign4(len);
182 readPtr += len;
183 while (readPtr < stop) {
184 REPORTER_ASSERT(reporter, *readPtr++ == 0);
185 }
186 }
187}
188
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000189DEF_TEST(Writer32, reporter) {
reed@google.comdde09562011-05-23 12:21:05 +0000190 // dynamic allocator
191 {
192 SkWriter32 writer(256 * 4);
reed@google.comdde09562011-05-23 12:21:05 +0000193 test1(reporter, &writer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000194
reed@google.comdde09562011-05-23 12:21:05 +0000195 writer.reset();
196 test2(reporter, &writer);
scroggo@google.comdd394882012-07-24 20:47:55 +0000197
198 writer.reset();
199 testWritePad(reporter, &writer);
reed@google.comdde09562011-05-23 12:21:05 +0000200 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000201
reed@google.com5595af12012-12-21 15:36:33 +0000202 // storage-block
reed@google.comdde09562011-05-23 12:21:05 +0000203 {
204 SkWriter32 writer(0);
205 uint32_t storage[256];
reed@google.comdde09562011-05-23 12:21:05 +0000206 writer.reset(storage, sizeof(storage));
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000207 // These three writes are small enough to fit in storage.
reed@google.comdde09562011-05-23 12:21:05 +0000208 test1(reporter, &writer);
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000209 REPORTER_ASSERT(reporter, writer.wroteOnlyToStorage());
reed@google.comdde09562011-05-23 12:21:05 +0000210
211 writer.reset(storage, sizeof(storage));
212 test2(reporter, &writer);
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000213 REPORTER_ASSERT(reporter, writer.wroteOnlyToStorage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000214
scroggo@google.comdd394882012-07-24 20:47:55 +0000215 writer.reset(storage, sizeof(storage));
216 testWritePad(reporter, &writer);
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000217 REPORTER_ASSERT(reporter, writer.wroteOnlyToStorage());
skia.committer@gmail.comb89a03c2012-12-22 02:02:33 +0000218
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000219 // Try overflowing the storage-block.
reed@google.com5595af12012-12-21 15:36:33 +0000220 uint32_t smallStorage[8];
221 writer.reset(smallStorage, sizeof(smallStorage));
222 test2(reporter, &writer);
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000223 REPORTER_ASSERT(reporter, !writer.wroteOnlyToStorage());
reed@google.comdde09562011-05-23 12:21:05 +0000224 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000225
reed@google.come49aca92012-04-24 21:12:39 +0000226 // small storage
227 {
reed@google.com51c62a62012-06-12 20:47:53 +0000228 SkSWriter32<8 * sizeof(intptr_t)> writer(100);
reed@google.come49aca92012-04-24 21:12:39 +0000229 test1(reporter, &writer);
230 writer.reset(); // should just rewind our storage
231 test2(reporter, &writer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000232
scroggo@google.comdd394882012-07-24 20:47:55 +0000233 writer.reset();
234 testWritePad(reporter, &writer);
reed@google.come49aca92012-04-24 21:12:39 +0000235 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000236
reed@google.come49aca92012-04-24 21:12:39 +0000237 // large storage
238 {
reed@google.com51c62a62012-06-12 20:47:53 +0000239 SkSWriter32<1024 * sizeof(intptr_t)> writer(100);
reed@google.come49aca92012-04-24 21:12:39 +0000240 test1(reporter, &writer);
241 writer.reset(); // should just rewind our storage
242 test2(reporter, &writer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000243
scroggo@google.comdd394882012-07-24 20:47:55 +0000244 writer.reset();
245 testWritePad(reporter, &writer);
reed@google.come49aca92012-04-24 21:12:39 +0000246 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000247
mtklein@google.com8b4ba632013-09-03 18:07:01 +0000248 test_reserve(reporter);
commit-bot@chromium.org47fa1362013-08-09 16:03:05 +0000249 test_string_null(reporter);
reed@google.com51c62a62012-06-12 20:47:53 +0000250 test_ptr(reporter);
reed@google.com1cf58d02012-08-30 14:03:21 +0000251 test_rewind(reporter);
reed@google.comdde09562011-05-23 12:21:05 +0000252}