Flutter Engine
The Flutter Engine
Macros | Functions
StreamTest.cpp File Reference
#include "include/core/SkData.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkStream.h"
#include "include/core/SkString.h"
#include "include/core/SkTypes.h"
#include "include/private/base/SkTemplates.h"
#include "include/private/base/SkTo.h"
#include "src/base/SkAutoMalloc.h"
#include "src/base/SkBuffer.h"
#include "src/base/SkRandom.h"
#include "src/core/SkOSFile.h"
#include "src/core/SkStreamPriv.h"
#include "src/utils/SkOSPath.h"
#include "tests/Test.h"
#include "tools/Resources.h"
#include <algorithm>
#include <climits>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <functional>
#include <limits>
#include <memory>
#include <string>
#include <fcntl.h>

Go to the source code of this file.

Macros

#define MAX_SIZE   (256 * 1024)
 

Functions

static void test_loop_stream (skiatest::Reporter *reporter, SkStream *stream, const void *src, size_t len, int repeat)
 
static void test_filestreams (skiatest::Reporter *reporter, const char *tmpDir)
 
static void TestWStream (skiatest::Reporter *reporter)
 
static void TestPackedUInt (skiatest::Reporter *reporter)
 
static void TestDereferencingData (skiatest::Reporter *reporter, SkMemoryStream *memStream)
 
static void TestNullData (skiatest::Reporter *reporter)
 
 DEF_TEST (Stream, reporter)
 
static size_t compare_peek_to_read (skiatest::Reporter *reporter, SkStream *stream, size_t bytesToPeek)
 
static void test_fully_peekable_stream (skiatest::Reporter *r, SkStream *stream, size_t limit)
 
 DEF_TEST (StreamPeek, reporter)
 
static void stream_peek_test (skiatest::Reporter *rep, SkStreamAsset *asset, const SkData *expected)
 
 DEF_TEST (StreamPeek_BlockMemoryStream, rep)
 
 DEF_TEST (StreamRemainingLengthIsBelow_MemoryStream, rep)
 
static void stream_copy_test (skiatest::Reporter *reporter, const void *srcData, size_t N, SkStream *stream)
 
 DEF_TEST (DynamicMemoryWStream_detachAsData, r)
 
 DEF_TEST (StreamCopy, reporter)
 
 DEF_TEST (StreamEmptyStreamMemoryBase, r)
 
 DEF_TEST (FILEStreamWithOffset, r)
 
 DEF_TEST (RBuffer, reporter)
 

Macro Definition Documentation

◆ MAX_SIZE

#define MAX_SIZE   (256 * 1024)

Definition at line 44 of file StreamTest.cpp.

Function Documentation

◆ compare_peek_to_read()

static size_t compare_peek_to_read ( skiatest::Reporter reporter,
SkStream stream,
size_t  bytesToPeek 
)
static

Tests peeking and then reading the same amount. The two should provide the same results. Returns the amount successfully read minus the amount successfully peeked.

Definition at line 221 of file StreamTest.cpp.

222 {
223 // The rest of our tests won't be very interesting if bytesToPeek is zero.
224 REPORTER_ASSERT(reporter, bytesToPeek > 0);
225 SkAutoMalloc peekStorage(bytesToPeek);
226 SkAutoMalloc readStorage(bytesToPeek);
227 void* peekPtr = peekStorage.get();
228 void* readPtr = peekStorage.get();
229
230 const size_t bytesPeeked = stream->peek(peekPtr, bytesToPeek);
231 const size_t bytesRead = stream->read(readPtr, bytesToPeek);
232
233 // bytesRead should only be less than attempted if the stream is at the
234 // end.
235 REPORTER_ASSERT(reporter, bytesRead == bytesToPeek || stream->isAtEnd());
236
237 // peek and read should behave the same, except peek returned to the
238 // original position, so they read the same data.
239 REPORTER_ASSERT(reporter, !memcmp(peekPtr, readPtr, bytesPeeked));
240
241 // A stream should never be able to peek more than it can read.
242 REPORTER_ASSERT(reporter, bytesRead >= bytesPeeked);
243
244 return bytesRead - bytesPeeked;
245}
reporter
Definition: FontMgrTest.cpp:39
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286

