2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/core/SkData.h"
9 #include "include/core/SkDataTable.h"
10 #include "include/core/SkRefCnt.h"
11 #include "include/core/SkStream.h"
12 #include "include/core/SkString.h"
13 #include "include/core/SkTypes.h"
14 #include "include/private/SkTArray.h"
15 #include "include/private/SkTemplates.h"
16 #include "src/core/SkOSFile.h"
17 #include "src/core/SkTaskGroup.h"
18 #include "src/utils/SkOSPath.h"
19 #include "tests/Test.h"
25 static void test_is_equal(skiatest::Reporter* reporter,
26 const SkDataTable* a, const SkDataTable* b) {
27 REPORTER_ASSERT(reporter, a->count() == b->count());
28 for (int i = 0; i < a->count(); ++i) {
30 const void* mema = a->at(i, &sizea);
31 const void* memb = b->at(i, &sizeb);
32 REPORTER_ASSERT(reporter, sizea == sizeb);
33 REPORTER_ASSERT(reporter, !memcmp(mema, memb, sizea));
37 static void test_datatable_is_empty(skiatest::Reporter* reporter, SkDataTable* table) {
38 REPORTER_ASSERT(reporter, table->isEmpty());
39 REPORTER_ASSERT(reporter, 0 == table->count());
42 static void test_emptytable(skiatest::Reporter* reporter) {
43 sk_sp<SkDataTable> table0(SkDataTable::MakeEmpty());
44 sk_sp<SkDataTable> table1(SkDataTable::MakeCopyArrays(nullptr, nullptr, 0));
45 sk_sp<SkDataTable> table2(SkDataTable::MakeCopyArray(nullptr, 0, 0));
46 sk_sp<SkDataTable> table3(SkDataTable::MakeArrayProc(nullptr, 0, 0, nullptr, nullptr));
48 test_datatable_is_empty(reporter, table0.get());
49 test_datatable_is_empty(reporter, table1.get());
50 test_datatable_is_empty(reporter, table2.get());
51 test_datatable_is_empty(reporter, table3.get());
53 test_is_equal(reporter, table0.get(), table1.get());
54 test_is_equal(reporter, table0.get(), table2.get());
55 test_is_equal(reporter, table0.get(), table3.get());
58 static void test_simpletable(skiatest::Reporter* reporter) {
59 const int idata[] = { 1, 4, 9, 16, 25, 63 };
60 int icount = SK_ARRAY_COUNT(idata);
61 sk_sp<SkDataTable> itable(SkDataTable::MakeCopyArray(idata, sizeof(idata[0]), icount));
62 REPORTER_ASSERT(reporter, itable->count() == icount);
63 for (int i = 0; i < icount; ++i) {
65 REPORTER_ASSERT(reporter, sizeof(int) == itable->atSize(i));
66 REPORTER_ASSERT(reporter, *itable->atT<int>(i, &size) == idata[i]);
67 REPORTER_ASSERT(reporter, sizeof(int) == size);
71 static void test_vartable(skiatest::Reporter* reporter) {
73 "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
75 int count = SK_ARRAY_COUNT(str);
76 size_t sizes[SK_ARRAY_COUNT(str)];
77 for (int i = 0; i < count; ++i) {
78 sizes[i] = strlen(str[i]) + 1;
81 sk_sp<SkDataTable> table(SkDataTable::MakeCopyArrays((const void*const*)str, sizes, count));
83 REPORTER_ASSERT(reporter, table->count() == count);
84 for (int i = 0; i < count; ++i) {
86 REPORTER_ASSERT(reporter, table->atSize(i) == sizes[i]);
87 REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size),
89 REPORTER_ASSERT(reporter, size == sizes[i]);
91 const char* s = table->atStr(i);
92 REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
96 static void test_globaltable(skiatest::Reporter* reporter) {
97 static const int gData[] = {
98 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
100 int count = SK_ARRAY_COUNT(gData);
102 sk_sp<SkDataTable> table(
103 SkDataTable::MakeArrayProc(gData, sizeof(gData[0]), count, nullptr, nullptr));
105 REPORTER_ASSERT(reporter, table->count() == count);
106 for (int i = 0; i < count; ++i) {
108 REPORTER_ASSERT(reporter, table->atSize(i) == sizeof(int));
109 REPORTER_ASSERT(reporter, *table->atT<const char>(i, &size) == i);
110 REPORTER_ASSERT(reporter, sizeof(int) == size);
114 DEF_TEST(DataTable, reporter) {
115 test_emptytable(reporter);
116 test_simpletable(reporter);
117 test_vartable(reporter);
118 test_globaltable(reporter);
121 static void* gGlobal;
123 static void delete_int_proc(const void* ptr, void* context) {
124 int* data = (int*)ptr;
125 SkASSERT(context == gGlobal);
129 static void assert_len(skiatest::Reporter* reporter, const sk_sp<SkData>& ref, size_t len) {
130 REPORTER_ASSERT(reporter, ref->size() == len);
133 static void assert_data(skiatest::Reporter* reporter, const sk_sp<SkData>& ref,
134 const void* data, size_t len) {
135 REPORTER_ASSERT(reporter, ref->size() == len);
136 REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
139 static void test_cstring(skiatest::Reporter* reporter) {
140 const char str[] = "Hello world";
141 size_t len = strlen(str);
143 sk_sp<SkData> r0(SkData::MakeWithCopy(str, len + 1));
144 sk_sp<SkData> r1(SkData::MakeWithCString(str));
146 REPORTER_ASSERT(reporter, r0->equals(r1.get()));
148 sk_sp<SkData> r2(SkData::MakeWithCString(nullptr));
149 REPORTER_ASSERT(reporter, 1 == r2->size());
150 REPORTER_ASSERT(reporter, 0 == *r2->bytes());
153 static void test_files(skiatest::Reporter* reporter) {
154 SkString tmpDir = skiatest::GetTmpDir();
155 if (tmpDir.isEmpty()) {
159 SkString path = SkOSPath::Join(tmpDir.c_str(), "data_test");
161 const char s[] = "abcdefghijklmnopqrstuvwxyz";
163 SkFILEWStream writer(path.c_str());
164 if (!writer.isValid()) {
165 ERRORF(reporter, "Failed to create tmp file %s\n", path.c_str());
171 FILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag);
172 sk_sp<SkData> r1(SkData::MakeFromFILE(file));
173 REPORTER_ASSERT(reporter, r1.get() != nullptr);
174 REPORTER_ASSERT(reporter, r1->size() == 26);
175 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r1->data()), s, 26) == 0);
177 int fd = sk_fileno(file);
178 sk_sp<SkData> r2(SkData::MakeFromFD(fd));
179 REPORTER_ASSERT(reporter, r2.get() != nullptr);
180 REPORTER_ASSERT(reporter, r2->size() == 26);
181 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 26) == 0);
184 DEF_TEST(Data, reporter) {
185 const char* str = "We the people, in order to form a more perfect union.";
188 sk_sp<SkData> r0(SkData::MakeEmpty());
189 sk_sp<SkData> r1(SkData::MakeWithCopy(str, strlen(str)));
190 sk_sp<SkData> r2(SkData::MakeWithProc(new int[N], N*sizeof(int), delete_int_proc, gGlobal));
191 sk_sp<SkData> r3(SkData::MakeSubset(r1.get(), 7, 6));
193 assert_len(reporter, r0, 0);
194 assert_len(reporter, r1, strlen(str));
195 assert_len(reporter, r2, N * sizeof(int));
196 assert_len(reporter, r3, 6);
198 assert_data(reporter, r1, str, strlen(str));
199 assert_data(reporter, r3, "people", 6);
201 sk_sp<SkData> tmp(SkData::MakeSubset(r1.get(), strlen(str), 10));
202 assert_len(reporter, tmp, 0);
203 tmp = SkData::MakeSubset(r1.get(), 0, 0);
204 assert_len(reporter, tmp, 0);
206 test_cstring(reporter);
207 test_files(reporter);
210 DEF_TEST(Data_empty, reporter) {
211 sk_sp<SkData> array[] = {
213 SkData::MakeUninitialized(0),
214 SkData::MakeFromMalloc(sk_malloc_throw(0), 0),
215 SkData::MakeWithCopy("", 0),
216 SkData::MakeWithProc(nullptr, 0, [](const void*, void*){}, nullptr),
217 SkData::MakeWithoutCopy(nullptr, 0),
219 constexpr int N = SK_ARRAY_COUNT(array);
221 for (int i = 0; i < N; ++i) {
222 REPORTER_ASSERT(reporter, array[i]->size() == 0);
223 for (int j = 0; j < N; ++j) {
224 REPORTER_ASSERT(reporter, array[i]->equals(array[j].get()));