| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkRandom.h" | 8 #include "SkRandom.h" |
| 9 #include "SkReader32.h" | 9 #include "SkReader32.h" |
| 10 #include "SkWriter32.h" | 10 #include "SkWriter32.h" |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 REPORTER_ASSERT(reporter, reader.readInt() == 0x33); | 95 REPORTER_ASSERT(reporter, reader.readInt() == 0x33); |
| 96 REPORTER_ASSERT(reporter, reader.readPtr() == p1); | 96 REPORTER_ASSERT(reporter, reader.readPtr() == p1); |
| 97 REPORTER_ASSERT(reporter, reader.readInt() == 0x66); | 97 REPORTER_ASSERT(reporter, reader.readInt() == 0x66); |
| 98 } | 98 } |
| 99 | 99 |
| 100 static void test1(skiatest::Reporter* reporter, SkWriter32* writer) { | 100 static void test1(skiatest::Reporter* reporter, SkWriter32* writer) { |
| 101 const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; | 101 const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 102 for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) { | 102 for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) { |
| 103 REPORTER_ASSERT(reporter, i*4 == writer->bytesWritten()); | 103 REPORTER_ASSERT(reporter, i*4 == writer->bytesWritten()); |
| 104 writer->write32(data[i]); | 104 writer->write32(data[i]); |
| 105 REPORTER_ASSERT(reporter, data[i] == writer->read32At(i*4)); | 105 REPORTER_ASSERT(reporter, data[i] == writer->readTAt<uint32_t>(i * 4)); |
| 106 } | 106 } |
| 107 | 107 |
| 108 char buffer[sizeof(data)]; | 108 char buffer[sizeof(data)]; |
| 109 REPORTER_ASSERT(reporter, sizeof(buffer) == writer->bytesWritten()); | 109 REPORTER_ASSERT(reporter, sizeof(buffer) == writer->bytesWritten()); |
| 110 writer->flatten(buffer); | 110 writer->flatten(buffer); |
| 111 REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer))); | 111 REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer))); |
| 112 } | 112 } |
| 113 | 113 |
| 114 static void test2(skiatest::Reporter* reporter, SkWriter32* writer) { | 114 static void test2(skiatest::Reporter* reporter, SkWriter32* writer) { |
| 115 static const char gStr[] = "abcdefghimjklmnopqrstuvwxyz"; | 115 static const char gStr[] = "abcdefghimjklmnopqrstuvwxyz"; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0)
; | 177 REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0)
; |
| 178 // Ensure that the rest is padded with zeroes. | 178 // Ensure that the rest is padded with zeroes. |
| 179 const char* stop = readPtr + SkAlign4(len); | 179 const char* stop = readPtr + SkAlign4(len); |
| 180 readPtr += len; | 180 readPtr += len; |
| 181 while (readPtr < stop) { | 181 while (readPtr < stop) { |
| 182 REPORTER_ASSERT(reporter, *readPtr++ == 0); | 182 REPORTER_ASSERT(reporter, *readPtr++ == 0); |
| 183 } | 183 } |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 | 186 |
| 187 static void testOverwriteT(skiatest::Reporter* reporter, SkWriter32* writer) { |
| 188 const size_t padding = 64; |
| 189 |
| 190 const uint32_t uint1 = 0x12345678; |
| 191 const uint32_t uint2 = 0x98765432; |
| 192 const SkScalar scalar1 = 1234.5678f; |
| 193 const SkScalar scalar2 = 9876.5432f; |
| 194 const SkRect rect1 = SkRect::MakeXYWH(1, 2, 3, 4); |
| 195 const SkRect rect2 = SkRect::MakeXYWH(5, 6, 7, 8); |
| 196 |
| 197 for (size_t i = 0; i < (padding >> 2); ++i) { |
| 198 writer->write32(0); |
| 199 } |
| 200 |
| 201 writer->write32(uint1); |
| 202 writer->writeRect(rect1); |
| 203 writer->writeScalar(scalar1); |
| 204 |
| 205 for (size_t i = 0; i < (padding >> 2); ++i) { |
| 206 writer->write32(0); |
| 207 } |
| 208 |
| 209 REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint1); |
| 210 REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)
) == rect1); |
| 211 REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>( |
| 212 padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar1)
; |
| 213 |
| 214 writer->overwriteTAt(uint2, padding); |
| 215 writer->overwriteTAt(rect2, padding + sizeof(uint32_t)); |
| 216 writer->overwriteTAt(scalar2, padding + sizeof(uint32_t) + sizeof(SkRect)); |
| 217 |
| 218 REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint2); |
| 219 REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)
) == rect2); |
| 220 REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>( |
| 221 padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar2)
; |
| 222 } |
| 223 |
| 187 DEF_TEST(Writer32_dynamic, reporter) { | 224 DEF_TEST(Writer32_dynamic, reporter) { |
| 188 SkWriter32 writer; | 225 SkWriter32 writer; |
| 189 test1(reporter, &writer); | 226 test1(reporter, &writer); |
| 190 | 227 |
| 191 writer.reset(); | 228 writer.reset(); |
| 192 test2(reporter, &writer); | 229 test2(reporter, &writer); |
| 193 | 230 |
| 194 writer.reset(); | 231 writer.reset(); |
| 195 testWritePad(reporter, &writer); | 232 testWritePad(reporter, &writer); |
| 233 |
| 234 writer.reset(); |
| 235 testOverwriteT(reporter, &writer); |
| 196 } | 236 } |
| 197 | 237 |
| 198 DEF_TEST(Writer32_contiguous, reporter) { | 238 DEF_TEST(Writer32_contiguous, reporter) { |
| 199 uint32_t storage[256]; | 239 uint32_t storage[256]; |
| 200 SkWriter32 writer; | 240 SkWriter32 writer; |
| 201 writer.reset(storage, sizeof(storage)); | 241 writer.reset(storage, sizeof(storage)); |
| 202 // This write is small enough to fit in storage, so it's contiguous. | 242 // This write is small enough to fit in storage, so it's contiguous. |
| 203 test1(reporter, &writer); | 243 test1(reporter, &writer); |
| 204 REPORTER_ASSERT(reporter, writer.contiguousArray() != NULL); | 244 REPORTER_ASSERT(reporter, writer.contiguousArray() != NULL); |
| 205 | 245 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 217 } | 257 } |
| 218 | 258 |
| 219 DEF_TEST(Writer32_small, reporter) { | 259 DEF_TEST(Writer32_small, reporter) { |
| 220 SkSWriter32<8 * sizeof(intptr_t)> writer; | 260 SkSWriter32<8 * sizeof(intptr_t)> writer; |
| 221 test1(reporter, &writer); | 261 test1(reporter, &writer); |
| 222 writer.reset(); // should just rewind our storage | 262 writer.reset(); // should just rewind our storage |
| 223 test2(reporter, &writer); | 263 test2(reporter, &writer); |
| 224 | 264 |
| 225 writer.reset(); | 265 writer.reset(); |
| 226 testWritePad(reporter, &writer); | 266 testWritePad(reporter, &writer); |
| 267 |
| 268 writer.reset(); |
| 269 testOverwriteT(reporter, &writer); |
| 227 } | 270 } |
| 228 | 271 |
| 229 DEF_TEST(Writer32_large, reporter) { | 272 DEF_TEST(Writer32_large, reporter) { |
| 230 SkSWriter32<1024 * sizeof(intptr_t)> writer; | 273 SkSWriter32<1024 * sizeof(intptr_t)> writer; |
| 231 test1(reporter, &writer); | 274 test1(reporter, &writer); |
| 232 writer.reset(); // should just rewind our storage | 275 writer.reset(); // should just rewind our storage |
| 233 test2(reporter, &writer); | 276 test2(reporter, &writer); |
| 234 | 277 |
| 235 writer.reset(); | 278 writer.reset(); |
| 236 testWritePad(reporter, &writer); | 279 testWritePad(reporter, &writer); |
| 280 |
| 281 writer.reset(); |
| 282 testOverwriteT(reporter, &writer); |
| 237 } | 283 } |
| 238 | 284 |
| 239 DEF_TEST(Writer32_misc, reporter) { | 285 DEF_TEST(Writer32_misc, reporter) { |
| 240 test_reserve(reporter); | 286 test_reserve(reporter); |
| 241 test_string_null(reporter); | 287 test_string_null(reporter); |
| 242 test_ptr(reporter); | 288 test_ptr(reporter); |
| 243 test_rewind(reporter); | 289 test_rewind(reporter); |
| 244 } | 290 } |
| OLD | NEW |