◆ DEF_TEST() [1/9]

DEF_TEST ( DynamicMemoryWStream_detachAsData  ,
 
)

Definition at line 484 of file StreamTest.cpp.

484 {
485 const char az[] = "abcdefghijklmnopqrstuvwxyz";
486 const unsigned N = 40000;
488 for (unsigned i = 0; i < N; ++i) {
489 dmws.writeText(az);
490 }
491 REPORTER_ASSERT(r, dmws.bytesWritten() == N * strlen(az));
492 auto data = dmws.detachAsData();
493 REPORTER_ASSERT(r, data->size() == N * strlen(az));
494 const uint8_t* ptr = data->bytes();
495 for (unsigned i = 0; i < N; ++i) {
496 if (0 != memcmp(ptr, az, strlen(az))) {
497 ERRORF(r, "detachAsData() memcmp failed");
498 return;
499 }
500 ptr += strlen(az);
501 }
502}
#define ERRORF(r,...)
Definition: Test.h:293
#define N
Definition: beziers.cpp:19
size_t bytesWritten() const override
Definition: SkStream.cpp:526
sk_sp< SkData > detachAsData()
Definition: SkStream.cpp:707
bool writeText(const char text[])
Definition: SkStream.h:247
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63

◆ DEF_TEST() [2/9]

DEF_TEST ( FILEStreamWithOffset  ,
 
)

Definition at line 524 of file StreamTest.cpp.

