blob: c32c4553caa4a97f9c31f650103dba38298a1ea9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@google.com1622c992011-06-14 19:22:21 +00008#include "Test.h"
reed@google.com8d0b5772011-06-24 13:07:31 +00009#include "SkData.h"
reed@google.com8c5c7a92013-04-19 20:16:01 +000010#include "SkDataTable.h"
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000011#include "SkOrderedReadBuffer.h"
12#include "SkOrderedWriteBuffer.h"
bungeman@google.com11c9a552013-06-03 17:10:35 +000013#include "SkOSFile.h"
14#include "SkStream.h"
reed@google.com91bd4592012-07-11 17:24:49 +000015
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000016static void test_is_equal(skiatest::Reporter* reporter,
17 const SkDataTable* a, const SkDataTable* b) {
18 REPORTER_ASSERT(reporter, a->count() == b->count());
19 for (int i = 0; i < a->count(); ++i) {
20 size_t sizea, sizeb;
21 const void* mema = a->at(i, &sizea);
22 const void* memb = b->at(i, &sizeb);
23 REPORTER_ASSERT(reporter, sizea == sizeb);
24 REPORTER_ASSERT(reporter, !memcmp(mema, memb, sizea));
25 }
26}
27
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000028static void test_datatable_is_empty(skiatest::Reporter* reporter,
29 SkDataTable* table) {
30 REPORTER_ASSERT(reporter, table->isEmpty());
31 REPORTER_ASSERT(reporter, 0 == table->count());
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000032}
33
34static void test_emptytable(skiatest::Reporter* reporter) {
35 SkAutoTUnref<SkDataTable> table0(SkDataTable::NewEmpty());
36 SkAutoTUnref<SkDataTable> table1(SkDataTable::NewCopyArrays(NULL, NULL, 0));
mike@reedtribe.org9ca81a72013-04-21 01:43:09 +000037 SkAutoTUnref<SkDataTable> table2(SkDataTable::NewCopyArray(NULL, 0, 0));
38 SkAutoTUnref<SkDataTable> table3(SkDataTable::NewArrayProc(NULL, 0, 0,
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000039 NULL, NULL));
40
41 test_datatable_is_empty(reporter, table0);
42 test_datatable_is_empty(reporter, table1);
43 test_datatable_is_empty(reporter, table2);
44 test_datatable_is_empty(reporter, table3);
45
46 test_is_equal(reporter, table0, table1);
47 test_is_equal(reporter, table0, table2);
48 test_is_equal(reporter, table0, table3);
49}
50
reed@google.com8c5c7a92013-04-19 20:16:01 +000051static void test_simpletable(skiatest::Reporter* reporter) {
52 const int idata[] = { 1, 4, 9, 16, 25, 63 };
53 int icount = SK_ARRAY_COUNT(idata);
54 SkAutoTUnref<SkDataTable> itable(SkDataTable::NewCopyArray(idata,
55 sizeof(idata[0]),
56 icount));
57 REPORTER_ASSERT(reporter, itable->count() == icount);
58 for (int i = 0; i < icount; ++i) {
59 size_t size;
60 REPORTER_ASSERT(reporter, sizeof(int) == itable->atSize(i));
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000061 REPORTER_ASSERT(reporter, *itable->atT<int>(i, &size) == idata[i]);
reed@google.com8c5c7a92013-04-19 20:16:01 +000062 REPORTER_ASSERT(reporter, sizeof(int) == size);
63 }
64}
65
66static void test_vartable(skiatest::Reporter* reporter) {
67 const char* str[] = {
68 "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
69 };
70 int count = SK_ARRAY_COUNT(str);
71 size_t sizes[SK_ARRAY_COUNT(str)];
72 for (int i = 0; i < count; ++i) {
73 sizes[i] = strlen(str[i]) + 1;
74 }
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000075
reed@google.com8c5c7a92013-04-19 20:16:01 +000076 SkAutoTUnref<SkDataTable> table(SkDataTable::NewCopyArrays(
77 (const void*const*)str, sizes, count));
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000078
reed@google.com8c5c7a92013-04-19 20:16:01 +000079 REPORTER_ASSERT(reporter, table->count() == count);
80 for (int i = 0; i < count; ++i) {
81 size_t size;
82 REPORTER_ASSERT(reporter, table->atSize(i) == sizes[i]);
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +000083 REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size),
reed@google.com8c5c7a92013-04-19 20:16:01 +000084 str[i]));
85 REPORTER_ASSERT(reporter, size == sizes[i]);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000086
reed@google.com8c5c7a92013-04-19 20:16:01 +000087 const char* s = table->atStr(i);
88 REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
89 }
90}
91
92static void test_tablebuilder(skiatest::Reporter* reporter) {
93 const char* str[] = {
94 "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
95 };
96 int count = SK_ARRAY_COUNT(str);
97
98 SkDataTableBuilder builder(16);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +000099
reed@google.com8c5c7a92013-04-19 20:16:01 +0000100 for (int i = 0; i < count; ++i) {
101 builder.append(str[i], strlen(str[i]) + 1);
102 }
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000103 SkAutoTUnref<SkDataTable> table(builder.detachDataTable());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000104
reed@google.com8c5c7a92013-04-19 20:16:01 +0000105 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) == strlen(str[i]) + 1);
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000109 REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size),
reed@google.com8c5c7a92013-04-19 20:16:01 +0000110 str[i]));
111 REPORTER_ASSERT(reporter, size == strlen(str[i]) + 1);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000112
reed@google.com8c5c7a92013-04-19 20:16:01 +0000113 const char* s = table->atStr(i);
114 REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
115 }
116}
117
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000118static void test_globaltable(skiatest::Reporter* reporter) {
119 static const int gData[] = {
120 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
121 };
122 int count = SK_ARRAY_COUNT(gData);
123
124 SkAutoTUnref<SkDataTable> table(SkDataTable::NewArrayProc(gData,
125 sizeof(gData[0]), count, NULL, NULL));
rmistry@google.comc9f3b382013-04-22 12:45:30 +0000126
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000127 REPORTER_ASSERT(reporter, table->count() == count);
128 for (int i = 0; i < count; ++i) {
129 size_t size;
130 REPORTER_ASSERT(reporter, table->atSize(i) == sizeof(int));
131 REPORTER_ASSERT(reporter, *table->atT<const char>(i, &size) == i);
132 REPORTER_ASSERT(reporter, sizeof(int) == size);
133 }
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000134}
135
136static void TestDataTable(skiatest::Reporter* reporter) {
137 test_emptytable(reporter);
reed@google.com8c5c7a92013-04-19 20:16:01 +0000138 test_simpletable(reporter);
139 test_vartable(reporter);
140 test_tablebuilder(reporter);
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000141 test_globaltable(reporter);
reed@google.com8c5c7a92013-04-19 20:16:01 +0000142}
143
reed@google.com1622c992011-06-14 19:22:21 +0000144static void* gGlobal;
145
146static void delete_int_proc(const void* ptr, size_t len, void* context) {
147 int* data = (int*)ptr;
148 SkASSERT(context == gGlobal);
149 delete[] data;
150}
151
reed@google.com8d0b5772011-06-24 13:07:31 +0000152static void assert_len(skiatest::Reporter* reporter, SkData* ref, size_t len) {
reed@google.com1622c992011-06-14 19:22:21 +0000153 REPORTER_ASSERT(reporter, ref->size() == len);
154}
155
reed@google.com8d0b5772011-06-24 13:07:31 +0000156static void assert_data(skiatest::Reporter* reporter, SkData* ref,
reed@google.com1622c992011-06-14 19:22:21 +0000157 const void* data, size_t len) {
158 REPORTER_ASSERT(reporter, ref->size() == len);
159 REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
160}
161
reed@google.comdbc936d2012-06-28 15:40:09 +0000162static void test_cstring(skiatest::Reporter* reporter) {
163 const char str[] = "Hello world";
164 size_t len = strlen(str);
165
reed@google.coma63a8512012-07-02 20:29:00 +0000166 SkAutoTUnref<SkData> r0(SkData::NewWithCopy(str, len + 1));
reed@google.comdbc936d2012-06-28 15:40:09 +0000167 SkAutoTUnref<SkData> r1(SkData::NewWithCString(str));
168
169 REPORTER_ASSERT(reporter, r0->equals(r1));
170
171 SkAutoTUnref<SkData> r2(SkData::NewWithCString(NULL));
reed@google.coma63a8512012-07-02 20:29:00 +0000172 REPORTER_ASSERT(reporter, 1 == r2->size());
173 REPORTER_ASSERT(reporter, 0 == *r2->bytes());
reed@google.comdbc936d2012-06-28 15:40:09 +0000174}
175
bungeman@google.com11c9a552013-06-03 17:10:35 +0000176static void test_files(skiatest::Reporter* reporter) {
scroggo@google.com99d43ff2013-06-07 14:30:36 +0000177 SkString tmpDir = skiatest::Test::GetTmpDir();
178 if (tmpDir.isEmpty()) {
bungeman@google.com11c9a552013-06-03 17:10:35 +0000179 return;
180 }
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000181
scroggo@google.com99d43ff2013-06-07 14:30:36 +0000182 SkString path = SkOSPath::SkPathJoin(tmpDir.c_str(), "data_test");
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000183
bungeman@google.com11c9a552013-06-03 17:10:35 +0000184 const char s[] = "abcdefghijklmnopqrstuvwxyz";
185 {
186 SkFILEWStream writer(path.c_str());
187 if (!writer.isValid()) {
188 SkString msg;
189 msg.printf("Failed to create tmp file %s\n", path.c_str());
commit-bot@chromium.org1f792862013-06-18 20:50:34 +0000190 reporter->reportFailed(msg);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000191 return;
192 }
193 writer.write(s, 26);
194 }
195
196 SkFILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag);
197 SkAutoTUnref<SkData> r1(SkData::NewFromFILE(file));
198 REPORTER_ASSERT(reporter, r1.get() != NULL);
199 REPORTER_ASSERT(reporter, r1->size() == 26);
200 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r1->data()), s, 26) == 0);
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000201
bungeman@google.com11c9a552013-06-03 17:10:35 +0000202 int fd = sk_fileno(file);
203 SkAutoTUnref<SkData> r2(SkData::NewFromFD(fd));
204 REPORTER_ASSERT(reporter, r2.get() != NULL);
205 REPORTER_ASSERT(reporter, r2->size() == 26);
206 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 26) == 0);
207}
208
reed@google.com91bd4592012-07-11 17:24:49 +0000209static void TestData(skiatest::Reporter* reporter) {
reed@google.com1622c992011-06-14 19:22:21 +0000210 const char* str = "We the people, in order to form a more perfect union.";
211 const int N = 10;
212
reed@google.comdbc936d2012-06-28 15:40:09 +0000213 SkAutoTUnref<SkData> r0(SkData::NewEmpty());
214 SkAutoTUnref<SkData> r1(SkData::NewWithCopy(str, strlen(str)));
215 SkAutoTUnref<SkData> r2(SkData::NewWithProc(new int[N], N*sizeof(int),
216 delete_int_proc, gGlobal));
217 SkAutoTUnref<SkData> r3(SkData::NewSubset(r1, 7, 6));
reed@google.com1622c992011-06-14 19:22:21 +0000218
reed@google.com1622c992011-06-14 19:22:21 +0000219 assert_len(reporter, r0, 0);
220 assert_len(reporter, r1, strlen(str));
221 assert_len(reporter, r2, N * sizeof(int));
222 assert_len(reporter, r3, 6);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000223
reed@google.com1622c992011-06-14 19:22:21 +0000224 assert_data(reporter, r1, str, strlen(str));
225 assert_data(reporter, r3, "people", 6);
226
reed@google.com8d0b5772011-06-24 13:07:31 +0000227 SkData* tmp = SkData::NewSubset(r1, strlen(str), 10);
reed@google.com1622c992011-06-14 19:22:21 +0000228 assert_len(reporter, tmp, 0);
229 tmp->unref();
reed@google.com8d0b5772011-06-24 13:07:31 +0000230 tmp = SkData::NewSubset(r1, 0, 0);
reed@google.com1622c992011-06-14 19:22:21 +0000231 assert_len(reporter, tmp, 0);
232 tmp->unref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000233
reed@google.comdbc936d2012-06-28 15:40:09 +0000234 test_cstring(reporter);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000235 test_files(reporter);
reed@google.com1622c992011-06-14 19:22:21 +0000236}
237
238#include "TestClassDef.h"
reed@google.com91bd4592012-07-11 17:24:49 +0000239DEFINE_TESTCLASS("Data", DataTestClass, TestData)
mike@reedtribe.orgcac3ae32013-04-21 01:37:46 +0000240DEFINE_TESTCLASS("DataTable", DataTableTestClass, TestDataTable)