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

Side by Side Diff: tests/StreamTest.cpp

Issue 1044953002: Add a method to read a stream without advancing it. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 8 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
« include/core/SkStream.h ('K') | « src/utils/SkFrontBufferedStream.cpp ('k') | no next file » | 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 2011 Google Inc. 2 * Copyright 2011 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 "Resources.h"
8 #include "SkData.h" 9 #include "SkData.h"
10 #include "SkFrontBufferedStream.h"
9 #include "SkOSFile.h" 11 #include "SkOSFile.h"
10 #include "SkRandom.h" 12 #include "SkRandom.h"
11 #include "SkStream.h" 13 #include "SkStream.h"
12 #include "Test.h" 14 #include "Test.h"
13 15
14 #ifndef SK_BUILD_FOR_WIN 16 #ifndef SK_BUILD_FOR_WIN
15 #include <unistd.h> 17 #include <unistd.h>
16 #include <fcntl.h> 18 #include <fcntl.h>
17 #endif 19 #endif
18 20
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 memStream.setData(nullData); 185 memStream.setData(nullData);
184 TestDereferencingData(&memStream); 186 TestDereferencingData(&memStream);
185 187
186 } 188 }
187 189
188 DEF_TEST(Stream, reporter) { 190 DEF_TEST(Stream, reporter) {
189 TestWStream(reporter); 191 TestWStream(reporter);
190 TestPackedUInt(reporter); 192 TestPackedUInt(reporter);
191 TestNullData(); 193 TestNullData();
192 } 194 }
195
196 /**
197 * Tests peeking and then reading the same amount. The two should provide the
198 * same results.
199 * Returns the amount successfully read minus the amount successfully peeked.
200 */
201 static size_t compare_peek_to_read(skiatest::Reporter* reporter,
202 SkStream* stream, size_t bytesToPeek) {
203 // The rest of our tests won't be very interesting if bytesToPeek is zero.
204 REPORTER_ASSERT(reporter, bytesToPeek > 0);
205 SkAutoMalloc peekStorage(bytesToPeek);
206 SkAutoMalloc readStorage(bytesToPeek);
207 void* peekPtr = peekStorage.get();
208 void* readPtr = peekStorage.get();
209
210 const size_t bytesPeeked = stream->peek(peekPtr, bytesToPeek);
211 const size_t bytesRead = stream->read(readPtr, bytesToPeek);
212
213 // bytesRead should only be less than attempted if the stream is at the
214 // end.
215 REPORTER_ASSERT(reporter, bytesRead == bytesToPeek || stream->isAtEnd());
216
217 if (0 == bytesPeeked) {
218 // The mem compare will not be very interesting here.
219 return bytesRead;
220 }
221
222 // peek and read should behave the same, except peek returned to the
223 // original position, so they read the same data.
224 REPORTER_ASSERT(reporter, !memcmp(peekPtr, readPtr, bytesPeeked));
225
226 // A stream should never be able to peek less than it can read.
227 REPORTER_ASSERT(reporter, bytesRead >= bytesPeeked);
228 return bytesRead - bytesPeeked;
229 }
230
231 // Test peeking on a stream for which peeking is always supported.
232 static void test_fully_peekable_stream(skiatest::Reporter* r,
233 SkStream* stream) {
234 for (size_t i = 1; !stream->isAtEnd(); i++) {
235 // We can always peek as many bytes as we can read.
236 REPORTER_ASSERT(r, compare_peek_to_read(r, stream, i) == 0);
237 }
238 }
239
240 static void test_peeking_front_buffered_stream(skiatest::Reporter* r,
241 const SkStream& original,
242 size_t bufferSize) {
243 SkStream* dupe = original.duplicate();
244 REPORTER_ASSERT(r, dupe != NULL);
245 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(dupe, b ufferSize));
246 REPORTER_ASSERT(r, bufferedStream != NULL);
247 size_t peeked = 0;
248 for (size_t i = 0; !bufferedStream->isAtEnd(); i++) {
249 const size_t unpeekableBytes = compare_peek_to_read(r, bufferedStream, i );
250 if (unpeekableBytes > 0) {
251 // This could not have returned a number greater than i.
252 REPORTER_ASSERT(r, unpeekableBytes <= i);
253
254 // We have reached the end of the buffer. Verify that it was at leas t
255 // bufferSize.
256 REPORTER_ASSERT(r, peeked + i - unpeekableBytes >= bufferSize);
257 // No more peeking is supported.
258 break;
259 }
260 peeked += i;
261 }
262 }
263
264 DEF_TEST(StreamPeek, reporter) {
265 // Test a memory stream.
266 const char gAbcs[] = "abcdefghijklmnopqrstuvwxyz";
267 SkMemoryStream memStream(gAbcs, strlen(gAbcs), false);
268 test_fully_peekable_stream(reporter, &memStream);
269
270 // Test an arbitrary file stream.
271 SkFILEStream fileStream(GetResourcePath("baby_tux.webp").c_str());
272 REPORTER_ASSERT(reporter, fileStream.isValid());
273 test_fully_peekable_stream(reporter, &fileStream);
274
275 // Now test some FrontBufferedStreams
276 for (size_t i = 1; i < memStream.getLength(); i++) {
277 test_peeking_front_buffered_stream(reporter, memStream, i);
278 }
279 }
OLDNEW
« include/core/SkStream.h ('K') | « src/utils/SkFrontBufferedStream.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698