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 static void stream_copy_test(skiatest::Reporter* reporter, | |
363 const void* srcData, | |
364 size_t N, | |
365 SkStream* stream) { | |
366 SkDynamicMemoryWStream tgt; | |
367 if (!SkStreamCopy(&tgt, stream)) { | |
368 ERRORF(reporter, "SkStreamCopy failed"); | |
369 return; | |
370 } | |
371 SkAutoTUnref<SkData> data(tgt.copyToData()); | |
372 tgt.reset(); | |
373 if (data->size() != N) { | |
374 ERRORF(reporter, "SkStreamCopy incorrect size"); | |
375 return; | |
376 } | |
377 if (0 != memcmp(data->data(), srcData, N)) { | |
378 ERRORF(reporter, "SkStreamCopy bad copy"); | |
379 } | |
380 } | |
381 | |
382 DEF_TEST(StreamCopy, reporter) { | |
383 SkRandom random(123456); | |
384 static const size_t N = 10000; | |
385 uint8_t src[N]; | |
386 for (size_t j = 0; j < N; ++j) { | |
387 src[j] = random.nextU() & 0xff; | |
388 } | |
389 // SkStreamCopy had two code paths; this test both. | |
hal.canary
2015/08/18 20:19:52
I also expanded this test.
| |
390 DumbStream dumbStream(src, N); | |
391 stream_copy_test(reporter, src, N, &dumbStream); | |
392 SkMemoryStream smartStream(src, N); | |
393 stream_copy_test(reporter, src, N, &smartStream); | |
394 | |
395 } | |
OLD | NEW |