524 {
525 if (GetResourcePath().isEmpty()) {
526 return;
527 }
528
529 SkString filename = GetResourcePath("images/baby_tux.png");
530 SkFILEStream stream1(filename.c_str());
531 if (!stream1.isValid()) {
532 ERRORF(r, "Could not create SkFILEStream from %s", filename.c_str());
533 return;
534 }
535 REPORTER_ASSERT(r, stream1.hasLength());
536 REPORTER_ASSERT(r, stream1.hasPosition());
537
538 // Seek halfway through the file. The second SkFILEStream will be created
539 // with the same filename and offset and therefore will treat that offset as
540 // the beginning.
541 const size_t size = stream1.getLength();
542 const size_t middle = size / 2;
543 if (!stream1.seek(middle)) {
544 ERRORF(r, "Could not seek SkFILEStream to %zu out of %zu", middle, size);
545 return;
546 }
547 REPORTER_ASSERT(r, stream1.getPosition() == middle);
548
549 FILE* file = sk_fopen(filename.c_str(), kRead_SkFILE_Flag);
550 if (!file) {
551 ERRORF(r, "Could not open %s as a FILE", filename.c_str());
552 return;
553 }
554
555 if (fseek(file, (long) middle, SEEK_SET) != 0) {
556 ERRORF(r, "Could not fseek FILE to %zu out of %zu", middle, size);
557 return;
558 }
559 SkFILEStream stream2(file);
560
561 const size_t remaining = size - middle;
562 AutoTMalloc<uint8_t> expected(remaining);
563 REPORTER_ASSERT(r, stream1.read(expected.get(), remaining) == remaining);
564
565 auto test_full_read = [&r, &expected, remaining](SkStream* stream) {
566 AutoTMalloc<uint8_t> actual(remaining);
567 REPORTER_ASSERT(r, stream->read(actual.get(), remaining) == remaining);
568 REPORTER_ASSERT(r, !memcmp(expected.get(), actual.get(), remaining));
569
570 REPORTER_ASSERT(r, stream->getPosition() == stream->getLength());
571 REPORTER_ASSERT(r, stream->isAtEnd());
572 };
573
574 auto test_rewind = [&r, &expected, remaining](SkStream* stream) {
575 // Rewind goes back to original offset.
576 REPORTER_ASSERT(r, stream->rewind());
577 REPORTER_ASSERT(r, stream->getPosition() == 0);
578 AutoTMalloc<uint8_t> actual(remaining);
579 REPORTER_ASSERT(r, stream->read(actual.get(), remaining) == remaining);
580 REPORTER_ASSERT(r, !memcmp(expected.get(), actual.get(), remaining));
581 };
582
583 auto test_move = [&r, &expected, size, remaining](SkStream* stream) {
584 // Cannot move to before the original offset.
585 REPORTER_ASSERT(r, stream->move(- (long) size));
586 REPORTER_ASSERT(r, stream->getPosition() == 0);
587
589 REPORTER_ASSERT(r, stream->getPosition() == 0);
590
591 AutoTMalloc<uint8_t> actual(remaining);
592 REPORTER_ASSERT(r, stream->read(actual.get(), remaining) == remaining);
593 REPORTER_ASSERT(r, !memcmp(expected.get(), actual.get(), remaining));
594
595 REPORTER_ASSERT(r, stream->isAtEnd());
596 REPORTER_ASSERT(r, stream->getPosition() == remaining);
597
598 // Cannot move beyond the end.
599 REPORTER_ASSERT(r, stream->move(1));
600 REPORTER_ASSERT(r, stream->isAtEnd());
601 REPORTER_ASSERT(r, stream->getPosition() == remaining);
602 };
603
604 auto test_seek = [&r, &expected, middle, remaining](SkStream* stream) {
605 // Seek to an arbitrary position.
606 const size_t arbitrary = middle / 2;
607 REPORTER_ASSERT(r, stream->seek(arbitrary));
608 REPORTER_ASSERT(r, stream->getPosition() == arbitrary);
609 const size_t miniRemaining = remaining - arbitrary;
610 AutoTMalloc<uint8_t> actual(miniRemaining);
611 REPORTER_ASSERT(r, stream->read(actual.get(), miniRemaining) == miniRemaining);
612 REPORTER_ASSERT(r, !memcmp(expected.get() + arbitrary, actual.get(), miniRemaining));
613 };
614
615 auto test_seek_beginning = [&r, &expected, remaining](SkStream* stream) {
616 // Seek to the beginning.
617 REPORTER_ASSERT(r, stream->seek(0));
618 REPORTER_ASSERT(r, stream->getPosition() == 0);
619 AutoTMalloc<uint8_t> actual(remaining);
620 REPORTER_ASSERT(r, stream->read(actual.get(), remaining) == remaining);
621 REPORTER_ASSERT(r, !memcmp(expected.get(), actual.get(), remaining));
622 };
623
624 auto test_seek_end = [&r, remaining](SkStream* stream) {
625 // Cannot seek past the end.
626 REPORTER_ASSERT(r, stream->isAtEnd());
627
628 REPORTER_ASSERT(r, stream->seek(remaining + 1));
629 REPORTER_ASSERT(r, stream->isAtEnd());
630 REPORTER_ASSERT(r, stream->getPosition() == remaining);
631
632 const size_t middle = remaining / 2;
633 REPORTER_ASSERT(r, stream->seek(middle));
634 REPORTER_ASSERT(r, !stream->isAtEnd());
635 REPORTER_ASSERT(r, stream->getPosition() == middle);
636
637 REPORTER_ASSERT(r, stream->seek(remaining * 2));
638 REPORTER_ASSERT(r, stream->isAtEnd());
639 REPORTER_ASSERT(r, stream->getPosition() == remaining);
640
642 REPORTER_ASSERT(r, stream->isAtEnd());
643 REPORTER_ASSERT(r, stream->getPosition() == remaining);
644 };
645
646
647 std::function<void (SkStream* stream, bool recurse)> test_all;
648 test_all = [&](SkStream* stream, bool recurse) {
649 REPORTER_ASSERT(r, stream->getLength() == remaining);
650 REPORTER_ASSERT(r, stream->getPosition() == 0);
651
652 test_full_read(stream);
654 test_move(stream);
655 test_seek(stream);
656 test_seek_beginning(stream);
657 test_seek_end(stream);
658
659 if (recurse) {
660 // Duplicate shares the original offset.
661 auto duplicate = stream->duplicate();
662 if (!duplicate) {
663 ERRORF(r, "Failed to duplicate the stream!");
664 } else {
665 test_all(duplicate.get(), false);
666 }
667
668 // Fork shares the original offset, too.
669 auto fork = stream->fork();
670 if (!fork) {
671 ERRORF(r, "Failed to fork the stream!");
672 } else {
673 REPORTER_ASSERT(r, fork->isAtEnd());
674 REPORTER_ASSERT(r, fork->getLength() == remaining);
675 REPORTER_ASSERT(r, fork->rewind());
676
677 test_all(fork.get(), false);
678 }
679 }
680 };
681
682 test_all(&stream2, true);
683}
SkString GetResourcePath(const char *resource)
Definition: Resources.cpp:23
FILE * sk_fopen(const char path[], SkFILE_Flags)
@ kRead_SkFILE_Flag
Definition: SkOSFile.h:20
static void test_rewind(skiatest::Reporter *reporter)
const char * c_str() const
Definition: SkString.h:133
Dart_NativeFunction function
Definition: fuchsia.cc:51
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259

