blob: 6a513923ffc3b1e27e059bc0ce318516c78b3d37 [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.com1622c992011-06-14 19:22:21 +00008#include "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@google.com8d0b5772011-06-24 13:07:31 +000010#include "SkData.h"
reed@google.com8c5c7a92013-04-19 20:16:01 +000011#include "SkDataTable.h"
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000012#include "SkOrderedReadBuffer.h"
13#include "SkOrderedWriteBuffer.h"
bungeman@google.com11c9a552013-06-03 17:10:35 +000014#include "SkOSFile.h"
15#include "SkStream.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
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000029static void test_datatable_is_empty(skiatest::Reporter* reporter,
30 SkDataTable* table) {
31 REPORTER_ASSERT(reporter, table->isEmpty());
32 REPORTER_ASSERT(reporter, 0 == table->count());
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000033}
34
35static void test_emptytable(skiatest::Reporter* reporter) {
36 SkAutoTUnref<SkDataTable> table0(SkDataTable::NewEmpty());
37 SkAutoTUnref<SkDataTable> table1(SkDataTable::NewCopyArrays(NULL, NULL, 0));
mike@reedtribe.org9ca81a72013-04-21 01:43:09 +000038 SkAutoTUnref<SkDataTable> table2(SkDataTable::NewCopyArray(NULL, 0, 0));
39 SkAutoTUnref<SkDataTable> table3(SkDataTable::NewArrayProc(NULL, 0, 0,
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000040 NULL, NULL));
41
42 test_datatable_is_empty(reporter, table0);
43 test_datatable_is_empty(reporter, table1);
44 test_datatable_is_empty(reporter, table2);
45 test_datatable_is_empty(reporter, table3);
46
47 test_is_equal(reporter, table0, table1);
48 test_is_equal(reporter, table0, table2);
49 test_is_equal(reporter, table0, table3);
50}
51
reed@google.com8c5c7a92013-04-19 20:16:01 +000052static void test_simpletable(skiatest::Reporter* reporter) {
53 const int idata[] = { 1, 4, 9, 16, 25, 63 };
54 int icount = SK_ARRAY_COUNT(idata);
55 SkAutoTUnref<SkDataTable> itable(SkDataTable::NewCopyArray(idata,
56 sizeof(idata[0]),
57 icount));
58 REPORTER_ASSERT(reporter, itable->count() == icount);
59 for (int i = 0; i < icount; ++i) {
60 size_t size;
61 REPORTER_ASSERT(reporter, sizeof(int) == itable->atSize(i));
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000062 REPORTER_ASSERT(reporter, *itable->atT<int>(i, &size) == idata[i]);
reed@google.com8c5c7a92013-04-19 20:16:01 +000063 REPORTER_ASSERT(reporter, sizeof(int) == size);
64 }
65}
66
67static void test_vartable(skiatest::Reporter* reporter) {
68 const char* str[] = {
69 "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
70 };
71 int count = SK_ARRAY_COUNT(str);
72 size_t sizes[SK_ARRAY_COUNT(str)];
73 for (int i = 0; i < count; ++i) {
74 sizes[i] = strlen(str[i]) + 1;
75 }
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000076
reed@google.com8c5c7a92013-04-19 20:16:01 +000077 SkAutoTUnref<SkDataTable> table(SkDataTable::NewCopyArrays(
78 (const void*const*)str, sizes, count));
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000079
reed@google.com8c5c7a92013-04-19 20:16:01 +000080 REPORTER_ASSERT(reporter, table->count() == count);
81 for (int i = 0; i < count; ++i) {
82 size_t size;
83 REPORTER_ASSERT(reporter, table->atSize(i) == sizes[i]);
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000084 REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size),
reed@google.com8c5c7a92013-04-19 20:16:01 +000085 str[i]));
86 REPORTER_ASSERT(reporter, size == sizes[i]);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000087
reed@google.com8c5c7a92013-04-19 20:16:01 +000088 const char* s = table->atStr(i);
89 REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
90 }
91}
92
93static void test_tablebuilder(skiatest::Reporter* reporter) {
94 const char* str[] = {
95 "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
96 };
97 int count = SK_ARRAY_COUNT(str);
98
99 SkDataTableBuilder builder(16);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000100
reed@google.com8c5c7a92013-04-19 20:16:01 +0000101 for (int i = 0; i < count; ++i) {
102 builder.append(str[i], strlen(str[i]) + 1);
103 }
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000104 SkAutoTUnref<SkDataTable> table(builder.detachDataTable());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000105
reed@google.com8c5c7a92013-04-19 20:16:01 +0000106 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) == strlen(str[i]) + 1);
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000110 REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size),
reed@google.com8c5c7a92013-04-19 20:16:01 +0000111 str[i]));
112 REPORTER_ASSERT(reporter, size == strlen(str[i]) + 1);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000113
reed@google.com8c5c7a92013-04-19 20:16:01 +0000114 const char* s = table->atStr(i);
115 REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
116 }
117}
118
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000119static void test_globaltable(skiatest::Reporter* reporter) {
120 static const int gData[] = {
121 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
122 };
123 int count = SK_ARRAY_COUNT(gData);
124
125 SkAutoTUnref<SkDataTable> table(SkDataTable::NewArrayProc(gData,
126 sizeof(gData[0]), count, NULL, NULL));
rmistry@google.comc9f3b382013-04-22 12:45:30 +0000127
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000128 REPORTER_ASSERT(reporter, table->count() == count);
129 for (int i = 0; i < count; ++i) {
130 size_t size;
131 REPORTER_ASSERT(reporter, table->atSize(i) == sizeof(int));
132 REPORTER_ASSERT(reporter, *table->atT<const char>(i, &size) == i);
133 REPORTER_ASSERT(reporter, sizeof(int) == size);
134 }
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000135}
136
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000137DEF_TEST(DataTable, reporter) {
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000138 test_emptytable(reporter);
reed@google.com8c5c7a92013-04-19 20:16:01 +0000139 test_simpletable(reporter);
140 test_vartable(reporter);
141 test_tablebuilder(reporter);
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000142 test_globaltable(reporter);
reed@google.com8c5c7a92013-04-19 20:16:01 +0000143}
144
reed@google.com1622c992011-06-14 19:22:21 +0000145static void* gGlobal;
146
147static void delete_int_proc(const void* ptr, size_t len, void* context) {
148 int* data = (int*)ptr;
149 SkASSERT(context == gGlobal);
150 delete[] data;
151}
152
reed@google.com8d0b5772011-06-24 13:07:31 +0000153static void assert_len(skiatest::Reporter* reporter, SkData* ref, size_t len) {
reed@google.com1622c992011-06-14 19:22:21 +0000154 REPORTER_ASSERT(reporter, ref->size() == len);
155}
156
reed@google.com8d0b5772011-06-24 13:07:31 +0000157static void assert_data(skiatest::Reporter* reporter, SkData* ref,
reed@google.com1622c992011-06-14 19:22:21 +0000158 const void* data, size_t len) {
159 REPORTER_ASSERT(reporter, ref->size() == len);
160 REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
161}
162
reed@google.comdbc936d2012-06-28 15:40:09 +0000163static void test_cstring(skiatest::Reporter* reporter) {
164 const char str[] = "Hello world";
165 size_t len = strlen(str);
166
reed@google.coma63a8512012-07-02 20:29:00 +0000167 SkAutoTUnref<SkData> r0(SkData::NewWithCopy(str, len + 1));
reed@google.comdbc936d2012-06-28 15:40:09 +0000168 SkAutoTUnref<SkData> r1(SkData::NewWithCString(str));
169
170 REPORTER_ASSERT(reporter, r0->equals(r1));
171
172 SkAutoTUnref<SkData> r2(SkData::NewWithCString(NULL));
reed@google.coma63a8512012-07-02 20:29:00 +0000173 REPORTER_ASSERT(reporter, 1 == r2->size());
174 REPORTER_ASSERT(reporter, 0 == *r2->bytes());
reed@google.comdbc936d2012-06-28 15:40:09 +0000175}
176
bungeman@google.com11c9a552013-06-03 17:10:35 +0000177static void test_files(skiatest::Reporter* reporter) {
scroggo@google.com99d43ff2013-06-07 14:30:36 +0000178 SkString tmpDir = skiatest::Test::GetTmpDir();
179 if (tmpDir.isEmpty()) {
bungeman@google.com11c9a552013-06-03 17:10:35 +0000180 return;
181 }
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000182
scroggo@google.com99d43ff2013-06-07 14:30:36 +0000183 SkString path = SkOSPath::SkPathJoin(tmpDir.c_str(), "data_test");
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000184
bungeman@google.com11c9a552013-06-03 17:10:35 +0000185 const char s[] = "abcdefghijklmnopqrstuvwxyz";
186 {
187 SkFILEWStream writer(path.c_str());
188 if (!writer.isValid()) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000189 ERRORF(reporter, "Failed to create tmp file %s\n", path.c_str());
bungeman@google.com11c9a552013-06-03 17:10:35 +0000190 return;
191 }
192 writer.write(s, 26);
193 }
194
195 SkFILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag);
196 SkAutoTUnref<SkData> r1(SkData::NewFromFILE(file));
197 REPORTER_ASSERT(reporter, r1.get() != NULL);
198 REPORTER_ASSERT(reporter, r1->size() == 26);
199 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r1->data()), s, 26) == 0);
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000200
bungeman@google.com11c9a552013-06-03 17:10:35 +0000201 int fd = sk_fileno(file);
202 SkAutoTUnref<SkData> r2(SkData::NewFromFD(fd));
203 REPORTER_ASSERT(reporter, r2.get() != NULL);
204 REPORTER_ASSERT(reporter, r2->size() == 26);
205 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 26) == 0);
206}
207
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000208DEF_TEST(Data, reporter) {
reed@google.com1622c992011-06-14 19:22:21 +0000209 const char* str = "We the people, in order to form a more perfect union.";
210 const int N = 10;
211
reed@google.comdbc936d2012-06-28 15:40:09 +0000212 SkAutoTUnref<SkData> r0(SkData::NewEmpty());
213 SkAutoTUnref<SkData> r1(SkData::NewWithCopy(str, strlen(str)));
214 SkAutoTUnref<SkData> r2(SkData::NewWithProc(new int[N], N*sizeof(int),
215 delete_int_proc, gGlobal));
216 SkAutoTUnref<SkData> r3(SkData::NewSubset(r1, 7, 6));
reed@google.com1622c992011-06-14 19:22:21 +0000217
reed@google.com1622c992011-06-14 19:22:21 +0000218 assert_len(reporter, r0, 0);
219 assert_len(reporter, r1, strlen(str));
220 assert_len(reporter, r2, N * sizeof(int));
221 assert_len(reporter, r3, 6);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000222
reed@google.com1622c992011-06-14 19:22:21 +0000223 assert_data(reporter, r1, str, strlen(str));
224 assert_data(reporter, r3, "people", 6);
225
reed@google.com8d0b5772011-06-24 13:07:31 +0000226 SkData* tmp = SkData::NewSubset(r1, strlen(str), 10);
reed@google.com1622c992011-06-14 19:22:21 +0000227 assert_len(reporter, tmp, 0);
228 tmp->unref();
reed@google.com8d0b5772011-06-24 13:07:31 +0000229 tmp = SkData::NewSubset(r1, 0, 0);
reed@google.com1622c992011-06-14 19:22:21 +0000230 assert_len(reporter, tmp, 0);
231 tmp->unref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000232
reed@google.comdbc936d2012-06-28 15:40:09 +0000233 test_cstring(reporter);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000234 test_files(reporter);
reed@google.com1622c992011-06-14 19:22:21 +0000235}