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/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" 20 21#include <cstdio> 22#include <cstring> 23#include <memory> 24 25static 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) { 29 size_t sizea, sizeb; 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)); 34 } 35} 36 37static void test_datatable_is_empty(skiatest::Reporter* reporter, SkDataTable* table) { 38 REPORTER_ASSERT(reporter, table->isEmpty()); 39 REPORTER_ASSERT(reporter, 0 == table->count()); 40} 41 42static 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)); 47 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()); 52 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()); 56} 57 58static 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) { 64 size_t size; 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); 68 } 69} 70 71static void test_vartable(skiatest::Reporter* reporter) { 72 const char* str[] = { 73 "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg" 74 }; 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; 79 } 80 81 sk_sp<SkDataTable> table(SkDataTable::MakeCopyArrays((const void*const*)str, sizes, count)); 82 83 REPORTER_ASSERT(reporter, table->count() == count); 84 for (int i = 0; i < count; ++i) { 85 size_t size; 86 REPORTER_ASSERT(reporter, table->atSize(i) == sizes[i]); 87 REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size), 88 str[i])); 89 REPORTER_ASSERT(reporter, size == sizes[i]); 90 91 const char* s = table->atStr(i); 92 REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i])); 93 } 94} 95 96static 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 99 }; 100 int count = SK_ARRAY_COUNT(gData); 101 102 sk_sp<SkDataTable> table( 103 SkDataTable::MakeArrayProc(gData, sizeof(gData[0]), count, nullptr, nullptr)); 104 105 REPORTER_ASSERT(reporter, table->count() == count); 106 for (int i = 0; i < count; ++i) { 107 size_t size; 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); 111 } 112} 113 114DEF_TEST(DataTable, reporter) { 115 test_emptytable(reporter); 116 test_simpletable(reporter); 117 test_vartable(reporter); 118 test_globaltable(reporter); 119} 120 121static void* gGlobal; 122 123static void delete_int_proc(const void* ptr, void* context) { 124 int* data = (int*)ptr; 125 SkASSERT(context == gGlobal); 126 delete[] data; 127} 128 129static void assert_len(skiatest::Reporter* reporter, const sk_sp<SkData>& ref, size_t len) { 130 REPORTER_ASSERT(reporter, ref->size() == len); 131} 132 133static 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)); 137} 138 139static void test_cstring(skiatest::Reporter* reporter) { 140 const char str[] = "Hello world"; 141 size_t len = strlen(str); 142 143 sk_sp<SkData> r0(SkData::MakeWithCopy(str, len + 1)); 144 sk_sp<SkData> r1(SkData::MakeWithCString(str)); 145 146 REPORTER_ASSERT(reporter, r0->equals(r1.get())); 147 148 sk_sp<SkData> r2(SkData::MakeWithCString(nullptr)); 149 REPORTER_ASSERT(reporter, 1 == r2->size()); 150 REPORTER_ASSERT(reporter, 0 == *r2->bytes()); 151} 152 153static void test_files(skiatest::Reporter* reporter) { 154 SkString tmpDir = skiatest::GetTmpDir(); 155 if (tmpDir.isEmpty()) { 156 return; 157 } 158 159 SkString path = SkOSPath::Join(tmpDir.c_str(), "data_test"); 160 161 const char s[] = "abcdefghijklmnopqrstuvwxyz"; 162 { 163 SkFILEWStream writer(path.c_str()); 164 if (!writer.isValid()) { 165 ERRORF(reporter, "Failed to create tmp file %s\n", path.c_str()); 166 return; 167 } 168 writer.write(s, 26); 169 } 170 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); 176 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); 182} 183 184DEF_TEST(Data, reporter) { 185 const char* str = "We the people, in order to form a more perfect union."; 186 const int N = 10; 187 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)); 192 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); 197 198 assert_data(reporter, r1, str, strlen(str)); 199 assert_data(reporter, r3, "people", 6); 200 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); 205 206 test_cstring(reporter); 207 test_files(reporter); 208} 209 210DEF_TEST(Data_empty, reporter) { 211 sk_sp<SkData> array[] = { 212 SkData::MakeEmpty(), 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), 218 }; 219 constexpr int N = SK_ARRAY_COUNT(array); 220 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())); 225 } 226 } 227} 228