◆ DEF_TEST() [3/9]

DEF_TEST ( RBuffer  ,
reporter   
)

Definition at line 685 of file StreamTest.cpp.

685 {
686 int32_t value = 0;
688 REPORTER_ASSERT(reporter, buffer.isValid());
689
690 int32_t tmp;
691 REPORTER_ASSERT(reporter, buffer.read(&tmp, 4));
692 REPORTER_ASSERT(reporter, buffer.isValid());
693
694 REPORTER_ASSERT(reporter, !buffer.read(&tmp, 4));
695 REPORTER_ASSERT(reporter, !buffer.isValid());
696}
uint8_t value
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126

◆ DEF_TEST() [4/9]

DEF_TEST ( Stream  ,
reporter   
)

Definition at line 209 of file StreamTest.cpp.

209 {
213}
static void TestWStream(skiatest::Reporter *reporter)
Definition: StreamTest.cpp:101
static void TestNullData(skiatest::Reporter *reporter)
Definition: StreamTest.cpp:200
static void TestPackedUInt(skiatest::Reporter *reporter)
Definition: StreamTest.cpp:158

◆ DEF_TEST() [5/9]

DEF_TEST ( StreamCopy  ,
reporter   
)

Definition at line 504 of file StreamTest.cpp.

504 {
505 SkRandom random(123456);
506 static const int N = 10000;
507 AutoTMalloc<uint8_t> src((size_t)N);
508 for (int j = 0; j < N; ++j) {
509 src[j] = random.nextU() & 0xff;
510 }
511 // SkStreamCopy had two code paths; this test both.
512 DumbStream dumbStream(src.get(), (size_t)N);
513 stream_copy_test(reporter, src, N, &dumbStream);
514 SkMemoryStream smartStream(src.get(), (size_t)N);
515 stream_copy_test(reporter, src, N, &smartStream);
516}
static void stream_copy_test(skiatest::Reporter *reporter, const void *srcData, size_t N, SkStream *stream)
Definition: StreamTest.cpp:465

◆ DEF_TEST() [6/9]

DEF_TEST ( StreamEmptyStreamMemoryBase  ,
 
)

Definition at line 518 of file StreamTest.cpp.

518 {
520 std::unique_ptr<SkStreamAsset> asset(tmp.detachAsStream());
521 REPORTER_ASSERT(r, nullptr == asset->getMemoryBase());
522}
std::unique_ptr< SkStreamAsset > detachAsStream()
Definition: SkStream.cpp:876

◆ DEF_TEST() [7/9]

