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 | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 8 | #include "SkData.h" |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 9 | #include "SkDataTable.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 10 | #include "SkOSFile.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 13 | #include "SkStream.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 14 | #include "Test.h" |
reed@google.com | 91bd459 | 2012-07-11 17:24:49 +0000 | [diff] [blame] | 15 | |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 16 | static 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.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 28 | static 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.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static void test_emptytable(skiatest::Reporter* reporter) { |
| 35 | SkAutoTUnref<SkDataTable> table0(SkDataTable::NewEmpty()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 36 | SkAutoTUnref<SkDataTable> table1(SkDataTable::NewCopyArrays(nullptr, nullptr, 0)); |
| 37 | SkAutoTUnref<SkDataTable> table2(SkDataTable::NewCopyArray(nullptr, 0, 0)); |
| 38 | SkAutoTUnref<SkDataTable> table3(SkDataTable::NewArrayProc(nullptr, 0, 0, |
| 39 | nullptr, nullptr)); |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 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.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 51 | static 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.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 61 | REPORTER_ASSERT(reporter, *itable->atT<int>(i, &size) == idata[i]); |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 62 | REPORTER_ASSERT(reporter, sizeof(int) == size); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static 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.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 75 | |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 76 | SkAutoTUnref<SkDataTable> table(SkDataTable::NewCopyArrays( |
| 77 | (const void*const*)str, sizes, count)); |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 78 | |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 79 | 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.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 83 | REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size), |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 84 | str[i])); |
| 85 | REPORTER_ASSERT(reporter, size == sizes[i]); |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 86 | |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 87 | const char* s = table->atStr(i); |
| 88 | REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i])); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static 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.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 99 | |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 100 | for (int i = 0; i < count; ++i) { |
| 101 | builder.append(str[i], strlen(str[i]) + 1); |
| 102 | } |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 103 | SkAutoTUnref<SkDataTable> table(builder.detachDataTable()); |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 104 | |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 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) == strlen(str[i]) + 1); |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 109 | REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size), |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 110 | str[i])); |
| 111 | REPORTER_ASSERT(reporter, size == strlen(str[i]) + 1); |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 112 | |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 113 | const char* s = table->atStr(i); |
| 114 | REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i])); |
| 115 | } |
| 116 | } |
| 117 | |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 118 | static 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, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 125 | sizeof(gData[0]), count, nullptr, nullptr)); |
rmistry@google.com | c9f3b38 | 2013-04-22 12:45:30 +0000 | [diff] [blame] | 126 | |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 127 | 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.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 134 | } |
| 135 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 136 | DEF_TEST(DataTable, reporter) { |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 137 | test_emptytable(reporter); |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 138 | test_simpletable(reporter); |
| 139 | test_vartable(reporter); |
| 140 | test_tablebuilder(reporter); |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 141 | test_globaltable(reporter); |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 142 | } |
| 143 | |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 144 | static void* gGlobal; |
| 145 | |
reed | 6b7f34e | 2015-06-17 09:58:24 -0700 | [diff] [blame] | 146 | static void delete_int_proc(const void* ptr, void* context) { |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 147 | int* data = (int*)ptr; |
| 148 | SkASSERT(context == gGlobal); |
| 149 | delete[] data; |
| 150 | } |
| 151 | |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 152 | 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] | 153 | REPORTER_ASSERT(reporter, ref->size() == len); |
| 154 | } |
| 155 | |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 156 | static void assert_data(skiatest::Reporter* reporter, SkData* ref, |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 157 | 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.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 162 | static void test_cstring(skiatest::Reporter* reporter) { |
| 163 | const char str[] = "Hello world"; |
| 164 | size_t len = strlen(str); |
| 165 | |
reed@google.com | a63a851 | 2012-07-02 20:29:00 +0000 | [diff] [blame] | 166 | SkAutoTUnref<SkData> r0(SkData::NewWithCopy(str, len + 1)); |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 167 | SkAutoTUnref<SkData> r1(SkData::NewWithCString(str)); |
| 168 | |
| 169 | REPORTER_ASSERT(reporter, r0->equals(r1)); |
| 170 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 171 | SkAutoTUnref<SkData> r2(SkData::NewWithCString(nullptr)); |
reed@google.com | a63a851 | 2012-07-02 20:29:00 +0000 | [diff] [blame] | 172 | REPORTER_ASSERT(reporter, 1 == r2->size()); |
| 173 | REPORTER_ASSERT(reporter, 0 == *r2->bytes()); |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 174 | } |
| 175 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 176 | static void test_files(skiatest::Reporter* reporter) { |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 177 | SkString tmpDir = skiatest::GetTmpDir(); |
scroggo@google.com | 99d43ff | 2013-06-07 14:30:36 +0000 | [diff] [blame] | 178 | if (tmpDir.isEmpty()) { |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 179 | return; |
| 180 | } |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 181 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 182 | SkString path = SkOSPath::Join(tmpDir.c_str(), "data_test"); |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 183 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 184 | const char s[] = "abcdefghijklmnopqrstuvwxyz"; |
| 185 | { |
| 186 | SkFILEWStream writer(path.c_str()); |
| 187 | if (!writer.isValid()) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 188 | ERRORF(reporter, "Failed to create tmp file %s\n", path.c_str()); |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | writer.write(s, 26); |
| 192 | } |
| 193 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 194 | FILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag); |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 195 | SkAutoTUnref<SkData> r1(SkData::NewFromFILE(file)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 196 | REPORTER_ASSERT(reporter, r1.get() != nullptr); |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 197 | REPORTER_ASSERT(reporter, r1->size() == 26); |
| 198 | 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] | 199 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 200 | int fd = sk_fileno(file); |
| 201 | SkAutoTUnref<SkData> r2(SkData::NewFromFD(fd)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 202 | REPORTER_ASSERT(reporter, r2.get() != nullptr); |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 203 | REPORTER_ASSERT(reporter, r2->size() == 26); |
| 204 | REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 26) == 0); |
| 205 | } |
| 206 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 207 | DEF_TEST(Data, reporter) { |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 208 | const char* str = "We the people, in order to form a more perfect union."; |
| 209 | const int N = 10; |
| 210 | |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 211 | SkAutoTUnref<SkData> r0(SkData::NewEmpty()); |
| 212 | SkAutoTUnref<SkData> r1(SkData::NewWithCopy(str, strlen(str))); |
| 213 | SkAutoTUnref<SkData> r2(SkData::NewWithProc(new int[N], N*sizeof(int), |
| 214 | delete_int_proc, gGlobal)); |
| 215 | SkAutoTUnref<SkData> r3(SkData::NewSubset(r1, 7, 6)); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 216 | |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 217 | assert_len(reporter, r0, 0); |
| 218 | assert_len(reporter, r1, strlen(str)); |
| 219 | assert_len(reporter, r2, N * sizeof(int)); |
| 220 | assert_len(reporter, r3, 6); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 221 | |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 222 | assert_data(reporter, r1, str, strlen(str)); |
| 223 | assert_data(reporter, r3, "people", 6); |
| 224 | |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 225 | SkData* tmp = SkData::NewSubset(r1, strlen(str), 10); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 226 | assert_len(reporter, tmp, 0); |
| 227 | tmp->unref(); |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 228 | tmp = SkData::NewSubset(r1, 0, 0); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 229 | assert_len(reporter, tmp, 0); |
| 230 | tmp->unref(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 231 | |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 232 | test_cstring(reporter); |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 233 | test_files(reporter); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 234 | } |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 235 | |
| 236 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 237 | #include "SkRWBuffer.h" |
| 238 | |
| 239 | const char gABC[] = "abcdefghijklmnopqrstuvwxyz"; |
| 240 | |
| 241 | static void check_abcs(skiatest::Reporter* reporter, const char buffer[], size_t size) { |
| 242 | REPORTER_ASSERT(reporter, size % 26 == 0); |
| 243 | for (size_t offset = 0; offset < size; offset += 26) { |
| 244 | REPORTER_ASSERT(reporter, !memcmp(&buffer[offset], gABC, 26)); |
| 245 | } |
| 246 | } |
| 247 | |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 248 | // stream should contain an integral number of copies of gABC. |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 249 | static void check_alphabet_stream(skiatest::Reporter* reporter, SkStream* stream) { |
| 250 | REPORTER_ASSERT(reporter, stream->hasLength()); |
| 251 | size_t size = stream->getLength(); |
| 252 | REPORTER_ASSERT(reporter, size % 26 == 0); |
| 253 | |
| 254 | SkAutoTMalloc<char> storage(size); |
| 255 | char* array = storage.get(); |
| 256 | size_t bytesRead = stream->read(array, size); |
| 257 | REPORTER_ASSERT(reporter, bytesRead == size); |
| 258 | check_abcs(reporter, array, size); |
| 259 | |
| 260 | // try checking backwards |
| 261 | for (size_t offset = size; offset > 0; offset -= 26) { |
| 262 | REPORTER_ASSERT(reporter, stream->seek(offset - 26)); |
| 263 | REPORTER_ASSERT(reporter, stream->getPosition() == offset - 26); |
| 264 | REPORTER_ASSERT(reporter, stream->read(array, 26) == 26); |
| 265 | check_abcs(reporter, array, 26); |
| 266 | REPORTER_ASSERT(reporter, stream->getPosition() == offset); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // reader should contains an integral number of copies of gABC. |
| 271 | static void check_alphabet_buffer(skiatest::Reporter* reporter, const SkROBuffer* reader) { |
| 272 | size_t size = reader->size(); |
| 273 | REPORTER_ASSERT(reporter, size % 26 == 0); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 274 | |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 275 | SkAutoTMalloc<char> storage(size); |
| 276 | SkROBuffer::Iter iter(reader); |
| 277 | size_t offset = 0; |
| 278 | do { |
| 279 | SkASSERT(offset + iter.size() <= size); |
| 280 | memcpy(storage.get() + offset, iter.data(), iter.size()); |
| 281 | offset += iter.size(); |
| 282 | } while (iter.next()); |
| 283 | REPORTER_ASSERT(reporter, offset == size); |
| 284 | check_abcs(reporter, storage.get(), size); |
| 285 | } |
| 286 | |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 287 | #include "SkTaskGroup.h" |
| 288 | |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 289 | DEF_TEST(RWBuffer, reporter) { |
| 290 | // Knowing that the default capacity is 4096, choose N large enough so we force it to use |
| 291 | // multiple buffers internally. |
| 292 | const int N = 1000; |
| 293 | SkROBuffer* readers[N]; |
| 294 | SkStream* streams[N]; |
| 295 | |
| 296 | { |
| 297 | SkRWBuffer buffer; |
| 298 | for (int i = 0; i < N; ++i) { |
reed | 377add7 | 2016-04-08 12:47:14 -0700 | [diff] [blame] | 299 | buffer.append(gABC, 26); |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 300 | readers[i] = buffer.newRBufferSnapshot(); |
| 301 | streams[i] = buffer.newStreamSnapshot(); |
| 302 | } |
| 303 | REPORTER_ASSERT(reporter, N*26 == buffer.size()); |
| 304 | } |
| 305 | |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 306 | // Verify that although the SkRWBuffer's destructor has run, the readers are still valid. |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 307 | for (int i = 0; i < N; ++i) { |
| 308 | REPORTER_ASSERT(reporter, (i + 1) * 26U == readers[i]->size()); |
| 309 | check_alphabet_buffer(reporter, readers[i]); |
| 310 | check_alphabet_stream(reporter, streams[i]); |
| 311 | readers[i]->unref(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 312 | delete streams[i]; |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
scroggo | b512aaa | 2016-01-11 06:38:00 -0800 | [diff] [blame] | 315 | |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 316 | DEF_TEST(RWBuffer_threaded, reporter) { |
| 317 | // Knowing that the default capacity is 4096, choose N large enough so we force it to use |
| 318 | // multiple buffers internally. |
| 319 | const int N = 1000; |
| 320 | SkTaskGroup tasks; |
| 321 | SkRWBuffer buffer; |
| 322 | for (int i = 0; i < N; ++i) { |
| 323 | buffer.append(gABC, 26); |
| 324 | sk_sp<SkROBuffer> reader = sk_sp<SkROBuffer>(buffer.newRBufferSnapshot()); |
| 325 | SkStream* stream = buffer.newStreamSnapshot(); |
| 326 | REPORTER_ASSERT(reporter, reader->size() == buffer.size()); |
| 327 | REPORTER_ASSERT(reporter, stream->getLength() == buffer.size()); |
| 328 | |
| 329 | // reader's copy constructor will ref the SkROBuffer, which will be unreffed |
| 330 | // when the task ends. |
| 331 | // Ownership of stream is passed to the task, which will delete it. |
| 332 | tasks.add([reporter, i, reader, stream] { |
| 333 | REPORTER_ASSERT(reporter, (i + 1) * 26U == reader->size()); |
| 334 | REPORTER_ASSERT(reporter, stream->getLength() == reader->size()); |
| 335 | check_alphabet_buffer(reporter, reader.get()); |
| 336 | check_alphabet_stream(reporter, stream); |
| 337 | REPORTER_ASSERT(reporter, stream->rewind()); |
| 338 | delete stream; |
| 339 | }); |
| 340 | } |
| 341 | REPORTER_ASSERT(reporter, N*26 == buffer.size()); |
| 342 | tasks.wait(); |
| 343 | } |
| 344 | |
scroggo | b512aaa | 2016-01-11 06:38:00 -0800 | [diff] [blame] | 345 | // Tests that it is safe to call SkROBuffer::Iter::size() when exhausted. |
| 346 | DEF_TEST(RWBuffer_size, r) { |
| 347 | SkRWBuffer buffer; |
| 348 | buffer.append(gABC, 26); |
| 349 | |
| 350 | SkAutoTUnref<SkROBuffer> roBuffer(buffer.newRBufferSnapshot()); |
| 351 | SkROBuffer::Iter iter(roBuffer); |
| 352 | REPORTER_ASSERT(r, iter.data()); |
| 353 | REPORTER_ASSERT(r, iter.size() == 26); |
| 354 | |
| 355 | // There is only one block in this buffer. |
| 356 | REPORTER_ASSERT(r, !iter.next()); |
| 357 | REPORTER_ASSERT(r, 0 == iter.size()); |
| 358 | } |
scroggo | 9dec5d2 | 2016-02-16 06:59:18 -0800 | [diff] [blame] | 359 | |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 360 | // Tests that operations (including the destructor) are safe on an SkRWBuffer |
| 361 | // without any data appended. |
scroggo | 9dec5d2 | 2016-02-16 06:59:18 -0800 | [diff] [blame] | 362 | DEF_TEST(RWBuffer_noAppend, r) { |
| 363 | SkRWBuffer buffer; |
| 364 | REPORTER_ASSERT(r, 0 == buffer.size()); |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 365 | |
| 366 | sk_sp<SkROBuffer> roBuffer = sk_sp<SkROBuffer>(buffer.newRBufferSnapshot()); |
| 367 | REPORTER_ASSERT(r, roBuffer); |
| 368 | if (roBuffer) { |
| 369 | REPORTER_ASSERT(r, roBuffer->size() == 0); |
| 370 | SkROBuffer::Iter iter(roBuffer.get()); |
| 371 | REPORTER_ASSERT(r, iter.size() == 0); |
| 372 | REPORTER_ASSERT(r, !iter.data()); |
| 373 | REPORTER_ASSERT(r, !iter.next()); |
| 374 | } |
| 375 | |
| 376 | SkAutoTDelete<SkStream> stream(buffer.newStreamSnapshot()); |
| 377 | REPORTER_ASSERT(r, stream); |
| 378 | if (stream) { |
| 379 | REPORTER_ASSERT(r, stream->hasLength()); |
| 380 | REPORTER_ASSERT(r, stream->getLength() == 0); |
| 381 | REPORTER_ASSERT(r, stream->skip(10) == 0); |
| 382 | } |
scroggo | 9dec5d2 | 2016-02-16 06:59:18 -0800 | [diff] [blame] | 383 | } |