epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame^] | 7 | |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 8 | #include "Test.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame^] | 9 | #include "TestClassDef.h" |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 10 | #include "SkData.h" |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 11 | #include "SkDataTable.h" |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 12 | #include "SkOrderedReadBuffer.h" |
| 13 | #include "SkOrderedWriteBuffer.h" |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 14 | #include "SkOSFile.h" |
| 15 | #include "SkStream.h" |
reed@google.com | 91bd459 | 2012-07-11 17:24:49 +0000 | [diff] [blame] | 16 | |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 17 | static 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.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 29 | static 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.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static void test_emptytable(skiatest::Reporter* reporter) { |
| 36 | SkAutoTUnref<SkDataTable> table0(SkDataTable::NewEmpty()); |
| 37 | SkAutoTUnref<SkDataTable> table1(SkDataTable::NewCopyArrays(NULL, NULL, 0)); |
mike@reedtribe.org | 9ca81a7 | 2013-04-21 01:43:09 +0000 | [diff] [blame] | 38 | SkAutoTUnref<SkDataTable> table2(SkDataTable::NewCopyArray(NULL, 0, 0)); |
| 39 | SkAutoTUnref<SkDataTable> table3(SkDataTable::NewArrayProc(NULL, 0, 0, |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 40 | 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.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 52 | static 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.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 62 | REPORTER_ASSERT(reporter, *itable->atT<int>(i, &size) == idata[i]); |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 63 | REPORTER_ASSERT(reporter, sizeof(int) == size); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static 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.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 76 | |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 77 | SkAutoTUnref<SkDataTable> table(SkDataTable::NewCopyArrays( |
| 78 | (const void*const*)str, sizes, count)); |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 79 | |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 80 | 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.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 84 | REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size), |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 85 | str[i])); |
| 86 | REPORTER_ASSERT(reporter, size == sizes[i]); |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 87 | |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 88 | const char* s = table->atStr(i); |
| 89 | REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i])); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static 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.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 100 | |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 101 | for (int i = 0; i < count; ++i) { |
| 102 | builder.append(str[i], strlen(str[i]) + 1); |
| 103 | } |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 104 | SkAutoTUnref<SkDataTable> table(builder.detachDataTable()); |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 105 | |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 106 | 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.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 110 | REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size), |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 111 | str[i])); |
| 112 | REPORTER_ASSERT(reporter, size == strlen(str[i]) + 1); |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 113 | |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 114 | const char* s = table->atStr(i); |
| 115 | REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i])); |
| 116 | } |
| 117 | } |
| 118 | |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 119 | static 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.com | c9f3b38 | 2013-04-22 12:45:30 +0000 | [diff] [blame] | 127 | |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 128 | 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.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 135 | } |
| 136 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame^] | 137 | DEF_TEST(DataTable, reporter) { |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 138 | test_emptytable(reporter); |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 139 | test_simpletable(reporter); |
| 140 | test_vartable(reporter); |
| 141 | test_tablebuilder(reporter); |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 142 | test_globaltable(reporter); |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 143 | } |
| 144 | |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 145 | static void* gGlobal; |
| 146 | |
| 147 | static 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.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 153 | static void assert_len(skiatest::Reporter* reporter, SkData* ref, size_t len) { |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 154 | REPORTER_ASSERT(reporter, ref->size() == len); |
| 155 | } |
| 156 | |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 157 | static void assert_data(skiatest::Reporter* reporter, SkData* ref, |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 158 | 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.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 163 | static void test_cstring(skiatest::Reporter* reporter) { |
| 164 | const char str[] = "Hello world"; |
| 165 | size_t len = strlen(str); |
| 166 | |
reed@google.com | a63a851 | 2012-07-02 20:29:00 +0000 | [diff] [blame] | 167 | SkAutoTUnref<SkData> r0(SkData::NewWithCopy(str, len + 1)); |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 168 | SkAutoTUnref<SkData> r1(SkData::NewWithCString(str)); |
| 169 | |
| 170 | REPORTER_ASSERT(reporter, r0->equals(r1)); |
| 171 | |
| 172 | SkAutoTUnref<SkData> r2(SkData::NewWithCString(NULL)); |
reed@google.com | a63a851 | 2012-07-02 20:29:00 +0000 | [diff] [blame] | 173 | REPORTER_ASSERT(reporter, 1 == r2->size()); |
| 174 | REPORTER_ASSERT(reporter, 0 == *r2->bytes()); |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 175 | } |
| 176 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 177 | static void test_files(skiatest::Reporter* reporter) { |
scroggo@google.com | 99d43ff | 2013-06-07 14:30:36 +0000 | [diff] [blame] | 178 | SkString tmpDir = skiatest::Test::GetTmpDir(); |
| 179 | if (tmpDir.isEmpty()) { |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 180 | return; |
| 181 | } |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 182 | |
scroggo@google.com | 99d43ff | 2013-06-07 14:30:36 +0000 | [diff] [blame] | 183 | SkString path = SkOSPath::SkPathJoin(tmpDir.c_str(), "data_test"); |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 184 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 185 | const char s[] = "abcdefghijklmnopqrstuvwxyz"; |
| 186 | { |
| 187 | SkFILEWStream writer(path.c_str()); |
| 188 | if (!writer.isValid()) { |
| 189 | SkString msg; |
| 190 | msg.printf("Failed to create tmp file %s\n", path.c_str()); |
commit-bot@chromium.org | 1f79286 | 2013-06-18 20:50:34 +0000 | [diff] [blame] | 191 | reporter->reportFailed(msg); |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | writer.write(s, 26); |
| 195 | } |
| 196 | |
| 197 | SkFILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag); |
| 198 | SkAutoTUnref<SkData> r1(SkData::NewFromFILE(file)); |
| 199 | REPORTER_ASSERT(reporter, r1.get() != NULL); |
| 200 | REPORTER_ASSERT(reporter, r1->size() == 26); |
| 201 | REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r1->data()), s, 26) == 0); |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 202 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 203 | int fd = sk_fileno(file); |
| 204 | SkAutoTUnref<SkData> r2(SkData::NewFromFD(fd)); |
| 205 | REPORTER_ASSERT(reporter, r2.get() != NULL); |
| 206 | REPORTER_ASSERT(reporter, r2->size() == 26); |
| 207 | REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 26) == 0); |
| 208 | } |
| 209 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame^] | 210 | DEF_TEST(Data, reporter) { |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 211 | const char* str = "We the people, in order to form a more perfect union."; |
| 212 | const int N = 10; |
| 213 | |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 214 | SkAutoTUnref<SkData> r0(SkData::NewEmpty()); |
| 215 | SkAutoTUnref<SkData> r1(SkData::NewWithCopy(str, strlen(str))); |
| 216 | SkAutoTUnref<SkData> r2(SkData::NewWithProc(new int[N], N*sizeof(int), |
| 217 | delete_int_proc, gGlobal)); |
| 218 | SkAutoTUnref<SkData> r3(SkData::NewSubset(r1, 7, 6)); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 219 | |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 220 | assert_len(reporter, r0, 0); |
| 221 | assert_len(reporter, r1, strlen(str)); |
| 222 | assert_len(reporter, r2, N * sizeof(int)); |
| 223 | assert_len(reporter, r3, 6); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 224 | |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 225 | assert_data(reporter, r1, str, strlen(str)); |
| 226 | assert_data(reporter, r3, "people", 6); |
| 227 | |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 228 | SkData* tmp = SkData::NewSubset(r1, strlen(str), 10); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 229 | assert_len(reporter, tmp, 0); |
| 230 | tmp->unref(); |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 231 | tmp = SkData::NewSubset(r1, 0, 0); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 232 | assert_len(reporter, tmp, 0); |
| 233 | tmp->unref(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 234 | |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 235 | test_cstring(reporter); |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 236 | test_files(reporter); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 237 | } |