blob: ab5049a36a3281341e8c106c76a0332d9af71b9a [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
junov@google.comf93e7172011-03-31 21:26:24 +00009#include "GrBinHashKey.h"
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000010#include "GrDrawTarget.h"
bsalomon@google.comb9086a02012-11-01 18:02:54 +000011#include "SkMatrix.h"
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000012#include "GrRedBlackTree.h"
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000013
caryclark@google.comcf6285b2012-06-06 12:09:01 +000014// FIXME: needs to be in a header
15void gr_run_unittests();
16
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000017// 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.
sugoi@google.com9c55f802013-03-07 20:52:59 +000020#if GR_DEBUG
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000021static bool LT(const int& elem, int value) {
22 return elem < value;
23}
24static bool EQ(const int& elem, int value) {
25 return elem == value;
26}
27#include "GrTBSearch.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000028
reed@google.comac10a2d2010-12-22 21:39:39 +000029static void test_bsearch() {
30 const int array[] = {
31 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99
32 };
33
34 for (size_t n = 0; n < GR_ARRAY_COUNT(array); n++) {
35 for (size_t i = 0; i < n; i++) {
36 int index = GrTBSearch<int, int>(array, n, array[i]);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000037 GrAssert(index == (int) i);
reed@google.comac10a2d2010-12-22 21:39:39 +000038 index = GrTBSearch<int, int>(array, n, -array[i]);
39 GrAssert(index < 0);
40 }
41 }
42}
sugoi@google.com9c55f802013-03-07 20:52:59 +000043#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000044
bsalomon@google.combeccee72011-04-01 14:51:07 +000045// bogus empty class for GrBinHashKey
46class BogusEntry {};
47
junov@google.comf93e7172011-03-31 21:26:24 +000048static void test_binHashKey()
49{
junov@google.comf7c00f62011-08-18 18:15:16 +000050 const char* testStringA_ = "abcdABCD";
51 const char* testStringB_ = "abcdBBCD";
52 const uint32_t* testStringA = reinterpret_cast<const uint32_t*>(testStringA_);
53 const uint32_t* testStringB = reinterpret_cast<const uint32_t*>(testStringB_);
junov@google.comf93e7172011-03-31 21:26:24 +000054 enum {
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +000055 kDataLenUsedForKey = 8
junov@google.comf93e7172011-03-31 21:26:24 +000056 };
57
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000058 GrTBinHashKey<BogusEntry, kDataLenUsedForKey> keyA;
junov@google.comf7c00f62011-08-18 18:15:16 +000059 keyA.setKeyData(testStringA);
60 // test copy constructor and comparison
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000061 GrTBinHashKey<BogusEntry, kDataLenUsedForKey> keyA2(keyA);
junov@google.comf7c00f62011-08-18 18:15:16 +000062 GrAssert(keyA.compare(keyA2) == 0);
junov@google.comf93e7172011-03-31 21:26:24 +000063 GrAssert(keyA.getHash() == keyA2.getHash());
junov@google.comf7c00f62011-08-18 18:15:16 +000064 // test re-init
65 keyA2.setKeyData(testStringA);
66 GrAssert(keyA.compare(keyA2) == 0);
67 GrAssert(keyA.getHash() == keyA2.getHash());
68 // test sorting
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000069 GrTBinHashKey<BogusEntry, kDataLenUsedForKey> keyB;
junov@google.comf7c00f62011-08-18 18:15:16 +000070 keyB.setKeyData(testStringB);
junov@google.comf93e7172011-03-31 21:26:24 +000071 GrAssert(keyA.compare(keyB) < 0);
rmistry@google.comd6176b02012-08-23 18:14:13 +000072 GrAssert(keyA.getHash() != keyB.getHash());
junov@google.comf93e7172011-03-31 21:26:24 +000073}
74
reed@google.com07f3ee12011-05-16 17:21:57 +000075
reed@google.comac10a2d2010-12-22 21:39:39 +000076void gr_run_unittests() {
sugoi@google.com9c55f802013-03-07 20:52:59 +000077 GR_DEBUGCODE(test_bsearch();)
junov@google.comf93e7172011-03-31 21:26:24 +000078 test_binHashKey();
bsalomon@google.com6034c502011-02-22 16:37:47 +000079 GrRedBlackTree<int>::UnitTest();
jvanverth@google.com9b855c72013-03-01 18:21:22 +000080 GrDrawState::VertexAttributesUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +000081}