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