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.cpp"
12
13using namespace skia_advanced_typeface_metrics_utils;
14
15namespace {
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
19const int16_t data1[] = {-1, 0, -3, 4, 5, 6, 7, 0, 0, 0, 8, 0, 0, 0, 0};
20const 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
24const int16_t data2[] = {0, 0, 0, 100, 100, 100, 100, 100, 100, 100, 0, 0};
25const 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
29const int16_t data3[] = {1, 2, 0, 0, 0, 3, 4, 0, 0, 0, 0, 5};
30const 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
34const int16_t data4[] = {1, 0, 0, 0, 1, 2, 2, 2, 3, 0, 0, 0, 0, 3, 4};
35const 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
39const int16_t data5[] = {1, 1, 2, 3, 0, 0, 0, 0, 5, 5, 6, 7, 0, 0, 0, 0, 8, 0};
40const 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
44const int16_t data6[] = {1, 0, 0, 0, 0, 1, 2, 3, 3, 4, 5, 5, 5, 6};
45const 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
49const int16_t data7[] = {1, 2, 10, 11, 2, 3};
50const char* expected7 = "0[1 2 10 11 2 3]";
51const uint32_t subset7[] = {0, 1, 4, 5};
52const 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
56const int16_t data8[] = {1, 2, 10, 11, 12, 2, 3};
57const char* expected8 = "0[1 2 10 11 12 2 3]";
58const uint32_t subset8[] = {0, 1, 5, 6};
59const char* expectedSubset8 = "0[1] 1 5 2 6[3]";
60
61// Leading don't cares.
62// Test rules: d         0  1   2  3  4
63const int16_t data9[] = {1, 1, 10, 2, 3};
64const char* expected9 = "0 1 1 2[10 2 3]";
65const uint32_t subset9[] = {0, 1, 3, 4};
66const 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
70const int16_t data10[] = {1, 2, 10, 11, 12, 3};
71const char* expected10 = "0[1 2 10 11 12 3]";
72const uint32_t subset10[] = {0, 1, 5};
73const 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
77const int16_t data11[] = {1, 2, 10, 11, 12, 13, 3};
78const char* expected11 = "0[1 2 10 11 12 13 3]";
79const uint32_t subset11[] = {0, 1, 6};
80const 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
84const int16_t data12[] = {1, 10, 11, 2, 12, 13, 3};
85const char* expected12 = "0[1 10 11 2 12 13 3]";
86const uint32_t subset12[] = {0, 3, 6};
87const 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
91const int16_t data13[] = {1, 10, 11, 2, 2, 12, 13, 3};
92const char* expected13 = "0[1 10 11 2 2 12 13 3]";
93const uint32_t subset13[] = {0, 3, 4, 7};
94const 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
98const int16_t data14[] = {1, 0, 0, 0, 0, 2};
99const char* expected14 = "0[1] 5[2]";
100const uint32_t subset14[] = {0, 5};
101const char* expectedSubset14 = "0[1] 5[2]";
102
103}
104
105static SkString stringify_advance_data(
106        SkAdvancedTypefaceMetrics::AdvanceMetric<int16_t>* data) {
107    SkString result;
108    bool leadingSpace = false;
109    while (data != NULL) {
110      if (leadingSpace) {
111        result.append(" ");
112      } else {
113        leadingSpace = true;
114      }
115      switch(data->fType) {
116        case SkAdvancedTypefaceMetrics::AdvanceMetric<int16_t>::kRun:
117          result.appendf("%d %d %d", data->fStartId, data->fEndId,
118              data->fAdvance[0]);
119          break;
120        case SkAdvancedTypefaceMetrics::AdvanceMetric<int16_t>::kRange:
121          result.appendf("%d[", data->fStartId);
122          for (int i = 0; i < data->fAdvance.count(); ++i) {
123            if (i > 0) {
124              result.append(" ");
125            }
126            result.appendf("%d", data->fAdvance[i]);
127          }
128          result.append("]");
129          break;
130        case SkAdvancedTypefaceMetrics::AdvanceMetric<int16_t>::kDefault:
131          result.appendf("<Default=%d>", data->fAdvance[0]);
132          break;
133      }
134      data = data->fNext.get();
135    }
136    return result;
137}
138
139class TestWData {
140  public:
141    TestWData(skiatest::Reporter* reporter,
142              const int16_t advances[],
143              int advanceLen,
144              const uint32_t subset[],
145              int subsetLen,
146              const char* expected)
147            : fAdvances(advances)
148            , fAdvancesLen(advanceLen)
149            , fSubset(subset)
150            , fSubsetLen(subsetLen)
151            , fExpected(expected) {
152        REPORTER_ASSERT(reporter, RunTest());
153    }
154
155  private:
156    const int16_t* fAdvances;
157    const int fAdvancesLen;
158    const uint32_t* fSubset;
159    const int fSubsetLen;
160    const char* fExpected;
161
162    static bool getAdvance(TestWData* testCase, int gId, int16_t* advance) {
163        if (gId >= 0 && gId < testCase->fAdvancesLen) {
164            *advance = testCase->fAdvances[gId];
165            return true;
166        }
167        return false;
168    }
169
170    bool RunTest() {
171        SkTScopedPtr<SkAdvancedTypefaceMetrics::AdvanceMetric<int16_t> > result;
172        result.reset(getAdvanceData(this, fAdvancesLen, fSubset, fSubsetLen,
173                                    getAdvance));
174
175        SkString stringResult = stringify_advance_data(result.get());
176        if (!stringResult.equals(fExpected)) {
177            printf("Expected: %s\n  Result: %s\n", fExpected,
178                   stringResult.c_str());
179            return false;
180        }
181        return true;
182    }
183};
184
185static void TestWArray(skiatest::Reporter* reporter) {
186    TestWData(reporter, data1, SK_ARRAY_COUNT(data1), NULL, 0, expected1);
187    TestWData(reporter, data2, SK_ARRAY_COUNT(data2), NULL, 0, expected2);
188    TestWData(reporter, data3, SK_ARRAY_COUNT(data3), NULL, 0, expected3);
189    TestWData(reporter, data4, SK_ARRAY_COUNT(data4), NULL, 0, expected4);
190    TestWData(reporter, data5, SK_ARRAY_COUNT(data5), NULL, 0, expected5);
191    TestWData(reporter, data6, SK_ARRAY_COUNT(data6), NULL, 0, expected6);
192    TestWData(reporter, data7, SK_ARRAY_COUNT(data7), NULL, 0, expected7);
193    TestWData(reporter, data7, SK_ARRAY_COUNT(data7), subset7,
194              SK_ARRAY_COUNT(subset7), expectedSubset7);
195    TestWData(reporter, data8, SK_ARRAY_COUNT(data8), NULL, 0, expected8);
196    TestWData(reporter, data8, SK_ARRAY_COUNT(data8), subset8,
197              SK_ARRAY_COUNT(subset8), expectedSubset8);
198    TestWData(reporter, data9, SK_ARRAY_COUNT(data9), NULL, 0, expected9);
199    TestWData(reporter, data9, SK_ARRAY_COUNT(data9), subset9,
200              SK_ARRAY_COUNT(subset9), expectedSubset9);
201    TestWData(reporter, data10, SK_ARRAY_COUNT(data10), NULL, 0, expected10);
202    TestWData(reporter, data10, SK_ARRAY_COUNT(data10), subset10,
203              SK_ARRAY_COUNT(subset10), expectedSubset10);
204    TestWData(reporter, data11, SK_ARRAY_COUNT(data11), NULL, 0, expected11);
205    TestWData(reporter, data11, SK_ARRAY_COUNT(data11), subset11,
206              SK_ARRAY_COUNT(subset11), expectedSubset11);
207    TestWData(reporter, data12, SK_ARRAY_COUNT(data12), NULL, 0, expected12);
208    TestWData(reporter, data12, SK_ARRAY_COUNT(data12), subset12,
209              SK_ARRAY_COUNT(subset12), expectedSubset12);
210    TestWData(reporter, data13, SK_ARRAY_COUNT(data13), NULL, 0, expected13);
211    TestWData(reporter, data13, SK_ARRAY_COUNT(data13), subset13,
212              SK_ARRAY_COUNT(subset13), expectedSubset13);
213    TestWData(reporter, data14, SK_ARRAY_COUNT(data14), NULL, 0, expected14);
214    TestWData(reporter, data14, SK_ARRAY_COUNT(data14), subset14,
215              SK_ARRAY_COUNT(subset14), expectedSubset14);
216}
217
218#include "TestClassDef.h"
219DEFINE_TESTCLASS("WArray", WArrayTest, TestWArray)
220