2012-09-18 17:16:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
// This is a GR test
|
|
|
|
#if SK_SUPPORT_GPU
|
2013-10-21 21:04:06 +00:00
|
|
|
#include "GrTHashTable.h"
|
2012-09-18 17:16:33 +00:00
|
|
|
|
|
|
|
struct HashElement {
|
|
|
|
int fKey;
|
|
|
|
int fValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
class GrFindPositivesFunctor {
|
|
|
|
public:
|
|
|
|
// only return elements with positive values
|
2012-09-19 02:01:47 +00:00
|
|
|
bool operator()(const HashElement* elem) const {
|
2012-09-18 17:16:33 +00:00
|
|
|
return elem->fValue > 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class GrFindNegativesFunctor {
|
|
|
|
public:
|
|
|
|
// only return elements with negative values
|
2012-09-19 02:01:47 +00:00
|
|
|
bool operator()(const HashElement* elem) const {
|
2012-09-18 17:16:33 +00:00
|
|
|
return elem->fValue < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class HashKey {
|
|
|
|
public:
|
|
|
|
HashKey(int key) : fKey(key) {}
|
|
|
|
|
|
|
|
uint32_t getHash() const { return fKey; }
|
|
|
|
|
2013-12-02 13:50:38 +00:00
|
|
|
static bool LessThan(const HashElement& entry, const HashKey& key) {
|
2012-09-18 17:16:33 +00:00
|
|
|
return entry.fKey < key.fKey;
|
|
|
|
}
|
2013-12-02 13:50:38 +00:00
|
|
|
static bool Equals(const HashElement& entry, const HashKey& key) {
|
2012-09-18 17:16:33 +00:00
|
|
|
return entry.fKey == key.fKey;
|
|
|
|
}
|
|
|
|
|
2013-08-28 14:17:03 +00:00
|
|
|
#ifdef SK_DEBUG
|
2013-12-02 13:50:38 +00:00
|
|
|
static bool LessThan(const HashElement& a, const HashElement& b) {
|
2012-09-18 17:16:33 +00:00
|
|
|
return a.fKey < b.fKey;
|
|
|
|
}
|
2013-12-02 13:50:38 +00:00
|
|
|
static bool Equals(const HashElement& a, const HashElement& b) {
|
2012-09-18 17:16:33 +00:00
|
|
|
return a.fKey == b.fKey;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int fKey;
|
|
|
|
};
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(HashCache, reporter) {
|
2012-09-18 17:16:33 +00:00
|
|
|
GrTHashTable<HashElement, HashKey, 4> cache;
|
|
|
|
|
2012-09-19 02:01:47 +00:00
|
|
|
HashElement negHashElements[10] = {
|
|
|
|
{ 0, 0 },
|
2012-09-18 17:16:33 +00:00
|
|
|
{ 1, -1 },
|
2012-09-19 02:01:47 +00:00
|
|
|
{ 2, -2 },
|
|
|
|
{ 3, -3 },
|
|
|
|
{ 4, -4 },
|
|
|
|
{ 5, -5 },
|
|
|
|
{ 6, -6 },
|
|
|
|
{ 7, -7 },
|
|
|
|
{ 8, -8 },
|
2012-09-18 17:16:33 +00:00
|
|
|
{ 9, -9 }
|
|
|
|
};
|
2012-09-19 02:01:47 +00:00
|
|
|
HashElement posHashElements[10] = {
|
|
|
|
{ 0, 0 },
|
2012-09-18 17:16:33 +00:00
|
|
|
{ 1, 1 },
|
2012-09-19 02:01:47 +00:00
|
|
|
{ 2, 2 },
|
|
|
|
{ 3, 3 },
|
|
|
|
{ 4, 4 },
|
|
|
|
{ 5, 5 },
|
|
|
|
{ 6, 6 },
|
|
|
|
{ 7, 7 },
|
|
|
|
{ 8, 8 },
|
2012-09-18 17:16:33 +00:00
|
|
|
{ 9, 9 }
|
|
|
|
};
|
|
|
|
|
|
|
|
// add i: -i pairs
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
cache.insert(HashKey(i), &negHashElements[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, 10 == cache.count());
|
|
|
|
|
|
|
|
// look for all i's and assert we found the -i's
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
HashElement* found = cache.find(i);
|
|
|
|
REPORTER_ASSERT(reporter, NULL != found && -i == found->fValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// look for something not in the cache
|
|
|
|
{
|
|
|
|
HashElement* found = cache.find(10);
|
|
|
|
REPORTER_ASSERT(reporter, NULL == found);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add i:i duplicates (so each i will have a positive & negative entry)
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
cache.insert(i, &posHashElements[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, 20 == cache.count());
|
2012-09-19 02:01:47 +00:00
|
|
|
|
2012-09-18 17:16:33 +00:00
|
|
|
// test out the find functor to find all the positive values
|
|
|
|
{
|
|
|
|
GrFindPositivesFunctor findPos;
|
|
|
|
|
|
|
|
HashElement* found = cache.find(0, findPos);
|
|
|
|
REPORTER_ASSERT(reporter, NULL == found);
|
|
|
|
|
|
|
|
for (int i = 1; i < 10; ++i) {
|
|
|
|
found = cache.find(i, findPos);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, NULL != found && found->fValue > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure finding the positives wasn't a fluke - find the negatives
|
|
|
|
{
|
|
|
|
GrFindNegativesFunctor findNeg;
|
|
|
|
|
|
|
|
HashElement* found = cache.find(0, findNeg);
|
|
|
|
REPORTER_ASSERT(reporter, NULL == found);
|
|
|
|
|
|
|
|
for (int i = 1; i < 10; ++i) {
|
|
|
|
found = cache.find(i, findNeg);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, NULL != found && found->fValue < 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the 0:0 entries
|
|
|
|
{
|
|
|
|
cache.remove(0, &negHashElements[0]);
|
|
|
|
cache.remove(0, &posHashElements[0]);
|
|
|
|
REPORTER_ASSERT(reporter, 18 == cache.count());
|
|
|
|
|
|
|
|
HashElement* found = cache.find(0);
|
|
|
|
REPORTER_ASSERT(reporter, NULL == found);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|