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 #include "GrBinHashKey.h"
10 #include "GrDrawTarget.h"
11 #include "SkMatrix.h"
12 #include "GrRedBlackTree.h"
13
14 // FIXME: needs to be in a header
15 void gr_run_unittests();
16
17 // If we aren't inheriting these as #defines from elsewhere,
18 // clang demands they be declared before we #include the template
19 // that relies on them.
LT(const int & elem,int value)20 static bool LT(const int& elem, int value) {
21 return elem < value;
22 }
EQ(const int & elem,int value)23 static bool EQ(const int& elem, int value) {
24 return elem == value;
25 }
26 #include "GrTBSearch.h"
27
test_bsearch()28 static void test_bsearch() {
29 const int array[] = {
30 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99
31 };
32
33 for (size_t n = 0; n < GR_ARRAY_COUNT(array); n++) {
34 for (size_t i = 0; i < n; i++) {
35 int index = GrTBSearch<int, int>(array, n, array[i]);
36 GrAssert(index == (int) i);
37 index = GrTBSearch<int, int>(array, n, -array[i]);
38 GrAssert(index < 0);
39 }
40 }
41 }
42
43 // bogus empty class for GrBinHashKey
44 class BogusEntry {};
45
test_binHashKey()46 static void test_binHashKey()
47 {
48 const char* testStringA_ = "abcdABCD";
49 const char* testStringB_ = "abcdBBCD";
50 const uint32_t* testStringA = reinterpret_cast<const uint32_t*>(testStringA_);
51 const uint32_t* testStringB = reinterpret_cast<const uint32_t*>(testStringB_);
52 enum {
53 kDataLenUsedForKey = 8
54 };
55
56 GrTBinHashKey<BogusEntry, kDataLenUsedForKey> keyA;
57 keyA.setKeyData(testStringA);
58 // test copy constructor and comparison
59 GrTBinHashKey<BogusEntry, kDataLenUsedForKey> keyA2(keyA);
60 GrAssert(keyA.compare(keyA2) == 0);
61 GrAssert(keyA.getHash() == keyA2.getHash());
62 // test re-init
63 keyA2.setKeyData(testStringA);
64 GrAssert(keyA.compare(keyA2) == 0);
65 GrAssert(keyA.getHash() == keyA2.getHash());
66 // test sorting
67 GrTBinHashKey<BogusEntry, kDataLenUsedForKey> keyB;
68 keyB.setKeyData(testStringB);
69 GrAssert(keyA.compare(keyB) < 0);
70 GrAssert(keyA.getHash() != keyB.getHash());
71 }
72
73
gr_run_unittests()74 void gr_run_unittests() {
75 test_bsearch();
76 test_binHashKey();
77 GrRedBlackTree<int>::UnitTest();
78 GrDrawState::VertexLayoutUnitTest();
79 }
80