blob: 74d511cc281d73cfec02ec7128bc862c2beb9e1f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
reed@google.com8d0b5772011-06-24 13:07:31 +00008#include "SkData.h"
reed@google.com8c5c7a92013-04-19 20:16:01 +00009#include "SkDataTable.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050011#include "SkOSPath.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000012#include "SkReadBuffer.h"
13#include "SkWriteBuffer.h"
bungeman@google.com11c9a552013-06-03 17:10:35 +000014#include "SkStream.h"
Florin Malita69270fb2017-06-01 09:43:01 -040015#include "SkTArray.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000016#include "Test.h"
reed@google.com91bd4592012-07-11 17:24:49 +000017
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000018static void test_is_equal(skiatest::Reporter* reporter,
19 const SkDataTable* a, const SkDataTable* b) {
20 REPORTER_ASSERT(reporter, a->count() == b->count());
21 for (int i = 0; i < a->count(); ++i) {
22 size_t sizea, sizeb;
23 const void* mema = a->at(i, &sizea);
24 const void* memb = b->at(i, &sizeb);
25 REPORTER_ASSERT(reporter, sizea == sizeb);
26 REPORTER_ASSERT(reporter, !memcmp(mema, memb, sizea));
27 }
28}
29
bungemanfeb3c1a2016-08-05 06:51:50 -070030static void test_datatable_is_empty(skiatest::Reporter* reporter, SkDataTable* table) {
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000031 REPORTER_ASSERT(reporter, table->isEmpty());
32 REPORTER_ASSERT(reporter, 0 == table->count());
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000033}
34
35static void test_emptytable(skiatest::Reporter* reporter) {
bungemanfeb3c1a2016-08-05 06:51:50 -070036 sk_sp<SkDataTable> table0(SkDataTable::MakeEmpty());
37 sk_sp<SkDataTable> table1(SkDataTable::MakeCopyArrays(nullptr, nullptr, 0));
38 sk_sp<SkDataTable> table2(SkDataTable::MakeCopyArray(nullptr, 0, 0));
39 sk_sp<SkDataTable> table3(SkDataTable::MakeArrayProc(nullptr, 0, 0, nullptr, nullptr));
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000040
bungemanfeb3c1a2016-08-05 06:51:50 -070041 test_datatable_is_empty(reporter, table0.get());
42 test_datatable_is_empty(reporter, table1.get());
43 test_datatable_is_empty(reporter, table2.get());
44 test_datatable_is_empty(reporter, table3.get());
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000045
bungemanfeb3c1a2016-08-05 06:51:50 -070046 test_is_equal(reporter, table0.get(), table1.get());
47 test_is_equal(reporter, table0.get(), table2.get());
48 test_is_equal(reporter, table0.get(), table3.get());
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000049}
50
reed@google.com8c5c7a92013-04-19 20:16:01 +000051static void test_simpletable(skiatest::Reporter* reporter) {
52 const int idata[] = { 1, 4, 9, 16, 25, 63 };
53 int icount = SK_ARRAY_COUNT(idata);
bungemanfeb3c1a2016-08-05 06:51:50 -070054 sk_sp<SkDataTable> itable(SkDataTable::MakeCopyArray(idata, sizeof(idata[0]), icount));
reed@google.com8c5c7a92013-04-19 20:16:01 +000055 REPORTER_ASSERT(reporter, itable->count() == icount);
56 for (int i = 0; i < icount; ++i) {
57 size_t size;
58 REPORTER_ASSERT(reporter, sizeof(int) == itable->atSize(i));
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000059 REPORTER_ASSERT(reporter, *itable->atT<int>(i, &size) == idata[i]);
reed@google.com8c5c7a92013-04-19 20:16:01 +000060 REPORTER_ASSERT(reporter, sizeof(int) == size);
61 }
62}
63
64static void test_vartable(skiatest::Reporter* reporter) {
65 const char* str[] = {
66 "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
67 };
68 int count = SK_ARRAY_COUNT(str);
69 size_t sizes[SK_ARRAY_COUNT(str)];
70 for (int i = 0; i < count; ++i) {
71 sizes[i] = strlen(str[i]) + 1;
72 }
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000073
bungemanfeb3c1a2016-08-05 06:51:50 -070074 sk_sp<SkDataTable> table(SkDataTable::MakeCopyArrays((const void*const*)str, sizes, count));
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000075
reed@google.com8c5c7a92013-04-19 20:16:01 +000076 REPORTER_ASSERT(reporter, table->count() == count);
77 for (int i = 0; i < count; ++i) {
78 size_t size;
79 REPORTER_ASSERT(reporter, table->atSize(i) == sizes[i]);
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000080 REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size),
reed@google.com8c5c7a92013-04-19 20:16:01 +000081 str[i]));
82 REPORTER_ASSERT(reporter, size == sizes[i]);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000083
reed@google.com8c5c7a92013-04-19 20:16:01 +000084 const char* s = table->atStr(i);
85 REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
86 }
87}
88
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000089static void test_globaltable(skiatest::Reporter* reporter) {
90 static const int gData[] = {
91 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
92 };
93 int count = SK_ARRAY_COUNT(gData);
94
bungemanfeb3c1a2016-08-05 06:51:50 -070095 sk_sp<SkDataTable> table(
96 SkDataTable::MakeArrayProc(gData, sizeof(gData[0]), count, nullptr, nullptr));
rmistry@google.comc9f3b382013-04-22 12:45:30 +000097
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000098 REPORTER_ASSERT(reporter, table->count() == count);
99 for (int i = 0; i < count; ++i) {
100 size_t size;
101 REPORTER_ASSERT(reporter, table->atSize(i) == sizeof(int));
102 REPORTER_ASSERT(reporter, *table->atT<const char>(i, &size) == i);
103 REPORTER_ASSERT(reporter, sizeof(int) == size);
104 }
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000105}
106
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000107DEF_TEST(DataTable, reporter) {
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000108 test_emptytable(reporter);
reed@google.com8c5c7a92013-04-19 20:16:01 +0000109 test_simpletable(reporter);
110 test_vartable(reporter);
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000111 test_globaltable(reporter);
reed@google.com8c5c7a92013-04-19 20:16:01 +0000112}
113
reed@google.com1622c992011-06-14 19:22:21 +0000114static void* gGlobal;
115
reed6b7f34e2015-06-17 09:58:24 -0700116static void delete_int_proc(const void* ptr, void* context) {
reed@google.com1622c992011-06-14 19:22:21 +0000117 int* data = (int*)ptr;
118 SkASSERT(context == gGlobal);
119 delete[] data;
120}
121
bungeman38d909e2016-08-02 14:40:46 -0700122static void assert_len(skiatest::Reporter* reporter, const sk_sp<SkData>& ref, size_t len) {
reed@google.com1622c992011-06-14 19:22:21 +0000123 REPORTER_ASSERT(reporter, ref->size() == len);
124}
125
bungeman38d909e2016-08-02 14:40:46 -0700126static void assert_data(skiatest::Reporter* reporter, const sk_sp<SkData>& ref,
reed@google.com1622c992011-06-14 19:22:21 +0000127 const void* data, size_t len) {
128 REPORTER_ASSERT(reporter, ref->size() == len);
129 REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
130}
131
reed@google.comdbc936d2012-06-28 15:40:09 +0000132static void test_cstring(skiatest::Reporter* reporter) {
133 const char str[] = "Hello world";
134 size_t len = strlen(str);
135
bungeman38d909e2016-08-02 14:40:46 -0700136 sk_sp<SkData> r0(SkData::MakeWithCopy(str, len + 1));
137 sk_sp<SkData> r1(SkData::MakeWithCString(str));
reed@google.comdbc936d2012-06-28 15:40:09 +0000138
bungeman38d909e2016-08-02 14:40:46 -0700139 REPORTER_ASSERT(reporter, r0->equals(r1.get()));
reed@google.comdbc936d2012-06-28 15:40:09 +0000140
bungeman38d909e2016-08-02 14:40:46 -0700141 sk_sp<SkData> r2(SkData::MakeWithCString(nullptr));
reed@google.coma63a8512012-07-02 20:29:00 +0000142 REPORTER_ASSERT(reporter, 1 == r2->size());
143 REPORTER_ASSERT(reporter, 0 == *r2->bytes());
reed@google.comdbc936d2012-06-28 15:40:09 +0000144}
145
bungeman@google.com11c9a552013-06-03 17:10:35 +0000146static void test_files(skiatest::Reporter* reporter) {
halcanary87f3ba42015-01-20 09:30:20 -0800147 SkString tmpDir = skiatest::GetTmpDir();
scroggo@google.com99d43ff2013-06-07 14:30:36 +0000148 if (tmpDir.isEmpty()) {
bungeman@google.com11c9a552013-06-03 17:10:35 +0000149 return;
150 }
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000151
tfarinaa8e2e152014-07-28 19:26:58 -0700152 SkString path = SkOSPath::Join(tmpDir.c_str(), "data_test");
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000153
bungeman@google.com11c9a552013-06-03 17:10:35 +0000154 const char s[] = "abcdefghijklmnopqrstuvwxyz";
155 {
156 SkFILEWStream writer(path.c_str());
157 if (!writer.isValid()) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000158 ERRORF(reporter, "Failed to create tmp file %s\n", path.c_str());
bungeman@google.com11c9a552013-06-03 17:10:35 +0000159 return;
160 }
161 writer.write(s, 26);
162 }
163
halcanaryd76be9c2015-11-20 13:47:49 -0800164 FILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag);
bungeman38d909e2016-08-02 14:40:46 -0700165 sk_sp<SkData> r1(SkData::MakeFromFILE(file));
halcanary96fcdcc2015-08-27 07:41:13 -0700166 REPORTER_ASSERT(reporter, r1.get() != nullptr);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000167 REPORTER_ASSERT(reporter, r1->size() == 26);
168 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r1->data()), s, 26) == 0);
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000169
bungeman@google.com11c9a552013-06-03 17:10:35 +0000170 int fd = sk_fileno(file);
bungeman38d909e2016-08-02 14:40:46 -0700171 sk_sp<SkData> r2(SkData::MakeFromFD(fd));
halcanary96fcdcc2015-08-27 07:41:13 -0700172 REPORTER_ASSERT(reporter, r2.get() != nullptr);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000173 REPORTER_ASSERT(reporter, r2->size() == 26);
174 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 26) == 0);
175}
176
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000177DEF_TEST(Data, reporter) {
reed@google.com1622c992011-06-14 19:22:21 +0000178 const char* str = "We the people, in order to form a more perfect union.";
179 const int N = 10;
180
bungeman38d909e2016-08-02 14:40:46 -0700181 sk_sp<SkData> r0(SkData::MakeEmpty());
182 sk_sp<SkData> r1(SkData::MakeWithCopy(str, strlen(str)));
183 sk_sp<SkData> r2(SkData::MakeWithProc(new int[N], N*sizeof(int), delete_int_proc, gGlobal));
184 sk_sp<SkData> r3(SkData::MakeSubset(r1.get(), 7, 6));
reed@google.com1622c992011-06-14 19:22:21 +0000185
reed@google.com1622c992011-06-14 19:22:21 +0000186 assert_len(reporter, r0, 0);
187 assert_len(reporter, r1, strlen(str));
188 assert_len(reporter, r2, N * sizeof(int));
189 assert_len(reporter, r3, 6);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000190
reed@google.com1622c992011-06-14 19:22:21 +0000191 assert_data(reporter, r1, str, strlen(str));
192 assert_data(reporter, r3, "people", 6);
193
bungeman38d909e2016-08-02 14:40:46 -0700194 sk_sp<SkData> tmp(SkData::MakeSubset(r1.get(), strlen(str), 10));
reed@google.com1622c992011-06-14 19:22:21 +0000195 assert_len(reporter, tmp, 0);
bungeman38d909e2016-08-02 14:40:46 -0700196 tmp = SkData::MakeSubset(r1.get(), 0, 0);
reed@google.com1622c992011-06-14 19:22:21 +0000197 assert_len(reporter, tmp, 0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000198
reed@google.comdbc936d2012-06-28 15:40:09 +0000199 test_cstring(reporter);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000200 test_files(reporter);
reed@google.com1622c992011-06-14 19:22:21 +0000201}
reed5b6db072015-04-28 17:50:31 -0700202
203///////////////////////////////////////////////////////////////////////////////////////////////////
204#include "SkRWBuffer.h"
205
206const char gABC[] = "abcdefghijklmnopqrstuvwxyz";
207
208static void check_abcs(skiatest::Reporter* reporter, const char buffer[], size_t size) {
209 REPORTER_ASSERT(reporter, size % 26 == 0);
210 for (size_t offset = 0; offset < size; offset += 26) {
211 REPORTER_ASSERT(reporter, !memcmp(&buffer[offset], gABC, 26));
212 }
213}
214
scroggo63516402016-04-22 06:59:01 -0700215// stream should contain an integral number of copies of gABC.
reed5b6db072015-04-28 17:50:31 -0700216static void check_alphabet_stream(skiatest::Reporter* reporter, SkStream* stream) {
217 REPORTER_ASSERT(reporter, stream->hasLength());
218 size_t size = stream->getLength();
219 REPORTER_ASSERT(reporter, size % 26 == 0);
220
221 SkAutoTMalloc<char> storage(size);
222 char* array = storage.get();
223 size_t bytesRead = stream->read(array, size);
224 REPORTER_ASSERT(reporter, bytesRead == size);
225 check_abcs(reporter, array, size);
226
227 // try checking backwards
228 for (size_t offset = size; offset > 0; offset -= 26) {
229 REPORTER_ASSERT(reporter, stream->seek(offset - 26));
230 REPORTER_ASSERT(reporter, stream->getPosition() == offset - 26);
231 REPORTER_ASSERT(reporter, stream->read(array, 26) == 26);
232 check_abcs(reporter, array, 26);
233 REPORTER_ASSERT(reporter, stream->getPosition() == offset);
234 }
235}
236
237// reader should contains an integral number of copies of gABC.
238static void check_alphabet_buffer(skiatest::Reporter* reporter, const SkROBuffer* reader) {
239 size_t size = reader->size();
240 REPORTER_ASSERT(reporter, size % 26 == 0);
halcanary9d524f22016-03-29 09:03:52 -0700241
reed5b6db072015-04-28 17:50:31 -0700242 SkAutoTMalloc<char> storage(size);
243 SkROBuffer::Iter iter(reader);
244 size_t offset = 0;
245 do {
246 SkASSERT(offset + iter.size() <= size);
247 memcpy(storage.get() + offset, iter.data(), iter.size());
248 offset += iter.size();
249 } while (iter.next());
250 REPORTER_ASSERT(reporter, offset == size);
251 check_abcs(reporter, storage.get(), size);
252}
253
scroggo63516402016-04-22 06:59:01 -0700254#include "SkTaskGroup.h"
255
reed5b6db072015-04-28 17:50:31 -0700256DEF_TEST(RWBuffer, reporter) {
257 // Knowing that the default capacity is 4096, choose N large enough so we force it to use
258 // multiple buffers internally.
Florin Malita69270fb2017-06-01 09:43:01 -0400259 static constexpr int N = 1000;
260 SkSTArray<N, sk_sp<SkROBuffer>> readers;
261 SkSTArray<N, std::unique_ptr<SkStream>> streams;
reed5b6db072015-04-28 17:50:31 -0700262
263 {
264 SkRWBuffer buffer;
265 for (int i = 0; i < N; ++i) {
reed377add72016-04-08 12:47:14 -0700266 buffer.append(gABC, 26);
Florin Malita69270fb2017-06-01 09:43:01 -0400267 readers.push_back(buffer.makeROBufferSnapshot());
268 streams.push_back(buffer.makeStreamSnapshot());
reed5b6db072015-04-28 17:50:31 -0700269 }
270 REPORTER_ASSERT(reporter, N*26 == buffer.size());
271 }
272
scroggo63516402016-04-22 06:59:01 -0700273 // Verify that although the SkRWBuffer's destructor has run, the readers are still valid.
reed5b6db072015-04-28 17:50:31 -0700274 for (int i = 0; i < N; ++i) {
275 REPORTER_ASSERT(reporter, (i + 1) * 26U == readers[i]->size());
Florin Malita69270fb2017-06-01 09:43:01 -0400276 check_alphabet_buffer(reporter, readers[i].get());
277 check_alphabet_stream(reporter, streams[i].get());
reed5b6db072015-04-28 17:50:31 -0700278 }
279}
scroggob512aaa2016-01-11 06:38:00 -0800280
scroggo63516402016-04-22 06:59:01 -0700281DEF_TEST(RWBuffer_threaded, reporter) {
282 // Knowing that the default capacity is 4096, choose N large enough so we force it to use
283 // multiple buffers internally.
284 const int N = 1000;
285 SkTaskGroup tasks;
286 SkRWBuffer buffer;
287 for (int i = 0; i < N; ++i) {
288 buffer.append(gABC, 26);
Florin Malita69270fb2017-06-01 09:43:01 -0400289 sk_sp<SkROBuffer> reader = buffer.makeROBufferSnapshot();
290 SkStream* stream = buffer.makeStreamSnapshot().release();
scroggo63516402016-04-22 06:59:01 -0700291 REPORTER_ASSERT(reporter, reader->size() == buffer.size());
292 REPORTER_ASSERT(reporter, stream->getLength() == buffer.size());
293
294 // reader's copy constructor will ref the SkROBuffer, which will be unreffed
295 // when the task ends.
296 // Ownership of stream is passed to the task, which will delete it.
297 tasks.add([reporter, i, reader, stream] {
298 REPORTER_ASSERT(reporter, (i + 1) * 26U == reader->size());
299 REPORTER_ASSERT(reporter, stream->getLength() == reader->size());
300 check_alphabet_buffer(reporter, reader.get());
301 check_alphabet_stream(reporter, stream);
302 REPORTER_ASSERT(reporter, stream->rewind());
303 delete stream;
304 });
305 }
306 REPORTER_ASSERT(reporter, N*26 == buffer.size());
307 tasks.wait();
308}
309
scroggob512aaa2016-01-11 06:38:00 -0800310// Tests that it is safe to call SkROBuffer::Iter::size() when exhausted.
311DEF_TEST(RWBuffer_size, r) {
312 SkRWBuffer buffer;
313 buffer.append(gABC, 26);
314
Florin Malita69270fb2017-06-01 09:43:01 -0400315 sk_sp<SkROBuffer> roBuffer(buffer.makeROBufferSnapshot());
Hal Canary342b7ac2016-11-04 11:49:42 -0400316 SkROBuffer::Iter iter(roBuffer.get());
scroggob512aaa2016-01-11 06:38:00 -0800317 REPORTER_ASSERT(r, iter.data());
318 REPORTER_ASSERT(r, iter.size() == 26);
319
320 // There is only one block in this buffer.
321 REPORTER_ASSERT(r, !iter.next());
322 REPORTER_ASSERT(r, 0 == iter.size());
323}
scroggo9dec5d22016-02-16 06:59:18 -0800324
scroggo63516402016-04-22 06:59:01 -0700325// Tests that operations (including the destructor) are safe on an SkRWBuffer
326// without any data appended.
scroggo9dec5d22016-02-16 06:59:18 -0800327DEF_TEST(RWBuffer_noAppend, r) {
328 SkRWBuffer buffer;
329 REPORTER_ASSERT(r, 0 == buffer.size());
scroggo63516402016-04-22 06:59:01 -0700330
Florin Malita69270fb2017-06-01 09:43:01 -0400331 sk_sp<SkROBuffer> roBuffer = buffer.makeROBufferSnapshot();
scroggo63516402016-04-22 06:59:01 -0700332 REPORTER_ASSERT(r, roBuffer);
333 if (roBuffer) {
334 REPORTER_ASSERT(r, roBuffer->size() == 0);
335 SkROBuffer::Iter iter(roBuffer.get());
336 REPORTER_ASSERT(r, iter.size() == 0);
337 REPORTER_ASSERT(r, !iter.data());
338 REPORTER_ASSERT(r, !iter.next());
339 }
340
Florin Malita69270fb2017-06-01 09:43:01 -0400341 std::unique_ptr<SkStream> stream(buffer.makeStreamSnapshot());
scroggo63516402016-04-22 06:59:01 -0700342 REPORTER_ASSERT(r, stream);
343 if (stream) {
344 REPORTER_ASSERT(r, stream->hasLength());
345 REPORTER_ASSERT(r, stream->getLength() == 0);
346 REPORTER_ASSERT(r, stream->skip(10) == 0);
347 }
scroggo9dec5d22016-02-16 06:59:18 -0800348}