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" |
11 #include "SkOSFile.h" | 11 #include "SkOSFile.h" |
12 #include "SkRandom.h" | 12 #include "SkRandom.h" |
13 #include "SkStream.h" | 13 #include "SkStream.h" |
| 14 #include "SkStreamPriv.h" |
14 #include "Test.h" | 15 #include "Test.h" |
15 | 16 |
16 #ifndef SK_BUILD_FOR_WIN | 17 #ifndef SK_BUILD_FOR_WIN |
17 #include <unistd.h> | 18 #include <unistd.h> |
18 #include <fcntl.h> | 19 #include <fcntl.h> |
19 #endif | 20 #endif |
20 | 21 |
21 #define MAX_SIZE (256 * 1024) | 22 #define MAX_SIZE (256 * 1024) |
22 | 23 |
23 static void test_loop_stream(skiatest::Reporter* reporter, SkStream* stream, | 24 static void test_loop_stream(skiatest::Reporter* reporter, SkStream* stream, |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 uint8_t* expectedPtr = static_cast<uint8_t*>(expected->writable_data()); | 329 uint8_t* expectedPtr = static_cast<uint8_t*>(expected->writable_data()); |
329 valueSource.setSeed(kSeed); // reseed. | 330 valueSource.setSeed(kSeed); // reseed. |
330 // We want the exact same same "random" string of numbers to put | 331 // 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 // in expected. i.e.: don't rely on SkDynamicMemoryStream to work |
332 // correctly while we are testing SkDynamicMemoryStream. | 333 // correctly while we are testing SkDynamicMemoryStream. |
333 for (size_t i = 0; i < asset->getLength(); ++i) { | 334 for (size_t i = 0; i < asset->getLength(); ++i) { |
334 expectedPtr[i] = valueSource.nextU() & 0xFF; | 335 expectedPtr[i] = valueSource.nextU() & 0xFF; |
335 } | 336 } |
336 stream_peek_test(rep, asset, expected); | 337 stream_peek_test(rep, asset, expected); |
337 } | 338 } |
| 339 |
| 340 namespace { |
| 341 class DumbStream : public SkStream { |
| 342 public: |
| 343 DumbStream(const uint8_t* data, size_t n) |
| 344 : fData(data), fCount(n), fIdx(0) {} |
| 345 size_t read(void* buffer, size_t size) override { |
| 346 size_t c = SkTMin(fCount - fIdx, size); |
| 347 if (c) { |
| 348 memcpy(buffer, &fData[fIdx], size); |
| 349 fIdx += c; |
| 350 } |
| 351 return c; |
| 352 } |
| 353 bool isAtEnd() const override { |
| 354 return fCount > fIdx; |
| 355 } |
| 356 private: |
| 357 const uint8_t* fData; |
| 358 size_t fCount, fIdx; |
| 359 }; |
| 360 } // namespace |
| 361 |
| 362 DEF_TEST(StreamCopy, reporter) { |
| 363 SkRandom random(123456); |
| 364 static const size_t N = 10000; |
| 365 uint8_t src[N]; |
| 366 for (size_t j = 0; j < N; ++j) { |
| 367 src[j] = random.nextU() & 0xff; |
| 368 } |
| 369 DumbStream dumbStream(src, N); |
| 370 SkDynamicMemoryWStream tgt; |
| 371 if (!SkStreamCopy(&tgt, &dumbStream)) { |
| 372 ERRORF(reporter, "SkStreamCopy failed"); |
| 373 return; |
| 374 } |
| 375 SkAutoTUnref<SkData> data(tgt.copyToData()); |
| 376 tgt.reset(); |
| 377 if (data->size() != N) { |
| 378 ERRORF(reporter, "SkStreamCopy incorrect size"); |
| 379 return; |
| 380 } |
| 381 if (0 != memcmp(data->data(), src, N)) { |
| 382 ERRORF(reporter, "SkStreamCopy bad copy"); |
| 383 } |
| 384 } |
OLD | NEW |