Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(547)

Side by Side Diff: tests/FrontBufferedStreamTest.cpp

Issue 1703293002: Remove position from FrontBufferedStream (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix test_peeking_front_buffered_stream Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tests/CodexTest.cpp ('k') | tests/StreamTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 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 "SkBitmap.h" 8 #include "SkBitmap.h"
9 #include "SkFrontBufferedStream.h" 9 #include "SkFrontBufferedStream.h"
10 #include "SkImageDecoder.h" 10 #include "SkImageDecoder.h"
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 61
62 // First, test reading less than the max buffer size. 62 // First, test reading less than the max buffer size.
63 test_read(reporter, bufferedStream, gAbcs, bufferSize / 2); 63 test_read(reporter, bufferedStream, gAbcs, bufferSize / 2);
64 64
65 // Now test rewinding back to the beginning and reading less than what was 65 // Now test rewinding back to the beginning and reading less than what was
66 // already buffered. 66 // already buffered.
67 test_rewind(reporter, bufferedStream, true); 67 test_rewind(reporter, bufferedStream, true);
68 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); 68 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4);
69 69
70 // Now test reading part of what was buffered, and buffering new data. 70 // Now test reading part of what was buffered, and buffering new data.
71 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), b ufferSize / 2); 71 test_read(reporter, bufferedStream, gAbcs + bufferSize / 4, bufferSize / 2);
72 72
73 // Now test reading what was buffered, buffering new data, and 73 // Now test reading what was buffered, buffering new data, and
74 // reading directly from the stream. 74 // reading directly from the stream.
75 test_rewind(reporter, bufferedStream, true); 75 test_rewind(reporter, bufferedStream, true);
76 test_read(reporter, bufferedStream, gAbcs, bufferSize << 1); 76 test_read(reporter, bufferedStream, gAbcs, bufferSize << 1);
77 77
78 // We have reached the end of the buffer, so rewinding will fail. 78 // We have reached the end of the buffer, so rewinding will fail.
79 // This test assumes that the stream is larger than the buffer; otherwise th e 79 // This test assumes that the stream is larger than the buffer; otherwise th e
80 // result of rewind should be true. 80 // result of rewind should be true.
81 test_rewind(reporter, bufferedStream, false); 81 test_rewind(reporter, bufferedStream, false);
82 } 82 }
83 83
84 static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t buf ferSize) { 84 static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t buf ferSize) {
85 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false); 85 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false);
86 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre am, bufferSize)); 86 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre am, bufferSize));
87 test_hasLength(reporter, *bufferedStream.get(), *memStream); 87 test_hasLength(reporter, *bufferedStream.get(), *memStream);
88 88
89 // Read exactly the amount that fits in the buffer. 89 // Read exactly the amount that fits in the buffer.
90 test_read(reporter, bufferedStream, gAbcs, bufferSize); 90 test_read(reporter, bufferedStream, gAbcs, bufferSize);
91 91
92 // Rewinding should succeed. 92 // Rewinding should succeed.
93 test_rewind(reporter, bufferedStream, true); 93 test_rewind(reporter, bufferedStream, true);
94 94
95 // Once again reading buffered info should succeed 95 // Once again reading buffered info should succeed
96 test_read(reporter, bufferedStream, gAbcs, bufferSize); 96 test_read(reporter, bufferedStream, gAbcs, bufferSize);
97 97
98 // Read past the size of the buffer. At this point, we cannot return. 98 // Read past the size of the buffer. At this point, we cannot return.
99 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), 1 ); 99 test_read(reporter, bufferedStream, gAbcs + memStream->getPosition(), 1);
100 test_rewind(reporter, bufferedStream, false); 100 test_rewind(reporter, bufferedStream, false);
101 } 101 }
102 102
103 static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) { 103 static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) {
104 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false); 104 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false);
105 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre am, bufferSize)); 105 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre am, bufferSize));
106 test_hasLength(reporter, *bufferedStream.get(), *memStream); 106 test_hasLength(reporter, *bufferedStream.get(), *memStream);
107 107
108 // Skip half the buffer. 108 // Skip half the buffer.
109 bufferedStream->skip(bufferSize / 2); 109 bufferedStream->skip(bufferSize / 2);
110 110
111 // Rewind, then read part of the buffer, which should have been read. 111 // Rewind, then read part of the buffer, which should have been read.
112 test_rewind(reporter, bufferedStream, true); 112 test_rewind(reporter, bufferedStream, true);
113 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); 113 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4);
114 114
115 // Now skip beyond the buffered piece, but still within the total buffer. 115 // Now skip beyond the buffered piece, but still within the total buffer.
116 bufferedStream->skip(bufferSize / 2); 116 bufferedStream->skip(bufferSize / 2);
117 117
118 // Test that reading will still work. 118 // Test that reading will still work.
119 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), b ufferSize / 4); 119 test_read(reporter, bufferedStream, gAbcs + memStream->getPosition(), buffer Size / 4);
120 120
121 test_rewind(reporter, bufferedStream, true); 121 test_rewind(reporter, bufferedStream, true);
122 test_read(reporter, bufferedStream, gAbcs, bufferSize); 122 test_read(reporter, bufferedStream, gAbcs, bufferSize);
123 } 123 }
124 124
125 // A custom class whose isAtEnd behaves the way Android's stream does - since it is an adaptor to a 125 // A custom class whose isAtEnd behaves the way Android's stream does - since it is an adaptor to a
126 // Java InputStream, it does not know that it is at the end until it has attempt ed to read beyond 126 // Java InputStream, it does not know that it is at the end until it has attempt ed to read beyond
127 // the end and failed. Used by test_read_beyond_buffer. 127 // the end and failed. Used by test_read_beyond_buffer.
128 class AndroidLikeMemoryStream : public SkMemoryStream { 128 class AndroidLikeMemoryStream : public SkMemoryStream {
129 public: 129 public:
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 // Test using a stream with an initial offset. 212 // Test using a stream with an initial offset.
213 static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize) { 213 static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize) {
214 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false); 214 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false);
215 215
216 // Skip a few characters into the memStream, so that bufferedStream represen ts an offset into 216 // Skip a few characters into the memStream, so that bufferedStream represen ts an offset into
217 // the stream it wraps. 217 // the stream it wraps.
218 const size_t arbitraryOffset = 17; 218 const size_t arbitraryOffset = 17;
219 memStream->skip(arbitraryOffset); 219 memStream->skip(arbitraryOffset);
220 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre am, bufferSize)); 220 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre am, bufferSize));
221 221
222 // Since SkMemoryStream has a length and a position, bufferedStream must als o. 222 // Since SkMemoryStream has a length, bufferedStream must also.
223 REPORTER_ASSERT(reporter, bufferedStream->hasLength()); 223 REPORTER_ASSERT(reporter, bufferedStream->hasLength());
224 224
225 const size_t amountToRead = 10; 225 const size_t amountToRead = 10;
226 const size_t bufferedLength = bufferedStream->getLength(); 226 const size_t bufferedLength = bufferedStream->getLength();
227 size_t currentPosition = bufferedStream->getPosition(); 227 size_t currentPosition = 0;
228 REPORTER_ASSERT(reporter, 0 == currentPosition);
229 228
230 // Read the stream in chunks. After each read, the position must match curre ntPosition, 229 // Read the stream in chunks. After each read, the position must match curre ntPosition,
231 // which sums the amount attempted to read, unless the end of the stream has been reached. 230 // which sums the amount attempted to read, unless the end of the stream has been reached.
232 // Importantly, the end should not have been reached until currentPosition = = bufferedLength. 231 // Importantly, the end should not have been reached until currentPosition = = bufferedLength.
233 while (currentPosition < bufferedLength) { 232 while (currentPosition < bufferedLength) {
234 REPORTER_ASSERT(reporter, !bufferedStream->isAtEnd()); 233 REPORTER_ASSERT(reporter, !bufferedStream->isAtEnd());
235 test_read(reporter, bufferedStream, gAbcs + arbitraryOffset + currentPos ition, 234 test_read(reporter, bufferedStream, gAbcs + arbitraryOffset + currentPos ition,
236 amountToRead); 235 amountToRead);
237 currentPosition = SkTMin(currentPosition + amountToRead, bufferedLength) ; 236 currentPosition = SkTMin(currentPosition + amountToRead, bufferedLength) ;
238 REPORTER_ASSERT(reporter, bufferedStream->getPosition() == currentPositi on); 237 REPORTER_ASSERT(reporter, memStream->getPosition() - arbitraryOffset == currentPosition);
239 } 238 }
240 REPORTER_ASSERT(reporter, bufferedStream->isAtEnd()); 239 REPORTER_ASSERT(reporter, bufferedStream->isAtEnd());
241 REPORTER_ASSERT(reporter, bufferedLength == currentPosition); 240 REPORTER_ASSERT(reporter, bufferedLength == currentPosition);
242 } 241 }
243 242
244 static void test_buffers(skiatest::Reporter* reporter, size_t bufferSize) { 243 static void test_buffers(skiatest::Reporter* reporter, size_t bufferSize) {
245 test_incremental_buffering(reporter, bufferSize); 244 test_incremental_buffering(reporter, bufferSize);
246 test_perfectly_sized_buffer(reporter, bufferSize); 245 test_perfectly_sized_buffer(reporter, bufferSize);
247 test_skipping(reporter, bufferSize); 246 test_skipping(reporter, bufferSize);
248 test_read_beyond_buffer(reporter, bufferSize); 247 test_read_beyond_buffer(reporter, bufferSize);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 FailingStream* failingStream = new FailingStream; 290 FailingStream* failingStream = new FailingStream;
292 SkAutoTDelete<SkStreamRewindable> stream(SkFrontBufferedStream::Create(faili ngStream, 64)); 291 SkAutoTDelete<SkStreamRewindable> stream(SkFrontBufferedStream::Create(faili ngStream, 64));
293 SkBitmap bm; 292 SkBitmap bm;
294 // The return value of DecodeStream is not important. We are just using Deco deStream because 293 // The return value of DecodeStream is not important. We are just using Deco deStream because
295 // it simulates a bug. DecodeStream will read the stream, then rewind, then attempt to read 294 // it simulates a bug. DecodeStream will read the stream, then rewind, then attempt to read
296 // again. FrontBufferedStream::read should not continue to read its underlyi ng stream beyond 295 // again. FrontBufferedStream::read should not continue to read its underlyi ng stream beyond
297 // its end. 296 // its end.
298 SkImageDecoder::DecodeStream(stream, &bm); 297 SkImageDecoder::DecodeStream(stream, &bm);
299 REPORTER_ASSERT(reporter, !failingStream->readAfterEnd()); 298 REPORTER_ASSERT(reporter, !failingStream->readAfterEnd());
300 } 299 }
OLDNEW
« no previous file with comments | « tests/CodexTest.cpp ('k') | tests/StreamTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698