blob: fd27f199a925ba10ebee09df4bf0c73b9f45c8fa [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000018#include "GrDrawTarget.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019#include "GrTDArray.h"
20#include "GrTBSearch.h"
21#include "GrMatrix.h"
bsalomon@google.com6034c502011-02-22 16:37:47 +000022#include "GrRedBlackTree.h"
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000023#include "GrPath.h"
junov@google.comf93e7172011-03-31 21:26:24 +000024#include "GrBinHashKey.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
26static void dump(const GrTDArray<int>& array) {
27#if 0
28 for (int i = 0; i < array.count(); i++) {
29 printf(" %d", array[i]);
30 }
31 printf("\n");
32#endif
33}
34
35static void test_tdarray() {
36 GrTDArray<int> array;
bsalomon@google.com6034c502011-02-22 16:37:47 +000037
reed@google.comac10a2d2010-12-22 21:39:39 +000038 *array.append() = 0; dump(array);
39 *array.append() = 2; dump(array);
40 *array.append() = 4; dump(array);
41 *array.append() = 6; dump(array);
42 GrAssert(array.count() == 4);
43
44 *array.insert(0) = -1; dump(array);
45 *array.insert(2) = 1; dump(array);
46 *array.insert(4) = 3; dump(array);
47 *array.insert(7) = 7; dump(array);
48 GrAssert(array.count() == 8);
49 array.remove(3); dump(array);
50 array.remove(0); dump(array);
51 array.removeShuffle(4); dump(array);
52 array.removeShuffle(1); dump(array);
53 GrAssert(array.count() == 4);
54}
55
56static bool LT(const int& elem, int value) {
57 return elem < value;
58}
59static bool EQ(const int& elem, int value) {
60 return elem == value;
61}
62
63static void test_bsearch() {
64 const int array[] = {
65 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99
66 };
67
68 for (size_t n = 0; n < GR_ARRAY_COUNT(array); n++) {
69 for (size_t i = 0; i < n; i++) {
70 int index = GrTBSearch<int, int>(array, n, array[i]);
71 GrAssert(index == i);
72 index = GrTBSearch<int, int>(array, n, -array[i]);
73 GrAssert(index < 0);
74 }
75 }
76}
77
bsalomon@google.combeccee72011-04-01 14:51:07 +000078// bogus empty class for GrBinHashKey
79class BogusEntry {};
80
junov@google.comf93e7172011-03-31 21:26:24 +000081static void test_binHashKey()
82{
83 const char* testStringA = "abcdA";
84 const char* testStringB = "abcdB";
85 enum {
86 kDataLenUsedForKey = 5
87 };
88
bsalomon@google.combeccee72011-04-01 14:51:07 +000089 typedef GrBinHashKey<BogusEntry, kDataLenUsedForKey> KeyType;
junov@google.comf93e7172011-03-31 21:26:24 +000090
91 KeyType keyA;
92 int passCnt = 0;
93 while (keyA.doPass()) {
94 ++passCnt;
95 keyA.keyData(reinterpret_cast<const uint8_t*>(testStringA), kDataLenUsedForKey);
96 }
97 GrAssert(passCnt == 1); //We expect the static allocation to suffice
bsalomon@google.combeccee72011-04-01 14:51:07 +000098 GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust;
junov@google.comf93e7172011-03-31 21:26:24 +000099 passCnt = 0;
100 while (keyBust.doPass()) {
101 ++passCnt;
102 // Exceed static storage by 1
103 keyBust.keyData(reinterpret_cast<const uint8_t*>(testStringA), kDataLenUsedForKey);
104 }
105 GrAssert(passCnt == 2); //We expect dynamic allocation to be necessary
106 GrAssert(keyA.getHash() == keyBust.getHash());
107
108 // Test that adding keyData in chunks gives
109 // the same hash as with one chunk
110 KeyType keyA2;
111 while (keyA2.doPass()) {
112 keyA2.keyData(reinterpret_cast<const uint8_t*>(testStringA), 2);
113 keyA2.keyData(&reinterpret_cast<const uint8_t*>(testStringA)[2], kDataLenUsedForKey-2);
114 }
115 GrAssert(keyA.getHash() == keyA2.getHash());
116
117 KeyType keyB;
118 while (keyB.doPass()){
119 keyB.keyData(reinterpret_cast<const uint8_t*>(testStringB), kDataLenUsedForKey);
120 }
121 GrAssert(keyA.compare(keyB) < 0);
122 GrAssert(keyA.compare(keyA2) == 0);
123
124 //Test ownership tranfer and copying
125 keyB.copyAndTakeOwnership(keyA);
126 GrAssert(keyA.fIsValid == false);
127 GrAssert(keyB.fIsValid);
128 GrAssert(keyB.getHash() == keyA2.getHash());
129 GrAssert(keyB.compare(keyA2) == 0);
130 keyA.deepCopyFrom(keyB);
131 GrAssert(keyA.fIsValid);
132 GrAssert(keyB.fIsValid);
133 GrAssert(keyA.getHash() == keyA2.getHash());
134 GrAssert(keyA.compare(keyA2) == 0);
135
136 //Test ownership tranfer and copying with key on heap
bsalomon@google.combeccee72011-04-01 14:51:07 +0000137 GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust2;
junov@google.comf93e7172011-03-31 21:26:24 +0000138 keyBust2.deepCopyFrom(keyBust);
139 GrAssert(keyBust.fIsValid);
140 GrAssert(keyBust2.fIsValid);
141 GrAssert(keyBust.getHash() == keyBust2.getHash());
142 GrAssert(keyBust.compare(keyBust2) == 0);
bsalomon@google.combeccee72011-04-01 14:51:07 +0000143 GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust3;
junov@google.comf93e7172011-03-31 21:26:24 +0000144 keyBust3.deepCopyFrom(keyBust);
145 GrAssert(keyBust.fIsValid == false);
146 GrAssert(keyBust3.fIsValid);
147 GrAssert(keyBust3.getHash() == keyBust2.getHash());
148 GrAssert(keyBust3.compare(keyBust2) == 0);
149}
150
reed@google.comac10a2d2010-12-22 21:39:39 +0000151void gr_run_unittests() {
152 test_tdarray();
153 test_bsearch();
junov@google.comf93e7172011-03-31 21:26:24 +0000154 test_binHashKey();
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 GrMatrix::UnitTest();
bsalomon@google.com6034c502011-02-22 16:37:47 +0000156 GrRedBlackTree<int>::UnitTest();
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000157 GrPath::ConvexUnitTest();
158 GrDrawTarget::VertexLayoutUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +0000159}
160
161