blob: 6a42485d3f3c56a57c3ceba3783c236bdc61157b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@google.com1622c992011-06-14 19:22:21 +00008#include "Test.h"
reed@google.com8d0b5772011-06-24 13:07:31 +00009#include "SkData.h"
reed@google.com1622c992011-06-14 19:22:21 +000010
11static void* gGlobal;
12
13static 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
reed@google.com8d0b5772011-06-24 13:07:31 +000019static void assert_len(skiatest::Reporter* reporter, SkData* ref, size_t len) {
reed@google.com1622c992011-06-14 19:22:21 +000020 REPORTER_ASSERT(reporter, ref->size() == len);
21}
22
reed@google.com8d0b5772011-06-24 13:07:31 +000023static void assert_data(skiatest::Reporter* reporter, SkData* ref,
reed@google.com1622c992011-06-14 19:22:21 +000024 const void* data, size_t len) {
25 REPORTER_ASSERT(reporter, ref->size() == len);
26 REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
27}
28
29void 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
reed@google.com8d0b5772011-06-24 13:07:31 +000033 SkData* r0 = SkData::NewEmpty();
34 SkData* r1 = SkData::NewWithCopy(str, strlen(str));
35 SkData* r2 = SkData::NewWithProc(new int[N], N*sizeof(int),
reed@google.com1622c992011-06-14 19:22:21 +000036 delete_int_proc, gGlobal);
reed@google.com8d0b5772011-06-24 13:07:31 +000037 SkData* r3 = SkData::NewSubset(r1, 7, 6);
reed@google.com1622c992011-06-14 19:22:21 +000038
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
reed@google.com8d0b5772011-06-24 13:07:31 +000052 SkData* tmp = SkData::NewSubset(r1, strlen(str), 10);
reed@google.com1622c992011-06-14 19:22:21 +000053 assert_len(reporter, tmp, 0);
54 tmp->unref();
reed@google.com8d0b5772011-06-24 13:07:31 +000055 tmp = SkData::NewSubset(r1, 0, 0);
reed@google.com1622c992011-06-14 19:22:21 +000056 assert_len(reporter, tmp, 0);
57 tmp->unref();
58}
59
60#include "TestClassDef.h"
61DEFINE_TESTCLASS("DataRef", DataRefTestClass, TestDataRef)