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

Side by Side Diff: tests/DataRefTest.cpp

Issue 1106113002: SkRWBuffer for thread-safe 'stream' sharing (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: make header private (for now at least) Created 5 years, 7 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
« no previous file with comments | « src/core/SkRWBuffer.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 "SkData.h" 8 #include "SkData.h"
9 #include "SkDataTable.h" 9 #include "SkDataTable.h"
10 #include "SkOSFile.h" 10 #include "SkOSFile.h"
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 SkData* tmp = SkData::NewSubset(r1, strlen(str), 10); 225 SkData* tmp = SkData::NewSubset(r1, strlen(str), 10);
226 assert_len(reporter, tmp, 0); 226 assert_len(reporter, tmp, 0);
227 tmp->unref(); 227 tmp->unref();
228 tmp = SkData::NewSubset(r1, 0, 0); 228 tmp = SkData::NewSubset(r1, 0, 0);
229 assert_len(reporter, tmp, 0); 229 assert_len(reporter, tmp, 0);
230 tmp->unref(); 230 tmp->unref();
231 231
232 test_cstring(reporter); 232 test_cstring(reporter);
233 test_files(reporter); 233 test_files(reporter);
234 } 234 }
235
236 //////////////////////////////////////////////////////////////////////////////// ///////////////////
237 #include "SkRWBuffer.h"
238
239 const char gABC[] = "abcdefghijklmnopqrstuvwxyz";
240
241 static void check_abcs(skiatest::Reporter* reporter, const char buffer[], size_t size) {
242 REPORTER_ASSERT(reporter, size % 26 == 0);
243 for (size_t offset = 0; offset < size; offset += 26) {
244 REPORTER_ASSERT(reporter, !memcmp(&buffer[offset], gABC, 26));
245 }
246 }
247
248 // stream should contains an integral number of copies of gABC.
249 static void check_alphabet_stream(skiatest::Reporter* reporter, SkStream* stream ) {
250 REPORTER_ASSERT(reporter, stream->hasLength());
251 size_t size = stream->getLength();
252 REPORTER_ASSERT(reporter, size % 26 == 0);
253
254 SkAutoTMalloc<char> storage(size);
255 char* array = storage.get();
256 size_t bytesRead = stream->read(array, size);
257 REPORTER_ASSERT(reporter, bytesRead == size);
258 check_abcs(reporter, array, size);
259
260 // try checking backwards
261 for (size_t offset = size; offset > 0; offset -= 26) {
262 REPORTER_ASSERT(reporter, stream->seek(offset - 26));
263 REPORTER_ASSERT(reporter, stream->getPosition() == offset - 26);
264 REPORTER_ASSERT(reporter, stream->read(array, 26) == 26);
265 check_abcs(reporter, array, 26);
266 REPORTER_ASSERT(reporter, stream->getPosition() == offset);
267 }
268 }
269
270 // reader should contains an integral number of copies of gABC.
271 static void check_alphabet_buffer(skiatest::Reporter* reporter, const SkROBuffer * reader) {
272 size_t size = reader->size();
273 REPORTER_ASSERT(reporter, size % 26 == 0);
274
275 SkAutoTMalloc<char> storage(size);
276 SkROBuffer::Iter iter(reader);
277 size_t offset = 0;
278 do {
279 SkASSERT(offset + iter.size() <= size);
280 memcpy(storage.get() + offset, iter.data(), iter.size());
281 offset += iter.size();
282 } while (iter.next());
283 REPORTER_ASSERT(reporter, offset == size);
284 check_abcs(reporter, storage.get(), size);
285 }
286
287 DEF_TEST(RWBuffer, reporter) {
288 // Knowing that the default capacity is 4096, choose N large enough so we fo rce it to use
289 // multiple buffers internally.
290 const int N = 1000;
291 SkROBuffer* readers[N];
292 SkStream* streams[N];
293
294 {
295 SkRWBuffer buffer;
296 for (int i = 0; i < N; ++i) {
297 if (0 == (i & 1)) {
298 buffer.append(gABC, 26);
299 } else {
300 memcpy(buffer.append(26), gABC, 26);
301 }
302 readers[i] = buffer.newRBufferSnapshot();
303 streams[i] = buffer.newStreamSnapshot();
304 }
305 REPORTER_ASSERT(reporter, N*26 == buffer.size());
306 }
307
308 for (int i = 0; i < N; ++i) {
309 REPORTER_ASSERT(reporter, (i + 1) * 26U == readers[i]->size());
310 check_alphabet_buffer(reporter, readers[i]);
311 check_alphabet_stream(reporter, streams[i]);
312 readers[i]->unref();
313 SkDELETE(streams[i]);
314 }
315 }
OLDNEW
« no previous file with comments | « src/core/SkRWBuffer.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698