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