DEF_TEST ( StreamPeek  ,
reporter   
)

Definition at line 312 of file StreamTest.cpp.

312 {
313 // Test a memory stream.
314 const char gAbcs[] = "abcdefghijklmnopqrstuvwxyz";
315 SkMemoryStream memStream(gAbcs, strlen(gAbcs), false);
316 test_fully_peekable_stream(reporter, &memStream, memStream.getLength());
317
318 // Test an arbitrary file stream. file streams do not support peeking.
319 auto tmpdir = skiatest::GetTmpDir();
320 if (tmpdir.isEmpty()) {
321 ERRORF(reporter, "no tmp dir!");
322 return;
323 }
324 auto path = SkOSPath::Join(tmpdir.c_str(), "file");
325 {
326 SkFILEWStream wStream(path.c_str());
327 constexpr char filename[] = "images/baby_tux.webp";
328 auto data = GetResourceAsData(filename);
329 if (!data || data->size() == 0) {
330 ERRORF(reporter, "resource missing: %s\n", filename);
331 return;
332 }
333 if (!wStream.isValid() || !wStream.write(data->data(), data->size())) {
334 ERRORF(reporter, "error wrtiting to file %s", path.c_str());
335 return;
336 }
337 }
338 SkFILEStream fileStream(path.c_str());
339 REPORTER_ASSERT(reporter, fileStream.isValid());
340 if (!fileStream.isValid()) {
341 return;
342 }
343 SkAutoMalloc storage(fileStream.getLength());
344 for (size_t i = 1; i < fileStream.getLength(); i++) {
345 REPORTER_ASSERT(reporter, fileStream.peek(storage.get(), i) == 0);
346 }
347
348#ifdef SK_ENABLE_ANDROID_UTILS
349 // Now test some FrontBufferedStreams
350 for (size_t i = 1; i < memStream.getLength(); i++) {
351 test_peeking_front_buffered_stream(reporter, memStream, i);
352 }
353#endif
354}
sk_sp< SkData > GetResourceAsData(const char *resource)
Definition: Resources.cpp:42
static void test_fully_peekable_stream(skiatest::Reporter *r, SkStream *stream, size_t limit)
Definition: StreamTest.cpp:247
static SkString Join(const char *rootPath, const char *relativePath)
Definition: SkOSPath.cpp:14
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
SkString GetTmpDir()
Definition: Test.cpp:53

◆ DEF_TEST() [8/9]

DEF_TEST ( StreamPeek_BlockMemoryStream  ,
rep   
)

Definition at line 392 of file StreamTest.cpp.

392 {
393 const static int kSeed = 1234;
394 SkRandom valueSource(kSeed);
395 SkRandom rand(kSeed << 1);
396 uint8_t buffer[4096];
397 SkDynamicMemoryWStream dynamicMemoryWStream;
398 size_t totalWritten = 0;
399 for (int i = 0; i < 32; ++i) {
400 // Randomize the length of the blocks.
401 size_t size = rand.nextRangeU(1, sizeof(buffer));
402 for (size_t j = 0; j < size; ++j) {
403 buffer[j] = valueSource.nextU() & 0xFF;
404 }
405 dynamicMemoryWStream.write(buffer, size);
406 totalWritten += size;
407 REPORTER_ASSERT(rep, totalWritten == dynamicMemoryWStream.bytesWritten());
408 }
409 std::unique_ptr<SkStreamAsset> asset(dynamicMemoryWStream.detachAsStream());
410 sk_sp<SkData> expected(SkData::MakeUninitialized(asset->getLength()));
411 uint8_t* expectedPtr = static_cast<uint8_t*>(expected->writable_data());
412 valueSource.setSeed(kSeed); // reseed.
413 // We want the exact same same "random" string of numbers to put
414 // in expected. i.e.: don't rely on SkDynamicMemoryStream to work
415 // correctly while we are testing SkDynamicMemoryStream.
416 for (size_t i = 0; i < asset->getLength(); ++i) {
417 expectedPtr[i] = valueSource.nextU() & 0xFF;
418 }
419 stream_peek_test(rep, asset.get(), expected.get());
420}
static void stream_peek_test(skiatest::Reporter *rep, SkStreamAsset *asset, const SkData *expected)
Definition: StreamTest.cpp:358
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition: SkData.cpp:116
bool write(const void *buffer, size_t size) override
Definition: SkStream.cpp:535

