blob: d0cafc3dde680806de50753c5e8aa020d3231058 [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"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000015#include "Test.h"
reed@google.com91bd4592012-07-11 17:24:49 +000016
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000017static void test_is_equal(skiatest::Reporter* reporter,
18 const SkDataTable* a, const SkDataTable* b) {
19 REPORTER_ASSERT(reporter, a->count() == b->count());
20 for (int i = 0; i < a->count(); ++i) {
21 size_t sizea, sizeb;
22 const void* mema = a->at(i, &sizea);
23 const void* memb = b->at(i, &sizeb);
24 REPORTER_ASSERT(reporter, sizea == sizeb);
25 REPORTER_ASSERT(reporter, !memcmp(mema, memb, sizea));
26 }
27}
28
bungemanfeb3c1a2016-08-05 06:51:50 -070029static void test_datatable_is_empty(skiatest::Reporter* reporter, SkDataTable* table) {
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000030 REPORTER_ASSERT(reporter, table->isEmpty());
31 REPORTER_ASSERT(reporter, 0 == table->count());
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000032}
33
34static void test_emptytable(skiatest::Reporter* reporter) {
bungemanfeb3c1a2016-08-05 06:51:50 -070035 sk_sp<SkDataTable> table0(SkDataTable::MakeEmpty());
36 sk_sp<SkDataTable> table1(SkDataTable::MakeCopyArrays(nullptr, nullptr, 0));
37 sk_sp<SkDataTable> table2(SkDataTable::MakeCopyArray(nullptr, 0, 0));
38 sk_sp<SkDataTable> table3(SkDataTable::MakeArrayProc(nullptr, 0, 0, nullptr, nullptr));
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000039
bungemanfeb3c1a2016-08-05 06:51:50 -070040 test_datatable_is_empty(reporter, table0.get());
41 test_datatable_is_empty(reporter, table1.get());
42 test_datatable_is_empty(reporter, table2.get());
43 test_datatable_is_empty(reporter, table3.get());
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000044
bungemanfeb3c1a2016-08-05 06:51:50 -070045 test_is_equal(reporter, table0.get(), table1.get());
46 test_is_equal(reporter, table0.get(), table2.get());
47 test_is_equal(reporter, table0.get(), table3.get());
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000048}
49
reed@google.com8c5c7a92013-04-19 20:16:01 +000050static void test_simpletable(skiatest::Reporter* reporter) {
51 const int idata[] = { 1, 4, 9, 16, 25, 63 };
52 int icount = SK_ARRAY_COUNT(idata);
bungemanfeb3c1a2016-08-05 06:51:50 -070053 sk_sp<SkDataTable> itable(SkDataTable::MakeCopyArray(idata, sizeof(idata[0]), icount));
reed@google.com8c5c7a92013-04-19 20:16:01 +000054 REPORTER_ASSERT(reporter, itable->count() == icount);
55 for (int i = 0; i < icount; ++i) {
56 size_t size;
57 REPORTER_ASSERT(reporter, sizeof(int) == itable->atSize(i));
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000058 REPORTER_ASSERT(reporter, *itable->atT<int>(i, &size) == idata[i]);
reed@google.com8c5c7a92013-04-19 20:16:01 +000059 REPORTER_ASSERT(reporter, sizeof(int) == size);
60 }
61}
62
63static void test_vartable(skiatest::Reporter* reporter) {
64 const char* str[] = {
65 "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
66 };
67 int count = SK_ARRAY_COUNT(str);
68 size_t sizes[SK_ARRAY_COUNT(str)];
69 for (int i = 0; i < count; ++i) {
70 sizes[i] = strlen(str[i]) + 1;
71 }
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000072
bungemanfeb3c1a2016-08-05 06:51:50 -070073 sk_sp<SkDataTable> table(SkDataTable::MakeCopyArrays((const void*const*)str, sizes, count));
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000074
reed@google.com8c5c7a92013-04-19 20:16:01 +000075 REPORTER_ASSERT(reporter, table->count() == count);
76 for (int i = 0; i < count; ++i) {
77 size_t size;
78 REPORTER_ASSERT(reporter, table->atSize(i) == sizes[i]);
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000079 REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size),
reed@google.com8c5c7a92013-04-19 20:16:01 +000080 str[i]));
81 REPORTER_ASSERT(reporter, size == sizes[i]);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000082
reed@google.com8c5c7a92013-04-19 20:16:01 +000083 const char* s = table->atStr(i);
84 REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
85 }
86}
87
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000088static void test_globaltable(skiatest::Reporter* reporter) {
89 static const int gData[] = {
90 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
91 };
92 int count = SK_ARRAY_COUNT(gData);
93
bungemanfeb3c1a2016-08-05 06:51:50 -070094 sk_sp<SkDataTable> table(
95 SkDataTable::MakeArrayProc(gData, sizeof(gData[0]), count, nullptr, nullptr));
rmistry@google.comc9f3b382013-04-22 12:45:30 +000096
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000097 REPORTER_ASSERT(reporter, table->count() == count);
98 for (int i = 0; i < count; ++i) {
99 size_t size;
100 REPORTER_ASSERT(reporter, table->atSize(i) == sizeof(int));
101 REPORTER_ASSERT(reporter, *table->atT<const char>(i, &size) == i);
102 REPORTER_ASSERT(reporter, sizeof(int) == size);
103 }
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000104}
105
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000106DEF_TEST(DataTable, reporter) {
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000107 test_emptytable(reporter);
reed@google.com8c5c7a92013-04-19 20:16:01 +0000108 test_simpletable(reporter);
109 test_vartable(reporter);
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000110 test_globaltable(reporter);
reed@google.com8c5c7a92013-04-19 20:16:01 +0000111}
112
reed@google.com1622c992011-06-14 19:22:21 +0000113static void* gGlobal;
114
reed6b7f34e2015-06-17 09:58:24 -0700115static void delete_int_proc(const void* ptr, void* context) {
reed@google.com1622c992011-06-14 19:22:21 +0000116 int* data = (int*)ptr;
117 SkASSERT(context == gGlobal);
118 delete[] data;
119}
120
bungeman38d909e2016-08-02 14:40:46 -0700121static void assert_len(skiatest::Reporter* reporter, const sk_sp<SkData>& ref, size_t len) {
reed@google.com1622c992011-06-14 19:22:21 +0000122 REPORTER_ASSERT(reporter, ref->size() == len);
123}
124
bungeman38d909e2016-08-02 14:40:46 -0700125static void assert_data(skiatest::Reporter* reporter, const sk_sp<SkData>& ref,
reed@google.com1622c992011-06-14 19:22:21 +0000126 const void* data, size_t len) {
127 REPORTER_ASSERT(reporter, ref->size() == len);
128 REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
129}
130
reed@google.comdbc936d2012-06-28 15:40:09 +0000131static void test_cstring(skiatest::Reporter* reporter) {
132 const char str[] = "Hello world";
133 size_t len = strlen(str);
134
bungeman38d909e2016-08-02 14:40:46 -0700135 sk_sp<SkData> r0(SkData::MakeWithCopy(str, len + 1));
136 sk_sp<SkData> r1(SkData::MakeWithCString(str));
reed@google.comdbc936d2012-06-28 15:40:09 +0000137
bungeman38d909e2016-08-02 14:40:46 -0700138 REPORTER_ASSERT(reporter, r0->equals(r1.get()));
reed@google.comdbc936d2012-06-28 15:40:09 +0000139
bungeman38d909e2016-08-02 14:40:46 -0700140 sk_sp<SkData> r2(SkData::MakeWithCString(nullptr));
reed@google.coma63a8512012-07-02 20:29:00 +0000141 REPORTER_ASSERT(reporter, 1 == r2->size());
142 REPORTER_ASSERT(reporter, 0 == *r2->bytes());
reed@google.comdbc936d2012-06-28 15:40:09 +0000143}
144
bungeman@google.com11c9a552013-06-03 17:10:35 +0000145static void test_files(skiatest::Reporter* reporter) {
halcanary87f3ba42015-01-20 09:30:20 -0800146 SkString tmpDir = skiatest::GetTmpDir();
scroggo@google.com99d43ff2013-06-07 14:30:36 +0000147 if (tmpDir.isEmpty()) {
bungeman@google.com11c9a552013-06-03 17:10:35 +0000148 return;
149 }
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000150
tfarinaa8e2e152014-07-28 19:26:58 -0700151 SkString path = SkOSPath::Join(tmpDir.c_str(), "data_test");
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000152
bungeman@google.com11c9a552013-06-03 17:10:35 +0000153 const char s[] = "abcdefghijklmnopqrstuvwxyz";
154 {
155 SkFILEWStream writer(path.c_str());
156 if (!writer.isValid()) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000157 ERRORF(reporter, "Failed to create tmp file %s\n", path.c_str());
bungeman@google.com11c9a552013-06-03 17:10:35 +0000158 return;
159 }
160 writer.write(s, 26);
161 }
162
halcanaryd76be9c2015-11-20 13:47:49 -0800163 FILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag);
bungeman38d909e2016-08-02 14:40:46 -0700164 sk_sp<SkData> r1(SkData::MakeFromFILE(file));
halcanary96fcdcc2015-08-27 07:41:13 -0700165 REPORTER_ASSERT(reporter, r1.get() != nullptr);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000166 REPORTER_ASSERT(reporter, r1->size() == 26);
167 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r1->data()), s, 26) == 0);
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000168
bungeman@google.com11c9a552013-06-03 17:10:35 +0000169 int fd = sk_fileno(file);
bungeman38d909e2016-08-02 14:40:46 -0700170 sk_sp<SkData> r2(SkData::MakeFromFD(fd));
halcanary96fcdcc2015-08-27 07:41:13 -0700171 REPORTER_ASSERT(reporter, r2.get() != nullptr);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000172 REPORTER_ASSERT(reporter, r2->size() == 26);
173 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 26) == 0);
174}
175
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000176DEF_TEST(Data, reporter) {
reed@google.com1622c992011-06-14 19:22:21 +0000177 const char* str = "We the people, in order to form a more perfect union.";
178 const int N = 10;
179
bungeman38d909e2016-08-02 14:40:46 -0700180 sk_sp<SkData> r0(SkData::MakeEmpty());
181 sk_sp<SkData> r1(SkData::MakeWithCopy(str, strlen(str)));
182 sk_sp<SkData> r2(SkData::MakeWithProc(new int[N], N*sizeof(int), delete_int_proc, gGlobal));
183 sk_sp<SkData> r3(SkData::MakeSubset(r1.get(), 7, 6));
reed@google.com1622c992011-06-14 19:22:21 +0000184
reed@google.com1622c992011-06-14 19:22:21 +0000185 assert_len(reporter, r0, 0);
186 assert_len(reporter, r1, strlen(str));
187 assert_len(reporter, r2, N * sizeof(int));
188 assert_len(reporter, r3, 6);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000189
reed@google.com1622c992011-06-14 19:22:21 +0000190 assert_data(reporter, r1, str, strlen(str));
191 assert_data(reporter, r3, "people", 6);
192
bungeman38d909e2016-08-02 14:40:46 -0700193 sk_sp<SkData> tmp(SkData::MakeSubset(r1.get(), strlen(str), 10));
reed@google.com1622c992011-06-14 19:22:21 +0000194 assert_len(reporter, tmp, 0);
bungeman38d909e2016-08-02 14:40:46 -0700195 tmp = SkData::MakeSubset(r1.get(), 0, 0);
reed@google.com1622c992011-06-14 19:22:21 +0000196 assert_len(reporter, tmp, 0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000197
reed@google.comdbc936d2012-06-28 15:40:09 +0000198 test_cstring(reporter);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000199 test_files(reporter);
reed@google.com1622c992011-06-14 19:22:21 +0000200}
reed5b6db072015-04-28 17:50:31 -0700201
202///////////////////////////////////////////////////////////////////////////////////////////////////
203#include "SkRWBuffer.h"
204
205const char gABC[] = "abcdefghijklmnopqrstuvwxyz";
206
207static void check_abcs(skiatest::Reporter* reporter, const char buffer[], size_t size) {
208 REPORTER_ASSERT(reporter, size % 26 == 0);
209 for (size_t offset = 0; offset < size; offset += 26) {
210 REPORTER_ASSERT(reporter, !memcmp(&buffer[offset], gABC, 26));
211 }
212}
213
scroggo63516402016-04-22 06:59:01 -0700214// stream should contain an integral number of copies of gABC.
reed5b6db072015-04-28 17:50:31 -0700215static void check_alphabet_stream(skiatest::Reporter* reporter, SkStream* stream) {
216 REPORTER_ASSERT(reporter, stream->hasLength());
217 size_t size = stream->getLength();
218 REPORTER_ASSERT(reporter, size % 26 == 0);
219
220 SkAutoTMalloc<char> storage(size);
221 char* array = storage.get();
222 size_t bytesRead = stream->read(array, size);
223 REPORTER_ASSERT(reporter, bytesRead == size);
224 check_abcs(reporter, array, size);
225
226 // try checking backwards
227 for (size_t offset = size; offset > 0; offset -= 26) {
228 REPORTER_ASSERT(reporter, stream->seek(offset - 26));
229 REPORTER_ASSERT(reporter, stream->getPosition() == offset - 26);
230 REPORTER_ASSERT(reporter, stream->read(array, 26) == 26);
231 check_abcs(reporter, array, 26);
232 REPORTER_ASSERT(reporter, stream->getPosition() == offset);
233 }
234}
235
236// reader should contains an integral number of copies of gABC.
237static void check_alphabet_buffer(skiatest::Reporter* reporter, const SkROBuffer* reader) {
238 size_t size = reader->size();
239 REPORTER_ASSERT(reporter, size % 26 == 0);
halcanary9d524f22016-03-29 09:03:52 -0700240
reed5b6db072015-04-28 17:50:31 -0700241 SkAutoTMalloc<char> storage(size);
242 SkROBuffer::Iter iter(reader);
243 size_t offset = 0;
244 do {
245 SkASSERT(offset + iter.size() <= size);
246 memcpy(storage.get() + offset, iter.data(), iter.size());
247 offset += iter.size();
248 } while (iter.next());
249 REPORTER_ASSERT(reporter, offset == size);
250 check_abcs(reporter, storage.get(), size);
251}
252
scroggo63516402016-04-22 06:59:01 -0700253#include "SkTaskGroup.h"
254
reed5b6db072015-04-28 17:50:31 -0700255DEF_TEST(RWBuffer, reporter) {
256 // Knowing that the default capacity is 4096, choose N large enough so we force it to use
257 // multiple buffers internally.
258 const int N = 1000;
259 SkROBuffer* readers[N];
260 SkStream* streams[N];
261
262 {
263 SkRWBuffer buffer;
264 for (int i = 0; i < N; ++i) {
reed377add72016-04-08 12:47:14 -0700265 buffer.append(gABC, 26);
reed5b6db072015-04-28 17:50:31 -0700266 readers[i] = buffer.newRBufferSnapshot();
267 streams[i] = buffer.newStreamSnapshot();
268 }
269 REPORTER_ASSERT(reporter, N*26 == buffer.size());
270 }
271
scroggo63516402016-04-22 06:59:01 -0700272 // Verify that although the SkRWBuffer's destructor has run, the readers are still valid.
reed5b6db072015-04-28 17:50:31 -0700273 for (int i = 0; i < N; ++i) {
274 REPORTER_ASSERT(reporter, (i + 1) * 26U == readers[i]->size());
275 check_alphabet_buffer(reporter, readers[i]);
276 check_alphabet_stream(reporter, streams[i]);
277 readers[i]->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700278 delete streams[i];
reed5b6db072015-04-28 17:50:31 -0700279 }
280}
scroggob512aaa2016-01-11 06:38:00 -0800281
scroggo63516402016-04-22 06:59:01 -0700282DEF_TEST(RWBuffer_threaded, reporter) {
283 // Knowing that the default capacity is 4096, choose N large enough so we force it to use
284 // multiple buffers internally.
285 const int N = 1000;
286 SkTaskGroup tasks;
287 SkRWBuffer buffer;
288 for (int i = 0; i < N; ++i) {
289 buffer.append(gABC, 26);
290 sk_sp<SkROBuffer> reader = sk_sp<SkROBuffer>(buffer.newRBufferSnapshot());
291 SkStream* stream = buffer.newStreamSnapshot();
292 REPORTER_ASSERT(reporter, reader->size() == buffer.size());
293 REPORTER_ASSERT(reporter, stream->getLength() == buffer.size());
294
295 // reader's copy constructor will ref the SkROBuffer, which will be unreffed
296 // when the task ends.
297 // Ownership of stream is passed to the task, which will delete it.
298 tasks.add([reporter, i, reader, stream] {
299 REPORTER_ASSERT(reporter, (i + 1) * 26U == reader->size());
300 REPORTER_ASSERT(reporter, stream->getLength() == reader->size());
301 check_alphabet_buffer(reporter, reader.get());
302 check_alphabet_stream(reporter, stream);
303 REPORTER_ASSERT(reporter, stream->rewind());
304 delete stream;
305 });
306 }
307 REPORTER_ASSERT(reporter, N*26 == buffer.size());
308 tasks.wait();
309}
310
scroggob512aaa2016-01-11 06:38:00 -0800311// Tests that it is safe to call SkROBuffer::Iter::size() when exhausted.
312DEF_TEST(RWBuffer_size, r) {
313 SkRWBuffer buffer;
314 buffer.append(gABC, 26);
315
Hal Canary342b7ac2016-11-04 11:49:42 -0400316 sk_sp<SkROBuffer> roBuffer(buffer.newRBufferSnapshot());
317 SkROBuffer::Iter iter(roBuffer.get());
scroggob512aaa2016-01-11 06:38:00 -0800318 REPORTER_ASSERT(r, iter.data());
319 REPORTER_ASSERT(r, iter.size() == 26);
320
321 // There is only one block in this buffer.
322 REPORTER_ASSERT(r, !iter.next());
323 REPORTER_ASSERT(r, 0 == iter.size());
324}
scroggo9dec5d22016-02-16 06:59:18 -0800325
scroggo63516402016-04-22 06:59:01 -0700326// Tests that operations (including the destructor) are safe on an SkRWBuffer
327// without any data appended.
scroggo9dec5d22016-02-16 06:59:18 -0800328DEF_TEST(RWBuffer_noAppend, r) {
329 SkRWBuffer buffer;
330 REPORTER_ASSERT(r, 0 == buffer.size());
scroggo63516402016-04-22 06:59:01 -0700331
332 sk_sp<SkROBuffer> roBuffer = sk_sp<SkROBuffer>(buffer.newRBufferSnapshot());
333 REPORTER_ASSERT(r, roBuffer);
334 if (roBuffer) {
335 REPORTER_ASSERT(r, roBuffer->size() == 0);
336 SkROBuffer::Iter iter(roBuffer.get());
337 REPORTER_ASSERT(r, iter.size() == 0);
338 REPORTER_ASSERT(r, !iter.data());
339 REPORTER_ASSERT(r, !iter.next());
340 }
341
Ben Wagner145dbcd2016-11-03 14:40:50 -0400342 std::unique_ptr<SkStream> stream(buffer.newStreamSnapshot());
scroggo63516402016-04-22 06:59:01 -0700343 REPORTER_ASSERT(r, stream);
344 if (stream) {
345 REPORTER_ASSERT(r, stream->hasLength());
346 REPORTER_ASSERT(r, stream->getLength() == 0);
347 REPORTER_ASSERT(r, stream->skip(10) == 0);
348 }
scroggo9dec5d22016-02-16 06:59:18 -0800349}