diff options
author | 2015-04-02 13:19:51 -0700 | |
---|---|---|
committer | 2015-04-02 13:19:51 -0700 | |
commit | 028a4135aa6404ccd3a494813befe6e1a35e5e6c (patch) | |
tree | c8fcabf479a597c8117fcad7a6a506d11beed9bd /tests/StreamTest.cpp | |
parent | f92ace90d89cc99b34162dda26be564e34ca80ef (diff) |
Add a method to read a stream without advancing it.
Add a virtual method on SkStream which will do a "peek" some bytes, so
that those bytes are read, but the next call to read will be
unaffected.
Implement peek for SkMemoryStream, where the implementation is simple
and obvious.
Implement peek on SkFrontBufferedStream.
Add tests.
Motivated by decoding streams which cannot be rewound.
TBR=reed@google.com
BUG=skia:3257
Review URL: https://codereview.chromium.org/1044953002
Diffstat (limited to 'tests/StreamTest.cpp')
-rw-r--r-- | tests/StreamTest.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/tests/StreamTest.cpp b/tests/StreamTest.cpp index ff22ecf554..7a89c99c0c 100644 --- a/tests/StreamTest.cpp +++ b/tests/StreamTest.cpp @@ -5,7 +5,9 @@ * found in the LICENSE file. */ +#include "Resources.h" #include "SkData.h" +#include "SkFrontBufferedStream.h" #include "SkOSFile.h" #include "SkRandom.h" #include "SkStream.h" @@ -190,3 +192,76 @@ DEF_TEST(Stream, reporter) { TestPackedUInt(reporter); TestNullData(); } + +/** + * Tests peeking and then reading the same amount. The two should provide the + * same results. + * Returns whether the stream could peek. + */ +static bool compare_peek_to_read(skiatest::Reporter* reporter, + SkStream* stream, size_t bytesToPeek) { + // The rest of our tests won't be very interesting if bytesToPeek is zero. + REPORTER_ASSERT(reporter, bytesToPeek > 0); + SkAutoMalloc peekStorage(bytesToPeek); + SkAutoMalloc readStorage(bytesToPeek); + void* peekPtr = peekStorage.get(); + void* readPtr = peekStorage.get(); + + if (!stream->peek(peekPtr, bytesToPeek)) { + return false; + } + const size_t bytesRead = stream->read(readPtr, bytesToPeek); + + // bytesRead should only be less than attempted if the stream is at the + // end. + REPORTER_ASSERT(reporter, bytesRead == bytesToPeek || stream->isAtEnd()); + + // peek and read should behave the same, except peek returned to the + // original position, so they read the same data. + REPORTER_ASSERT(reporter, !memcmp(peekPtr, readPtr, bytesRead)); + + return true; +} + +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 != NULL); + SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(dupe, bufferSize)); + REPORTER_ASSERT(r, bufferedStream != NULL); + test_peeking_stream(r, bufferedStream, bufferSize); +} + +DEF_TEST(StreamPeek, reporter) { + // Test a memory stream. + const char gAbcs[] = "abcdefghijklmnopqrstuvwxyz"; + SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); + test_peeking_stream(reporter, &memStream, memStream.getLength()); + + // Test an arbitrary file stream. file streams do not support peeking. + SkFILEStream fileStream(GetResourcePath("baby_tux.webp").c_str()); + REPORTER_ASSERT(reporter, fileStream.isValid()); + SkAutoMalloc storage(fileStream.getLength()); + for (size_t i = 1; i < fileStream.getLength(); i++) { + REPORTER_ASSERT(reporter, !fileStream.peek(storage.get(), i)); + } + + // Now test some FrontBufferedStreams + for (size_t i = 1; i < memStream.getLength(); i++) { + test_peeking_front_buffered_stream(reporter, memStream, i); + } +} |