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 whether the stream could peek. | |
| 200 */ | |
| 201 static bool 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 if (!stream->peek(peekPtr, bytesToPeek)) { | |
| 211 return false; | |
| 212 } | |
| 213 const size_t bytesRead = stream->read(readPtr, bytesToPeek); | |
| 214 | |
| 215 // bytesRead should only be less than attempted if the stream is at the | |
| 216 // end. | |
| 217 REPORTER_ASSERT(reporter, bytesRead == bytesToPeek || stream->isAtEnd()); | |
| 218 | |
| 219 // peek and read should behave the same, except peek returned to the | |
| 220 // original position, so they read the same data. | |
| 221 REPORTER_ASSERT(reporter, !memcmp(peekPtr, readPtr, bytesRead)); | |
| 222 | |
| 223 return true; | |
| 224 } | |
| 225 | |
| 226 static void test_peeking_stream(skiatest::Reporter* r, SkStream* stream, size_t limit) { | |
| 227 size_t peeked = 0; | |
| 228 for (size_t i = 1; !stream->isAtEnd(); i++) { | |
| 229 const bool couldPeek = compare_peek_to_read(r, stream, i); | |
| 230 if (!couldPeek) { | |
| 231 REPORTER_ASSERT(r, peeked + i > limit); | |
| 232 // No more peeking is supported. | |
| 233 break; | |
| 234 } | |
| 235 peeked += i; | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 static void test_peeking_front_buffered_stream(skiatest::Reporter* r, | |
| 240 const SkStream& original, | |
| 241 size_t bufferSize) { | |
| 242 SkStream* dupe = original.duplicate(); | |
| 243 REPORTER_ASSERT(r, dupe != NULL); | |
| 244 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(dupe, b ufferSize)); | |
| 245 REPORTER_ASSERT(r, bufferedStream != NULL); | |
| 246 test_peeking_stream(r, bufferedStream, bufferSize); | |
| 247 } | |
| 248 | |
| 249 DEF_TEST(StreamPeek, reporter) { | |
| 250 // Test a memory stream. | |
| 251 const char gAbcs[] = "abcdefghijklmnopqrstuvwxyz"; | |
| 252 SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); | |
| 253 test_peeking_stream(reporter, &memStream, memStream.getLength()); | |
| 254 | |
| 255 // Test an arbitrary file stream. file streams do not support peeking. | |
|
scroggo
2015/04/02 18:04:53
If we decide to implement SkFILEStream::peek to va
| |
| 256 SkFILEStream fileStream(GetResourcePath("baby_tux.webp").c_str()); | |
| 257 REPORTER_ASSERT(reporter, fileStream.isValid()); | |
| 258 SkAutoMalloc storage(fileStream.getLength()); | |
| 259 for (size_t i = 1; i < fileStream.getLength(); i++) { | |
| 260 REPORTER_ASSERT(reporter, !fileStream.peek(storage.get(), i)); | |
| 261 } | |
| 262 | |
| 263 // Now test some FrontBufferedStreams | |
| 264 for (size_t i = 1; i < memStream.getLength(); i++) { | |
| 265 test_peeking_front_buffered_stream(reporter, memStream, i); | |
| 266 } | |
| 267 } | |
| OLD | NEW |