blob: bcdc559407281c0c43943dc2f95a40f2c7b5653e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comdde09562011-05-23 12:21:05 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comdde09562011-05-23 12:21:05 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
scroggo@google.comdd394882012-07-24 20:47:55 +000011#include "SkRandom.h"
reed@google.comdde09562011-05-23 12:21:05 +000012#include "SkReader32.h"
13#include "SkWriter32.h"
14#include "Test.h"
15
reed@google.com1cf58d02012-08-30 14:03:21 +000016static void check_contents(skiatest::Reporter* reporter, const SkWriter32& writer,
17 const void* expected, size_t size) {
18 SkAutoSMalloc<256> storage(size);
19 REPORTER_ASSERT(reporter, writer.bytesWritten() == size);
20 writer.flatten(storage.get());
21 REPORTER_ASSERT(reporter, !memcmp(storage.get(), expected, size));
22}
23
24static void test_rewind(skiatest::Reporter* reporter) {
25 SkSWriter32<32> writer(32);
26 int32_t array[3] = { 1, 2, 4 };
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +000027
reed@google.com1cf58d02012-08-30 14:03:21 +000028 REPORTER_ASSERT(reporter, 0 == writer.bytesWritten());
29 for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) {
30 writer.writeInt(array[i]);
31 }
32 check_contents(reporter, writer, array, sizeof(array));
33
34 writer.rewindToOffset(2*sizeof(int32_t));
35 REPORTER_ASSERT(reporter, sizeof(array) - 4 == writer.bytesWritten());
36 writer.writeInt(3);
37 REPORTER_ASSERT(reporter, sizeof(array) == writer.bytesWritten());
38 array[2] = 3;
39 check_contents(reporter, writer, array, sizeof(array));
reed@google.comd7e27822012-08-31 20:17:56 +000040
41 // test rewinding past allocated chunks. This used to crash because we
42 // didn't truncate our link-list after freeing trailing blocks
43 {
44 SkWriter32 writer(64);
45 for (int i = 0; i < 100; ++i) {
46 writer.writeInt(i);
47 }
48 REPORTER_ASSERT(reporter, 100*4 == writer.bytesWritten());
49 for (int j = 100*4; j >= 0; j -= 16) {
50 writer.rewindToOffset(j);
51 }
52 REPORTER_ASSERT(reporter, writer.bytesWritten() < 16);
53 }
reed@google.com1cf58d02012-08-30 14:03:21 +000054}
55
reed@google.com51c62a62012-06-12 20:47:53 +000056static void test_ptr(skiatest::Reporter* reporter) {
57 SkSWriter32<32> writer(32);
rmistry@google.comd6176b02012-08-23 18:14:13 +000058
reed@google.com51c62a62012-06-12 20:47:53 +000059 void* p0 = reporter;
60 void* p1 = &writer;
61
62 // try writing ptrs where at least one of them may be at a non-multiple of
63 // 8 boundary, to confirm this works on 64bit machines.
64
65 writer.writePtr(p0);
66 writer.write8(0x33);
67 writer.writePtr(p1);
68 writer.write8(0x66);
69
70 size_t size = writer.size();
71 REPORTER_ASSERT(reporter, 2 * sizeof(void*) + 2 * sizeof(int32_t));
72
73 char buffer[32];
74 SkASSERT(sizeof(buffer) >= size);
75 writer.flatten(buffer);
76
77 SkReader32 reader(buffer, size);
78 REPORTER_ASSERT(reporter, reader.readPtr() == p0);
79 REPORTER_ASSERT(reporter, reader.readInt() == 0x33);
80 REPORTER_ASSERT(reporter, reader.readPtr() == p1);
81 REPORTER_ASSERT(reporter, reader.readInt() == 0x66);
82}
83
reed@google.comdde09562011-05-23 12:21:05 +000084static 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 < SK_ARRAY_COUNT(data); ++i) {
87 REPORTER_ASSERT(reporter, i*4 == writer->size());
88 writer->write32(data[i]);
89 uint32_t* addr = writer->peek32(i * 4);
90 REPORTER_ASSERT(reporter, data[i] == *addr);
91 }
92
93 char buffer[sizeof(data)];
94 REPORTER_ASSERT(reporter, sizeof(buffer) == writer->size());
95 writer->flatten(buffer);
96 REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer)));
97}
98
99static void test2(skiatest::Reporter* reporter, SkWriter32* writer) {
100 static const char gStr[] = "abcdefghimjklmnopqrstuvwxyz";
101 size_t i;
102
103 size_t len = 0;
104 for (i = 0; i <= 26; ++i) {
105 len += SkWriter32::WriteStringSize(gStr, i);
106 writer->writeString(gStr, i);
107 }
108 REPORTER_ASSERT(reporter, writer->size() == len);
109
110 SkAutoMalloc storage(len);
111 writer->flatten(storage.get());
112
113 SkReader32 reader;
114 reader.setMemory(storage.get(), len);
115 for (i = 0; i <= 26; ++i) {
116 REPORTER_ASSERT(reporter, !reader.eof());
117 const char* str = reader.readString(&len);
118 REPORTER_ASSERT(reporter, i == len);
119 REPORTER_ASSERT(reporter, strlen(str) == len);
120 REPORTER_ASSERT(reporter, !memcmp(str, gStr, len));
scroggo@google.come9617eb2012-07-23 13:44:10 +0000121 // Ensure that the align4 of the string is padded with zeroes.
122 size_t alignedSize = SkAlign4(len + 1);
123 for (size_t j = len; j < alignedSize; j++) {
124 REPORTER_ASSERT(reporter, 0 == str[j]);
125 }
reed@google.comdde09562011-05-23 12:21:05 +0000126 }
127 REPORTER_ASSERT(reporter, reader.eof());
128}
129
scroggo@google.comdd394882012-07-24 20:47:55 +0000130static void testWritePad(skiatest::Reporter* reporter, SkWriter32* writer) {
131 // Create some random data to write.
132 const size_t dataSize = 10<<2;
133 SkASSERT(SkIsAlign4(dataSize));
134
135 SkAutoMalloc originalData(dataSize);
136 {
137 SkRandom rand(0);
138 uint32_t* ptr = static_cast<uint32_t*>(originalData.get());
139 uint32_t* stop = ptr + (dataSize>>2);
140 while (ptr < stop) {
141 *ptr++ = rand.nextU();
142 }
143
144 // Write the random data to the writer at different lengths for
145 // different alignments.
146 for (size_t len = 0; len < dataSize; len++) {
147 writer->writePad(originalData.get(), len);
148 }
149 }
150
151 uint32_t totalBytes = writer->size();
152
153 SkAutoMalloc readStorage(totalBytes);
154 writer->flatten(readStorage.get());
155
156 SkReader32 reader;
157 reader.setMemory(readStorage.get(), totalBytes);
158
159 for (size_t len = 0; len < dataSize; len++) {
160 const char* readPtr = static_cast<const char*>(reader.skip(len));
161 // Ensure that the data read is the same as what was written.
162 REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0);
163 // Ensure that the rest is padded with zeroes.
164 const char* stop = readPtr + SkAlign4(len);
165 readPtr += len;
166 while (readPtr < stop) {
167 REPORTER_ASSERT(reporter, *readPtr++ == 0);
168 }
169 }
170}
171
reed@google.comdde09562011-05-23 12:21:05 +0000172static void Tests(skiatest::Reporter* reporter) {
173 // dynamic allocator
174 {
175 SkWriter32 writer(256 * 4);
reed@google.comdde09562011-05-23 12:21:05 +0000176 test1(reporter, &writer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000177
reed@google.comdde09562011-05-23 12:21:05 +0000178 writer.reset();
179 test2(reporter, &writer);
scroggo@google.comdd394882012-07-24 20:47:55 +0000180
181 writer.reset();
182 testWritePad(reporter, &writer);
reed@google.comdde09562011-05-23 12:21:05 +0000183 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000184
reed@google.com5595af12012-12-21 15:36:33 +0000185 // storage-block
reed@google.comdde09562011-05-23 12:21:05 +0000186 {
187 SkWriter32 writer(0);
188 uint32_t storage[256];
reed@google.comdde09562011-05-23 12:21:05 +0000189 writer.reset(storage, sizeof(storage));
reed@google.comdde09562011-05-23 12:21:05 +0000190 test1(reporter, &writer);
191
192 writer.reset(storage, sizeof(storage));
193 test2(reporter, &writer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000194
scroggo@google.comdd394882012-07-24 20:47:55 +0000195 writer.reset(storage, sizeof(storage));
196 testWritePad(reporter, &writer);
reed@google.com5595af12012-12-21 15:36:33 +0000197
198 // try overflowing the storage-block
199 uint32_t smallStorage[8];
200 writer.reset(smallStorage, sizeof(smallStorage));
201 test2(reporter, &writer);
reed@google.comdde09562011-05-23 12:21:05 +0000202 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000203
reed@google.come49aca92012-04-24 21:12:39 +0000204 // small storage
205 {
reed@google.com51c62a62012-06-12 20:47:53 +0000206 SkSWriter32<8 * sizeof(intptr_t)> writer(100);
reed@google.come49aca92012-04-24 21:12:39 +0000207 test1(reporter, &writer);
208 writer.reset(); // should just rewind our storage
209 test2(reporter, &writer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000210
scroggo@google.comdd394882012-07-24 20:47:55 +0000211 writer.reset();
212 testWritePad(reporter, &writer);
reed@google.come49aca92012-04-24 21:12:39 +0000213 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000214
reed@google.come49aca92012-04-24 21:12:39 +0000215 // large storage
216 {
reed@google.com51c62a62012-06-12 20:47:53 +0000217 SkSWriter32<1024 * sizeof(intptr_t)> writer(100);
reed@google.come49aca92012-04-24 21:12:39 +0000218 test1(reporter, &writer);
219 writer.reset(); // should just rewind our storage
220 test2(reporter, &writer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000221
scroggo@google.comdd394882012-07-24 20:47:55 +0000222 writer.reset();
223 testWritePad(reporter, &writer);
reed@google.come49aca92012-04-24 21:12:39 +0000224 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000225
reed@google.com51c62a62012-06-12 20:47:53 +0000226 test_ptr(reporter);
reed@google.com1cf58d02012-08-30 14:03:21 +0000227 test_rewind(reporter);
reed@google.comdde09562011-05-23 12:21:05 +0000228}
229
230#include "TestClassDef.h"
231DEFINE_TESTCLASS("Writer32", Writer32Class, Tests)
232