◆ DEF_TEST() [9/9]

DEF_TEST ( StreamRemainingLengthIsBelow_MemoryStream  ,
rep   
)

Definition at line 422 of file StreamTest.cpp.

◆ stream_copy_test()

static void stream_copy_test ( skiatest::Reporter reporter,
const void *  srcData,
size_t  N,
SkStream stream 
)
static

Definition at line 465 of file StreamTest.cpp.

468 {
470 if (!SkStreamCopy(&tgt, stream)) {
471 ERRORF(reporter, "SkStreamCopy failed");
472 return;
473 }
475 if (data->size() != N) {
476 ERRORF(reporter, "SkStreamCopy incorrect size");
477 return;
478 }
479 if (0 != memcmp(data->data(), srcData, N)) {
480 ERRORF(reporter, "SkStreamCopy bad copy");
481 }
482}
bool SkStreamCopy(SkWStream *out, SkStream *input)
Definition: SkStream.cpp:954

◆ stream_peek_test()

static void stream_peek_test ( skiatest::Reporter rep,
SkStreamAsset asset,
const SkData expected 
)
static

Definition at line 358 of file StreamTest.cpp.

360 {
361 if (asset->getLength() != expected->size()) {
362 ERRORF(rep, "Unexpected length.");
363 return;
364 }
365 SkRandom rand;
366 uint8_t buffer[4096];
367 const uint8_t* expect = expected->bytes();
368 for (size_t i = 0; i < asset->getLength(); ++i) {
369 uint32_t maxSize =
370 SkToU32(std::min(sizeof(buffer), asset->getLength() - i));
371 size_t size = rand.nextRangeU(1, maxSize);
372 SkASSERT(size >= 1);
373 SkASSERT(size <= sizeof(buffer));
374 SkASSERT(size + i <= asset->getLength());
375 if (asset->peek(buffer, size) < size) {
376 ERRORF(rep, "Peek Failed!");
377 return;
378 }
379 if (0 != memcmp(buffer, &expect[i], size)) {
380 ERRORF(rep, "Peek returned wrong bytes!");
381 return;
382 }
383 uint8_t value;
384 REPORTER_ASSERT(rep, 1 == asset->read(&value, 1));
385 if (value != expect[i]) {
386 ERRORF(rep, "Read Failed!");
387 return;
388 }
389 }
390}
#define SkASSERT(cond)
Definition: SkAssert.h:116
constexpr uint32_t SkToU32(S x)
Definition: SkTo.h:26
const uint8_t * bytes() const
Definition: SkData.h:43
size_t size() const
Definition: SkData.h:30
uint32_t nextRangeU(uint32_t min, uint32_t max)
Definition: SkRandom.h:80
size_t getLength() const override=0
virtual size_t peek(void *, size_t) const
Definition: SkStream.h:68
virtual size_t read(void *buffer, size_t size)=0

◆ test_filestreams()

static void test_filestreams ( skiatest::Reporter reporter,
const char *  tmpDir 
)
static

Definition at line 64 of file StreamTest.cpp.

