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 "Resources.h" | 8 #include "Resources.h" |
9 #include "SkData.h" | 9 #include "SkData.h" |
10 #include "SkFrontBufferedStream.h" | 10 #include "SkFrontBufferedStream.h" |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 SkAutoMalloc storage(fileStream.getLength()); | 265 SkAutoMalloc storage(fileStream.getLength()); |
266 for (size_t i = 1; i < fileStream.getLength(); i++) { | 266 for (size_t i = 1; i < fileStream.getLength(); i++) { |
267 REPORTER_ASSERT(reporter, !fileStream.peek(storage.get(), i)); | 267 REPORTER_ASSERT(reporter, !fileStream.peek(storage.get(), i)); |
268 } | 268 } |
269 | 269 |
270 // Now test some FrontBufferedStreams | 270 // Now test some FrontBufferedStreams |
271 for (size_t i = 1; i < memStream.getLength(); i++) { | 271 for (size_t i = 1; i < memStream.getLength(); i++) { |
272 test_peeking_front_buffered_stream(reporter, memStream, i); | 272 test_peeking_front_buffered_stream(reporter, memStream, i); |
273 } | 273 } |
274 } | 274 } |
275 #endif | 275 #endif |
| 276 |
| 277 // Asserts that asset == expected and is peekable. |
| 278 static void stream_peek_test(skiatest::Reporter* rep, |
| 279 SkStreamAsset* asset, |
| 280 const SkData* expected) { |
| 281 if (asset->getLength() != expected->size()) { |
| 282 ERRORF(rep, "Unexpected length."); |
| 283 return; |
| 284 } |
| 285 SkRandom rand; |
| 286 uint8_t buffer[4096]; |
| 287 const uint8_t* expect = expected->bytes(); |
| 288 for (size_t i = 0; i < asset->getLength(); ++i) { |
| 289 uint32_t maxSize = |
| 290 SkToU32(SkTMin(sizeof(buffer), asset->getLength() - i)); |
| 291 size_t size = rand.nextRangeU(1, maxSize); |
| 292 SkASSERT(size >= 1); |
| 293 SkASSERT(size <= sizeof(buffer)); |
| 294 SkASSERT(size + i <= asset->getLength()); |
| 295 if (!asset->peek(buffer, size)) { |
| 296 ERRORF(rep, "Peek Failed!"); |
| 297 return; |
| 298 } |
| 299 if (0 != memcmp(buffer, &expect[i], size)) { |
| 300 ERRORF(rep, "Peek returned wrong bytes!"); |
| 301 return; |
| 302 } |
| 303 uint8_t value; |
| 304 REPORTER_ASSERT(rep, 1 == asset->read(&value, 1)); |
| 305 if (value != expect[i]) { |
| 306 ERRORF(rep, "Read Failed!"); |
| 307 return; |
| 308 } |
| 309 } |
| 310 } |
| 311 |
| 312 DEF_TEST(StreamPeek_BlockMemoryStream, rep) { |
| 313 const static int kSeed = 1234; |
| 314 SkRandom valueSource(kSeed); |
| 315 SkRandom rand(kSeed << 1); |
| 316 uint8_t buffer[4096]; |
| 317 SkDynamicMemoryWStream dynamicMemoryWStream; |
| 318 for (int i = 0; i < 32; ++i) { |
| 319 // Randomize the length of the blocks. |
| 320 size_t size = rand.nextRangeU(1, sizeof(buffer)); |
| 321 for (size_t j = 0; j < size; ++j) { |
| 322 buffer[j] = valueSource.nextU() & 0xFF; |
| 323 } |
| 324 dynamicMemoryWStream.write(buffer, size); |
| 325 } |
| 326 SkAutoTDelete<SkStreamAsset> asset(dynamicMemoryWStream.detachAsStream()); |
| 327 SkAutoTUnref<SkData> expected(SkData::NewUninitialized(asset->getLength())); |
| 328 uint8_t* expectedPtr = static_cast<uint8_t*>(expected->writable_data()); |
| 329 valueSource.setSeed(kSeed); // reseed. |
| 330 // We want the exact same same "random" string of numbers to put |
| 331 // in expected. i.e.: don't rely on SkDynamicMemoryStream to work |
| 332 // correctly while we are testing SkDynamicMemoryStream. |
| 333 for (size_t i = 0; i < asset->getLength(); ++i) { |
| 334 expectedPtr[i] = valueSource.nextU() & 0xFF; |
| 335 } |
| 336 stream_peek_test(rep, asset, expected); |
| 337 } |
OLD | NEW |