blob: ef6b1e77dc68e8bc6839e519bb4f4b237eeef189 [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
junov@google.comf93e7172011-03-31 21:26:24 +000078static void test_binHashKey()
79{
80 const char* testStringA = "abcdA";
81 const char* testStringB = "abcdB";
82 enum {
83 kDataLenUsedForKey = 5
84 };
85
86 class Entry {
87 // bogus empty class
88 };
89
90 typedef GrBinHashKey<Entry, kDataLenUsedForKey> KeyType;
91
92 KeyType keyA;
93 int passCnt = 0;
94 while (keyA.doPass()) {
95 ++passCnt;
96 keyA.keyData(reinterpret_cast<const uint8_t*>(testStringA), kDataLenUsedForKey);
97 }
98 GrAssert(passCnt == 1); //We expect the static allocation to suffice
99 GrBinHashKey<Entry, kDataLenUsedForKey-1> keyBust;
100 passCnt = 0;
101 while (keyBust.doPass()) {
102 ++passCnt;
103 // Exceed static storage by 1
104 keyBust.keyData(reinterpret_cast<const uint8_t*>(testStringA), kDataLenUsedForKey);
105 }
106 GrAssert(passCnt == 2); //We expect dynamic allocation to be necessary
107 GrAssert(keyA.getHash() == keyBust.getHash());
108
109 // Test that adding keyData in chunks gives
110 // the same hash as with one chunk
111 KeyType keyA2;
112 while (keyA2.doPass()) {
113 keyA2.keyData(reinterpret_cast<const uint8_t*>(testStringA), 2);
114 keyA2.keyData(&reinterpret_cast<const uint8_t*>(testStringA)[2], kDataLenUsedForKey-2);
115 }
116 GrAssert(keyA.getHash() == keyA2.getHash());
117
118 KeyType keyB;
119 while (keyB.doPass()){
120 keyB.keyData(reinterpret_cast<const uint8_t*>(testStringB), kDataLenUsedForKey);
121 }
122 GrAssert(keyA.compare(keyB) < 0);
123 GrAssert(keyA.compare(keyA2) == 0);
124
125 //Test ownership tranfer and copying
126 keyB.copyAndTakeOwnership(keyA);
127 GrAssert(keyA.fIsValid == false);
128 GrAssert(keyB.fIsValid);
129 GrAssert(keyB.getHash() == keyA2.getHash());
130 GrAssert(keyB.compare(keyA2) == 0);
131 keyA.deepCopyFrom(keyB);
132 GrAssert(keyA.fIsValid);
133 GrAssert(keyB.fIsValid);
134 GrAssert(keyA.getHash() == keyA2.getHash());
135 GrAssert(keyA.compare(keyA2) == 0);
136
137 //Test ownership tranfer and copying with key on heap
138 GrBinHashKey<Entry, kDataLenUsedForKey-1> keyBust2;
139 keyBust2.deepCopyFrom(keyBust);
140 GrAssert(keyBust.fIsValid);
141 GrAssert(keyBust2.fIsValid);
142 GrAssert(keyBust.getHash() == keyBust2.getHash());
143 GrAssert(keyBust.compare(keyBust2) == 0);
144 GrBinHashKey<Entry, kDataLenUsedForKey-1> keyBust3;
145 keyBust3.deepCopyFrom(keyBust);
146 GrAssert(keyBust.fIsValid == false);
147 GrAssert(keyBust3.fIsValid);
148 GrAssert(keyBust3.getHash() == keyBust2.getHash());
149 GrAssert(keyBust3.compare(keyBust2) == 0);
150}
151
reed@google.comac10a2d2010-12-22 21:39:39 +0000152void gr_run_unittests() {
153 test_tdarray();
154 test_bsearch();
junov@google.comf93e7172011-03-31 21:26:24 +0000155 test_binHashKey();
reed@google.comac10a2d2010-12-22 21:39:39 +0000156 GrMatrix::UnitTest();
bsalomon@google.com6034c502011-02-22 16:37:47 +0000157 GrRedBlackTree<int>::UnitTest();
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000158 GrPath::ConvexUnitTest();
159 GrDrawTarget::VertexLayoutUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +0000160}
161
162