64 {
65 SkString path = SkOSPath::Join(tmpDir, "wstream_test");
66
67 const char s[] = "abcdefghijklmnopqrstuvwxyz";
68
69 {
70 SkFILEWStream writer(path.c_str());
71 if (!writer.isValid()) {
72 ERRORF(reporter, "Failed to create tmp file %s\n", path.c_str());
73 return;
74 }
75
76 for (int i = 0; i < 100; ++i) {
77 writer.write(s, 26);
78 }
79 }
80
81 {
82 SkFILEStream stream(path.c_str());
84 test_loop_stream(reporter, &stream, s, 26, 100);
85
86 std::unique_ptr<SkStreamAsset> stream2(stream.duplicate());
87 test_loop_stream(reporter, stream2.get(), s, 26, 100);
88 }
89
90 {
91 FILE* file = ::fopen(path.c_str(), "rb");
94 test_loop_stream(reporter, &stream, s, 26, 100);
95
96 std::unique_ptr<SkStreamAsset> stream2(stream.duplicate());
97 test_loop_stream(reporter, stream2.get(), s, 26, 100);
98 }
99}
static void test_loop_stream(skiatest::Reporter *reporter, SkStream *stream, const void *src, size_t len, int repeat)
Definition: StreamTest.cpp:46
struct MyStruct s

◆ test_fully_peekable_stream()

static void test_fully_peekable_stream ( skiatest::Reporter r,
SkStream stream,
size_t  limit 
)
static

Definition at line 247 of file StreamTest.cpp.

247 {
248 for (size_t i = 1; !stream->isAtEnd(); i++) {
250 }
251}
static size_t compare_peek_to_read(skiatest::Reporter *reporter, SkStream *stream, size_t bytesToPeek)
Definition: StreamTest.cpp:221

◆ test_loop_stream()

static void test_loop_stream ( skiatest::Reporter reporter,
SkStream stream,
const void *  src,
size_t  len,
int  repeat 
)
static

Definition at line 46 of file StreamTest.cpp.

47 {
48 SkAutoSMalloc<256> storage(len);
49 void* tmp = storage.get();
50
51 for (int i = 0; i < repeat; ++i) {
52 size_t bytes = stream->read(tmp, len);
53 REPORTER_ASSERT(reporter, bytes == len);
54 REPORTER_ASSERT(reporter, !memcmp(tmp, src, len));
55 }
56
57 // expect EOF
58 size_t bytes = stream->read(tmp, 1);
59 REPORTER_ASSERT(reporter, 0 == bytes);
60 // isAtEnd might not return true until after the first failing read.
61 REPORTER_ASSERT(reporter, stream->isAtEnd());
62}
static unsigned repeat(SkFixed fx, int max)

◆ TestDereferencingData()

static void TestDereferencingData ( skiatest::Reporter reporter,
SkMemoryStream memStream 
)
static

Definition at line 191 of file StreamTest.cpp.

191 {
192 REPORTER_ASSERT(reporter, memStream->read(nullptr, 0) == 0);
193 // Reading non-zero bytes from an empty stream should cleanly read zero bytes.
194 char buf[1];
195 REPORTER_ASSERT(reporter, memStream->read(buf, sizeof(buf)) == 0);
196 memStream->getMemoryBase();
197 (void)memStream->getData();
198}
sk_sp< SkData > getData() const override
Definition: SkStream.h:395
size_t read(void *buffer, size_t size) override
Definition: SkStream.cpp:337
const void * getMemoryBase() override
Definition: SkStream.cpp:399

◆ TestNullData()

static void TestNullData ( skiatest::Reporter reporter)
static

Definition at line 200 of file StreamTest.cpp.

200 {
201 SkMemoryStream memStream(nullptr);
202 TestDereferencingData(reporter, &memStream);
203
204 memStream.setData(nullptr);
205 TestDereferencingData(reporter, &memStream);
206
207}
static void TestDereferencingData(skiatest::Reporter *reporter, SkMemoryStream *memStream)
Definition: StreamTest.cpp:191

◆ TestPackedUInt()

static void TestPackedUInt ( skiatest::Reporter reporter)
static

Definition at line 158 of file StreamTest.cpp.

