blob: 4adc7e9c82820842d18e5de84769b1f07958f756 [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
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.
20static bool LT(const int& elem, int value) {
21 return elem < value;
22}
23static bool EQ(const int& elem, int value) {
24 return elem == value;
25}
26#include "GrTBSearch.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28static void dump(const GrTDArray<int>& array) {
29#if 0
30 for (int i = 0; i < array.count(); i++) {
31 printf(" %d", array[i]);
32 }
33 printf("\n");
34#endif
35}
36
37static void test_tdarray() {
38 GrTDArray<int> array;
bsalomon@google.com6034c502011-02-22 16:37:47 +000039
reed@google.comac10a2d2010-12-22 21:39:39 +000040 *array.append() = 0; dump(array);
41 *array.append() = 2; dump(array);
42 *array.append() = 4; dump(array);
43 *array.append() = 6; dump(array);
44 GrAssert(array.count() == 4);
45
46 *array.insert(0) = -1; dump(array);
47 *array.insert(2) = 1; dump(array);
48 *array.insert(4) = 3; dump(array);
49 *array.insert(7) = 7; dump(array);
50 GrAssert(array.count() == 8);
51 array.remove(3); dump(array);
52 array.remove(0); dump(array);
53 array.removeShuffle(4); dump(array);
54 array.removeShuffle(1); dump(array);
55 GrAssert(array.count() == 4);
56}
57
reed@google.comac10a2d2010-12-22 21:39:39 +000058
59static void test_bsearch() {
60 const int array[] = {
61 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99
62 };
63
64 for (size_t n = 0; n < GR_ARRAY_COUNT(array); n++) {
65 for (size_t i = 0; i < n; i++) {
66 int index = GrTBSearch<int, int>(array, n, array[i]);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000067 GrAssert(index == (int) i);
reed@google.comac10a2d2010-12-22 21:39:39 +000068 index = GrTBSearch<int, int>(array, n, -array[i]);
69 GrAssert(index < 0);
70 }
71 }
72}
73
bsalomon@google.combeccee72011-04-01 14:51:07 +000074// bogus empty class for GrBinHashKey
75class BogusEntry {};
76
junov@google.comf93e7172011-03-31 21:26:24 +000077static void test_binHashKey()
78{
junov@google.comf7c00f62011-08-18 18:15:16 +000079 const char* testStringA_ = "abcdABCD";
80 const char* testStringB_ = "abcdBBCD";
81 const uint32_t* testStringA = reinterpret_cast<const uint32_t*>(testStringA_);
82 const uint32_t* testStringB = reinterpret_cast<const uint32_t*>(testStringB_);
junov@google.comf93e7172011-03-31 21:26:24 +000083 enum {
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +000084 kDataLenUsedForKey = 8
junov@google.comf93e7172011-03-31 21:26:24 +000085 };
86
bsalomon@google.com0a17bec2011-08-18 20:23:09 +000087 GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyA;
junov@google.comf7c00f62011-08-18 18:15:16 +000088 keyA.setKeyData(testStringA);
89 // test copy constructor and comparison
bsalomon@google.com0a17bec2011-08-18 20:23:09 +000090 GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyA2(keyA);
junov@google.comf7c00f62011-08-18 18:15:16 +000091 GrAssert(keyA.compare(keyA2) == 0);
junov@google.comf93e7172011-03-31 21:26:24 +000092 GrAssert(keyA.getHash() == keyA2.getHash());
junov@google.comf7c00f62011-08-18 18:15:16 +000093 // test re-init
94 keyA2.setKeyData(testStringA);
95 GrAssert(keyA.compare(keyA2) == 0);
96 GrAssert(keyA.getHash() == keyA2.getHash());
97 // test sorting
bsalomon@google.com0a17bec2011-08-18 20:23:09 +000098 GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyB;
junov@google.comf7c00f62011-08-18 18:15:16 +000099 keyB.setKeyData(testStringB);
junov@google.comf93e7172011-03-31 21:26:24 +0000100 GrAssert(keyA.compare(keyB) < 0);
junov@google.comf7c00f62011-08-18 18:15:16 +0000101 GrAssert(keyA.getHash() != keyB.getHash());
junov@google.comf93e7172011-03-31 21:26:24 +0000102}
103
reed@google.com07f3ee12011-05-16 17:21:57 +0000104
reed@google.comac10a2d2010-12-22 21:39:39 +0000105void gr_run_unittests() {
106 test_tdarray();
107 test_bsearch();
junov@google.comf93e7172011-03-31 21:26:24 +0000108 test_binHashKey();
bsalomon@google.com6034c502011-02-22 16:37:47 +0000109 GrRedBlackTree<int>::UnitTest();
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000110 GrDrawTarget::VertexLayoutUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +0000111}