blob: d55486130d354c99651bbf15922c4945feaf1274 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
junov@google.comf93e7172011-03-31 21:26:24 +000011#include "GrBinHashKey.h"
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000012#include "GrDrawTarget.h"
13#include "GrMatrix.h"
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000014#include "GrRedBlackTree.h"
15#include "GrTDArray.h"
16
caryclark@google.comcf6285b2012-06-06 12:09:01 +000017// FIXME: needs to be in a header
18void gr_run_unittests();
19
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000020// If we aren't inheriting these as #defines from elsewhere,
21// clang demands they be declared before we #include the template
22// that relies on them.
23static bool LT(const int& elem, int value) {
24 return elem < value;
25}
26static bool EQ(const int& elem, int value) {
27 return elem == value;
28}
29#include "GrTBSearch.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000030
31static void dump(const GrTDArray<int>& array) {
32#if 0
33 for (int i = 0; i < array.count(); i++) {
34 printf(" %d", array[i]);
35 }
36 printf("\n");
37#endif
38}
39
40static void test_tdarray() {
41 GrTDArray<int> array;
bsalomon@google.com6034c502011-02-22 16:37:47 +000042
reed@google.comac10a2d2010-12-22 21:39:39 +000043 *array.append() = 0; dump(array);
44 *array.append() = 2; dump(array);
45 *array.append() = 4; dump(array);
46 *array.append() = 6; dump(array);
47 GrAssert(array.count() == 4);
48
49 *array.insert(0) = -1; dump(array);
50 *array.insert(2) = 1; dump(array);
51 *array.insert(4) = 3; dump(array);
52 *array.insert(7) = 7; dump(array);
53 GrAssert(array.count() == 8);
54 array.remove(3); dump(array);
55 array.remove(0); dump(array);
56 array.removeShuffle(4); dump(array);
57 array.removeShuffle(1); dump(array);
58 GrAssert(array.count() == 4);
59}
60
reed@google.comac10a2d2010-12-22 21:39:39 +000061
62static void test_bsearch() {
63 const int array[] = {
64 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99
65 };
66
67 for (size_t n = 0; n < GR_ARRAY_COUNT(array); n++) {
68 for (size_t i = 0; i < n; i++) {
69 int index = GrTBSearch<int, int>(array, n, array[i]);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000070 GrAssert(index == (int) i);
reed@google.comac10a2d2010-12-22 21:39:39 +000071 index = GrTBSearch<int, int>(array, n, -array[i]);
72 GrAssert(index < 0);
73 }
74 }
75}
76
bsalomon@google.combeccee72011-04-01 14:51:07 +000077// bogus empty class for GrBinHashKey
78class BogusEntry {};
79
junov@google.comf93e7172011-03-31 21:26:24 +000080static void test_binHashKey()
81{
junov@google.comf7c00f62011-08-18 18:15:16 +000082 const char* testStringA_ = "abcdABCD";
83 const char* testStringB_ = "abcdBBCD";
84 const uint32_t* testStringA = reinterpret_cast<const uint32_t*>(testStringA_);
85 const uint32_t* testStringB = reinterpret_cast<const uint32_t*>(testStringB_);
junov@google.comf93e7172011-03-31 21:26:24 +000086 enum {
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +000087 kDataLenUsedForKey = 8
junov@google.comf93e7172011-03-31 21:26:24 +000088 };
89
bsalomon@google.com0a17bec2011-08-18 20:23:09 +000090 GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyA;
junov@google.comf7c00f62011-08-18 18:15:16 +000091 keyA.setKeyData(testStringA);
92 // test copy constructor and comparison
bsalomon@google.com0a17bec2011-08-18 20:23:09 +000093 GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyA2(keyA);
junov@google.comf7c00f62011-08-18 18:15:16 +000094 GrAssert(keyA.compare(keyA2) == 0);
junov@google.comf93e7172011-03-31 21:26:24 +000095 GrAssert(keyA.getHash() == keyA2.getHash());
junov@google.comf7c00f62011-08-18 18:15:16 +000096 // test re-init
97 keyA2.setKeyData(testStringA);
98 GrAssert(keyA.compare(keyA2) == 0);
99 GrAssert(keyA.getHash() == keyA2.getHash());
100 // test sorting
bsalomon@google.com0a17bec2011-08-18 20:23:09 +0000101 GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyB;
junov@google.comf7c00f62011-08-18 18:15:16 +0000102 keyB.setKeyData(testStringB);
junov@google.comf93e7172011-03-31 21:26:24 +0000103 GrAssert(keyA.compare(keyB) < 0);
junov@google.comf7c00f62011-08-18 18:15:16 +0000104 GrAssert(keyA.getHash() != keyB.getHash());
junov@google.comf93e7172011-03-31 21:26:24 +0000105}
106
reed@google.com07f3ee12011-05-16 17:21:57 +0000107
reed@google.comac10a2d2010-12-22 21:39:39 +0000108void gr_run_unittests() {
109 test_tdarray();
110 test_bsearch();
junov@google.comf93e7172011-03-31 21:26:24 +0000111 test_binHashKey();
bsalomon@google.com6034c502011-02-22 16:37:47 +0000112 GrRedBlackTree<int>::UnitTest();
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000113 GrDrawTarget::VertexLayoutUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +0000114}