// Tests reading the stream across boundaries of what has been buffered so far and what // the total buffer size is. static void test_incremental_buffering(skiatest::Reporter* reporter, size_t bufferSize) { // NOTE: For this and other tests in this file, we cheat and continue to refer to the // wrapped stream, but that's okay because we know the wrapping stream has not been // deleted yet (and we only call const methods in it). SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs), false)); SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize)); test_hasLength(reporter, *bufferedStream.get(), *memStream); // First, test reading less than the max buffer size. test_read(reporter, bufferedStream, gAbcs, bufferSize / 2); // Now test rewinding back to the beginning and reading less than what was // already buffered. test_rewind(reporter, bufferedStream, true); test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); // Now test reading part of what was buffered, and buffering new data. test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), bufferSize / 2); // Now test reading what was buffered, buffering new data, and // reading directly from the stream. test_rewind(reporter, bufferedStream, true); test_read(reporter, bufferedStream, gAbcs, bufferSize << 1); // We have reached the end of the buffer, so rewinding will fail. // This test assumes that the stream is larger than the buffer; otherwise the // result of rewind should be true. test_rewind(reporter, bufferedStream, false); }
static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t bufferSize) { SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs), false)); SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize)); test_hasLength(reporter, *bufferedStream.get(), *memStream); // Read exactly the amount that fits in the buffer. test_read(reporter, bufferedStream, gAbcs, bufferSize); // Rewinding should succeed. test_rewind(reporter, bufferedStream, true); // Once again reading buffered info should succeed test_read(reporter, bufferedStream, gAbcs, bufferSize); // Read past the size of the buffer. At this point, we cannot return. test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), 1); test_rewind(reporter, bufferedStream, false); }
static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) { SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs), false)); SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize)); test_hasLength(reporter, *bufferedStream.get(), *memStream); // Skip half the buffer. bufferedStream->skip(bufferSize / 2); // Rewind, then read part of the buffer, which should have been read. test_rewind(reporter, bufferedStream, true); test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); // Now skip beyond the buffered piece, but still within the total buffer. bufferedStream->skip(bufferSize / 2); // Test that reading will still work. test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), bufferSize / 4); test_rewind(reporter, bufferedStream, true); test_read(reporter, bufferedStream, gAbcs, bufferSize); }
// This test ensures that buffering the exact length of the stream and attempting to read beyond it // does not invalidate the buffer. static void test_read_beyond_buffer(skiatest::Reporter* reporter, size_t bufferSize) { // Use a stream that behaves like Android's stream. AndroidLikeMemoryStream* memStream = new AndroidLikeMemoryStream((void*)gAbcs, bufferSize, false); // Create a buffer that matches the length of the stream. std::unique_ptr<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize)); test_hasLength(reporter, *bufferedStream.get(), *memStream); // Attempt to read one more than the bufferSize test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1); test_rewind(reporter, bufferedStream.get(), true); // Ensure that the initial read did not invalidate the buffer. test_read(reporter, bufferedStream.get(), gAbcs, bufferSize); }
DEF_TEST(Writer32_misc, reporter) { test_reserve(reporter); test_string_null(reporter); test_ptr(reporter); test_rewind(reporter); }