158 {
159 // we know that packeduint tries to write 1, 2 or 4 bytes for the length,
160 // so we test values around each of those transitions (and a few others)
161 const size_t sizes[] = {
162 0, 1, 2, 0xFC, 0xFD, 0xFE, 0xFF, 0x100, 0x101, 32767, 32768, 32769,
163 0xFFFD, 0xFFFE, 0xFFFF, 0x10000, 0x10001,
164 0xFFFFFD, 0xFFFFFE, 0xFFFFFF, 0x1000000, 0x1000001,
165 0x7FFFFFFE, 0x7FFFFFFF, 0x80000000, 0x80000001, 0xFFFFFFFE, 0xFFFFFFFF
166 };
167
168
169 size_t i;
171
172 for (i = 0; i < std::size(sizes); ++i) {
173 bool success = wstream.writePackedUInt(sizes[i]);
174 REPORTER_ASSERT(reporter, success);
175 }
176
177 std::unique_ptr<SkStreamAsset> rstream(wstream.detachAsStream());
178 for (i = 0; i < std::size(sizes); ++i) {
179 size_t n;
180 if (!rstream->readPackedUInt(&n)) {
181 ERRORF(reporter, "[%zu] sizes:%zx could not be read\n", i, sizes[i]);
182 }
183 if (sizes[i] != n) {
184 ERRORF(reporter, "[%zu] sizes:%zx != n:%zx\n", i, sizes[i], n);
185 }
186 }
187}
bool writePackedUInt(size_t)
Definition: SkStream.cpp:122

◆ TestWStream()

static void TestWStream ( skiatest::Reporter reporter)
static

Definition at line 101 of file StreamTest.cpp.

101 {
103 const char s[] = "abcdefghijklmnopqrstuvwxyz";
104 int i;
105 for (i = 0; i < 100; i++) {
107 }
108 REPORTER_ASSERT(reporter, ds.bytesWritten() == 100 * 26);
109
110 char* dst = new char[100 * 26 + 1];
111 dst[100*26] = '*';
112 ds.copyTo(dst);
113 REPORTER_ASSERT(reporter, dst[100*26] == '*');
114 for (i = 0; i < 100; i++) {
115 REPORTER_ASSERT(reporter, memcmp(&dst[i * 26], s, 26) == 0);
116 }
117
118 {
119 std::unique_ptr<SkStreamAsset> stream(ds.detachAsStream());
120 REPORTER_ASSERT(reporter, 100 * 26 == stream->getLength());
122 test_loop_stream(reporter, stream.get(), s, 26, 100);
123
124 std::unique_ptr<SkStreamAsset> stream2(stream->duplicate());
125 test_loop_stream(reporter, stream2.get(), s, 26, 100);
126
127 std::unique_ptr<SkStreamAsset> stream3(stream->fork());
128 REPORTER_ASSERT(reporter, stream3->isAtEnd());
129 char tmp;
130 size_t bytes = stream->read(&tmp, 1);
131 REPORTER_ASSERT(reporter, 0 == bytes);
132 stream3->rewind();
133 test_loop_stream(reporter, stream3.get(), s, 26, 100);
134 }
135
136 for (i = 0; i < 100; i++) {
138 }
139 REPORTER_ASSERT(reporter, ds.bytesWritten() == 100 * 26);
140
141 {
142 // Test that this works after a snapshot.
143 std::unique_ptr<SkStreamAsset> stream(ds.detachAsStream());
145 test_loop_stream(reporter, stream.get(), s, 26, 100);
146
147 std::unique_ptr<SkStreamAsset> stream2(stream->duplicate());
148 test_loop_stream(reporter, stream2.get(), s, 26, 100);
149 }
150 delete[] dst;
151
152 SkString tmpDir = skiatest::GetTmpDir();
153 if (!tmpDir.isEmpty()) {
155 }
156}
static void test_filestreams(skiatest::Reporter *reporter, const char *tmpDir)
Definition: StreamTest.cpp:64
void copyTo(void *dst) const
Definition: SkStream.cpp:631
bool isEmpty() const
Definition: SkString.h:130
dst
Definition: cp.py:12