blob: e7ada5f09d1baa24385fd0cd661598424e45fa91 [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"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040012#include "SkRWBuffer.h"
13#include "SkRefCnt.h"
bungeman@google.com11c9a552013-06-03 17:10:35 +000014#include "SkStream.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040015#include "SkString.h"
Florin Malita69270fb2017-06-01 09:43:01 -040016#include "SkTArray.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040017#include "SkTaskGroup.h"
18#include "SkTemplates.h"
19#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000020#include "Test.h"
reed@google.com91bd4592012-07-11 17:24:49 +000021
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040022#include <cstdio>
23#include <cstring>
24#include <memory>
25#include <utility>
26
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000027static void test_is_equal(skiatest::Reporter* reporter,
28 const SkDataTable* a, const SkDataTable* b) {
29 REPORTER_ASSERT(reporter, a->count() == b->count());
30 for (int i = 0; i < a->count(); ++i) {
31 size_t sizea, sizeb;
32 const void* mema = a->at(i, &sizea);
33 const void* memb = b->at(i, &sizeb);
34 REPORTER_ASSERT(reporter, sizea == sizeb);
35 REPORTER_ASSERT(reporter, !memcmp(mema, memb, sizea));
36 }
37}
38
bungemanfeb3c1a2016-08-05 06:51:50 -070039static void test_datatable_is_empty(skiatest::Reporter* reporter, SkDataTable* table) {
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000040 REPORTER_ASSERT(reporter, table->isEmpty());
41 REPORTER_ASSERT(reporter, 0 == table->count());
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000042}
43
44static void test_emptytable(skiatest::Reporter* reporter) {
bungemanfeb3c1a2016-08-05 06:51:50 -070045 sk_sp<SkDataTable> table0(SkDataTable::MakeEmpty());
46 sk_sp<SkDataTable> table1(SkDataTable::MakeCopyArrays(nullptr, nullptr, 0));
47 sk_sp<SkDataTable> table2(SkDataTable::MakeCopyArray(nullptr, 0, 0));
48 sk_sp<SkDataTable> table3(SkDataTable::MakeArrayProc(nullptr, 0, 0, nullptr, nullptr));
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000049
bungemanfeb3c1a2016-08-05 06:51:50 -070050 test_datatable_is_empty(reporter, table0.get());
51 test_datatable_is_empty(reporter, table1.get());
52 test_datatable_is_empty(reporter, table2.get());
53 test_datatable_is_empty(reporter, table3.get());
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000054
bungemanfeb3c1a2016-08-05 06:51:50 -070055 test_is_equal(reporter, table0.get(), table1.get());
56 test_is_equal(reporter, table0.get(), table2.get());
57 test_is_equal(reporter, table0.get(), table3.get());
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000058}
59
reed@google.com8c5c7a92013-04-19 20:16:01 +000060static void test_simpletable(skiatest::Reporter* reporter) {
61 const int idata[] = { 1, 4, 9, 16, 25, 63 };
62 int icount = SK_ARRAY_COUNT(idata);
bungemanfeb3c1a2016-08-05 06:51:50 -070063 sk_sp<SkDataTable> itable(SkDataTable::MakeCopyArray(idata, sizeof(idata[0]), icount));
reed@google.com8c5c7a92013-04-19 20:16:01 +000064 REPORTER_ASSERT(reporter, itable->count() == icount);
65 for (int i = 0; i < icount; ++i) {
66 size_t size;
67 REPORTER_ASSERT(reporter, sizeof(int) == itable->atSize(i));
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000068 REPORTER_ASSERT(reporter, *itable->atT<int>(i, &size) == idata[i]);
reed@google.com8c5c7a92013-04-19 20:16:01 +000069 REPORTER_ASSERT(reporter, sizeof(int) == size);
70 }
71}
72
73static void test_vartable(skiatest::Reporter* reporter) {
74 const char* str[] = {
75 "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
76 };
77 int count = SK_ARRAY_COUNT(str);
78 size_t sizes[SK_ARRAY_COUNT(str)];
79 for (int i = 0; i < count; ++i) {
80 sizes[i] = strlen(str[i]) + 1;
81 }
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000082
bungemanfeb3c1a2016-08-05 06:51:50 -070083 sk_sp<SkDataTable> table(SkDataTable::MakeCopyArrays((const void*const*)str, sizes, count));
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000084
reed@google.com8c5c7a92013-04-19 20:16:01 +000085 REPORTER_ASSERT(reporter, table->count() == count);
86 for (int i = 0; i < count; ++i) {
87 size_t size;
88 REPORTER_ASSERT(reporter, table->atSize(i) == sizes[i]);
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000089 REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size),
reed@google.com8c5c7a92013-04-19 20:16:01 +000090 str[i]));
91 REPORTER_ASSERT(reporter, size == sizes[i]);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000092
reed@google.com8c5c7a92013-04-19 20:16:01 +000093 const char* s = table->atStr(i);
94 REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
95 }
96}
97
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000098static void test_globaltable(skiatest::Reporter* reporter) {
99 static const int gData[] = {
100 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
101 };
102 int count = SK_ARRAY_COUNT(gData);
103
bungemanfeb3c1a2016-08-05 06:51:50 -0700104 sk_sp<SkDataTable> table(
105 SkDataTable::MakeArrayProc(gData, sizeof(gData[0]), count, nullptr, nullptr));
rmistry@google.comc9f3b382013-04-22 12:45:30 +0000106
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000107 REPORTER_ASSERT(reporter, table->count() == count);
108 for (int i = 0; i < count; ++i) {
109 size_t size;
110 REPORTER_ASSERT(reporter, table->atSize(i) == sizeof(int));
111 REPORTER_ASSERT(reporter, *table->atT<const char>(i, &size) == i);
112 REPORTER_ASSERT(reporter, sizeof(int) == size);
113 }
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000114}
115
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000116DEF_TEST(DataTable, reporter) {
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000117 test_emptytable(reporter);
reed@google.com8c5c7a92013-04-19 20:16:01 +0000118 test_simpletable(reporter);
119 test_vartable(reporter);
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000120 test_globaltable(reporter);
reed@google.com8c5c7a92013-04-19 20:16:01 +0000121}
122
reed@google.com1622c992011-06-14 19:22:21 +0000123static void* gGlobal;
124
reed6b7f34e2015-06-17 09:58:24 -0700125static void delete_int_proc(const void* ptr, void* context) {
reed@google.com1622c992011-06-14 19:22:21 +0000126 int* data = (int*)ptr;
127 SkASSERT(context == gGlobal);
128 delete[] data;
129}
130
bungeman38d909e2016-08-02 14:40:46 -0700131static void assert_len(skiatest::Reporter* reporter, const sk_sp<SkData>& ref, size_t len) {
reed@google.com1622c992011-06-14 19:22:21 +0000132 REPORTER_ASSERT(reporter, ref->size() == len);
133}
134
bungeman38d909e2016-08-02 14:40:46 -0700135static void assert_data(skiatest::Reporter* reporter, const sk_sp<SkData>& ref,
reed@google.com1622c992011-06-14 19:22:21 +0000136 const void* data, size_t len) {
137 REPORTER_ASSERT(reporter, ref->size() == len);
138 REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
139}
140
reed@google.comdbc936d2012-06-28 15:40:09 +0000141static void test_cstring(skiatest::Reporter* reporter) {
142 const char str[] = "Hello world";
143 size_t len = strlen(str);
144
bungeman38d909e2016-08-02 14:40:46 -0700145 sk_sp<SkData> r0(SkData::MakeWithCopy(str, len + 1));
146 sk_sp<SkData> r1(SkData::MakeWithCString(str));
reed@google.comdbc936d2012-06-28 15:40:09 +0000147
bungeman38d909e2016-08-02 14:40:46 -0700148 REPORTER_ASSERT(reporter, r0->equals(r1.get()));
reed@google.comdbc936d2012-06-28 15:40:09 +0000149
bungeman38d909e2016-08-02 14:40:46 -0700150 sk_sp<SkData> r2(SkData::MakeWithCString(nullptr));
reed@google.coma63a8512012-07-02 20:29:00 +0000151 REPORTER_ASSERT(reporter, 1 == r2->size());
152 REPORTER_ASSERT(reporter, 0 == *r2->bytes());
reed@google.comdbc936d2012-06-28 15:40:09 +0000153}
154
bungeman@google.com11c9a552013-06-03 17:10:35 +0000155static void test_files(skiatest::Reporter* reporter) {
halcanary87f3ba42015-01-20 09:30:20 -0800156 SkString tmpDir = skiatest::GetTmpDir();
scroggo@google.com99d43ff2013-06-07 14:30:36 +0000157 if (tmpDir.isEmpty()) {
bungeman@google.com11c9a552013-06-03 17:10:35 +0000158 return;
159 }
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000160
tfarinaa8e2e152014-07-28 19:26:58 -0700161 SkString path = SkOSPath::Join(tmpDir.c_str(), "data_test");
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000162
bungeman@google.com11c9a552013-06-03 17:10:35 +0000163 const char s[] = "abcdefghijklmnopqrstuvwxyz";
164 {
165 SkFILEWStream writer(path.c_str());
166 if (!writer.isValid()) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000167 ERRORF(reporter, "Failed to create tmp file %s\n", path.c_str());
bungeman@google.com11c9a552013-06-03 17:10:35 +0000168 return;
169 }
170 writer.write(s, 26);
171 }
172
halcanaryd76be9c2015-11-20 13:47:49 -0800173 FILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag);
bungeman38d909e2016-08-02 14:40:46 -0700174 sk_sp<SkData> r1(SkData::MakeFromFILE(file));
halcanary96fcdcc2015-08-27 07:41:13 -0700175 REPORTER_ASSERT(reporter, r1.get() != nullptr);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000176 REPORTER_ASSERT(reporter, r1->size() == 26);
177 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r1->data()), s, 26) == 0);
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000178
bungeman@google.com11c9a552013-06-03 17:10:35 +0000179 int fd = sk_fileno(file);
bungeman38d909e2016-08-02 14:40:46 -0700180 sk_sp<SkData> r2(SkData::MakeFromFD(fd));
halcanary96fcdcc2015-08-27 07:41:13 -0700181 REPORTER_ASSERT(reporter, r2.get() != nullptr);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000182 REPORTER_ASSERT(reporter, r2->size() == 26);
183 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 26) == 0);
184}
185
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000186DEF_TEST(Data, reporter) {
reed@google.com1622c992011-06-14 19:22:21 +0000187 const char* str = "We the people, in order to form a more perfect union.";
188 const int N = 10;
189
bungeman38d909e2016-08-02 14:40:46 -0700190 sk_sp<SkData> r0(SkData::MakeEmpty());
191 sk_sp<SkData> r1(SkData::MakeWithCopy(str, strlen(str)));
192 sk_sp<SkData> r2(SkData::MakeWithProc(new int[N], N*sizeof(int), delete_int_proc, gGlobal));
193 sk_sp<SkData> r3(SkData::MakeSubset(r1.get(), 7, 6));
reed@google.com1622c992011-06-14 19:22:21 +0000194
reed@google.com1622c992011-06-14 19:22:21 +0000195 assert_len(reporter, r0, 0);
196 assert_len(reporter, r1, strlen(str));
197 assert_len(reporter, r2, N * sizeof(int));
198 assert_len(reporter, r3, 6);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000199
reed@google.com1622c992011-06-14 19:22:21 +0000200 assert_data(reporter, r1, str, strlen(str));
201 assert_data(reporter, r3, "people", 6);
202
bungeman38d909e2016-08-02 14:40:46 -0700203 sk_sp<SkData> tmp(SkData::MakeSubset(r1.get(), strlen(str), 10));
reed@google.com1622c992011-06-14 19:22:21 +0000204 assert_len(reporter, tmp, 0);
bungeman38d909e2016-08-02 14:40:46 -0700205 tmp = SkData::MakeSubset(r1.get(), 0, 0);
reed@google.com1622c992011-06-14 19:22:21 +0000206 assert_len(reporter, tmp, 0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000207
reed@google.comdbc936d2012-06-28 15:40:09 +0000208 test_cstring(reporter);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000209 test_files(reporter);
reed@google.com1622c992011-06-14 19:22:21 +0000210}
reed5b6db072015-04-28 17:50:31 -0700211
212///////////////////////////////////////////////////////////////////////////////////////////////////
reed5b6db072015-04-28 17:50:31 -0700213
214const char gABC[] = "abcdefghijklmnopqrstuvwxyz";
215
216static void check_abcs(skiatest::Reporter* reporter, const char buffer[], size_t size) {
217 REPORTER_ASSERT(reporter, size % 26 == 0);
218 for (size_t offset = 0; offset < size; offset += 26) {
219 REPORTER_ASSERT(reporter, !memcmp(&buffer[offset], gABC, 26));
220 }
221}
222
scroggo63516402016-04-22 06:59:01 -0700223// stream should contain an integral number of copies of gABC.
reed5b6db072015-04-28 17:50:31 -0700224static void check_alphabet_stream(skiatest::Reporter* reporter, SkStream* stream) {
225 REPORTER_ASSERT(reporter, stream->hasLength());
226 size_t size = stream->getLength();
227 REPORTER_ASSERT(reporter, size % 26 == 0);
228
229 SkAutoTMalloc<char> storage(size);
230 char* array = storage.get();
231 size_t bytesRead = stream->read(array, size);
232 REPORTER_ASSERT(reporter, bytesRead == size);
233 check_abcs(reporter, array, size);
234
235 // try checking backwards
236 for (size_t offset = size; offset > 0; offset -= 26) {
237 REPORTER_ASSERT(reporter, stream->seek(offset - 26));
238 REPORTER_ASSERT(reporter, stream->getPosition() == offset - 26);
239 REPORTER_ASSERT(reporter, stream->read(array, 26) == 26);
240 check_abcs(reporter, array, 26);
241 REPORTER_ASSERT(reporter, stream->getPosition() == offset);
242 }
243}
244
245// reader should contains an integral number of copies of gABC.
246static void check_alphabet_buffer(skiatest::Reporter* reporter, const SkROBuffer* reader) {
247 size_t size = reader->size();
248 REPORTER_ASSERT(reporter, size % 26 == 0);
halcanary9d524f22016-03-29 09:03:52 -0700249
reed5b6db072015-04-28 17:50:31 -0700250 SkAutoTMalloc<char> storage(size);
251 SkROBuffer::Iter iter(reader);
252 size_t offset = 0;
253 do {
254 SkASSERT(offset + iter.size() <= size);
255 memcpy(storage.get() + offset, iter.data(), iter.size());
256 offset += iter.size();
257 } while (iter.next());
258 REPORTER_ASSERT(reporter, offset == size);
259 check_abcs(reporter, storage.get(), size);
260}
261
262DEF_TEST(RWBuffer, reporter) {
263 // Knowing that the default capacity is 4096, choose N large enough so we force it to use
264 // multiple buffers internally.
Florin Malita69270fb2017-06-01 09:43:01 -0400265 static constexpr int N = 1000;
266 SkSTArray<N, sk_sp<SkROBuffer>> readers;
267 SkSTArray<N, std::unique_ptr<SkStream>> streams;
reed5b6db072015-04-28 17:50:31 -0700268
269 {
270 SkRWBuffer buffer;
271 for (int i = 0; i < N; ++i) {
reed377add72016-04-08 12:47:14 -0700272 buffer.append(gABC, 26);
Florin Malita69270fb2017-06-01 09:43:01 -0400273 readers.push_back(buffer.makeROBufferSnapshot());
274 streams.push_back(buffer.makeStreamSnapshot());
reed5b6db072015-04-28 17:50:31 -0700275 }
276 REPORTER_ASSERT(reporter, N*26 == buffer.size());
277 }
278
scroggo63516402016-04-22 06:59:01 -0700279 // Verify that although the SkRWBuffer's destructor has run, the readers are still valid.
reed5b6db072015-04-28 17:50:31 -0700280 for (int i = 0; i < N; ++i) {
281 REPORTER_ASSERT(reporter, (i + 1) * 26U == readers[i]->size());
Florin Malita69270fb2017-06-01 09:43:01 -0400282 check_alphabet_buffer(reporter, readers[i].get());
283 check_alphabet_stream(reporter, streams[i].get());
reed5b6db072015-04-28 17:50:31 -0700284 }
285}
scroggob512aaa2016-01-11 06:38:00 -0800286
scroggo63516402016-04-22 06:59:01 -0700287DEF_TEST(RWBuffer_threaded, reporter) {
288 // Knowing that the default capacity is 4096, choose N large enough so we force it to use
289 // multiple buffers internally.
290 const int N = 1000;
291 SkTaskGroup tasks;
292 SkRWBuffer buffer;
293 for (int i = 0; i < N; ++i) {
294 buffer.append(gABC, 26);
Florin Malita69270fb2017-06-01 09:43:01 -0400295 sk_sp<SkROBuffer> reader = buffer.makeROBufferSnapshot();
296 SkStream* stream = buffer.makeStreamSnapshot().release();
scroggo63516402016-04-22 06:59:01 -0700297 REPORTER_ASSERT(reporter, reader->size() == buffer.size());
298 REPORTER_ASSERT(reporter, stream->getLength() == buffer.size());
299
300 // reader's copy constructor will ref the SkROBuffer, which will be unreffed
301 // when the task ends.
302 // Ownership of stream is passed to the task, which will delete it.
303 tasks.add([reporter, i, reader, stream] {
304 REPORTER_ASSERT(reporter, (i + 1) * 26U == reader->size());
305 REPORTER_ASSERT(reporter, stream->getLength() == reader->size());
306 check_alphabet_buffer(reporter, reader.get());
307 check_alphabet_stream(reporter, stream);
308 REPORTER_ASSERT(reporter, stream->rewind());
309 delete stream;
310 });
311 }
312 REPORTER_ASSERT(reporter, N*26 == buffer.size());
313 tasks.wait();
314}
315
scroggob512aaa2016-01-11 06:38:00 -0800316// Tests that it is safe to call SkROBuffer::Iter::size() when exhausted.
317DEF_TEST(RWBuffer_size, r) {
318 SkRWBuffer buffer;
319 buffer.append(gABC, 26);
320
Florin Malita69270fb2017-06-01 09:43:01 -0400321 sk_sp<SkROBuffer> roBuffer(buffer.makeROBufferSnapshot());
Hal Canary342b7ac2016-11-04 11:49:42 -0400322 SkROBuffer::Iter iter(roBuffer.get());
scroggob512aaa2016-01-11 06:38:00 -0800323 REPORTER_ASSERT(r, iter.data());
324 REPORTER_ASSERT(r, iter.size() == 26);
325
326 // There is only one block in this buffer.
327 REPORTER_ASSERT(r, !iter.next());
328 REPORTER_ASSERT(r, 0 == iter.size());
329}
scroggo9dec5d22016-02-16 06:59:18 -0800330
scroggo63516402016-04-22 06:59:01 -0700331// Tests that operations (including the destructor) are safe on an SkRWBuffer
332// without any data appended.
scroggo9dec5d22016-02-16 06:59:18 -0800333DEF_TEST(RWBuffer_noAppend, r) {
334 SkRWBuffer buffer;
335 REPORTER_ASSERT(r, 0 == buffer.size());
scroggo63516402016-04-22 06:59:01 -0700336
Florin Malita69270fb2017-06-01 09:43:01 -0400337 sk_sp<SkROBuffer> roBuffer = buffer.makeROBufferSnapshot();
scroggo63516402016-04-22 06:59:01 -0700338 REPORTER_ASSERT(r, roBuffer);
339 if (roBuffer) {
340 REPORTER_ASSERT(r, roBuffer->size() == 0);
341 SkROBuffer::Iter iter(roBuffer.get());
342 REPORTER_ASSERT(r, iter.size() == 0);
343 REPORTER_ASSERT(r, !iter.data());
344 REPORTER_ASSERT(r, !iter.next());
345 }
346
Florin Malita69270fb2017-06-01 09:43:01 -0400347 std::unique_ptr<SkStream> stream(buffer.makeStreamSnapshot());
scroggo63516402016-04-22 06:59:01 -0700348 REPORTER_ASSERT(r, stream);
349 if (stream) {
350 REPORTER_ASSERT(r, stream->hasLength());
351 REPORTER_ASSERT(r, stream->getLength() == 0);
352 REPORTER_ASSERT(r, stream->skip(10) == 0);
353 }
scroggo9dec5d22016-02-16 06:59:18 -0800354}