1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "Test.h"
9
10 // Include the implementation so we can make an appropriate template instance.
11 #include "SkAdvancedTypefaceMetrics.h"
12
13 using namespace skia_advanced_typeface_metrics_utils;
14
15 namespace {
16
17 // Negative values and zeros in a range plus trailing zeros.
18 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
19 const int16_t data1[] = {-1, 0, -3, 4, 5, 6, 7, 0, 0, 0, 8, 0, 0, 0, 0};
20 const char* expected1 = "0[-1 0 -3 4 5 6 7 0 0 0 8]";
21
22 // Run with leading and trailing zeros.
23 // Test rules: d 0 1 2 3 4 5 6 7 8 9 10 11
24 const int16_t data2[] = {0, 0, 0, 100, 100, 100, 100, 100, 100, 100, 0, 0};
25 const char* expected2 = "3 9 100";
26
27 // Removing 0's from a range.
28 // Test rules: a 0 1 2 3 4 5 6 7 8 9 10 11
29 const int16_t data3[] = {1, 2, 0, 0, 0, 3, 4, 0, 0, 0, 0, 5};
30 const char* expected3 = "0[1 2 0 0 0 3 4] 11[5]";
31
32 // Removing 0's from a run/range and between runs.
33 // Test rules: a, b 0 1 2 3 4 5 6 7 8 9 10 11 12 14 15
34 const int16_t data4[] = {1, 0, 0, 0, 1, 2, 2, 2, 3, 0, 0, 0, 0, 3, 4};
35 const char* expected4 = "0[1 0 0 0 1] 5 7 2 8[3] 13[3 4]";
36
37 // Runs that starts outside a range.
38 // Test rules: a, e 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
39 const int16_t data5[] = {1, 1, 2, 3, 0, 0, 0, 0, 5, 5, 6, 7, 0, 0, 0, 0, 8, 0};
40 const char* expected5 = "0 1 1 2[2 3] 8 9 5 10[6 7] 16[8]";
41
42 // Zeros and runs that should be broken out.
43 // Test rules: a, b, e 0 1 2 3 4 5 6 7 8 9 10 11 12 13
44 const int16_t data6[] = {1, 0, 0, 0, 0, 1, 2, 3, 3, 4, 5, 5, 5, 6};
45 const char* expected6 = "0[1] 5[1 2 3 3 4] 10 12 5 13[6]";
46
47 // Don't cares that aren't enough to break out a run.
48 // Test rules: c 0 1 2 3 4 5
49 const int16_t data7[] = {1, 2, 10, 11, 2, 3};
50 const char* expected7 = "0[1 2 10 11 2 3]";
51 const uint32_t subset7[] = {0, 1, 4, 5};
52 const char* expectedSubset7 = "0[1 2 0 0 2 3]";
53
54 // Don't cares that are enough to break out a run.
55 // Test rules: c 0 1 2 3 4 5 6
56 const int16_t data8[] = {1, 2, 10, 11, 12, 2, 3};
57 const char* expected8 = "0[1 2 10 11 12 2 3]";
58 const uint32_t subset8[] = {0, 1, 5, 6};
59 const char* expectedSubset8 = "0[1] 1 5 2 6[3]";
60
61 // Leading don't cares.
62 // Test rules: d 0 1 2 3 4
63 const int16_t data9[] = {1, 1, 10, 2, 3};
64 const char* expected9 = "0 1 1 2[10 2 3]";
65 const uint32_t subset9[] = {0, 1, 3, 4};
66 const char* expectedSubset9 = "0 1 1 3[2 3]";
67
68 // Almost run of don't cares inside a range.
69 // Test rules: c 0 1 2 3 4 5
70 const int16_t data10[] = {1, 2, 10, 11, 12, 3};
71 const char* expected10 = "0[1 2 10 11 12 3]";
72 const uint32_t subset10[] = {0, 1, 5};
73 const char* expectedSubset10 = "0[1 2 0 0 0 3]";
74
75 // Run of don't cares inside a range.
76 // Test rules: c 0 1 2 3 4 5 6
77 const int16_t data11[] = {1, 2, 10, 11, 12, 13, 3};
78 const char* expected11 = "0[1 2 10 11 12 13 3]";
79 const uint32_t subset11[] = {0, 1, 6};
80 const char* expectedSubset11 = "0[1 2] 6[3]";
81
82 // Almost run within a range with leading don't cares.
83 // Test rules: c 0 1 2 3 4 5 6
84 const int16_t data12[] = {1, 10, 11, 2, 12, 13, 3};
85 const char* expected12 = "0[1 10 11 2 12 13 3]";
86 const uint32_t subset12[] = {0, 3, 6};
87 const char* expectedSubset12 = "0[1 0 0 2 0 0 3]";
88
89 // Run within a range with leading don't cares.
90 // Test rules: c 0 1 2 3 4 5 6 7
91 const int16_t data13[] = {1, 10, 11, 2, 2, 12, 13, 3};
92 const char* expected13 = "0[1 10 11 2 2 12 13 3]";
93 const uint32_t subset13[] = {0, 3, 4, 7};
94 const char* expectedSubset13 = "0[1] 1 6 2 7[3]";
95
96 // Enough don't cares to breakup something.
97 // Test rules: a 0 1 2 3 4 5
98 const int16_t data14[] = {1, 0, 0, 0, 0, 2};
99 const char* expected14 = "0[1] 5[2]";
100 const uint32_t subset14[] = {0, 5};
101 const char* expectedSubset14 = "0[1] 5[2]";
102
103 }
104
stringify_advance_data(SkAdvancedTypefaceMetrics::AdvanceMetric<int16_t> * data)105 static SkString stringify_advance_data(SkAdvancedTypefaceMetrics::AdvanceMetric<int16_t>* data) {
106 SkString result;
107 bool leadingSpace = false;
108 while (data != NULL) {
109 if (leadingSpace) {
110 result.append(" ");
111 } else {
112 leadingSpace = true;
113 }
114 switch(data->fType) {
115 case SkAdvancedTypefaceMetrics::AdvanceMetric<int16_t>::kRun:
116 result.appendf("%d %d %d", data->fStartId, data->fEndId, data->fAdvance[0]);
117 break;
118 case SkAdvancedTypefaceMetrics::AdvanceMetric<int16_t>::kRange:
119 result.appendf("%d[", data->fStartId);
120 for (int i = 0; i < data->fAdvance.count(); ++i) {
121 if (i > 0) {
122 result.append(" ");
123 }
124 result.appendf("%d", data->fAdvance[i]);
125 }
126 result.append("]");
127 break;
128 case SkAdvancedTypefaceMetrics::AdvanceMetric<int16_t>::kDefault:
129 result.appendf("<Default=%d>", data->fAdvance[0]);
130 break;
131 }
132 data = data->fNext.get();
133 }
134 return result;
135 }
136
137 class TestWData {
138 public:
TestWData(skiatest::Reporter * reporter,const int16_t advances[],int advanceLen,const uint32_t subset[],int subsetLen,const char * expected)139 TestWData(skiatest::Reporter* reporter,
140 const int16_t advances[], int advanceLen,
141 const uint32_t subset[], int subsetLen,
142 const char* expected)
143 : fAdvances(advances)
144 , fAdvancesLen(advanceLen)
145 , fSubset(subset)
146 , fSubsetLen(subsetLen)
147 , fExpected(expected) {
148 REPORTER_ASSERT(reporter, RunTest());
149 }
150
151 private:
152 const int16_t* fAdvances;
153 const int fAdvancesLen;
154 const uint32_t* fSubset;
155 const int fSubsetLen;
156 const char* fExpected;
157
getAdvance(void * tc,int gId,int16_t * advance)158 static bool getAdvance(void* tc, int gId, int16_t* advance) {
159 TestWData* testCase = (TestWData*)tc;
160 if (gId >= 0 && gId < testCase->fAdvancesLen) {
161 *advance = testCase->fAdvances[gId];
162 return true;
163 }
164 return false;
165 }
166
RunTest()167 bool RunTest() {
168 SkTScopedPtr<SkAdvancedTypefaceMetrics::AdvanceMetric<int16_t> > result;
169 result.reset(getAdvanceData((void*)this, fAdvancesLen, fSubset, fSubsetLen, getAdvance));
170
171 SkString stringResult = stringify_advance_data(result.get());
172 if (!stringResult.equals(fExpected)) {
173 printf("Expected: %s\n Result: %s\n", fExpected, stringResult.c_str());
174 return false;
175 }
176 return true;
177 }
178 };
179
TestWArray(skiatest::Reporter * reporter)180 static void TestWArray(skiatest::Reporter* reporter) {
181 TestWData(reporter, data1, SK_ARRAY_COUNT(data1), NULL, 0, expected1);
182 TestWData(reporter, data2, SK_ARRAY_COUNT(data2), NULL, 0, expected2);
183 TestWData(reporter, data3, SK_ARRAY_COUNT(data3), NULL, 0, expected3);
184 TestWData(reporter, data4, SK_ARRAY_COUNT(data4), NULL, 0, expected4);
185 TestWData(reporter, data5, SK_ARRAY_COUNT(data5), NULL, 0, expected5);
186 TestWData(reporter, data6, SK_ARRAY_COUNT(data6), NULL, 0, expected6);
187 TestWData(reporter, data7, SK_ARRAY_COUNT(data7), NULL, 0, expected7);
188 TestWData(reporter, data7, SK_ARRAY_COUNT(data7), subset7,
189 SK_ARRAY_COUNT(subset7), expectedSubset7);
190 TestWData(reporter, data8, SK_ARRAY_COUNT(data8), NULL, 0, expected8);
191 TestWData(reporter, data8, SK_ARRAY_COUNT(data8), subset8,
192 SK_ARRAY_COUNT(subset8), expectedSubset8);
193 TestWData(reporter, data9, SK_ARRAY_COUNT(data9), NULL, 0, expected9);
194 TestWData(reporter, data9, SK_ARRAY_COUNT(data9), subset9,
195 SK_ARRAY_COUNT(subset9), expectedSubset9);
196 TestWData(reporter, data10, SK_ARRAY_COUNT(data10), NULL, 0, expected10);
197 TestWData(reporter, data10, SK_ARRAY_COUNT(data10), subset10,
198 SK_ARRAY_COUNT(subset10), expectedSubset10);
199 TestWData(reporter, data11, SK_ARRAY_COUNT(data11), NULL, 0, expected11);
200 TestWData(reporter, data11, SK_ARRAY_COUNT(data11), subset11,
201 SK_ARRAY_COUNT(subset11), expectedSubset11);
202 TestWData(reporter, data12, SK_ARRAY_COUNT(data12), NULL, 0, expected12);
203 TestWData(reporter, data12, SK_ARRAY_COUNT(data12), subset12,
204 SK_ARRAY_COUNT(subset12), expectedSubset12);
205 TestWData(reporter, data13, SK_ARRAY_COUNT(data13), NULL, 0, expected13);
206 TestWData(reporter, data13, SK_ARRAY_COUNT(data13), subset13,
207 SK_ARRAY_COUNT(subset13), expectedSubset13);
208 TestWData(reporter, data14, SK_ARRAY_COUNT(data14), NULL, 0, expected14);
209 TestWData(reporter, data14, SK_ARRAY_COUNT(data14), subset14,
210 SK_ARRAY_COUNT(subset14), expectedSubset14);
211 }
212
213 #include "TestClassDef.h"
214 DEFINE_TESTCLASS("WArray", WArrayTest, TestWArray)
215