xref: /third_party/skia/tests/DataRefTest.cpp (revision cb93a386)
1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2011 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/core/SkData.h"
9cb93a386Sopenharmony_ci#include "include/core/SkDataTable.h"
10cb93a386Sopenharmony_ci#include "include/core/SkRefCnt.h"
11cb93a386Sopenharmony_ci#include "include/core/SkStream.h"
12cb93a386Sopenharmony_ci#include "include/core/SkString.h"
13cb93a386Sopenharmony_ci#include "include/core/SkTypes.h"
14cb93a386Sopenharmony_ci#include "include/private/SkTArray.h"
15cb93a386Sopenharmony_ci#include "include/private/SkTemplates.h"
16cb93a386Sopenharmony_ci#include "src/core/SkOSFile.h"
17cb93a386Sopenharmony_ci#include "src/core/SkTaskGroup.h"
18cb93a386Sopenharmony_ci#include "src/utils/SkOSPath.h"
19cb93a386Sopenharmony_ci#include "tests/Test.h"
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_ci#include <cstdio>
22cb93a386Sopenharmony_ci#include <cstring>
23cb93a386Sopenharmony_ci#include <memory>
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_cistatic void test_is_equal(skiatest::Reporter* reporter,
26cb93a386Sopenharmony_ci                          const SkDataTable* a, const SkDataTable* b) {
27cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, a->count() == b->count());
28cb93a386Sopenharmony_ci    for (int i = 0; i < a->count(); ++i) {
29cb93a386Sopenharmony_ci        size_t sizea, sizeb;
30cb93a386Sopenharmony_ci        const void* mema = a->at(i, &sizea);
31cb93a386Sopenharmony_ci        const void* memb = b->at(i, &sizeb);
32cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, sizea == sizeb);
33cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, !memcmp(mema, memb, sizea));
34cb93a386Sopenharmony_ci    }
35cb93a386Sopenharmony_ci}
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_cistatic void test_datatable_is_empty(skiatest::Reporter* reporter, SkDataTable* table) {
38cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, table->isEmpty());
39cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, 0 == table->count());
40cb93a386Sopenharmony_ci}
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_cistatic void test_emptytable(skiatest::Reporter* reporter) {
43cb93a386Sopenharmony_ci    sk_sp<SkDataTable> table0(SkDataTable::MakeEmpty());
44cb93a386Sopenharmony_ci    sk_sp<SkDataTable> table1(SkDataTable::MakeCopyArrays(nullptr, nullptr, 0));
45cb93a386Sopenharmony_ci    sk_sp<SkDataTable> table2(SkDataTable::MakeCopyArray(nullptr, 0, 0));
46cb93a386Sopenharmony_ci    sk_sp<SkDataTable> table3(SkDataTable::MakeArrayProc(nullptr, 0, 0, nullptr, nullptr));
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci    test_datatable_is_empty(reporter, table0.get());
49cb93a386Sopenharmony_ci    test_datatable_is_empty(reporter, table1.get());
50cb93a386Sopenharmony_ci    test_datatable_is_empty(reporter, table2.get());
51cb93a386Sopenharmony_ci    test_datatable_is_empty(reporter, table3.get());
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ci    test_is_equal(reporter, table0.get(), table1.get());
54cb93a386Sopenharmony_ci    test_is_equal(reporter, table0.get(), table2.get());
55cb93a386Sopenharmony_ci    test_is_equal(reporter, table0.get(), table3.get());
56cb93a386Sopenharmony_ci}
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_cistatic void test_simpletable(skiatest::Reporter* reporter) {
59cb93a386Sopenharmony_ci    const int idata[] = { 1, 4, 9, 16, 25, 63 };
60cb93a386Sopenharmony_ci    int icount = SK_ARRAY_COUNT(idata);
61cb93a386Sopenharmony_ci    sk_sp<SkDataTable> itable(SkDataTable::MakeCopyArray(idata, sizeof(idata[0]), icount));
62cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, itable->count() == icount);
63cb93a386Sopenharmony_ci    for (int i = 0; i < icount; ++i) {
64cb93a386Sopenharmony_ci        size_t size;
65cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, sizeof(int) == itable->atSize(i));
66cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, *itable->atT<int>(i, &size) == idata[i]);
67cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, sizeof(int) == size);
68cb93a386Sopenharmony_ci    }
69cb93a386Sopenharmony_ci}
70cb93a386Sopenharmony_ci
71cb93a386Sopenharmony_cistatic void test_vartable(skiatest::Reporter* reporter) {
72cb93a386Sopenharmony_ci    const char* str[] = {
73cb93a386Sopenharmony_ci        "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
74cb93a386Sopenharmony_ci    };
75cb93a386Sopenharmony_ci    int count = SK_ARRAY_COUNT(str);
76cb93a386Sopenharmony_ci    size_t sizes[SK_ARRAY_COUNT(str)];
77cb93a386Sopenharmony_ci    for (int i = 0; i < count; ++i) {
78cb93a386Sopenharmony_ci        sizes[i] = strlen(str[i]) + 1;
79cb93a386Sopenharmony_ci    }
80cb93a386Sopenharmony_ci
81cb93a386Sopenharmony_ci    sk_sp<SkDataTable> table(SkDataTable::MakeCopyArrays((const void*const*)str, sizes, count));
82cb93a386Sopenharmony_ci
83cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, table->count() == count);
84cb93a386Sopenharmony_ci    for (int i = 0; i < count; ++i) {
85cb93a386Sopenharmony_ci        size_t size;
86cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, table->atSize(i) == sizes[i]);
87cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size),
88cb93a386Sopenharmony_ci                                          str[i]));
89cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, size == sizes[i]);
90cb93a386Sopenharmony_ci
91cb93a386Sopenharmony_ci        const char* s = table->atStr(i);
92cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
93cb93a386Sopenharmony_ci    }
94cb93a386Sopenharmony_ci}
95cb93a386Sopenharmony_ci
96cb93a386Sopenharmony_cistatic void test_globaltable(skiatest::Reporter* reporter) {
97cb93a386Sopenharmony_ci    static const int gData[] = {
98cb93a386Sopenharmony_ci        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
99cb93a386Sopenharmony_ci    };
100cb93a386Sopenharmony_ci    int count = SK_ARRAY_COUNT(gData);
101cb93a386Sopenharmony_ci
102cb93a386Sopenharmony_ci    sk_sp<SkDataTable> table(
103cb93a386Sopenharmony_ci        SkDataTable::MakeArrayProc(gData, sizeof(gData[0]), count, nullptr, nullptr));
104cb93a386Sopenharmony_ci
105cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, table->count() == count);
106cb93a386Sopenharmony_ci    for (int i = 0; i < count; ++i) {
107cb93a386Sopenharmony_ci        size_t size;
108cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, table->atSize(i) == sizeof(int));
109cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, *table->atT<const char>(i, &size) == i);
110cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, sizeof(int) == size);
111cb93a386Sopenharmony_ci    }
112cb93a386Sopenharmony_ci}
113cb93a386Sopenharmony_ci
114cb93a386Sopenharmony_ciDEF_TEST(DataTable, reporter) {
115cb93a386Sopenharmony_ci    test_emptytable(reporter);
116cb93a386Sopenharmony_ci    test_simpletable(reporter);
117cb93a386Sopenharmony_ci    test_vartable(reporter);
118cb93a386Sopenharmony_ci    test_globaltable(reporter);
119cb93a386Sopenharmony_ci}
120cb93a386Sopenharmony_ci
121cb93a386Sopenharmony_cistatic void* gGlobal;
122cb93a386Sopenharmony_ci
123cb93a386Sopenharmony_cistatic void delete_int_proc(const void* ptr, void* context) {
124cb93a386Sopenharmony_ci    int* data = (int*)ptr;
125cb93a386Sopenharmony_ci    SkASSERT(context == gGlobal);
126cb93a386Sopenharmony_ci    delete[] data;
127cb93a386Sopenharmony_ci}
128cb93a386Sopenharmony_ci
129cb93a386Sopenharmony_cistatic void assert_len(skiatest::Reporter* reporter, const sk_sp<SkData>& ref, size_t len) {
130cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, ref->size() == len);
131cb93a386Sopenharmony_ci}
132cb93a386Sopenharmony_ci
133cb93a386Sopenharmony_cistatic void assert_data(skiatest::Reporter* reporter, const sk_sp<SkData>& ref,
134cb93a386Sopenharmony_ci                        const void* data, size_t len) {
135cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, ref->size() == len);
136cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
137cb93a386Sopenharmony_ci}
138cb93a386Sopenharmony_ci
139cb93a386Sopenharmony_cistatic void test_cstring(skiatest::Reporter* reporter) {
140cb93a386Sopenharmony_ci    const char str[] = "Hello world";
141cb93a386Sopenharmony_ci    size_t     len = strlen(str);
142cb93a386Sopenharmony_ci
143cb93a386Sopenharmony_ci    sk_sp<SkData> r0(SkData::MakeWithCopy(str, len + 1));
144cb93a386Sopenharmony_ci    sk_sp<SkData> r1(SkData::MakeWithCString(str));
145cb93a386Sopenharmony_ci
146cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, r0->equals(r1.get()));
147cb93a386Sopenharmony_ci
148cb93a386Sopenharmony_ci    sk_sp<SkData> r2(SkData::MakeWithCString(nullptr));
149cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, 1 == r2->size());
150cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, 0 == *r2->bytes());
151cb93a386Sopenharmony_ci}
152cb93a386Sopenharmony_ci
153cb93a386Sopenharmony_cistatic void test_files(skiatest::Reporter* reporter) {
154cb93a386Sopenharmony_ci    SkString tmpDir = skiatest::GetTmpDir();
155cb93a386Sopenharmony_ci    if (tmpDir.isEmpty()) {
156cb93a386Sopenharmony_ci        return;
157cb93a386Sopenharmony_ci    }
158cb93a386Sopenharmony_ci
159cb93a386Sopenharmony_ci    SkString path = SkOSPath::Join(tmpDir.c_str(), "data_test");
160cb93a386Sopenharmony_ci
161cb93a386Sopenharmony_ci    const char s[] = "abcdefghijklmnopqrstuvwxyz";
162cb93a386Sopenharmony_ci    {
163cb93a386Sopenharmony_ci        SkFILEWStream writer(path.c_str());
164cb93a386Sopenharmony_ci        if (!writer.isValid()) {
165cb93a386Sopenharmony_ci            ERRORF(reporter, "Failed to create tmp file %s\n", path.c_str());
166cb93a386Sopenharmony_ci            return;
167cb93a386Sopenharmony_ci        }
168cb93a386Sopenharmony_ci        writer.write(s, 26);
169cb93a386Sopenharmony_ci    }
170cb93a386Sopenharmony_ci
171cb93a386Sopenharmony_ci    FILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag);
172cb93a386Sopenharmony_ci    sk_sp<SkData> r1(SkData::MakeFromFILE(file));
173cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, r1.get() != nullptr);
174cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, r1->size() == 26);
175cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r1->data()), s, 26) == 0);
176cb93a386Sopenharmony_ci
177cb93a386Sopenharmony_ci    int fd = sk_fileno(file);
178cb93a386Sopenharmony_ci    sk_sp<SkData> r2(SkData::MakeFromFD(fd));
179cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, r2.get() != nullptr);
180cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, r2->size() == 26);
181cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 26) == 0);
182cb93a386Sopenharmony_ci}
183cb93a386Sopenharmony_ci
184cb93a386Sopenharmony_ciDEF_TEST(Data, reporter) {
185cb93a386Sopenharmony_ci    const char* str = "We the people, in order to form a more perfect union.";
186cb93a386Sopenharmony_ci    const int N = 10;
187cb93a386Sopenharmony_ci
188cb93a386Sopenharmony_ci    sk_sp<SkData> r0(SkData::MakeEmpty());
189cb93a386Sopenharmony_ci    sk_sp<SkData> r1(SkData::MakeWithCopy(str, strlen(str)));
190cb93a386Sopenharmony_ci    sk_sp<SkData> r2(SkData::MakeWithProc(new int[N], N*sizeof(int), delete_int_proc, gGlobal));
191cb93a386Sopenharmony_ci    sk_sp<SkData> r3(SkData::MakeSubset(r1.get(), 7, 6));
192cb93a386Sopenharmony_ci
193cb93a386Sopenharmony_ci    assert_len(reporter, r0, 0);
194cb93a386Sopenharmony_ci    assert_len(reporter, r1, strlen(str));
195cb93a386Sopenharmony_ci    assert_len(reporter, r2, N * sizeof(int));
196cb93a386Sopenharmony_ci    assert_len(reporter, r3, 6);
197cb93a386Sopenharmony_ci
198cb93a386Sopenharmony_ci    assert_data(reporter, r1, str, strlen(str));
199cb93a386Sopenharmony_ci    assert_data(reporter, r3, "people", 6);
200cb93a386Sopenharmony_ci
201cb93a386Sopenharmony_ci    sk_sp<SkData> tmp(SkData::MakeSubset(r1.get(), strlen(str), 10));
202cb93a386Sopenharmony_ci    assert_len(reporter, tmp, 0);
203cb93a386Sopenharmony_ci    tmp = SkData::MakeSubset(r1.get(), 0, 0);
204cb93a386Sopenharmony_ci    assert_len(reporter, tmp, 0);
205cb93a386Sopenharmony_ci
206cb93a386Sopenharmony_ci    test_cstring(reporter);
207cb93a386Sopenharmony_ci    test_files(reporter);
208cb93a386Sopenharmony_ci}
209cb93a386Sopenharmony_ci
210cb93a386Sopenharmony_ciDEF_TEST(Data_empty, reporter) {
211cb93a386Sopenharmony_ci    sk_sp<SkData> array[] = {
212cb93a386Sopenharmony_ci        SkData::MakeEmpty(),
213cb93a386Sopenharmony_ci        SkData::MakeUninitialized(0),
214cb93a386Sopenharmony_ci        SkData::MakeFromMalloc(sk_malloc_throw(0), 0),
215cb93a386Sopenharmony_ci        SkData::MakeWithCopy("", 0),
216cb93a386Sopenharmony_ci        SkData::MakeWithProc(nullptr, 0, [](const void*, void*){}, nullptr),
217cb93a386Sopenharmony_ci        SkData::MakeWithoutCopy(nullptr, 0),
218cb93a386Sopenharmony_ci    };
219cb93a386Sopenharmony_ci    constexpr int N = SK_ARRAY_COUNT(array);
220cb93a386Sopenharmony_ci
221cb93a386Sopenharmony_ci    for (int i = 0; i < N; ++i) {
222cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, array[i]->size() == 0);
223cb93a386Sopenharmony_ci        for (int j = 0; j < N; ++j) {
224cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, array[i]->equals(array[j].get()));
225cb93a386Sopenharmony_ci        }
226cb93a386Sopenharmony_ci    }
227cb93a386Sopenharmony_ci}
228