2013-10-12 17:25:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "src/core/SkTDynamicHash.h"
|
|
|
|
#include "tests/Test.h"
|
2013-08-05 22:31:20 +00:00
|
|
|
|
2018-05-22 14:48:08 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
2013-08-05 22:31:20 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct Entry {
|
|
|
|
int key;
|
2013-08-05 23:08:19 +00:00
|
|
|
double value;
|
2014-04-02 19:17:00 +00:00
|
|
|
|
|
|
|
static const int& GetKey(const Entry& entry) { return entry.key; }
|
|
|
|
static uint32_t Hash(const int& key) { return key; }
|
2013-08-05 22:31:20 +00:00
|
|
|
};
|
2013-10-12 17:25:17 +00:00
|
|
|
|
2013-08-05 23:08:19 +00:00
|
|
|
|
2014-04-02 19:17:00 +00:00
|
|
|
class Hash : public SkTDynamicHash<Entry, int> {
|
2013-08-05 22:31:20 +00:00
|
|
|
public:
|
|
|
|
Hash() : INHERITED() {}
|
|
|
|
|
|
|
|
// Promote protected methods to public for this test.
|
|
|
|
int capacity() const { return this->INHERITED::capacity(); }
|
|
|
|
int countCollisions(const int& key) const { return this->INHERITED::countCollisions(key); }
|
|
|
|
|
|
|
|
private:
|
2014-04-02 19:17:00 +00:00
|
|
|
typedef SkTDynamicHash<Entry, int> INHERITED;
|
2013-08-05 22:31:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#define ASSERT(x) REPORTER_ASSERT(reporter, x)
|
|
|
|
|
2014-03-05 01:00:50 +00:00
|
|
|
DEF_TEST(DynamicHash_growth, reporter) {
|
2013-08-05 22:31:20 +00:00
|
|
|
Entry a = { 1, 2.0 };
|
|
|
|
Entry b = { 2, 3.0 };
|
|
|
|
Entry c = { 3, 4.0 };
|
|
|
|
Entry d = { 4, 5.0 };
|
|
|
|
Entry e = { 5, 6.0 };
|
|
|
|
|
2014-01-13 18:28:14 +00:00
|
|
|
Hash hash;
|
|
|
|
ASSERT(hash.capacity() == 0);
|
2013-08-05 22:31:20 +00:00
|
|
|
|
|
|
|
hash.add(&a);
|
2013-08-12 14:51:25 +00:00
|
|
|
ASSERT(hash.capacity() == 4);
|
2013-08-05 22:31:20 +00:00
|
|
|
|
|
|
|
hash.add(&b);
|
|
|
|
ASSERT(hash.capacity() == 4);
|
|
|
|
|
|
|
|
hash.add(&c);
|
2013-08-12 14:51:25 +00:00
|
|
|
ASSERT(hash.capacity() == 4);
|
2013-08-05 22:31:20 +00:00
|
|
|
|
|
|
|
hash.add(&d);
|
|
|
|
ASSERT(hash.capacity() == 8);
|
|
|
|
|
|
|
|
hash.add(&e);
|
2013-08-12 14:51:25 +00:00
|
|
|
ASSERT(hash.capacity() == 8);
|
2013-08-05 22:31:20 +00:00
|
|
|
|
|
|
|
ASSERT(hash.count() == 5);
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:57:32 +00:00
|
|
|
DEF_TEST(DynamicHash_growth_bounded, reporter) {
|
|
|
|
Entry a = { 1, 2.0 };
|
|
|
|
Entry b = { 2, 3.0 };
|
|
|
|
Entry c = { 3, 4.0 };
|
|
|
|
Entry d = { 4, 5.0 };
|
|
|
|
Entry e = { 5, 6.0 };
|
|
|
|
|
|
|
|
Hash hash;
|
|
|
|
ASSERT(hash.capacity() == 0);
|
|
|
|
|
|
|
|
hash.add(&a);
|
|
|
|
ASSERT(hash.capacity() == 4);
|
|
|
|
|
|
|
|
hash.remove(a.key);
|
|
|
|
hash.add(&b);
|
|
|
|
ASSERT(hash.capacity() == 4);
|
|
|
|
|
|
|
|
hash.remove(b.key);
|
|
|
|
hash.add(&c);
|
|
|
|
ASSERT(hash.capacity() == 4);
|
|
|
|
|
|
|
|
hash.remove(c.key);
|
|
|
|
hash.add(&d);
|
|
|
|
ASSERT(hash.capacity() == 4);
|
|
|
|
|
|
|
|
hash.remove(d.key);
|
|
|
|
hash.add(&e);
|
|
|
|
ASSERT(hash.capacity() == 4);
|
|
|
|
|
|
|
|
ASSERT(hash.count() == 1);
|
|
|
|
}
|
|
|
|
|
2014-03-05 01:00:50 +00:00
|
|
|
DEF_TEST(DynamicHash_add, reporter) {
|
2013-08-05 22:31:20 +00:00
|
|
|
Hash hash;
|
|
|
|
Entry a = { 1, 2.0 };
|
|
|
|
Entry b = { 2, 3.0 };
|
|
|
|
|
|
|
|
ASSERT(hash.count() == 0);
|
|
|
|
hash.add(&a);
|
|
|
|
ASSERT(hash.count() == 1);
|
|
|
|
hash.add(&b);
|
|
|
|
ASSERT(hash.count() == 2);
|
|
|
|
}
|
|
|
|
|
2014-03-05 01:00:50 +00:00
|
|
|
DEF_TEST(DynamicHash_lookup, reporter) {
|
2014-01-13 18:28:14 +00:00
|
|
|
Hash hash;
|
2013-08-05 22:31:20 +00:00
|
|
|
|
|
|
|
// These collide.
|
|
|
|
Entry a = { 1, 2.0 };
|
|
|
|
Entry b = { 5, 3.0 };
|
|
|
|
|
|
|
|
// Before we insert anything, nothing can collide.
|
|
|
|
ASSERT(hash.countCollisions(1) == 0);
|
|
|
|
ASSERT(hash.countCollisions(5) == 0);
|
|
|
|
ASSERT(hash.countCollisions(9) == 0);
|
|
|
|
|
|
|
|
// First is easy.
|
|
|
|
hash.add(&a);
|
|
|
|
ASSERT(hash.countCollisions(1) == 0);
|
|
|
|
ASSERT(hash.countCollisions(5) == 1);
|
|
|
|
ASSERT(hash.countCollisions(9) == 1);
|
|
|
|
|
|
|
|
// Second is one step away.
|
|
|
|
hash.add(&b);
|
|
|
|
ASSERT(hash.countCollisions(1) == 0);
|
|
|
|
ASSERT(hash.countCollisions(5) == 1);
|
|
|
|
ASSERT(hash.countCollisions(9) == 2);
|
|
|
|
|
|
|
|
// We can find our data right?
|
2015-08-27 14:41:13 +00:00
|
|
|
ASSERT(hash.find(1) != nullptr);
|
2013-08-05 22:31:20 +00:00
|
|
|
ASSERT(hash.find(1)->value == 2.0);
|
2015-08-27 14:41:13 +00:00
|
|
|
ASSERT(hash.find(5) != nullptr);
|
2013-08-05 22:31:20 +00:00
|
|
|
ASSERT(hash.find(5)->value == 3.0);
|
|
|
|
|
|
|
|
// These aren't in the hash.
|
2015-08-27 14:41:13 +00:00
|
|
|
ASSERT(hash.find(2) == nullptr);
|
|
|
|
ASSERT(hash.find(9) == nullptr);
|
2013-08-05 22:31:20 +00:00
|
|
|
}
|
|
|
|
|
2014-03-05 01:00:50 +00:00
|
|
|
DEF_TEST(DynamicHash_remove, reporter) {
|
2014-01-13 18:28:14 +00:00
|
|
|
Hash hash;
|
2013-08-05 22:31:20 +00:00
|
|
|
|
|
|
|
// These collide.
|
|
|
|
Entry a = { 1, 2.0 };
|
|
|
|
Entry b = { 5, 3.0 };
|
|
|
|
Entry c = { 9, 4.0 };
|
|
|
|
|
|
|
|
hash.add(&a);
|
|
|
|
hash.add(&b);
|
|
|
|
hash.remove(1);
|
|
|
|
// a should be marked deleted, and b should still be findable.
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
ASSERT(hash.find(1) == nullptr);
|
|
|
|
ASSERT(hash.find(5) != nullptr);
|
2013-08-05 22:31:20 +00:00
|
|
|
ASSERT(hash.find(5)->value == 3.0);
|
|
|
|
|
|
|
|
// This will go in the same slot as 'a' did before.
|
|
|
|
ASSERT(hash.countCollisions(9) == 0);
|
|
|
|
hash.add(&c);
|
2015-08-27 14:41:13 +00:00
|
|
|
ASSERT(hash.find(9) != nullptr);
|
2013-08-05 22:31:20 +00:00
|
|
|
ASSERT(hash.find(9)->value == 4.0);
|
2015-08-27 14:41:13 +00:00
|
|
|
ASSERT(hash.find(5) != nullptr);
|
2013-08-05 22:31:20 +00:00
|
|
|
ASSERT(hash.find(5)->value == 3.0);
|
|
|
|
}
|
|
|
|
|
2014-07-20 16:40:00 +00:00
|
|
|
template<typename T> static void TestIter(skiatest::Reporter* reporter) {
|
2014-03-05 01:00:50 +00:00
|
|
|
Hash hash;
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
// this should fall out of loop immediately
|
2014-07-20 16:40:00 +00:00
|
|
|
for (T iter(&hash); !iter.done(); ++iter) {
|
2014-03-05 01:00:50 +00:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
ASSERT(0 == count);
|
|
|
|
|
|
|
|
// These collide.
|
|
|
|
Entry a = { 1, 2.0 };
|
|
|
|
Entry b = { 5, 3.0 };
|
|
|
|
Entry c = { 9, 4.0 };
|
|
|
|
|
|
|
|
hash.add(&a);
|
|
|
|
hash.add(&b);
|
|
|
|
hash.add(&c);
|
|
|
|
|
|
|
|
// should see all 3 unique keys when iterating over hash
|
|
|
|
count = 0;
|
|
|
|
int keys[3] = {0, 0, 0};
|
2014-07-20 16:40:00 +00:00
|
|
|
for (T iter(&hash); !iter.done(); ++iter) {
|
2014-03-05 01:00:50 +00:00
|
|
|
int key = (*iter).key;
|
|
|
|
keys[count] = key;
|
2015-08-27 14:41:13 +00:00
|
|
|
ASSERT(hash.find(key) != nullptr);
|
2014-03-05 01:00:50 +00:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
ASSERT(3 == count);
|
|
|
|
ASSERT(keys[0] != keys[1]);
|
|
|
|
ASSERT(keys[0] != keys[2]);
|
|
|
|
ASSERT(keys[1] != keys[2]);
|
|
|
|
|
|
|
|
// should see 2 unique keys when iterating over hash that aren't 1
|
|
|
|
hash.remove(1);
|
|
|
|
count = 0;
|
2014-07-20 16:40:00 +00:00
|
|
|
memset(keys, 0, sizeof(keys));
|
|
|
|
for (T iter(&hash); !iter.done(); ++iter) {
|
2014-03-05 01:00:50 +00:00
|
|
|
int key = (*iter).key;
|
|
|
|
keys[count] = key;
|
|
|
|
ASSERT(key != 1);
|
2015-08-27 14:41:13 +00:00
|
|
|
ASSERT(hash.find(key) != nullptr);
|
2014-03-05 01:00:50 +00:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
ASSERT(2 == count);
|
|
|
|
ASSERT(keys[0] != keys[1]);
|
2013-08-05 22:31:20 +00:00
|
|
|
}
|
2014-07-20 16:40:00 +00:00
|
|
|
|
|
|
|
DEF_TEST(DynamicHash_iterator, reporter) {
|
|
|
|
TestIter<Hash::Iter>(reporter);
|
|
|
|
TestIter<Hash::ConstIter>(reporter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TestResetOrRewind(skiatest::Reporter* reporter, bool testReset) {
|
|
|
|
Hash hash;
|
|
|
|
Entry a = { 1, 2.0 };
|
|
|
|
Entry b = { 2, 3.0 };
|
|
|
|
|
|
|
|
ASSERT(hash.capacity() == 0);
|
|
|
|
hash.add(&a);
|
|
|
|
hash.add(&b);
|
|
|
|
ASSERT(hash.count() == 2);
|
|
|
|
ASSERT(hash.capacity() == 4);
|
|
|
|
|
|
|
|
if (testReset) {
|
|
|
|
hash.reset();
|
|
|
|
ASSERT(hash.capacity() == 0);
|
|
|
|
} else {
|
|
|
|
hash.rewind();
|
|
|
|
ASSERT(hash.capacity() == 4);
|
|
|
|
}
|
|
|
|
ASSERT(hash.count() == 0);
|
|
|
|
|
|
|
|
// make sure things still work
|
|
|
|
hash.add(&a);
|
|
|
|
hash.add(&b);
|
|
|
|
ASSERT(hash.count() == 2);
|
|
|
|
ASSERT(hash.capacity() == 4);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
ASSERT(hash.find(1) != nullptr);
|
|
|
|
ASSERT(hash.find(2) != nullptr);
|
2014-07-20 16:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(DynamicHash_reset, reporter) {
|
|
|
|
TestResetOrRewind(reporter, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(DynamicHash_rewind, reporter) {
|
|
|
|
TestResetOrRewind(reporter, false);
|
|
|
|
}
|