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" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 11 | #include "SkOSPath.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
| 13 | #include "SkWriteBuffer.h" |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 14 | #include "SkStream.h" |
Florin Malita | 69270fb | 2017-06-01 09:43:01 -0400 | [diff] [blame] | 15 | #include "SkTArray.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 16 | #include "Test.h" |
reed@google.com | 91bd459 | 2012-07-11 17:24:49 +0000 | [diff] [blame] | 17 | |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 18 | static void test_is_equal(skiatest::Reporter* reporter, |
| 19 | const SkDataTable* a, const SkDataTable* b) { |
| 20 | REPORTER_ASSERT(reporter, a->count() == b->count()); |
| 21 | for (int i = 0; i < a->count(); ++i) { |
| 22 | size_t sizea, sizeb; |
| 23 | const void* mema = a->at(i, &sizea); |
| 24 | const void* memb = b->at(i, &sizeb); |
| 25 | REPORTER_ASSERT(reporter, sizea == sizeb); |
| 26 | REPORTER_ASSERT(reporter, !memcmp(mema, memb, sizea)); |
| 27 | } |
| 28 | } |
| 29 | |
bungeman | feb3c1a | 2016-08-05 06:51:50 -0700 | [diff] [blame] | 30 | static void test_datatable_is_empty(skiatest::Reporter* reporter, SkDataTable* table) { |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 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) { |
bungeman | feb3c1a | 2016-08-05 06:51:50 -0700 | [diff] [blame] | 36 | sk_sp<SkDataTable> table0(SkDataTable::MakeEmpty()); |
| 37 | sk_sp<SkDataTable> table1(SkDataTable::MakeCopyArrays(nullptr, nullptr, 0)); |
| 38 | sk_sp<SkDataTable> table2(SkDataTable::MakeCopyArray(nullptr, 0, 0)); |
| 39 | sk_sp<SkDataTable> table3(SkDataTable::MakeArrayProc(nullptr, 0, 0, nullptr, nullptr)); |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 40 | |
bungeman | feb3c1a | 2016-08-05 06:51:50 -0700 | [diff] [blame] | 41 | test_datatable_is_empty(reporter, table0.get()); |
| 42 | test_datatable_is_empty(reporter, table1.get()); |
| 43 | test_datatable_is_empty(reporter, table2.get()); |
| 44 | test_datatable_is_empty(reporter, table3.get()); |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 45 | |
bungeman | feb3c1a | 2016-08-05 06:51:50 -0700 | [diff] [blame] | 46 | test_is_equal(reporter, table0.get(), table1.get()); |
| 47 | test_is_equal(reporter, table0.get(), table2.get()); |
| 48 | test_is_equal(reporter, table0.get(), table3.get()); |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 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); |
bungeman | feb3c1a | 2016-08-05 06:51:50 -0700 | [diff] [blame] | 54 | sk_sp<SkDataTable> itable(SkDataTable::MakeCopyArray(idata, sizeof(idata[0]), icount)); |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 55 | REPORTER_ASSERT(reporter, itable->count() == icount); |
| 56 | for (int i = 0; i < icount; ++i) { |
| 57 | size_t size; |
| 58 | REPORTER_ASSERT(reporter, sizeof(int) == itable->atSize(i)); |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 59 | REPORTER_ASSERT(reporter, *itable->atT<int>(i, &size) == idata[i]); |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 60 | REPORTER_ASSERT(reporter, sizeof(int) == size); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static void test_vartable(skiatest::Reporter* reporter) { |
| 65 | const char* str[] = { |
| 66 | "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg" |
| 67 | }; |
| 68 | int count = SK_ARRAY_COUNT(str); |
| 69 | size_t sizes[SK_ARRAY_COUNT(str)]; |
| 70 | for (int i = 0; i < count; ++i) { |
| 71 | sizes[i] = strlen(str[i]) + 1; |
| 72 | } |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 73 | |
bungeman | feb3c1a | 2016-08-05 06:51:50 -0700 | [diff] [blame] | 74 | sk_sp<SkDataTable> table(SkDataTable::MakeCopyArrays((const void*const*)str, sizes, count)); |
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 | REPORTER_ASSERT(reporter, table->count() == count); |
| 77 | for (int i = 0; i < count; ++i) { |
| 78 | size_t size; |
| 79 | REPORTER_ASSERT(reporter, table->atSize(i) == sizes[i]); |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 80 | REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size), |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 81 | str[i])); |
| 82 | REPORTER_ASSERT(reporter, size == sizes[i]); |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 83 | |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 84 | const char* s = table->atStr(i); |
| 85 | REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i])); |
| 86 | } |
| 87 | } |
| 88 | |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 89 | static void test_globaltable(skiatest::Reporter* reporter) { |
| 90 | static const int gData[] = { |
| 91 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 |
| 92 | }; |
| 93 | int count = SK_ARRAY_COUNT(gData); |
| 94 | |
bungeman | feb3c1a | 2016-08-05 06:51:50 -0700 | [diff] [blame] | 95 | sk_sp<SkDataTable> table( |
| 96 | SkDataTable::MakeArrayProc(gData, sizeof(gData[0]), count, nullptr, nullptr)); |
rmistry@google.com | c9f3b38 | 2013-04-22 12:45:30 +0000 | [diff] [blame] | 97 | |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 98 | REPORTER_ASSERT(reporter, table->count() == count); |
| 99 | for (int i = 0; i < count; ++i) { |
| 100 | size_t size; |
| 101 | REPORTER_ASSERT(reporter, table->atSize(i) == sizeof(int)); |
| 102 | REPORTER_ASSERT(reporter, *table->atT<const char>(i, &size) == i); |
| 103 | REPORTER_ASSERT(reporter, sizeof(int) == size); |
| 104 | } |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 105 | } |
| 106 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 107 | DEF_TEST(DataTable, reporter) { |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 108 | test_emptytable(reporter); |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 109 | test_simpletable(reporter); |
| 110 | test_vartable(reporter); |
mike@reedtribe.org | cac3ae3 | 2013-04-21 01:37:46 +0000 | [diff] [blame] | 111 | test_globaltable(reporter); |
reed@google.com | 8c5c7a9 | 2013-04-19 20:16:01 +0000 | [diff] [blame] | 112 | } |
| 113 | |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 114 | static void* gGlobal; |
| 115 | |
reed | 6b7f34e | 2015-06-17 09:58:24 -0700 | [diff] [blame] | 116 | static void delete_int_proc(const void* ptr, void* context) { |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 117 | int* data = (int*)ptr; |
| 118 | SkASSERT(context == gGlobal); |
| 119 | delete[] data; |
| 120 | } |
| 121 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 122 | static void assert_len(skiatest::Reporter* reporter, const sk_sp<SkData>& ref, size_t len) { |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 123 | REPORTER_ASSERT(reporter, ref->size() == len); |
| 124 | } |
| 125 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 126 | static void assert_data(skiatest::Reporter* reporter, const sk_sp<SkData>& ref, |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 127 | const void* data, size_t len) { |
| 128 | REPORTER_ASSERT(reporter, ref->size() == len); |
| 129 | REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len)); |
| 130 | } |
| 131 | |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 132 | static void test_cstring(skiatest::Reporter* reporter) { |
| 133 | const char str[] = "Hello world"; |
| 134 | size_t len = strlen(str); |
| 135 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 136 | sk_sp<SkData> r0(SkData::MakeWithCopy(str, len + 1)); |
| 137 | sk_sp<SkData> r1(SkData::MakeWithCString(str)); |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 138 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 139 | REPORTER_ASSERT(reporter, r0->equals(r1.get())); |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 140 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 141 | sk_sp<SkData> r2(SkData::MakeWithCString(nullptr)); |
reed@google.com | a63a851 | 2012-07-02 20:29:00 +0000 | [diff] [blame] | 142 | REPORTER_ASSERT(reporter, 1 == r2->size()); |
| 143 | REPORTER_ASSERT(reporter, 0 == *r2->bytes()); |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 144 | } |
| 145 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 146 | static void test_files(skiatest::Reporter* reporter) { |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 147 | SkString tmpDir = skiatest::GetTmpDir(); |
scroggo@google.com | 99d43ff | 2013-06-07 14:30:36 +0000 | [diff] [blame] | 148 | if (tmpDir.isEmpty()) { |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 149 | return; |
| 150 | } |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 151 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 152 | SkString path = SkOSPath::Join(tmpDir.c_str(), "data_test"); |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 153 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 154 | const char s[] = "abcdefghijklmnopqrstuvwxyz"; |
| 155 | { |
| 156 | SkFILEWStream writer(path.c_str()); |
| 157 | if (!writer.isValid()) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 158 | 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] | 159 | return; |
| 160 | } |
| 161 | writer.write(s, 26); |
| 162 | } |
| 163 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 164 | FILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 165 | sk_sp<SkData> r1(SkData::MakeFromFILE(file)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 166 | REPORTER_ASSERT(reporter, r1.get() != nullptr); |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 167 | REPORTER_ASSERT(reporter, r1->size() == 26); |
| 168 | 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] | 169 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 170 | int fd = sk_fileno(file); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 171 | sk_sp<SkData> r2(SkData::MakeFromFD(fd)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 172 | REPORTER_ASSERT(reporter, r2.get() != nullptr); |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 173 | REPORTER_ASSERT(reporter, r2->size() == 26); |
| 174 | REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 26) == 0); |
| 175 | } |
| 176 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 177 | DEF_TEST(Data, reporter) { |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 178 | const char* str = "We the people, in order to form a more perfect union."; |
| 179 | const int N = 10; |
| 180 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 181 | sk_sp<SkData> r0(SkData::MakeEmpty()); |
| 182 | sk_sp<SkData> r1(SkData::MakeWithCopy(str, strlen(str))); |
| 183 | sk_sp<SkData> r2(SkData::MakeWithProc(new int[N], N*sizeof(int), delete_int_proc, gGlobal)); |
| 184 | sk_sp<SkData> r3(SkData::MakeSubset(r1.get(), 7, 6)); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 185 | |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 186 | assert_len(reporter, r0, 0); |
| 187 | assert_len(reporter, r1, strlen(str)); |
| 188 | assert_len(reporter, r2, N * sizeof(int)); |
| 189 | assert_len(reporter, r3, 6); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 190 | |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 191 | assert_data(reporter, r1, str, strlen(str)); |
| 192 | assert_data(reporter, r3, "people", 6); |
| 193 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 194 | sk_sp<SkData> tmp(SkData::MakeSubset(r1.get(), strlen(str), 10)); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 195 | assert_len(reporter, tmp, 0); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 196 | tmp = SkData::MakeSubset(r1.get(), 0, 0); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 197 | assert_len(reporter, tmp, 0); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 198 | |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 199 | test_cstring(reporter); |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 200 | test_files(reporter); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 201 | } |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 202 | |
| 203 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 204 | #include "SkRWBuffer.h" |
| 205 | |
| 206 | const char gABC[] = "abcdefghijklmnopqrstuvwxyz"; |
| 207 | |
| 208 | static void check_abcs(skiatest::Reporter* reporter, const char buffer[], size_t size) { |
| 209 | REPORTER_ASSERT(reporter, size % 26 == 0); |
| 210 | for (size_t offset = 0; offset < size; offset += 26) { |
| 211 | REPORTER_ASSERT(reporter, !memcmp(&buffer[offset], gABC, 26)); |
| 212 | } |
| 213 | } |
| 214 | |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 215 | // stream should contain an integral number of copies of gABC. |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 216 | static void check_alphabet_stream(skiatest::Reporter* reporter, SkStream* stream) { |
| 217 | REPORTER_ASSERT(reporter, stream->hasLength()); |
| 218 | size_t size = stream->getLength(); |
| 219 | REPORTER_ASSERT(reporter, size % 26 == 0); |
| 220 | |
| 221 | SkAutoTMalloc<char> storage(size); |
| 222 | char* array = storage.get(); |
| 223 | size_t bytesRead = stream->read(array, size); |
| 224 | REPORTER_ASSERT(reporter, bytesRead == size); |
| 225 | check_abcs(reporter, array, size); |
| 226 | |
| 227 | // try checking backwards |
| 228 | for (size_t offset = size; offset > 0; offset -= 26) { |
| 229 | REPORTER_ASSERT(reporter, stream->seek(offset - 26)); |
| 230 | REPORTER_ASSERT(reporter, stream->getPosition() == offset - 26); |
| 231 | REPORTER_ASSERT(reporter, stream->read(array, 26) == 26); |
| 232 | check_abcs(reporter, array, 26); |
| 233 | REPORTER_ASSERT(reporter, stream->getPosition() == offset); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // reader should contains an integral number of copies of gABC. |
| 238 | static void check_alphabet_buffer(skiatest::Reporter* reporter, const SkROBuffer* reader) { |
| 239 | size_t size = reader->size(); |
| 240 | REPORTER_ASSERT(reporter, size % 26 == 0); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 241 | |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 242 | SkAutoTMalloc<char> storage(size); |
| 243 | SkROBuffer::Iter iter(reader); |
| 244 | size_t offset = 0; |
| 245 | do { |
| 246 | SkASSERT(offset + iter.size() <= size); |
| 247 | memcpy(storage.get() + offset, iter.data(), iter.size()); |
| 248 | offset += iter.size(); |
| 249 | } while (iter.next()); |
| 250 | REPORTER_ASSERT(reporter, offset == size); |
| 251 | check_abcs(reporter, storage.get(), size); |
| 252 | } |
| 253 | |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 254 | #include "SkTaskGroup.h" |
| 255 | |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 256 | DEF_TEST(RWBuffer, reporter) { |
| 257 | // Knowing that the default capacity is 4096, choose N large enough so we force it to use |
| 258 | // multiple buffers internally. |
Florin Malita | 69270fb | 2017-06-01 09:43:01 -0400 | [diff] [blame] | 259 | static constexpr int N = 1000; |
| 260 | SkSTArray<N, sk_sp<SkROBuffer>> readers; |
| 261 | SkSTArray<N, std::unique_ptr<SkStream>> streams; |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 262 | |
| 263 | { |
| 264 | SkRWBuffer buffer; |
| 265 | for (int i = 0; i < N; ++i) { |
reed | 377add7 | 2016-04-08 12:47:14 -0700 | [diff] [blame] | 266 | buffer.append(gABC, 26); |
Florin Malita | 69270fb | 2017-06-01 09:43:01 -0400 | [diff] [blame] | 267 | readers.push_back(buffer.makeROBufferSnapshot()); |
| 268 | streams.push_back(buffer.makeStreamSnapshot()); |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 269 | } |
| 270 | REPORTER_ASSERT(reporter, N*26 == buffer.size()); |
| 271 | } |
| 272 | |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 273 | // 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] | 274 | for (int i = 0; i < N; ++i) { |
| 275 | REPORTER_ASSERT(reporter, (i + 1) * 26U == readers[i]->size()); |
Florin Malita | 69270fb | 2017-06-01 09:43:01 -0400 | [diff] [blame] | 276 | check_alphabet_buffer(reporter, readers[i].get()); |
| 277 | check_alphabet_stream(reporter, streams[i].get()); |
reed | 5b6db07 | 2015-04-28 17:50:31 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
scroggo | b512aaa | 2016-01-11 06:38:00 -0800 | [diff] [blame] | 280 | |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 281 | DEF_TEST(RWBuffer_threaded, reporter) { |
| 282 | // Knowing that the default capacity is 4096, choose N large enough so we force it to use |
| 283 | // multiple buffers internally. |
| 284 | const int N = 1000; |
| 285 | SkTaskGroup tasks; |
| 286 | SkRWBuffer buffer; |
| 287 | for (int i = 0; i < N; ++i) { |
| 288 | buffer.append(gABC, 26); |
Florin Malita | 69270fb | 2017-06-01 09:43:01 -0400 | [diff] [blame] | 289 | sk_sp<SkROBuffer> reader = buffer.makeROBufferSnapshot(); |
| 290 | SkStream* stream = buffer.makeStreamSnapshot().release(); |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 291 | REPORTER_ASSERT(reporter, reader->size() == buffer.size()); |
| 292 | REPORTER_ASSERT(reporter, stream->getLength() == buffer.size()); |
| 293 | |
| 294 | // reader's copy constructor will ref the SkROBuffer, which will be unreffed |
| 295 | // when the task ends. |
| 296 | // Ownership of stream is passed to the task, which will delete it. |
| 297 | tasks.add([reporter, i, reader, stream] { |
| 298 | REPORTER_ASSERT(reporter, (i + 1) * 26U == reader->size()); |
| 299 | REPORTER_ASSERT(reporter, stream->getLength() == reader->size()); |
| 300 | check_alphabet_buffer(reporter, reader.get()); |
| 301 | check_alphabet_stream(reporter, stream); |
| 302 | REPORTER_ASSERT(reporter, stream->rewind()); |
| 303 | delete stream; |
| 304 | }); |
| 305 | } |
| 306 | REPORTER_ASSERT(reporter, N*26 == buffer.size()); |
| 307 | tasks.wait(); |
| 308 | } |
| 309 | |
scroggo | b512aaa | 2016-01-11 06:38:00 -0800 | [diff] [blame] | 310 | // Tests that it is safe to call SkROBuffer::Iter::size() when exhausted. |
| 311 | DEF_TEST(RWBuffer_size, r) { |
| 312 | SkRWBuffer buffer; |
| 313 | buffer.append(gABC, 26); |
| 314 | |
Florin Malita | 69270fb | 2017-06-01 09:43:01 -0400 | [diff] [blame] | 315 | sk_sp<SkROBuffer> roBuffer(buffer.makeROBufferSnapshot()); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 316 | SkROBuffer::Iter iter(roBuffer.get()); |
scroggo | b512aaa | 2016-01-11 06:38:00 -0800 | [diff] [blame] | 317 | REPORTER_ASSERT(r, iter.data()); |
| 318 | REPORTER_ASSERT(r, iter.size() == 26); |
| 319 | |
| 320 | // There is only one block in this buffer. |
| 321 | REPORTER_ASSERT(r, !iter.next()); |
| 322 | REPORTER_ASSERT(r, 0 == iter.size()); |
| 323 | } |
scroggo | 9dec5d2 | 2016-02-16 06:59:18 -0800 | [diff] [blame] | 324 | |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 325 | // Tests that operations (including the destructor) are safe on an SkRWBuffer |
| 326 | // without any data appended. |
scroggo | 9dec5d2 | 2016-02-16 06:59:18 -0800 | [diff] [blame] | 327 | DEF_TEST(RWBuffer_noAppend, r) { |
| 328 | SkRWBuffer buffer; |
| 329 | REPORTER_ASSERT(r, 0 == buffer.size()); |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 330 | |
Florin Malita | 69270fb | 2017-06-01 09:43:01 -0400 | [diff] [blame] | 331 | sk_sp<SkROBuffer> roBuffer = buffer.makeROBufferSnapshot(); |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 332 | REPORTER_ASSERT(r, roBuffer); |
| 333 | if (roBuffer) { |
| 334 | REPORTER_ASSERT(r, roBuffer->size() == 0); |
| 335 | SkROBuffer::Iter iter(roBuffer.get()); |
| 336 | REPORTER_ASSERT(r, iter.size() == 0); |
| 337 | REPORTER_ASSERT(r, !iter.data()); |
| 338 | REPORTER_ASSERT(r, !iter.next()); |
| 339 | } |
| 340 | |
Florin Malita | 69270fb | 2017-06-01 09:43:01 -0400 | [diff] [blame] | 341 | std::unique_ptr<SkStream> stream(buffer.makeStreamSnapshot()); |
scroggo | 6351640 | 2016-04-22 06:59:01 -0700 | [diff] [blame] | 342 | REPORTER_ASSERT(r, stream); |
| 343 | if (stream) { |
| 344 | REPORTER_ASSERT(r, stream->hasLength()); |
| 345 | REPORTER_ASSERT(r, stream->getLength() == 0); |
| 346 | REPORTER_ASSERT(r, stream->skip(10) == 0); |
| 347 | } |
scroggo | 9dec5d2 | 2016-02-16 06:59:18 -0800 | [diff] [blame] | 348 | } |