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 "SkData.h" | 8 #include "SkData.h" |
9 #include "SkDataTable.h" | 9 #include "SkDataTable.h" |
10 #include "SkOSFile.h" | 10 #include "SkOSFile.h" |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 | 238 |
239 const char gABC[] = "abcdefghijklmnopqrstuvwxyz"; | 239 const char gABC[] = "abcdefghijklmnopqrstuvwxyz"; |
240 | 240 |
241 static void check_abcs(skiatest::Reporter* reporter, const char buffer[], size_t
size) { | 241 static void check_abcs(skiatest::Reporter* reporter, const char buffer[], size_t
size) { |
242 REPORTER_ASSERT(reporter, size % 26 == 0); | 242 REPORTER_ASSERT(reporter, size % 26 == 0); |
243 for (size_t offset = 0; offset < size; offset += 26) { | 243 for (size_t offset = 0; offset < size; offset += 26) { |
244 REPORTER_ASSERT(reporter, !memcmp(&buffer[offset], gABC, 26)); | 244 REPORTER_ASSERT(reporter, !memcmp(&buffer[offset], gABC, 26)); |
245 } | 245 } |
246 } | 246 } |
247 | 247 |
248 // stream should contains an integral number of copies of gABC. | 248 // stream should contain an integral number of copies of gABC. |
249 static void check_alphabet_stream(skiatest::Reporter* reporter, SkStream* stream
) { | 249 static void check_alphabet_stream(skiatest::Reporter* reporter, SkStream* stream
) { |
250 REPORTER_ASSERT(reporter, stream->hasLength()); | 250 REPORTER_ASSERT(reporter, stream->hasLength()); |
251 size_t size = stream->getLength(); | 251 size_t size = stream->getLength(); |
252 REPORTER_ASSERT(reporter, size % 26 == 0); | 252 REPORTER_ASSERT(reporter, size % 26 == 0); |
253 | 253 |
254 SkAutoTMalloc<char> storage(size); | 254 SkAutoTMalloc<char> storage(size); |
255 char* array = storage.get(); | 255 char* array = storage.get(); |
256 size_t bytesRead = stream->read(array, size); | 256 size_t bytesRead = stream->read(array, size); |
257 REPORTER_ASSERT(reporter, bytesRead == size); | 257 REPORTER_ASSERT(reporter, bytesRead == size); |
258 check_abcs(reporter, array, size); | 258 check_abcs(reporter, array, size); |
(...skipping 18 matching lines...) Expand all Loading... |
277 size_t offset = 0; | 277 size_t offset = 0; |
278 do { | 278 do { |
279 SkASSERT(offset + iter.size() <= size); | 279 SkASSERT(offset + iter.size() <= size); |
280 memcpy(storage.get() + offset, iter.data(), iter.size()); | 280 memcpy(storage.get() + offset, iter.data(), iter.size()); |
281 offset += iter.size(); | 281 offset += iter.size(); |
282 } while (iter.next()); | 282 } while (iter.next()); |
283 REPORTER_ASSERT(reporter, offset == size); | 283 REPORTER_ASSERT(reporter, offset == size); |
284 check_abcs(reporter, storage.get(), size); | 284 check_abcs(reporter, storage.get(), size); |
285 } | 285 } |
286 | 286 |
| 287 #include "SkTaskGroup.h" |
| 288 |
287 DEF_TEST(RWBuffer, reporter) { | 289 DEF_TEST(RWBuffer, reporter) { |
288 // Knowing that the default capacity is 4096, choose N large enough so we fo
rce it to use | 290 // Knowing that the default capacity is 4096, choose N large enough so we fo
rce it to use |
289 // multiple buffers internally. | 291 // multiple buffers internally. |
290 const int N = 1000; | 292 const int N = 1000; |
291 SkROBuffer* readers[N]; | |
292 SkStream* streams[N]; | |
293 | 293 |
294 { | 294 { |
| 295 SkTaskGroup tasks; |
295 SkRWBuffer buffer; | 296 SkRWBuffer buffer; |
296 for (int i = 0; i < N; ++i) { | 297 for (int i = 0; i < N; ++i) { |
297 buffer.append(gABC, 26); | 298 buffer.append(gABC, 26); |
298 readers[i] = buffer.newRBufferSnapshot(); | 299 sk_sp<SkROBuffer> reader = sk_sp<SkROBuffer>(buffer.newRBufferSnapsh
ot()); |
299 streams[i] = buffer.newStreamSnapshot(); | 300 SkStream* stream = buffer.newStreamSnapshot(); |
| 301 REPORTER_ASSERT(reporter, reader->size() == buffer.size()); |
| 302 REPORTER_ASSERT(reporter, stream->getLength() == buffer.size()); |
| 303 |
| 304 // reader's copy constructor will ref the SkROBuffer, which will be
unreffed |
| 305 // when the task ends. |
| 306 // Ownership of stream is passed to the task, which will delete it. |
| 307 tasks.add([reporter, i, reader, stream] { |
| 308 REPORTER_ASSERT(reporter, (i + 1) * 26U == reader->size()); |
| 309 REPORTER_ASSERT(reporter, stream->getLength() == reader->size())
; |
| 310 check_alphabet_buffer(reporter, reader.get()); |
| 311 check_alphabet_stream(reporter, stream); |
| 312 REPORTER_ASSERT(reporter, stream->rewind()); |
| 313 delete stream; |
| 314 }); |
300 } | 315 } |
301 REPORTER_ASSERT(reporter, N*26 == buffer.size()); | 316 REPORTER_ASSERT(reporter, N*26 == buffer.size()); |
302 } | 317 tasks.wait(); |
303 | |
304 for (int i = 0; i < N; ++i) { | |
305 REPORTER_ASSERT(reporter, (i + 1) * 26U == readers[i]->size()); | |
306 check_alphabet_buffer(reporter, readers[i]); | |
307 check_alphabet_stream(reporter, streams[i]); | |
308 readers[i]->unref(); | |
309 delete streams[i]; | |
310 } | 318 } |
311 } | 319 } |
312 | 320 |
313 // Tests that it is safe to call SkROBuffer::Iter::size() when exhausted. | 321 // Tests that it is safe to call SkROBuffer::Iter::size() when exhausted. |
314 DEF_TEST(RWBuffer_size, r) { | 322 DEF_TEST(RWBuffer_size, r) { |
315 SkRWBuffer buffer; | 323 SkRWBuffer buffer; |
316 buffer.append(gABC, 26); | 324 buffer.append(gABC, 26); |
317 | 325 |
318 SkAutoTUnref<SkROBuffer> roBuffer(buffer.newRBufferSnapshot()); | 326 SkAutoTUnref<SkROBuffer> roBuffer(buffer.newRBufferSnapshot()); |
319 SkROBuffer::Iter iter(roBuffer); | 327 SkROBuffer::Iter iter(roBuffer); |
320 REPORTER_ASSERT(r, iter.data()); | 328 REPORTER_ASSERT(r, iter.data()); |
321 REPORTER_ASSERT(r, iter.size() == 26); | 329 REPORTER_ASSERT(r, iter.size() == 26); |
322 | 330 |
323 // There is only one block in this buffer. | 331 // There is only one block in this buffer. |
324 REPORTER_ASSERT(r, !iter.next()); | 332 REPORTER_ASSERT(r, !iter.next()); |
325 REPORTER_ASSERT(r, 0 == iter.size()); | 333 REPORTER_ASSERT(r, 0 == iter.size()); |
326 } | 334 } |
327 | 335 |
328 // Tests that it is safe to destruct an SkRWBuffer without appending | 336 // Tests that operations (including the destructor) are safe on an SkRWBuffer |
329 // anything to it. | 337 // without any data appended. |
330 DEF_TEST(RWBuffer_noAppend, r) { | 338 DEF_TEST(RWBuffer_noAppend, r) { |
331 SkRWBuffer buffer; | 339 SkRWBuffer buffer; |
332 REPORTER_ASSERT(r, 0 == buffer.size()); | 340 REPORTER_ASSERT(r, 0 == buffer.size()); |
| 341 |
| 342 sk_sp<SkROBuffer> roBuffer = sk_sp<SkROBuffer>(buffer.newRBufferSnapshot()); |
| 343 REPORTER_ASSERT(r, roBuffer); |
| 344 if (roBuffer) { |
| 345 REPORTER_ASSERT(r, roBuffer->size() == 0); |
| 346 SkROBuffer::Iter iter(roBuffer.get()); |
| 347 REPORTER_ASSERT(r, iter.size() == 0); |
| 348 REPORTER_ASSERT(r, !iter.data()); |
| 349 REPORTER_ASSERT(r, !iter.next()); |
| 350 } |
| 351 |
| 352 SkAutoTDelete<SkStream> stream(buffer.newStreamSnapshot()); |
| 353 REPORTER_ASSERT(r, stream); |
| 354 if (stream) { |
| 355 REPORTER_ASSERT(r, stream->hasLength()); |
| 356 REPORTER_ASSERT(r, stream->getLength() == 0); |
| 357 REPORTER_ASSERT(r, stream->skip(10) == 0); |
| 358 } |
333 } | 359 } |
OLD | NEW |