blob: c7daf7718f202073ff9ff52b1bf5e5b140e1792d [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.
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
reed@google.comac10a2d2010-12-22 21:39:39 +000028static void test_bsearch() {
29 const int array[] = {
30 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99
31 };
32
33 for (size_t n = 0; n < GR_ARRAY_COUNT(array); n++) {
34 for (size_t i = 0; i < n; i++) {
35 int index = GrTBSearch<int, int>(array, n, array[i]);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000036 GrAssert(index == (int) i);
reed@google.comac10a2d2010-12-22 21:39:39 +000037 index = GrTBSearch<int, int>(array, n, -array[i]);
38 GrAssert(index < 0);
39 }
40 }
41}
42
bsalomon@google.combeccee72011-04-01 14:51:07 +000043// bogus empty class for GrBinHashKey
44class BogusEntry {};
45
junov@google.comf93e7172011-03-31 21:26:24 +000046static void test_binHashKey()
47{
junov@google.comf7c00f62011-08-18 18:15:16 +000048 const char* testStringA_ = "abcdABCD";
49 const char* testStringB_ = "abcdBBCD";
50 const uint32_t* testStringA = reinterpret_cast<const uint32_t*>(testStringA_);
51 const uint32_t* testStringB = reinterpret_cast<const uint32_t*>(testStringB_);
junov@google.comf93e7172011-03-31 21:26:24 +000052 enum {
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +000053 kDataLenUsedForKey = 8
junov@google.comf93e7172011-03-31 21:26:24 +000054 };
55
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000056 GrTBinHashKey<BogusEntry, kDataLenUsedForKey> keyA;
junov@google.comf7c00f62011-08-18 18:15:16 +000057 keyA.setKeyData(testStringA);
58 // test copy constructor and comparison
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000059 GrTBinHashKey<BogusEntry, kDataLenUsedForKey> keyA2(keyA);
junov@google.comf7c00f62011-08-18 18:15:16 +000060 GrAssert(keyA.compare(keyA2) == 0);
junov@google.comf93e7172011-03-31 21:26:24 +000061 GrAssert(keyA.getHash() == keyA2.getHash());
junov@google.comf7c00f62011-08-18 18:15:16 +000062 // test re-init
63 keyA2.setKeyData(testStringA);
64 GrAssert(keyA.compare(keyA2) == 0);
65 GrAssert(keyA.getHash() == keyA2.getHash());
66 // test sorting
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000067 GrTBinHashKey<BogusEntry, kDataLenUsedForKey> keyB;
junov@google.comf7c00f62011-08-18 18:15:16 +000068 keyB.setKeyData(testStringB);
junov@google.comf93e7172011-03-31 21:26:24 +000069 GrAssert(keyA.compare(keyB) < 0);
rmistry@google.comd6176b02012-08-23 18:14:13 +000070 GrAssert(keyA.getHash() != keyB.getHash());
junov@google.comf93e7172011-03-31 21:26:24 +000071}
72
reed@google.com07f3ee12011-05-16 17:21:57 +000073
reed@google.comac10a2d2010-12-22 21:39:39 +000074void gr_run_unittests() {
reed@google.comac10a2d2010-12-22 21:39:39 +000075 test_bsearch();
junov@google.comf93e7172011-03-31 21:26:24 +000076 test_binHashKey();
bsalomon@google.com6034c502011-02-22 16:37:47 +000077 GrRedBlackTree<int>::UnitTest();
jvanverth@google.com9b855c72013-03-01 18:21:22 +000078 GrDrawState::VertexAttributesUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +000079}