2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkFrontBufferedStream.h"
10 #include "SkImageDecoder.h"
16 static void test_read(skiatest::Reporter* reporter, SkStream* bufferedStream,
17 const void* expectations, size_t bytesToRead) {
18 // output for reading bufferedStream.
19 SkAutoMalloc storage(bytesToRead);
21 const size_t bytesRead = bufferedStream->read(storage.get(), bytesToRead);
22 REPORTER_ASSERT(reporter, bytesRead == bytesToRead || bufferedStream->isAtEnd());
23 REPORTER_ASSERT(reporter, memcmp(storage.get(), expectations, bytesRead) == 0);
26 static void test_rewind(skiatest::Reporter* reporter,
27 SkStream* bufferedStream, bool shouldSucceed) {
28 const bool success = bufferedStream->rewind();
29 REPORTER_ASSERT(reporter, success == shouldSucceed);
32 // Test that hasLength() returns the correct value, based on the stream
33 // being wrapped. A length can only be known if the wrapped stream has a
34 // length and it has a position (so its initial position can be taken into
35 // account when computing the length).
36 static void test_hasLength(skiatest::Reporter* reporter,
37 const SkStream& bufferedStream,
38 const SkStream& streamBeingBuffered) {
39 if (streamBeingBuffered.hasLength() && streamBeingBuffered.hasPosition()) {
40 REPORTER_ASSERT(reporter, bufferedStream.hasLength());
42 REPORTER_ASSERT(reporter, !bufferedStream.hasLength());
46 // All tests will buffer this string, and compare output to the original.
47 // The string is long to ensure that all of our lengths being tested are
48 // smaller than the string length.
49 const char gAbcs[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx";
51 // Tests reading the stream across boundaries of what has been buffered so far and what
52 // the total buffer size is.
53 static void test_incremental_buffering(skiatest::Reporter* reporter, size_t bufferSize) {
54 SkMemoryStream memStream(gAbcs, strlen(gAbcs), false);
56 SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize));
57 test_hasLength(reporter, *bufferedStream.get(), memStream);
59 // First, test reading less than the max buffer size.
60 test_read(reporter, bufferedStream, gAbcs, bufferSize / 2);
62 // Now test rewinding back to the beginning and reading less than what was
64 test_rewind(reporter, bufferedStream, true);
65 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4);
67 // Now test reading part of what was buffered, and buffering new data.
68 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), bufferSize / 2);
70 // Now test reading what was buffered, buffering new data, and
71 // reading directly from the stream.
72 test_rewind(reporter, bufferedStream, true);
73 test_read(reporter, bufferedStream, gAbcs, bufferSize << 1);
75 // We have reached the end of the buffer, so rewinding will fail.
76 // This test assumes that the stream is larger than the buffer; otherwise the
77 // result of rewind should be true.
78 test_rewind(reporter, bufferedStream, false);
81 static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
82 SkMemoryStream memStream(gAbcs, strlen(gAbcs), false);
83 SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize));
84 test_hasLength(reporter, *bufferedStream.get(), memStream);
86 // Read exactly the amount that fits in the buffer.
87 test_read(reporter, bufferedStream, gAbcs, bufferSize);
89 // Rewinding should succeed.
90 test_rewind(reporter, bufferedStream, true);
92 // Once again reading buffered info should succeed
93 test_read(reporter, bufferedStream, gAbcs, bufferSize);
95 // Read past the size of the buffer. At this point, we cannot return.
96 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), 1);
97 test_rewind(reporter, bufferedStream, false);
100 static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) {
101 SkMemoryStream memStream(gAbcs, strlen(gAbcs), false);
102 SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize));
103 test_hasLength(reporter, *bufferedStream.get(), memStream);
105 // Skip half the buffer.
106 bufferedStream->skip(bufferSize / 2);
108 // Rewind, then read part of the buffer, which should have been read.
109 test_rewind(reporter, bufferedStream, true);
110 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4);
112 // Now skip beyond the buffered piece, but still within the total buffer.
113 bufferedStream->skip(bufferSize / 2);
115 // Test that reading will still work.
116 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), bufferSize / 4);
118 test_rewind(reporter, bufferedStream, true);
119 test_read(reporter, bufferedStream, gAbcs, bufferSize);
122 // A custom class whose isAtEnd behaves the way Android's stream does - since it is an adaptor to a
123 // Java InputStream, it does not know that it is at the end until it has attempted to read beyond
124 // the end and failed. Used by test_read_beyond_buffer.
125 class AndroidLikeMemoryStream : public SkMemoryStream {
127 AndroidLikeMemoryStream(void* data, size_t size, bool ownMemory)
128 : INHERITED(data, size, ownMemory)
131 size_t read(void* dst, size_t requested) SK_OVERRIDE {
132 size_t bytesRead = this->INHERITED::read(dst, requested);
133 if (bytesRead < requested) {
139 bool isAtEnd() const SK_OVERRIDE {
145 typedef SkMemoryStream INHERITED;
148 // This test ensures that buffering the exact length of the stream and attempting to read beyond it
149 // does not invalidate the buffer.
150 static void test_read_beyond_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
151 // Use a stream that behaves like Android's stream.
152 AndroidLikeMemoryStream memStream((void*)gAbcs, bufferSize, false);
154 // Create a buffer that matches the length of the stream.
155 SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize));
156 test_hasLength(reporter, *bufferedStream.get(), memStream);
158 // Attempt to read one more than the bufferSize
159 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1);
160 test_rewind(reporter, bufferedStream.get(), true);
162 // Ensure that the initial read did not invalidate the buffer.
163 test_read(reporter, bufferedStream, gAbcs, bufferSize);
166 // Dummy stream that optionally has a length and/or position. Tests that FrontBufferedStream's
167 // length depends on the stream it's buffering having a length and position.
168 class LengthOptionalStream : public SkStream {
170 LengthOptionalStream(bool hasLength, bool hasPosition)
171 : fHasLength(hasLength)
172 , fHasPosition(hasPosition)
175 virtual bool hasLength() const SK_OVERRIDE {
179 virtual bool hasPosition() const SK_OVERRIDE {
183 virtual size_t read(void*, size_t) SK_OVERRIDE {
187 virtual bool isAtEnd() const SK_OVERRIDE {
192 const bool fHasLength;
193 const bool fHasPosition;
196 // Test all possible combinations of the wrapped stream having a length and a position.
197 static void test_length_combos(skiatest::Reporter* reporter, size_t bufferSize) {
198 for (int hasLen = 0; hasLen <= 1; hasLen++) {
199 for (int hasPos = 0; hasPos <= 1; hasPos++) {
200 LengthOptionalStream stream(SkToBool(hasLen), SkToBool(hasPos));
201 SkAutoTUnref<SkStream> buffered(SkFrontBufferedStream::Create(&stream, bufferSize));
202 test_hasLength(reporter, *buffered.get(), stream);
207 // Test using a stream with an initial offset.
208 static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize) {
209 SkMemoryStream memStream(gAbcs, strlen(gAbcs), false);
211 // Skip a few characters into the memStream, so that bufferedStream represents an offset into
212 // the stream it wraps.
213 const size_t arbitraryOffset = 17;
214 memStream.skip(arbitraryOffset);
215 SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize));
217 // Since SkMemoryStream has a length and a position, bufferedStream must also.
218 REPORTER_ASSERT(reporter, bufferedStream->hasLength());
220 const size_t amountToRead = 10;
221 const size_t bufferedLength = bufferedStream->getLength();
222 size_t currentPosition = bufferedStream->getPosition();
223 REPORTER_ASSERT(reporter, 0 == currentPosition);
225 // Read the stream in chunks. After each read, the position must match currentPosition,
226 // which sums the amount attempted to read, unless the end of the stream has been reached.
227 // Importantly, the end should not have been reached until currentPosition == bufferedLength.
228 while (currentPosition < bufferedLength) {
229 REPORTER_ASSERT(reporter, !bufferedStream->isAtEnd());
230 test_read(reporter, bufferedStream, gAbcs + arbitraryOffset + currentPosition,
232 currentPosition = SkTMin(currentPosition + amountToRead, bufferedLength);
233 REPORTER_ASSERT(reporter, bufferedStream->getPosition() == currentPosition);
235 REPORTER_ASSERT(reporter, bufferedStream->isAtEnd());
236 REPORTER_ASSERT(reporter, bufferedLength == currentPosition);
239 static void test_buffers(skiatest::Reporter* reporter, size_t bufferSize) {
240 test_incremental_buffering(reporter, bufferSize);
241 test_perfectly_sized_buffer(reporter, bufferSize);
242 test_skipping(reporter, bufferSize);
243 test_read_beyond_buffer(reporter, bufferSize);
244 test_length_combos(reporter, bufferSize);
245 test_initial_offset(reporter, bufferSize);
248 DEF_TEST(FrontBufferedStream, reporter) {
249 // Test 6 and 64, which are used by Android, as well as another arbitrary length.
250 test_buffers(reporter, 6);
251 test_buffers(reporter, 15);
252 test_buffers(reporter, 64);
255 // Test that a FrontBufferedStream does not allow reading after the end of a stream.
256 // This class is a dummy SkStream which reports that it is at the end on the first
257 // read (simulating a failure). Then it tracks whether someone calls read() again.
258 class FailingStream : public SkStream {
262 , fReadAfterEnd(false)
264 virtual size_t read(void* buffer, size_t size) SK_OVERRIDE {
266 fReadAfterEnd = true;
273 virtual bool isAtEnd() const SK_OVERRIDE {
277 bool readAfterEnd() const {
278 return fReadAfterEnd;
285 DEF_TEST(ShortFrontBufferedStream, reporter) {
286 FailingStream failingStream;
287 SkAutoTUnref<SkStreamRewindable> stream(SkFrontBufferedStream::Create(&failingStream, 64));
289 // The return value of DecodeStream is not important. We are just using DecodeStream because
290 // it simulates a bug. DecodeStream will read the stream, then rewind, then attempt to read
291 // again. FrontBufferedStream::read should not continue to read its underlying stream beyond
293 SkImageDecoder::DecodeStream(stream, &bm);
294 REPORTER_ASSERT(reporter, !failingStream.readAfterEnd());