blob: 8505fa96a04ae7e7f3810e7ad933b0a220d58f95 [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.com91bd4592012-07-11 17:24:49 +000010#include "SkDataSet.h"
11#include "SkStream.h"
12
13template <typename T> class SkTUnref {
14public:
15 SkTUnref(T* ref) : fRef(ref) {}
16 ~SkTUnref() { fRef->unref(); }
17
18 operator T*() { return fRef; }
19 operator const T*() { return fRef; }
20
21private:
22 T* fRef;
23};
24
25static void unrefAll(const SkDataSet::Pair pairs[], int count) {
26 for (int i = 0; i < count; ++i) {
27 pairs[i].fValue->unref();
28 }
29}
30
31// asserts that inner is a subset of outer
32static void test_dataset_subset(skiatest::Reporter* reporter,
33 const SkDataSet& outer, const SkDataSet& inner) {
34 SkDataSet::Iter iter(inner);
35 for (; !iter.done(); iter.next()) {
36 SkData* outerData = outer.find(iter.key());
37 REPORTER_ASSERT(reporter, outerData);
38 REPORTER_ASSERT(reporter, outerData->equals(iter.value()));
39 }
40}
41
42static void test_datasets_equal(skiatest::Reporter* reporter,
43 const SkDataSet& ds0, const SkDataSet& ds1) {
44 REPORTER_ASSERT(reporter, ds0.count() == ds1.count());
45
46 test_dataset_subset(reporter, ds0, ds1);
47 test_dataset_subset(reporter, ds1, ds0);
48}
49
50static void test_dataset(skiatest::Reporter* reporter, const SkDataSet& ds,
51 int count) {
52 REPORTER_ASSERT(reporter, ds.count() == count);
53
54 SkDataSet::Iter iter(ds);
55 int index = 0;
56 for (; !iter.done(); iter.next()) {
57 const char* name = iter.key();
58 SkData* data = iter.value();
reed@google.com79382af2012-07-11 18:55:23 +000059// SkDebugf("[%d] %s:%s\n", index, name, (const char*)data->bytes());
reed@google.com91bd4592012-07-11 17:24:49 +000060 index += 1;
61 }
62 REPORTER_ASSERT(reporter, index == count);
63
64 SkDynamicMemoryWStream ostream;
65 ds.writeToStream(&ostream);
66 SkMemoryStream istream;
67 istream.setData(ostream.copyToData())->unref();
68 SkDataSet copy(&istream);
69
70 test_datasets_equal(reporter, ds, copy);
71}
72
73static void test_dataset(skiatest::Reporter* reporter) {
74 SkDataSet set0(NULL, 0);
75 SkDataSet set1("hello", SkTUnref<SkData>(SkData::NewWithCString("world")));
76
77 const SkDataSet::Pair pairs[] = {
78 { "one", SkData::NewWithCString("1") },
79 { "two", SkData::NewWithCString("2") },
80 { "three", SkData::NewWithCString("3") },
81 };
82 SkDataSet set3(pairs, 3);
83 unrefAll(pairs, 3);
84
85 test_dataset(reporter, set0, 0);
86 test_dataset(reporter, set1, 1);
87 test_dataset(reporter, set3, 3);
88}
reed@google.com1622c992011-06-14 19:22:21 +000089
90static void* gGlobal;
91
92static void delete_int_proc(const void* ptr, size_t len, void* context) {
93 int* data = (int*)ptr;
94 SkASSERT(context == gGlobal);
95 delete[] data;
96}
97
reed@google.com8d0b5772011-06-24 13:07:31 +000098static void assert_len(skiatest::Reporter* reporter, SkData* ref, size_t len) {
reed@google.com1622c992011-06-14 19:22:21 +000099 REPORTER_ASSERT(reporter, ref->size() == len);
100}
101
reed@google.com8d0b5772011-06-24 13:07:31 +0000102static void assert_data(skiatest::Reporter* reporter, SkData* ref,
reed@google.com1622c992011-06-14 19:22:21 +0000103 const void* data, size_t len) {
104 REPORTER_ASSERT(reporter, ref->size() == len);
105 REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
106}
107
reed@google.comdbc936d2012-06-28 15:40:09 +0000108static void test_cstring(skiatest::Reporter* reporter) {
109 const char str[] = "Hello world";
110 size_t len = strlen(str);
111
reed@google.coma63a8512012-07-02 20:29:00 +0000112 SkAutoTUnref<SkData> r0(SkData::NewWithCopy(str, len + 1));
reed@google.comdbc936d2012-06-28 15:40:09 +0000113 SkAutoTUnref<SkData> r1(SkData::NewWithCString(str));
114
115 REPORTER_ASSERT(reporter, r0->equals(r1));
116
117 SkAutoTUnref<SkData> r2(SkData::NewWithCString(NULL));
reed@google.coma63a8512012-07-02 20:29:00 +0000118 REPORTER_ASSERT(reporter, 1 == r2->size());
119 REPORTER_ASSERT(reporter, 0 == *r2->bytes());
reed@google.comdbc936d2012-06-28 15:40:09 +0000120}
121
reed@google.com91bd4592012-07-11 17:24:49 +0000122static void TestData(skiatest::Reporter* reporter) {
reed@google.com1622c992011-06-14 19:22:21 +0000123 const char* str = "We the people, in order to form a more perfect union.";
124 const int N = 10;
125
reed@google.comdbc936d2012-06-28 15:40:09 +0000126 SkAutoTUnref<SkData> r0(SkData::NewEmpty());
127 SkAutoTUnref<SkData> r1(SkData::NewWithCopy(str, strlen(str)));
128 SkAutoTUnref<SkData> r2(SkData::NewWithProc(new int[N], N*sizeof(int),
129 delete_int_proc, gGlobal));
130 SkAutoTUnref<SkData> r3(SkData::NewSubset(r1, 7, 6));
reed@google.com1622c992011-06-14 19:22:21 +0000131
reed@google.com1622c992011-06-14 19:22:21 +0000132 assert_len(reporter, r0, 0);
133 assert_len(reporter, r1, strlen(str));
134 assert_len(reporter, r2, N * sizeof(int));
135 assert_len(reporter, r3, 6);
136
137 assert_data(reporter, r1, str, strlen(str));
138 assert_data(reporter, r3, "people", 6);
139
reed@google.com8d0b5772011-06-24 13:07:31 +0000140 SkData* tmp = SkData::NewSubset(r1, strlen(str), 10);
reed@google.com1622c992011-06-14 19:22:21 +0000141 assert_len(reporter, tmp, 0);
142 tmp->unref();
reed@google.com8d0b5772011-06-24 13:07:31 +0000143 tmp = SkData::NewSubset(r1, 0, 0);
reed@google.com1622c992011-06-14 19:22:21 +0000144 assert_len(reporter, tmp, 0);
145 tmp->unref();
reed@google.comdbc936d2012-06-28 15:40:09 +0000146
147 test_cstring(reporter);
reed@google.com91bd4592012-07-11 17:24:49 +0000148 test_dataset(reporter);
reed@google.com1622c992011-06-14 19:22:21 +0000149}
150
151#include "TestClassDef.h"
reed@google.com91bd4592012-07-11 17:24:49 +0000152DEFINE_TESTCLASS("Data", DataTestClass, TestData)