• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
DEF_TEST(Data_empty,reporter)211 DEF_TEST(Data_empty, reporter) {
212     sk_sp<SkData> array[] = {
213         SkData::MakeEmpty(),
214         SkData::MakeUninitialized(0),
215         SkData::MakeFromMalloc(sk_malloc_throw(0), 0),
216         SkData::MakeWithCopy("", 0),
217         SkData::MakeWithProc(nullptr, 0, [](const void*, void*){}, nullptr),
218         SkData::MakeWithoutCopy(nullptr, 0),
219     };
220     constexpr int N = SK_ARRAY_COUNT(array);
221 
222     for (int i = 0; i < N; ++i) {
223         REPORTER_ASSERT(reporter, array[i]->size() == 0);
224         for (int j = 0; j < N; ++j) {
225             REPORTER_ASSERT(reporter, array[i]->equals(array[j].get()));
226         }
227     }
228 }
229 
230 ///////////////////////////////////////////////////////////////////////////////////////////////////
231 
232 const char gABC[] = "abcdefghijklmnopqrstuvwxyz";
233 
check_abcs(skiatest::Reporter * reporter,const char buffer[],size_t size)234 static void check_abcs(skiatest::Reporter* reporter, const char buffer[], size_t size) {
235     REPORTER_ASSERT(reporter, size % 26 == 0);
236     for (size_t offset = 0; offset < size; offset += 26) {
237         REPORTER_ASSERT(reporter, !memcmp(&buffer[offset], gABC, 26));
238     }
239 }
240 
241 // stream should contain an integral number of copies of gABC.
check_alphabet_stream(skiatest::Reporter * reporter,SkStream * stream)242 static void check_alphabet_stream(skiatest::Reporter* reporter, SkStream* stream) {
243     REPORTER_ASSERT(reporter, stream->hasLength());
244     size_t size = stream->getLength();
245     REPORTER_ASSERT(reporter, size % 26 == 0);
246 
247     SkAutoTMalloc<char> storage(size);
248     char* array = storage.get();
249     size_t bytesRead = stream->read(array, size);
250     REPORTER_ASSERT(reporter, bytesRead == size);
251     check_abcs(reporter, array, size);
252 
253     // try checking backwards
254     for (size_t offset = size; offset > 0; offset -= 26) {
255         REPORTER_ASSERT(reporter, stream->seek(offset - 26));
256         REPORTER_ASSERT(reporter, stream->getPosition() == offset - 26);
257         REPORTER_ASSERT(reporter, stream->read(array, 26) == 26);
258         check_abcs(reporter, array, 26);
259         REPORTER_ASSERT(reporter, stream->getPosition() == offset);
260     }
261 }
262 
263 // reader should contains an integral number of copies of gABC.
check_alphabet_buffer(skiatest::Reporter * reporter,const SkROBuffer * reader)264 static void check_alphabet_buffer(skiatest::Reporter* reporter, const SkROBuffer* reader) {
265     size_t size = reader->size();
266     REPORTER_ASSERT(reporter, size % 26 == 0);
267 
268     SkAutoTMalloc<char> storage(size);
269     SkROBuffer::Iter iter(reader);
270     size_t offset = 0;
271     do {
272         SkASSERT(offset + iter.size() <= size);
273         memcpy(storage.get() + offset, iter.data(), iter.size());
274         offset += iter.size();
275     } while (iter.next());
276     REPORTER_ASSERT(reporter, offset == size);
277     check_abcs(reporter, storage.get(), size);
278 }
279 
DEF_TEST(RWBuffer,reporter)280 DEF_TEST(RWBuffer, reporter) {
281     // Knowing that the default capacity is 4096, choose N large enough so we force it to use
282     // multiple buffers internally.
283     static constexpr int N = 1000;
284     SkSTArray<N, sk_sp<SkROBuffer>> readers;
285     SkSTArray<N, std::unique_ptr<SkStream>> streams;
286 
287     {
288         SkRWBuffer buffer;
289         for (int i = 0; i < N; ++i) {
290             buffer.append(gABC, 26);
291             readers.push_back(buffer.makeROBufferSnapshot());
292             streams.push_back(buffer.makeStreamSnapshot());
293         }
294         REPORTER_ASSERT(reporter, N*26 == buffer.size());
295     }
296 
297     // Verify that although the SkRWBuffer's destructor has run, the readers are still valid.
298     for (int i = 0; i < N; ++i) {
299         REPORTER_ASSERT(reporter, (i + 1) * 26U == readers[i]->size());
300         check_alphabet_buffer(reporter, readers[i].get());
301         check_alphabet_stream(reporter, streams[i].get());
302     }
303 }
304 
DEF_TEST(RWBuffer_threaded,reporter)305 DEF_TEST(RWBuffer_threaded, reporter) {
306     // Knowing that the default capacity is 4096, choose N large enough so we force it to use
307     // multiple buffers internally.
308     const int N = 1000;
309     SkTaskGroup tasks;
310     SkRWBuffer buffer;
311     for (int i = 0; i < N; ++i) {
312         buffer.append(gABC, 26);
313         sk_sp<SkROBuffer> reader = buffer.makeROBufferSnapshot();
314         SkStream* stream = buffer.makeStreamSnapshot().release();
315         REPORTER_ASSERT(reporter, reader->size() == buffer.size());
316         REPORTER_ASSERT(reporter, stream->getLength() == buffer.size());
317 
318         // reader's copy constructor will ref the SkROBuffer, which will be unreffed
319         // when the task ends.
320         // Ownership of stream is passed to the task, which will delete it.
321         tasks.add([reporter, i, reader, stream] {
322             REPORTER_ASSERT(reporter, (i + 1) * 26U == reader->size());
323             REPORTER_ASSERT(reporter, stream->getLength() == reader->size());
324             check_alphabet_buffer(reporter, reader.get());
325             check_alphabet_stream(reporter, stream);
326             REPORTER_ASSERT(reporter, stream->rewind());
327             delete stream;
328         });
329     }
330     REPORTER_ASSERT(reporter, N*26 == buffer.size());
331     tasks.wait();
332 }
333 
334 // Tests that it is safe to call SkROBuffer::Iter::size() when exhausted.
DEF_TEST(RWBuffer_size,r)335 DEF_TEST(RWBuffer_size, r) {
336     SkRWBuffer buffer;
337     buffer.append(gABC, 26);
338 
339     sk_sp<SkROBuffer> roBuffer(buffer.makeROBufferSnapshot());
340     SkROBuffer::Iter iter(roBuffer.get());
341     REPORTER_ASSERT(r, iter.data());
342     REPORTER_ASSERT(r, iter.size() == 26);
343 
344     // There is only one block in this buffer.
345     REPORTER_ASSERT(r, !iter.next());
346     REPORTER_ASSERT(r, 0 == iter.size());
347 }
348 
349 // Tests that operations (including the destructor) are safe on an SkRWBuffer
350 // without any data appended.
DEF_TEST(RWBuffer_noAppend,r)351 DEF_TEST(RWBuffer_noAppend, r) {
352     SkRWBuffer buffer;
353     REPORTER_ASSERT(r, 0 == buffer.size());
354 
355     sk_sp<SkROBuffer> roBuffer = buffer.makeROBufferSnapshot();
356     REPORTER_ASSERT(r, roBuffer);
357     if (roBuffer) {
358         REPORTER_ASSERT(r, roBuffer->size() == 0);
359         SkROBuffer::Iter iter(roBuffer.get());
360         REPORTER_ASSERT(r, iter.size() == 0);
361         REPORTER_ASSERT(r, !iter.data());
362         REPORTER_ASSERT(r, !iter.next());
363     }
364 
365     std::unique_ptr<SkStream> stream(buffer.makeStreamSnapshot());
366     REPORTER_ASSERT(r, stream);
367     if (stream) {
368         REPORTER_ASSERT(r, stream->hasLength());
369         REPORTER_ASSERT(r, stream->getLength() == 0);
370         REPORTER_ASSERT(r, stream->skip(10) == 0);
371     }
372 }
373