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

Side by Side Diff: tests/DataRefTest.cpp

Issue 14188049: add SkDataTable, to efficiently store an immutable array. Includes a builder (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 8 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
« no previous file with comments | « src/core/SkDataTable.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"
11 #include "SkDataTable.h"
11 #include "SkStream.h" 12 #include "SkStream.h"
12 13
13 template <typename T> class SkTUnref { 14 template <typename T> class SkTUnref {
14 public: 15 public:
15 SkTUnref(T* ref) : fRef(ref) {} 16 SkTUnref(T* ref) : fRef(ref) {}
16 ~SkTUnref() { fRef->unref(); } 17 ~SkTUnref() { fRef->unref(); }
17 18
18 operator T*() { return fRef; } 19 operator T*() { return fRef; }
19 operator const T*() { return fRef; } 20 operator const T*() { return fRef; }
20 21
21 private: 22 private:
22 T* fRef; 23 T* fRef;
23 }; 24 };
24 25
26 static void test_simpletable(skiatest::Reporter* reporter) {
27 const int idata[] = { 1, 4, 9, 16, 25, 63 };
28 int icount = SK_ARRAY_COUNT(idata);
29 SkAutoTUnref<SkDataTable> itable(SkDataTable::NewCopyArray(idata,
30 sizeof(idata[0]),
31 icount));
32 REPORTER_ASSERT(reporter, itable->count() == icount);
33 for (int i = 0; i < icount; ++i) {
34 size_t size;
35 REPORTER_ASSERT(reporter, sizeof(int) == itable->atSize(i));
36 REPORTER_ASSERT(reporter, *itable->atDataT<int>(i, &size) == idata[i]);
37 REPORTER_ASSERT(reporter, sizeof(int) == size);
38 }
39 }
40
41 static void test_vartable(skiatest::Reporter* reporter) {
42 const char* str[] = {
43 "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
44 };
45 int count = SK_ARRAY_COUNT(str);
46 size_t sizes[SK_ARRAY_COUNT(str)];
47 for (int i = 0; i < count; ++i) {
48 sizes[i] = strlen(str[i]) + 1;
49 }
50
51 SkAutoTUnref<SkDataTable> table(SkDataTable::NewCopyArrays(
52 (const void*const*)str, sizes, count));
53
54 REPORTER_ASSERT(reporter, table->count() == count);
55 for (int i = 0; i < count; ++i) {
56 size_t size;
57 REPORTER_ASSERT(reporter, table->atSize(i) == sizes[i]);
58 REPORTER_ASSERT(reporter, !strcmp(table->atDataT<const char>(i, &size),
59 str[i]));
60 REPORTER_ASSERT(reporter, size == sizes[i]);
61
62 const char* s = table->atStr(i);
63 REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
64 }
65 }
66
67 static void test_tablebuilder(skiatest::Reporter* reporter) {
68 const char* str[] = {
69 "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
70 };
71 int count = SK_ARRAY_COUNT(str);
72
73 SkDataTableBuilder builder(16);
74
75 for (int i = 0; i < count; ++i) {
76 builder.append(str[i], strlen(str[i]) + 1);
77 }
78 SkAutoTUnref<SkDataTable> table(builder.createDataTable());
79
80 REPORTER_ASSERT(reporter, table->count() == count);
81 for (int i = 0; i < count; ++i) {
82 size_t size;
83 REPORTER_ASSERT(reporter, table->atSize(i) == strlen(str[i]) + 1);
84 REPORTER_ASSERT(reporter, !strcmp(table->atDataT<const char>(i, &size),
85 str[i]));
86 REPORTER_ASSERT(reporter, size == strlen(str[i]) + 1);
87
88 const char* s = table->atStr(i);
89 REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
90 }
91 }
92
93 static void test_datatable(skiatest::Reporter* reporter) {
94 test_simpletable(reporter);
95 test_vartable(reporter);
96 test_tablebuilder(reporter);
97 }
98
25 static void unrefAll(const SkDataSet::Pair pairs[], int count) { 99 static void unrefAll(const SkDataSet::Pair pairs[], int count) {
26 for (int i = 0; i < count; ++i) { 100 for (int i = 0; i < count; ++i) {
27 pairs[i].fValue->unref(); 101 pairs[i].fValue->unref();
28 } 102 }
29 } 103 }
30 104
31 // asserts that inner is a subset of outer 105 // asserts that inner is a subset of outer
32 static void test_dataset_subset(skiatest::Reporter* reporter, 106 static void test_dataset_subset(skiatest::Reporter* reporter,
33 const SkDataSet& outer, const SkDataSet& inner) { 107 const SkDataSet& outer, const SkDataSet& inner) {
34 SkDataSet::Iter iter(inner); 108 SkDataSet::Iter iter(inner);
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 213
140 SkData* tmp = SkData::NewSubset(r1, strlen(str), 10); 214 SkData* tmp = SkData::NewSubset(r1, strlen(str), 10);
141 assert_len(reporter, tmp, 0); 215 assert_len(reporter, tmp, 0);
142 tmp->unref(); 216 tmp->unref();
143 tmp = SkData::NewSubset(r1, 0, 0); 217 tmp = SkData::NewSubset(r1, 0, 0);
144 assert_len(reporter, tmp, 0); 218 assert_len(reporter, tmp, 0);
145 tmp->unref(); 219 tmp->unref();
146 220
147 test_cstring(reporter); 221 test_cstring(reporter);
148 test_dataset(reporter); 222 test_dataset(reporter);
223 test_datatable(reporter);
149 } 224 }
150 225
151 #include "TestClassDef.h" 226 #include "TestClassDef.h"
152 DEFINE_TESTCLASS("Data", DataTestClass, TestData) 227 DEFINE_TESTCLASS("Data", DataTestClass, TestData)
OLDNEW
« no previous file with comments | « src/core/SkDataTable.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698