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

Side by Side Diff: tests/DataRefTest.cpp

Issue 15675025: One allocation for an SkData which makes a copy. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Clean Created 7 years, 6 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 | Annotate | Revision Log
« include/core/SkWeakRefCnt.h ('K') | « src/pdf/SkPDFFont.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 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 #include "Test.h" 8 #include "Test.h"
9 #include "SkData.h" 9 #include "SkData.h"
10 #include "SkDataSet.h" 10 #include "SkDataSet.h"
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 SkDataSet set3(pairs, 3); 222 SkDataSet set3(pairs, 3);
223 unrefAll(pairs, 3); 223 unrefAll(pairs, 3);
224 224
225 test_dataset(reporter, set0, 0); 225 test_dataset(reporter, set0, 0);
226 test_dataset(reporter, set1, 1); 226 test_dataset(reporter, set1, 1);
227 test_dataset(reporter, set3, 3); 227 test_dataset(reporter, set3, 3);
228 } 228 }
229 229
230 static void* gGlobal; 230 static void* gGlobal;
231 231
232 static void delete_int_proc(const void* ptr, size_t len, void* context) { 232 struct DeleteIntFunctor {
233 int* data = (int*)ptr; 233 explicit DeleteIntFunctor(void* context) : fContext(context) { }
234 SkASSERT(context == gGlobal); 234 void operator()(const void* ptr, size_t len) const {
235 delete[] data; 235 int* data = (int*)ptr;
236 } 236 SkASSERT(fContext == gGlobal);
237 delete[] data;
238 }
239 private:
240 void* fContext;
241 };
237 242
238 static void assert_len(skiatest::Reporter* reporter, SkData* ref, size_t len) { 243 static void assert_len(skiatest::Reporter* reporter, SkData* ref, size_t len) {
239 REPORTER_ASSERT(reporter, ref->size() == len); 244 REPORTER_ASSERT(reporter, ref->size() == len);
240 } 245 }
241 246
242 static void assert_data(skiatest::Reporter* reporter, SkData* ref, 247 static void assert_data(skiatest::Reporter* reporter, SkData* ref,
243 const void* data, size_t len) { 248 const void* data, size_t len) {
244 REPORTER_ASSERT(reporter, ref->size() == len); 249 REPORTER_ASSERT(reporter, ref->size() == len);
245 REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len)); 250 REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
246 } 251 }
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 2 6) == 0); 297 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 2 6) == 0);
293 } 298 }
294 299
295 static void TestData(skiatest::Reporter* reporter) { 300 static void TestData(skiatest::Reporter* reporter) {
296 const char* str = "We the people, in order to form a more perfect union."; 301 const char* str = "We the people, in order to form a more perfect union.";
297 const int N = 10; 302 const int N = 10;
298 303
299 SkAutoTUnref<SkData> r0(SkData::NewEmpty()); 304 SkAutoTUnref<SkData> r0(SkData::NewEmpty());
300 SkAutoTUnref<SkData> r1(SkData::NewWithCopy(str, strlen(str))); 305 SkAutoTUnref<SkData> r1(SkData::NewWithCopy(str, strlen(str)));
301 SkAutoTUnref<SkData> r2(SkData::NewWithProc(new int[N], N*sizeof(int), 306 SkAutoTUnref<SkData> r2(SkData::NewWithProc(new int[N], N*sizeof(int),
302 delete_int_proc, gGlobal)); 307 DeleteIntFunctor(gGlobal)));
303 SkAutoTUnref<SkData> r3(SkData::NewSubset(r1, 7, 6)); 308 SkAutoTUnref<SkData> r3(SkData::NewSubset(r1, 7, 6));
304 309
305 assert_len(reporter, r0, 0); 310 assert_len(reporter, r0, 0);
306 assert_len(reporter, r1, strlen(str)); 311 assert_len(reporter, r1, strlen(str));
307 assert_len(reporter, r2, N * sizeof(int)); 312 assert_len(reporter, r2, N * sizeof(int));
308 assert_len(reporter, r3, 6); 313 assert_len(reporter, r3, 6);
309 314
310 assert_data(reporter, r1, str, strlen(str)); 315 assert_data(reporter, r1, str, strlen(str));
311 assert_data(reporter, r3, "people", 6); 316 assert_data(reporter, r3, "people", 6);
312 317
313 SkData* tmp = SkData::NewSubset(r1, strlen(str), 10); 318 SkData* tmp = SkData::NewSubset(r1, strlen(str), 10);
314 assert_len(reporter, tmp, 0); 319 assert_len(reporter, tmp, 0);
315 tmp->unref(); 320 tmp->unref();
316 tmp = SkData::NewSubset(r1, 0, 0); 321 tmp = SkData::NewSubset(r1, 0, 0);
317 assert_len(reporter, tmp, 0); 322 assert_len(reporter, tmp, 0);
318 tmp->unref(); 323 tmp->unref();
319 324
320 test_cstring(reporter); 325 test_cstring(reporter);
321 test_dataset(reporter); 326 test_dataset(reporter);
322 test_files(reporter); 327 test_files(reporter);
323 } 328 }
324 329
325 #include "TestClassDef.h" 330 #include "TestClassDef.h"
326 DEFINE_TESTCLASS("Data", DataTestClass, TestData) 331 DEFINE_TESTCLASS("Data", DataTestClass, TestData)
327 DEFINE_TESTCLASS("DataTable", DataTableTestClass, TestDataTable) 332 DEFINE_TESTCLASS("DataTable", DataTableTestClass, TestDataTable)
OLDNEW
« include/core/SkWeakRefCnt.h ('K') | « src/pdf/SkPDFFont.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698