• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #include "Test.h"
9 #include "SkData.h"
10 
11 static void* gGlobal;
12 
delete_int_proc(const void * ptr,size_t len,void * context)13 static void delete_int_proc(const void* ptr, size_t len, void* context) {
14     int* data = (int*)ptr;
15     SkASSERT(context == gGlobal);
16     delete[] data;
17 }
18 
assert_len(skiatest::Reporter * reporter,SkData * ref,size_t len)19 static void assert_len(skiatest::Reporter* reporter, SkData* ref, size_t len) {
20     REPORTER_ASSERT(reporter, ref->size() == len);
21 }
22 
assert_data(skiatest::Reporter * reporter,SkData * ref,const void * data,size_t len)23 static void assert_data(skiatest::Reporter* reporter, SkData* ref,
24                         const void* data, size_t len) {
25     REPORTER_ASSERT(reporter, ref->size() == len);
26     REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
27 }
28 
TestDataRef(skiatest::Reporter * reporter)29 void TestDataRef(skiatest::Reporter* reporter) {
30     const char* str = "We the people, in order to form a more perfect union.";
31     const int N = 10;
32 
33     SkData* r0 = SkData::NewEmpty();
34     SkData* r1 = SkData::NewWithCopy(str, strlen(str));
35     SkData* r2 = SkData::NewWithProc(new int[N], N*sizeof(int),
36                                            delete_int_proc, gGlobal);
37     SkData* r3 = SkData::NewSubset(r1, 7, 6);
38 
39     SkAutoUnref aur0(r0);
40     SkAutoUnref aur1(r1);
41     SkAutoUnref aur2(r2);
42     SkAutoUnref aur3(r3);
43 
44     assert_len(reporter, r0, 0);
45     assert_len(reporter, r1, strlen(str));
46     assert_len(reporter, r2, N * sizeof(int));
47     assert_len(reporter, r3, 6);
48 
49     assert_data(reporter, r1, str, strlen(str));
50     assert_data(reporter, r3, "people", 6);
51 
52     SkData* tmp = SkData::NewSubset(r1, strlen(str), 10);
53     assert_len(reporter, tmp, 0);
54     tmp->unref();
55     tmp = SkData::NewSubset(r1, 0, 0);
56     assert_len(reporter, tmp, 0);
57     tmp->unref();
58 }
59 
60 #include "TestClassDef.h"
61 DEFINE_TESTCLASS("DataRef", DataRefTestClass, TestDataRef)
62