static void test_peeking_stream(skiatest::Reporter* r, SkStream* stream, size_t limit) { size_t peeked = 0; for (size_t i = 1; !stream->isAtEnd(); i++) { const bool couldPeek = compare_peek_to_read(r, stream, i); if (!couldPeek) { REPORTER_ASSERT(r, peeked + i > limit); // No more peeking is supported. break; } peeked += i; } }
static void test_peeking_front_buffered_stream(skiatest::Reporter* r, const SkStream& original, size_t bufferSize) { SkStream* dupe = original.duplicate(); REPORTER_ASSERT(r, dupe != nullptr); SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(dupe, bufferSize)); REPORTER_ASSERT(r, bufferedStream != nullptr); size_t peeked = 0; for (size_t i = 1; !bufferedStream->isAtEnd(); i++) { const size_t unpeekableBytes = compare_peek_to_read(r, bufferedStream, i); if (unpeekableBytes > 0) { // This could not have returned a number greater than i. REPORTER_ASSERT(r, unpeekableBytes <= i); // We have reached the end of the buffer. Verify that it was at least // bufferSize. REPORTER_ASSERT(r, peeked + i - unpeekableBytes >= bufferSize); // No more peeking is supported. break; } peeked += i; } // Test that attempting to peek beyond the length of the buffer does not prevent rewinding. bufferedStream.reset(SkFrontBufferedStream::Create(original.duplicate(), bufferSize)); REPORTER_ASSERT(r, bufferedStream != nullptr); const size_t bytesToPeek = bufferSize + 1; SkAutoMalloc peekStorage(bytesToPeek); SkAutoMalloc readStorage(bytesToPeek); for (size_t start = 0; start <= bufferSize; start++) { // Skip to the starting point REPORTER_ASSERT(r, bufferedStream->skip(start) == start); const size_t bytesPeeked = bufferedStream->peek(peekStorage.get(), bytesToPeek); if (0 == bytesPeeked) { // Peeking should only fail completely if we have read/skipped beyond the buffer. REPORTER_ASSERT(r, start >= bufferSize); break; } // Only read the amount that was successfully peeked. const size_t bytesRead = bufferedStream->read(readStorage.get(), bytesPeeked); REPORTER_ASSERT(r, bytesRead == bytesPeeked); REPORTER_ASSERT(r, !memcmp(peekStorage.get(), readStorage.get(), bytesPeeked)); // This should be safe to rewind. REPORTER_ASSERT(r, bufferedStream->rewind()); } }
static void test_fully_peekable_stream(skiatest::Reporter* r, SkStream* stream, size_t limit) { for (size_t i = 1; !stream->isAtEnd(); i++) { REPORTER_ASSERT(r, compare_peek_to_read(r, stream, i) == 0); } }