Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 // The only way this can fail with our existing streams is if it is a file | |
| 218 // stream and seek() failed. | |
| 219 REPORTER_ASSERT(reporter, bytesPeeked != SkStream::kFailed_PeekResult); | |
| 220 | |
| 221 if (0 == bytesPeeked || SkStream::kUnsupported_PeekResult == bytesPeeked) { | |
| 222 // The mem compare will not be very interesting here. | |
| 223 return bytesRead; | |
| 224 } | |
| 225 | |
| 226 // peek and read should behave the same, except peek returned to the | |
| 227 // original position, so they read the same data. | |
| 228 REPORTER_ASSERT(reporter, !memcmp(peekPtr, readPtr, bytesPeeked)); | |
| 229 | |
| 230 // A stream should never be able to peek less than it can read. | |
|
bungeman-skia
2015/04/01 19:04:40
Should this be
// A stream should never be able t
| |
| 231 REPORTER_ASSERT(reporter, bytesRead >= bytesPeeked); | |
| 232 return bytesRead - bytesPeeked; | |
| 233 } | |
| 234 | |
| 235 // Test peeking on a stream for which peeking is always supported. | |
| 236 static void test_fully_peekable_stream(skiatest::Reporter* r, | |
| 237 SkStream* stream) { | |
|
bungeman-skia
2015/04/01 19:04:40
Seems that this would fit on the previous line?
scroggo
2015/04/02 18:04:53
82 chars. I'm never sure how to treat our soft lim
| |
| 238 for (size_t i = 1; !stream->isAtEnd(); i++) { | |
| 239 // We can always peek as many bytes as we can read. | |
| 240 REPORTER_ASSERT(r, compare_peek_to_read(r, stream, i) == 0); | |
| 241 } | |
| 242 } | |
| 243 | |
| 244 static void test_peeking_front_buffered_stream(skiatest::Reporter* r, | |
| 245 const SkStream& original, | |
| 246 size_t bufferSize) { | |
| 247 SkStream* dupe = original.duplicate(); | |
| 248 REPORTER_ASSERT(r, dupe != NULL); | |
| 249 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(dupe, b ufferSize)); | |
| 250 REPORTER_ASSERT(r, bufferedStream != NULL); | |
| 251 size_t peeked = 0; | |
| 252 for (size_t i = 1; !bufferedStream->isAtEnd(); i++) { | |
| 253 const size_t unpeekableBytes = compare_peek_to_read(r, bufferedStream, i ); | |
| 254 if (unpeekableBytes > 0) { | |
| 255 // This could not have returned a number greater than i. | |
| 256 REPORTER_ASSERT(r, unpeekableBytes <= i); | |
| 257 | |
| 258 // We have reached the end of the buffer. Verify that it was at leas t | |
| 259 // bufferSize. | |
| 260 REPORTER_ASSERT(r, peeked + i - unpeekableBytes >= bufferSize); | |
| 261 // No more peeking is supported. | |
| 262 break; | |
| 263 } | |
| 264 peeked += i; | |
| 265 } | |
| 266 } | |
| 267 | |
| 268 DEF_TEST(StreamPeek, reporter) { | |
| 269 // Test a memory stream. | |
| 270 const char gAbcs[] = "abcdefghijklmnopqrstuvwxyz"; | |
| 271 SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); | |
| 272 test_fully_peekable_stream(reporter, &memStream); | |
| 273 | |
| 274 // Test an arbitrary file stream. | |
| 275 SkFILEStream fileStream(GetResourcePath("baby_tux.webp").c_str()); | |
| 276 REPORTER_ASSERT(reporter, fileStream.isValid()); | |
| 277 test_fully_peekable_stream(reporter, &fileStream); | |
| 278 | |
| 279 // Now test some FrontBufferedStreams | |
| 280 for (size_t i = 1; i < memStream.getLength(); i++) { | |
| 281 test_peeking_front_buffered_stream(reporter, memStream, i); | |
| 282 } | |
| 283 } | |
| OLD | NEW |