reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/utils/SkRandom.h" |
| 9 | #include "src/core/SkAutoMalloc.h" |
Brian Osman | ff7bee9 | 2020-06-09 16:04:18 -0400 | [diff] [blame] | 10 | #include "src/core/SkReadBuffer.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/core/SkWriter32.h" |
| 12 | #include "tests/Test.h" |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 13 | |
reed@google.com | 1cf58d0 | 2012-08-30 14:03:21 +0000 | [diff] [blame] | 14 | static void check_contents(skiatest::Reporter* reporter, const SkWriter32& writer, |
| 15 | const void* expected, size_t size) { |
| 16 | SkAutoSMalloc<256> storage(size); |
| 17 | REPORTER_ASSERT(reporter, writer.bytesWritten() == size); |
| 18 | writer.flatten(storage.get()); |
| 19 | REPORTER_ASSERT(reporter, !memcmp(storage.get(), expected, size)); |
| 20 | } |
| 21 | |
mtklein@google.com | 8b4ba63 | 2013-09-03 18:07:01 +0000 | [diff] [blame] | 22 | |
| 23 | static void test_reserve(skiatest::Reporter* reporter) { |
| 24 | // There used to be a bug where we'd assert your first reservation had to |
| 25 | // fit in external storage if you used it. This would crash in debug mode. |
| 26 | uint8_t storage[4]; |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 27 | SkWriter32 writer(storage, sizeof(storage)); |
mtklein@google.com | 8b4ba63 | 2013-09-03 18:07:01 +0000 | [diff] [blame] | 28 | writer.reserve(40); |
| 29 | } |
| 30 | |
commit-bot@chromium.org | 47fa136 | 2013-08-09 16:03:05 +0000 | [diff] [blame] | 31 | static void test_string_null(skiatest::Reporter* reporter) { |
| 32 | uint8_t storage[8]; |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 33 | SkWriter32 writer(storage, sizeof(storage)); |
commit-bot@chromium.org | 47fa136 | 2013-08-09 16:03:05 +0000 | [diff] [blame] | 34 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | // Can we write nullptr? |
| 36 | writer.writeString(nullptr); |
mtklein@google.com | 0038c12 | 2013-08-15 21:01:32 +0000 | [diff] [blame] | 37 | const int32_t expected[] = { 0x0, 0x0 }; |
| 38 | check_contents(reporter, writer, expected, sizeof(expected)); |
commit-bot@chromium.org | 47fa136 | 2013-08-09 16:03:05 +0000 | [diff] [blame] | 39 | } |
| 40 | |
reed@google.com | 1cf58d0 | 2012-08-30 14:03:21 +0000 | [diff] [blame] | 41 | static void test_rewind(skiatest::Reporter* reporter) { |
John Stiles | fa088a6 | 2021-08-12 23:01:41 -0400 | [diff] [blame] | 42 | SkSWriter32<32> swriter; |
reed@google.com | 1cf58d0 | 2012-08-30 14:03:21 +0000 | [diff] [blame] | 43 | int32_t array[3] = { 1, 2, 4 }; |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 44 | |
John Stiles | fa088a6 | 2021-08-12 23:01:41 -0400 | [diff] [blame] | 45 | REPORTER_ASSERT(reporter, 0 == swriter.bytesWritten()); |
reed@google.com | 1cf58d0 | 2012-08-30 14:03:21 +0000 | [diff] [blame] | 46 | for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) { |
John Stiles | fa088a6 | 2021-08-12 23:01:41 -0400 | [diff] [blame] | 47 | swriter.writeInt(array[i]); |
reed@google.com | 1cf58d0 | 2012-08-30 14:03:21 +0000 | [diff] [blame] | 48 | } |
John Stiles | fa088a6 | 2021-08-12 23:01:41 -0400 | [diff] [blame] | 49 | check_contents(reporter, swriter, array, sizeof(array)); |
reed@google.com | 1cf58d0 | 2012-08-30 14:03:21 +0000 | [diff] [blame] | 50 | |
John Stiles | fa088a6 | 2021-08-12 23:01:41 -0400 | [diff] [blame] | 51 | swriter.rewindToOffset(2*sizeof(int32_t)); |
| 52 | REPORTER_ASSERT(reporter, sizeof(array) - 4 == swriter.bytesWritten()); |
| 53 | swriter.writeInt(3); |
| 54 | REPORTER_ASSERT(reporter, sizeof(array) == swriter.bytesWritten()); |
reed@google.com | 1cf58d0 | 2012-08-30 14:03:21 +0000 | [diff] [blame] | 55 | array[2] = 3; |
John Stiles | fa088a6 | 2021-08-12 23:01:41 -0400 | [diff] [blame] | 56 | check_contents(reporter, swriter, array, sizeof(array)); |
reed@google.com | d7e2782 | 2012-08-31 20:17:56 +0000 | [diff] [blame] | 57 | |
| 58 | // test rewinding past allocated chunks. This used to crash because we |
| 59 | // didn't truncate our link-list after freeing trailing blocks |
John Stiles | fa088a6 | 2021-08-12 23:01:41 -0400 | [diff] [blame] | 60 | SkWriter32 writer; |
| 61 | for (int i = 0; i < 100; ++i) { |
| 62 | writer.writeInt(i); |
reed@google.com | d7e2782 | 2012-08-31 20:17:56 +0000 | [diff] [blame] | 63 | } |
John Stiles | fa088a6 | 2021-08-12 23:01:41 -0400 | [diff] [blame] | 64 | REPORTER_ASSERT(reporter, 100*4 == writer.bytesWritten()); |
| 65 | for (int j = 100*4; j >= 0; j -= 16) { |
| 66 | writer.rewindToOffset(j); |
| 67 | } |
| 68 | REPORTER_ASSERT(reporter, writer.bytesWritten() < 16); |
reed@google.com | 1cf58d0 | 2012-08-30 14:03:21 +0000 | [diff] [blame] | 69 | } |
| 70 | |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 71 | static void test1(skiatest::Reporter* reporter, SkWriter32* writer) { |
| 72 | const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 73 | for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 74 | REPORTER_ASSERT(reporter, i*4 == writer->bytesWritten()); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 75 | writer->write32(data[i]); |
commit-bot@chromium.org | 536ac5e | 2014-02-11 22:38:51 +0000 | [diff] [blame] | 76 | REPORTER_ASSERT(reporter, data[i] == writer->readTAt<uint32_t>(i * 4)); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | char buffer[sizeof(data)]; |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 80 | REPORTER_ASSERT(reporter, sizeof(buffer) == writer->bytesWritten()); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 81 | writer->flatten(buffer); |
| 82 | REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer))); |
| 83 | } |
| 84 | |
scroggo@google.com | dd39488 | 2012-07-24 20:47:55 +0000 | [diff] [blame] | 85 | static void testWritePad(skiatest::Reporter* reporter, SkWriter32* writer) { |
| 86 | // Create some random data to write. |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 87 | const size_t dataSize = 10; |
scroggo@google.com | dd39488 | 2012-07-24 20:47:55 +0000 | [diff] [blame] | 88 | |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 89 | SkAutoTMalloc<uint32_t> originalData(dataSize); |
scroggo@google.com | dd39488 | 2012-07-24 20:47:55 +0000 | [diff] [blame] | 90 | { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 91 | SkRandom rand(0); |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 92 | for (size_t i = 0; i < dataSize; i++) { |
| 93 | originalData[(int) i] = rand.nextU(); |
scroggo@google.com | dd39488 | 2012-07-24 20:47:55 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Write the random data to the writer at different lengths for |
| 97 | // different alignments. |
| 98 | for (size_t len = 0; len < dataSize; len++) { |
| 99 | writer->writePad(originalData.get(), len); |
| 100 | } |
| 101 | } |
| 102 | |
bsalomon | fbaace0 | 2014-12-12 16:41:46 -0800 | [diff] [blame] | 103 | size_t totalBytes = writer->bytesWritten(); |
scroggo@google.com | dd39488 | 2012-07-24 20:47:55 +0000 | [diff] [blame] | 104 | |
| 105 | SkAutoMalloc readStorage(totalBytes); |
| 106 | writer->flatten(readStorage.get()); |
| 107 | |
Brian Osman | ff7bee9 | 2020-06-09 16:04:18 -0400 | [diff] [blame] | 108 | SkReadBuffer reader(readStorage.get(), totalBytes); |
scroggo@google.com | dd39488 | 2012-07-24 20:47:55 +0000 | [diff] [blame] | 109 | |
| 110 | for (size_t len = 0; len < dataSize; len++) { |
| 111 | const char* readPtr = static_cast<const char*>(reader.skip(len)); |
| 112 | // Ensure that the data read is the same as what was written. |
| 113 | REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0); |
| 114 | // Ensure that the rest is padded with zeroes. |
| 115 | const char* stop = readPtr + SkAlign4(len); |
| 116 | readPtr += len; |
| 117 | while (readPtr < stop) { |
| 118 | REPORTER_ASSERT(reporter, *readPtr++ == 0); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
commit-bot@chromium.org | 536ac5e | 2014-02-11 22:38:51 +0000 | [diff] [blame] | 123 | static void testOverwriteT(skiatest::Reporter* reporter, SkWriter32* writer) { |
| 124 | const size_t padding = 64; |
| 125 | |
| 126 | const uint32_t uint1 = 0x12345678; |
| 127 | const uint32_t uint2 = 0x98765432; |
| 128 | const SkScalar scalar1 = 1234.5678f; |
| 129 | const SkScalar scalar2 = 9876.5432f; |
| 130 | const SkRect rect1 = SkRect::MakeXYWH(1, 2, 3, 4); |
| 131 | const SkRect rect2 = SkRect::MakeXYWH(5, 6, 7, 8); |
| 132 | |
| 133 | for (size_t i = 0; i < (padding / 4); ++i) { |
| 134 | writer->write32(0); |
| 135 | } |
| 136 | |
| 137 | writer->write32(uint1); |
| 138 | writer->writeRect(rect1); |
| 139 | writer->writeScalar(scalar1); |
| 140 | |
| 141 | for (size_t i = 0; i < (padding / 4); ++i) { |
| 142 | writer->write32(0); |
| 143 | } |
| 144 | |
| 145 | REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint1); |
| 146 | REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)) == rect1); |
| 147 | REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>( |
| 148 | padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar1); |
| 149 | |
| 150 | writer->overwriteTAt(padding, uint2); |
| 151 | writer->overwriteTAt(padding + sizeof(uint32_t), rect2); |
| 152 | writer->overwriteTAt(padding + sizeof(uint32_t) + sizeof(SkRect), scalar2); |
| 153 | |
| 154 | REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint2); |
| 155 | REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)) == rect2); |
| 156 | REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>( |
| 157 | padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar2); |
| 158 | } |
| 159 | |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 160 | DEF_TEST(Writer32_dynamic, reporter) { |
| 161 | SkWriter32 writer; |
| 162 | test1(reporter, &writer); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 163 | |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 164 | writer.reset(); |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 165 | testWritePad(reporter, &writer); |
commit-bot@chromium.org | 536ac5e | 2014-02-11 22:38:51 +0000 | [diff] [blame] | 166 | |
| 167 | writer.reset(); |
| 168 | testOverwriteT(reporter, &writer); |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 169 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 170 | |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 171 | DEF_TEST(Writer32_small, reporter) { |
| 172 | SkSWriter32<8 * sizeof(intptr_t)> writer; |
| 173 | test1(reporter, &writer); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 174 | |
Brian Osman | ff7bee9 | 2020-06-09 16:04:18 -0400 | [diff] [blame] | 175 | writer.reset(); // should just rewind our storage |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 176 | testWritePad(reporter, &writer); |
commit-bot@chromium.org | 536ac5e | 2014-02-11 22:38:51 +0000 | [diff] [blame] | 177 | |
| 178 | writer.reset(); |
| 179 | testOverwriteT(reporter, &writer); |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 180 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 181 | |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 182 | DEF_TEST(Writer32_large, reporter) { |
| 183 | SkSWriter32<1024 * sizeof(intptr_t)> writer; |
| 184 | test1(reporter, &writer); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 185 | |
Brian Osman | ff7bee9 | 2020-06-09 16:04:18 -0400 | [diff] [blame] | 186 | writer.reset(); // should just rewind our storage |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 187 | testWritePad(reporter, &writer); |
commit-bot@chromium.org | 536ac5e | 2014-02-11 22:38:51 +0000 | [diff] [blame] | 188 | |
| 189 | writer.reset(); |
| 190 | testOverwriteT(reporter, &writer); |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 191 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 192 | |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 193 | DEF_TEST(Writer32_misc, reporter) { |
mtklein@google.com | 8b4ba63 | 2013-09-03 18:07:01 +0000 | [diff] [blame] | 194 | test_reserve(reporter); |
commit-bot@chromium.org | 47fa136 | 2013-08-09 16:03:05 +0000 | [diff] [blame] | 195 | test_string_null(reporter); |
reed@google.com | 1cf58d0 | 2012-08-30 14:03:21 +0000 | [diff] [blame] | 196 | test_rewind(reporter); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 197 | } |
commit-bot@chromium.org | c30dcb9 | 2014-03-12 17:04:28 +0000 | [diff] [blame] | 198 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 199 | DEF_TEST(Writer32_data, reporter) { |
| 200 | const char* str = "0123456789"; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 201 | sk_sp<SkData> data0(SkData::MakeWithCString(str)); |
| 202 | sk_sp<SkData> data1(SkData::MakeEmpty()); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 203 | |
| 204 | const size_t sizes[] = { |
| 205 | SkWriter32::WriteDataSize(nullptr), |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 206 | SkWriter32::WriteDataSize(data0.get()), |
| 207 | SkWriter32::WriteDataSize(data1.get()), |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 208 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 209 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 210 | SkSWriter32<1000> writer; |
| 211 | size_t sizeWritten = 0; |
| 212 | |
| 213 | writer.writeData(nullptr); |
| 214 | sizeWritten += sizes[0]; |
| 215 | REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 216 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 217 | writer.writeData(data0.get()); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 218 | sizeWritten += sizes[1]; |
| 219 | REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten()); |
| 220 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 221 | writer.writeData(data1.get()); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 222 | sizeWritten += sizes[2]; |
| 223 | REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten()); |
| 224 | |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 225 | auto result(writer.snapshotAsData()); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 226 | |
Brian Osman | ff7bee9 | 2020-06-09 16:04:18 -0400 | [diff] [blame] | 227 | SkReadBuffer reader(result->data(), result->size()); |
| 228 | auto d0(reader.readByteArrayAsData()), |
| 229 | d1(reader.readByteArrayAsData()), |
| 230 | d2(reader.readByteArrayAsData()); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 231 | |
| 232 | REPORTER_ASSERT(reporter, 0 == d0->size()); |
| 233 | REPORTER_ASSERT(reporter, strlen(str)+1 == d1->size()); |
| 234 | REPORTER_ASSERT(reporter, !memcmp(str, d1->data(), strlen(str)+1)); |
| 235 | REPORTER_ASSERT(reporter, 0 == d2->size()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 236 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 237 | REPORTER_ASSERT(reporter, reader.offset() == sizeWritten); |
| 238 | REPORTER_ASSERT(reporter, reader.eof()); |
| 239 | } |