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/SkDataTable.h"
10 #include "include/core/SkRWBuffer.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkStream.h"
13 #include "include/core/SkString.h"
14 #include "include/core/SkTypes.h"
15 #include "include/private/SkTArray.h"
16 #include "include/private/SkTemplates.h"
17 #include "src/core/SkOSFile.h"
18 #include "src/core/SkTaskGroup.h"
19 #include "src/utils/SkOSPath.h"
20 #include "tests/Test.h"
21
22 #include <cstdio>
23 #include <cstring>
24 #include <memory>
25
test_is_equal(skiatest::Reporter * reporter,const SkDataTable * a,const SkDataTable * b)26 static void test_is_equal(skiatest::Reporter* reporter,
27 const SkDataTable* a, const SkDataTable* b) {
28 REPORTER_ASSERT(reporter, a->count() == b->count());
29 for (int i = 0; i < a->count(); ++i) {
30 size_t sizea, sizeb;
31 const void* mema = a->at(i, &sizea);
32 const void* memb = b->at(i, &sizeb);
33 REPORTER_ASSERT(reporter, sizea == sizeb);
34 REPORTER_ASSERT(reporter, !memcmp(mema, memb, sizea));
35 }
36 }
37
test_datatable_is_empty(skiatest::Reporter * reporter,SkDataTable * table)38 static void test_datatable_is_empty(skiatest::Reporter* reporter, SkDataTable* table) {
39 REPORTER_ASSERT(reporter, table->isEmpty());
40 REPORTER_ASSERT(reporter, 0 == table->count());
41 }
42
test_emptytable(skiatest::Reporter * reporter)43 static void test_emptytable(skiatest::Reporter* reporter) {
44 sk_sp<SkDataTable> table0(SkDataTable::MakeEmpty());
45 sk_sp<SkDataTable> table1(SkDataTable::MakeCopyArrays(nullptr, nullptr, 0));
46 sk_sp<SkDataTable> table2(SkDataTable::MakeCopyArray(nullptr, 0, 0));
47 sk_sp<SkDataTable> table3(SkDataTable::MakeArrayProc(nullptr, 0, 0, nullptr, nullptr));
48
49 test_datatable_is_empty(reporter, table0.get());
50 test_datatable_is_empty(reporter, table1.get());
51 test_datatable_is_empty(reporter, table2.get());
52 test_datatable_is_empty(reporter, table3.get());
53
54 test_is_equal(reporter, table0.get(), table1.get());
55 test_is_equal(reporter, table0.get(), table2.get());
56 test_is_equal(reporter, table0.get(), table3.get());
57 }
58
test_simpletable(skiatest::Reporter * reporter)59 static void test_simpletable(skiatest::Reporter* reporter) {
60 const int idata[] = { 1, 4, 9, 16, 25, 63 };
61 int icount = SK_ARRAY_COUNT(idata);
62 sk_sp<SkDataTable> itable(SkDataTable::MakeCopyArray(idata, sizeof(idata[0]), icount));
63 REPORTER_ASSERT(reporter, itable->count() == icount);
64 for (int i = 0; i < icount; ++i) {
65 size_t size;
66 REPORTER_ASSERT(reporter, sizeof(int) == itable->atSize(i));
67 REPORTER_ASSERT(reporter, *itable->atT<int>(i, &size) == idata[i]);
68 REPORTER_ASSERT(reporter, sizeof(int) == size);
69 }
70 }
71
test_vartable(skiatest::Reporter * reporter)72 static void test_vartable(skiatest::Reporter* reporter) {
73 const char* str[] = {
74 "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
75 };
76 int count = SK_ARRAY_COUNT(str);
77 size_t sizes[SK_ARRAY_COUNT(str)];
78 for (int i = 0; i < count; ++i) {
79 sizes[i] = strlen(str[i]) + 1;
80 }
81
82 sk_sp<SkDataTable> table(SkDataTable::MakeCopyArrays((const void*const*)str, sizes, count));
83
84 REPORTER_ASSERT(reporter, table->count() == count);
85 for (int i = 0; i < count; ++i) {
86 size_t size;
87 REPORTER_ASSERT(reporter, table->atSize(i) == sizes[i]);
88 REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size),
89 str[i]));
90 REPORTER_ASSERT(reporter, size == sizes[i]);
91
92 const char* s = table->atStr(i);
93 REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
94 }
95 }
96
test_globaltable(skiatest::Reporter * reporter)97 static void test_globaltable(skiatest::Reporter* reporter) {
98 static const int gData[] = {
99 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
100 };
101 int count = SK_ARRAY_COUNT(gData);
102
103 sk_sp<SkDataTable> table(
104 SkDataTable::MakeArrayProc(gData, sizeof(gData[0]), count, nullptr, nullptr));
105
106 REPORTER_ASSERT(reporter, table->count() == count);
107 for (int i = 0; i < count; ++i) {
108 size_t size;
109 REPORTER_ASSERT(reporter, table->atSize(i) == sizeof(int));
110 REPORTER_ASSERT(reporter, *table->atT<const char>(i, &size) == i);
111 REPORTER_ASSERT(reporter, sizeof(int) == size);
112 }
113 }
114
DEF_TEST(DataTable,reporter)115 DEF_TEST(DataTable, reporter) {
116 test_emptytable(reporter);
117 test_simpletable(reporter);
118 test_vartable(reporter);
119 test_globaltable(reporter);
120 }
121
122 static void* gGlobal;
123
delete_int_proc(const void * ptr,void * context)124 static void delete_int_proc(const void* ptr, void* context) {
125 int* data = (int*)ptr;
126 SkASSERT(context == gGlobal);
127 delete[] data;
128 }
129
assert_len(skiatest::Reporter * reporter,const sk_sp<SkData> & ref,size_t len)130 static void assert_len(skiatest::Reporter* reporter, const sk_sp<SkData>& ref, size_t len) {
131 REPORTER_ASSERT(reporter, ref->size() == len);
132 }
133
assert_data(skiatest::Reporter * reporter,const sk_sp<SkData> & ref,const void * data,size_t len)134 static void assert_data(skiatest::Reporter* reporter, const sk_sp<SkData>& ref,
135 const void* data, size_t len) {
136 REPORTER_ASSERT(reporter, ref->size() == len);
137 REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
138 }
139
test_cstring(skiatest::Reporter * reporter)140 static void test_cstring(skiatest::Reporter* reporter) {
141 const char str[] = "Hello world";
142 size_t len = strlen(str);
143
144 sk_sp<SkData> r0(SkData::MakeWithCopy(str, len + 1));
145 sk_sp<SkData> r1(SkData::MakeWithCString(str));
146
147 REPORTER_ASSERT(reporter, r0->equals(r1.get()));
148
149 sk_sp<SkData> r2(SkData::MakeWithCString(nullptr));
150 REPORTER_ASSERT(reporter, 1 == r2->size());
151 REPORTER_ASSERT(reporter, 0 == *r2->bytes());
152 }
153
test_files(skiatest::Reporter * reporter)154 static void test_files(skiatest::Reporter* reporter) {
155 SkString tmpDir = skiatest::GetTmpDir();
156 if (tmpDir.isEmpty()) {
157 return;
158 }
159
160 SkString path = SkOSPath::Join(tmpDir.c_str(), "data_test");
161
162 const char s[] = "abcdefghijklmnopqrstuvwxyz";
163 {
164 SkFILEWStream writer(path.c_str());
165 if (!writer.isValid()) {
166 ERRORF(reporter, "Failed to create tmp file %s\n", path.c_str());
167 return;
168 }
169 writer.write(s, 26);
170 }
171
172 FILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag);
173 sk_sp<SkData> r1(SkData::MakeFromFILE(file));
174 REPORTER_ASSERT(reporter, r1.get() != nullptr);
175 REPORTER_ASSERT(reporter, r1->size() == 26);
176 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r1->data()), s, 26) == 0);
177
178 int fd = sk_fileno(file);
179 sk_sp<SkData> r2(SkData::MakeFromFD(fd));
180 REPORTER_ASSERT(reporter, r2.get() != nullptr);
181 REPORTER_ASSERT(reporter, r2->size() == 26);
182 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 26) == 0);
183 }
184
DEF_TEST(Data,reporter)185 DEF_TEST(Data, reporter) {
186 const char* str = "We the people, in order to form a more perfect union.";
187 const int N = 10;
188
189 sk_sp<SkData> r0(SkData::MakeEmpty());
190 sk_sp<SkData> r1(SkData::MakeWithCopy(str, strlen(str)));
191 sk_sp<SkData> r2(SkData::MakeWithProc(new int[N], N*sizeof(int), delete_int_proc, gGlobal));
192 sk_sp<SkData> r3(SkData::MakeSubset(r1.get(), 7, 6));
193
194 assert_len(reporter, r0, 0);
195 assert_len(reporter, r1, strlen(str));
196 assert_len(reporter, r2, N * sizeof(int));
197 assert_len(reporter, r3, 6);
198
199 assert_data(reporter, r1, str, strlen(str));
200 assert_data(reporter, r3, "people", 6);
201
202 sk_sp<SkData> tmp(SkData::MakeSubset(r1.get(), strlen(str), 10));
203 assert_len(reporter, tmp, 0);
204 tmp = SkData::MakeSubset(r1.get(), 0, 0);
205 assert_len(reporter, tmp, 0);
206
207 test_cstring(reporter);
208 test_files(reporter);
209 }
210
211 ///////////////////////////////////////////////////////////////////////////////////////////////////
212
213 const char gABC[] = "abcdefghijklmnopqrstuvwxyz";
214
check_abcs(skiatest::Reporter * reporter,const char buffer[],size_t size)215 static void check_abcs(skiatest::Reporter* reporter, const char buffer[], size_t size) {
216 REPORTER_ASSERT(reporter, size % 26 == 0);
217 for (size_t offset = 0; offset < size; offset += 26) {
218 REPORTER_ASSERT(reporter, !memcmp(&buffer[offset], gABC, 26));
219 }
220 }
221
222 // stream should contain an integral number of copies of gABC.
check_alphabet_stream(skiatest::Reporter * reporter,SkStream * stream)223 static void check_alphabet_stream(skiatest::Reporter* reporter, SkStream* stream) {
224 REPORTER_ASSERT(reporter, stream->hasLength());
225 size_t size = stream->getLength();
226 REPORTER_ASSERT(reporter, size % 26 == 0);
227
228 SkAutoTMalloc<char> storage(size);
229 char* array = storage.get();
230 size_t bytesRead = stream->read(array, size);
231 REPORTER_ASSERT(reporter, bytesRead == size);
232 check_abcs(reporter, array, size);
233
234 // try checking backwards
235 for (size_t offset = size; offset > 0; offset -= 26) {
236 REPORTER_ASSERT(reporter, stream->seek(offset - 26));
237 REPORTER_ASSERT(reporter, stream->getPosition() == offset - 26);
238 REPORTER_ASSERT(reporter, stream->read(array, 26) == 26);
239 check_abcs(reporter, array, 26);
240 REPORTER_ASSERT(reporter, stream->getPosition() == offset);
241 }
242 }
243
244 // reader should contains an integral number of copies of gABC.
check_alphabet_buffer(skiatest::Reporter * reporter,const SkROBuffer * reader)245 static void check_alphabet_buffer(skiatest::Reporter* reporter, const SkROBuffer* reader) {
246 size_t size = reader->size();
247 REPORTER_ASSERT(reporter, size % 26 == 0);
248
249 SkAutoTMalloc<char> storage(size);
250 SkROBuffer::Iter iter(reader);
251 size_t offset = 0;
252 do {
253 SkASSERT(offset + iter.size() <= size);
254 memcpy(storage.get() + offset, iter.data(), iter.size());
255 offset += iter.size();
256 } while (iter.next());
257 REPORTER_ASSERT(reporter, offset == size);
258 check_abcs(reporter, storage.get(), size);
259 }
260
DEF_TEST(RWBuffer,reporter)261 DEF_TEST(RWBuffer, reporter) {
262 // Knowing that the default capacity is 4096, choose N large enough so we force it to use
263 // multiple buffers internally.
264 static constexpr int N = 1000;
265 SkSTArray<N, sk_sp<SkROBuffer>> readers;
266 SkSTArray<N, std::unique_ptr<SkStream>> streams;
267
268 {
269 SkRWBuffer buffer;
270 for (int i = 0; i < N; ++i) {
271 buffer.append(gABC, 26);
272 readers.push_back(buffer.makeROBufferSnapshot());
273 streams.push_back(buffer.makeStreamSnapshot());
274 }
275 REPORTER_ASSERT(reporter, N*26 == buffer.size());
276 }
277
278 // Verify that although the SkRWBuffer's destructor has run, the readers are still valid.
279 for (int i = 0; i < N; ++i) {
280 REPORTER_ASSERT(reporter, (i + 1) * 26U == readers[i]->size());
281 check_alphabet_buffer(reporter, readers[i].get());
282 check_alphabet_stream(reporter, streams[i].get());
283 }
284 }
285
DEF_TEST(RWBuffer_threaded,reporter)286 DEF_TEST(RWBuffer_threaded, reporter) {
287 // Knowing that the default capacity is 4096, choose N large enough so we force it to use
288 // multiple buffers internally.
289 const int N = 1000;
290 SkTaskGroup tasks;
291 SkRWBuffer buffer;
292 for (int i = 0; i < N; ++i) {
293 buffer.append(gABC, 26);
294 sk_sp<SkROBuffer> reader = buffer.makeROBufferSnapshot();
295 SkStream* stream = buffer.makeStreamSnapshot().release();
296 REPORTER_ASSERT(reporter, reader->size() == buffer.size());
297 REPORTER_ASSERT(reporter, stream->getLength() == buffer.size());
298
299 // reader's copy constructor will ref the SkROBuffer, which will be unreffed
300 // when the task ends.
301 // Ownership of stream is passed to the task, which will delete it.
302 tasks.add([reporter, i, reader, stream] {
303 REPORTER_ASSERT(reporter, (i + 1) * 26U == reader->size());
304 REPORTER_ASSERT(reporter, stream->getLength() == reader->size());
305 check_alphabet_buffer(reporter, reader.get());
306 check_alphabet_stream(reporter, stream);
307 REPORTER_ASSERT(reporter, stream->rewind());
308 delete stream;
309 });
310 }
311 REPORTER_ASSERT(reporter, N*26 == buffer.size());
312 tasks.wait();
313 }
314
315 // Tests that it is safe to call SkROBuffer::Iter::size() when exhausted.
DEF_TEST(RWBuffer_size,r)316 DEF_TEST(RWBuffer_size, r) {
317 SkRWBuffer buffer;
318 buffer.append(gABC, 26);
319
320 sk_sp<SkROBuffer> roBuffer(buffer.makeROBufferSnapshot());
321 SkROBuffer::Iter iter(roBuffer.get());
322 REPORTER_ASSERT(r, iter.data());
323 REPORTER_ASSERT(r, iter.size() == 26);
324
325 // There is only one block in this buffer.
326 REPORTER_ASSERT(r, !iter.next());
327 REPORTER_ASSERT(r, 0 == iter.size());
328 }
329
330 // Tests that operations (including the destructor) are safe on an SkRWBuffer
331 // without any data appended.
DEF_TEST(RWBuffer_noAppend,r)332 DEF_TEST(RWBuffer_noAppend, r) {
333 SkRWBuffer buffer;
334 REPORTER_ASSERT(r, 0 == buffer.size());
335
336 sk_sp<SkROBuffer> roBuffer = buffer.makeROBufferSnapshot();
337 REPORTER_ASSERT(r, roBuffer);
338 if (roBuffer) {
339 REPORTER_ASSERT(r, roBuffer->size() == 0);
340 SkROBuffer::Iter iter(roBuffer.get());
341 REPORTER_ASSERT(r, iter.size() == 0);
342 REPORTER_ASSERT(r, !iter.data());
343 REPORTER_ASSERT(r, !iter.next());
344 }
345
346 std::unique_ptr<SkStream> stream(buffer.makeStreamSnapshot());
347 REPORTER_ASSERT(r, stream);
348 if (stream) {
349 REPORTER_ASSERT(r, stream->hasLength());
350 REPORTER_ASSERT(r, stream->getLength() == 0);
351 REPORTER_ASSERT(r, stream->skip(10) == 0);
352 }
353 }
354