Home | History | Annotate | Download | only in gpu
      1 
      2 /*
      3  * Copyright 2010 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 
     11 #include "GrBinHashKey.h"
     12 #include "GrDrawTarget.h"
     13 #include "GrMatrix.h"
     14 #include "GrPath.h"
     15 #include "GrRedBlackTree.h"
     16 #include "GrTDArray.h"
     17 
     18 // If we aren't inheriting these as #defines from elsewhere,
     19 // clang demands they be declared before we #include the template
     20 // that relies on them.
     21 static bool LT(const int& elem, int value) {
     22     return elem < value;
     23 }
     24 static bool EQ(const int& elem, int value) {
     25     return elem == value;
     26 }
     27 #include "GrTBSearch.h"
     28 
     29 static void dump(const GrTDArray<int>& array) {
     30 #if 0
     31     for (int i = 0; i < array.count(); i++) {
     32         printf(" %d", array[i]);
     33     }
     34     printf("\n");
     35 #endif
     36 }
     37 
     38 static void test_tdarray() {
     39     GrTDArray<int> array;
     40 
     41     *array.append() = 0; dump(array);
     42     *array.append() = 2; dump(array);
     43     *array.append() = 4; dump(array);
     44     *array.append() = 6; dump(array);
     45     GrAssert(array.count() == 4);
     46 
     47     *array.insert(0) = -1; dump(array);
     48     *array.insert(2) = 1; dump(array);
     49     *array.insert(4) = 3; dump(array);
     50     *array.insert(7) = 7; dump(array);
     51     GrAssert(array.count() == 8);
     52     array.remove(3); dump(array);
     53     array.remove(0); dump(array);
     54     array.removeShuffle(4); dump(array);
     55     array.removeShuffle(1); dump(array);
     56     GrAssert(array.count() == 4);
     57 }
     58 
     59 
     60 static void test_bsearch() {
     61     const int array[] = {
     62         1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99
     63     };
     64 
     65     for (size_t n = 0; n < GR_ARRAY_COUNT(array); n++) {
     66         for (size_t i = 0; i < n; i++) {
     67             int index = GrTBSearch<int, int>(array, n, array[i]);
     68             GrAssert(index == (int) i);
     69             index = GrTBSearch<int, int>(array, n, -array[i]);
     70             GrAssert(index < 0);
     71         }
     72     }
     73 }
     74 
     75 // bogus empty class for GrBinHashKey
     76 class BogusEntry {};
     77 
     78 static void test_binHashKey()
     79 {
     80     const char* testStringA_ = "abcdABCD";
     81     const char* testStringB_ = "abcdBBCD";
     82     const uint32_t* testStringA = reinterpret_cast<const uint32_t*>(testStringA_);
     83     const uint32_t* testStringB = reinterpret_cast<const uint32_t*>(testStringB_);
     84     enum {
     85         kDataLenUsedForKey = 8
     86     };
     87 
     88     GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyA;
     89     keyA.setKeyData(testStringA);
     90     // test copy constructor and comparison
     91     GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyA2(keyA);
     92     GrAssert(keyA.compare(keyA2) == 0);
     93     GrAssert(keyA.getHash() == keyA2.getHash());
     94     // test re-init
     95     keyA2.setKeyData(testStringA);
     96     GrAssert(keyA.compare(keyA2) == 0);
     97     GrAssert(keyA.getHash() == keyA2.getHash());
     98     // test sorting
     99     GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyB;
    100     keyB.setKeyData(testStringB);
    101     GrAssert(keyA.compare(keyB) < 0);
    102     GrAssert(keyA.getHash() != keyB.getHash());
    103 }
    104 
    105 static void test_convex() {
    106 #if 0
    107     GrPath testPath;
    108     GrPath::Iter testIter;
    109 
    110     GrPath pt;
    111     pt.moveTo(0, 0);
    112     pt.close();
    113 
    114     testIter.reset(pt);
    115     testPath.resetFromIter(&testIter);
    116     GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
    117 
    118     GrPath line;
    119     line.moveTo(GrIntToScalar(12), GrIntToScalar(20));
    120     line.lineTo(GrIntToScalar(-12), GrIntToScalar(-20));
    121     line.close();
    122 
    123     testIter.reset(line);
    124     testPath.resetFromIter(&testIter);
    125     GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
    126 
    127     GrPath triLeft;
    128     triLeft.moveTo(0, 0);
    129     triLeft.lineTo(1, 0);
    130     triLeft.lineTo(1, 1);
    131     triLeft.close();
    132 
    133     testIter.reset(triLeft);
    134     testPath.resetFromIter(&testIter);
    135     GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
    136 
    137     GrPath triRight;
    138     triRight.moveTo(0, 0);
    139     triRight.lineTo(-1, 0);
    140     triRight.lineTo(1, 1);
    141     triRight.close();
    142 
    143     testIter.reset(triRight);
    144     testPath.resetFromIter(&testIter);
    145     GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
    146 
    147     GrPath square;
    148     square.moveTo(0, 0);
    149     square.lineTo(1, 0);
    150     square.lineTo(1, 1);
    151     square.lineTo(0, 1);
    152     square.close();
    153 
    154     testIter.reset(square);
    155     testPath.resetFromIter(&testIter);
    156     GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
    157 
    158     GrPath redundantSquare;
    159     square.moveTo(0, 0);
    160     square.lineTo(0, 0);
    161     square.lineTo(0, 0);
    162     square.lineTo(1, 0);
    163     square.lineTo(1, 0);
    164     square.lineTo(1, 0);
    165     square.lineTo(1, 1);
    166     square.lineTo(1, 1);
    167     square.lineTo(1, 1);
    168     square.lineTo(0, 1);
    169     square.lineTo(0, 1);
    170     square.lineTo(0, 1);
    171     square.close();
    172 
    173     testIter.reset(redundantSquare);
    174     testPath.resetFromIter(&testIter);
    175     GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
    176 
    177     GrPath bowTie;
    178     bowTie.moveTo(0, 0);
    179     bowTie.lineTo(0, 0);
    180     bowTie.lineTo(0, 0);
    181     bowTie.lineTo(1, 1);
    182     bowTie.lineTo(1, 1);
    183     bowTie.lineTo(1, 1);
    184     bowTie.lineTo(1, 0);
    185     bowTie.lineTo(1, 0);
    186     bowTie.lineTo(1, 0);
    187     bowTie.lineTo(0, 1);
    188     bowTie.lineTo(0, 1);
    189     bowTie.lineTo(0, 1);
    190     bowTie.close();
    191 
    192     testIter.reset(bowTie);
    193     testPath.resetFromIter(&testIter);
    194     GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
    195 
    196     GrPath spiral;
    197     spiral.moveTo(0, 0);
    198     spiral.lineTo(1, 0);
    199     spiral.lineTo(1, 1);
    200     spiral.lineTo(0, 1);
    201     spiral.lineTo(0,.5);
    202     spiral.lineTo(.5,.5);
    203     spiral.lineTo(.5,.75);
    204     spiral.close();
    205 
    206     testIter.reset(spiral);
    207     testPath.resetFromIter(&testIter);
    208     GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
    209 
    210     GrPath dent;
    211     dent.moveTo(0, 0);
    212     dent.lineTo(1, 1);
    213     dent.lineTo(0, 1);
    214     dent.lineTo(-.5,2);
    215     dent.lineTo(-2, 1);
    216     dent.close();
    217 
    218     testIter.reset(dent);
    219     testPath.resetFromIter(&testIter);
    220     GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
    221 #endif
    222 }
    223 
    224 void gr_run_unittests() {
    225     test_tdarray();
    226     test_bsearch();
    227     test_binHashKey();
    228     test_convex();
    229     GrRedBlackTree<int>::UnitTest();
    230     GrDrawTarget::VertexLayoutUnitTest();
    231 }
    232