2015-04-01 18:21:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 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/SkRefCnt.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/private/SkChecksum.h"
|
|
|
|
#include "include/private/SkTHash.h"
|
|
|
|
#include "tests/Test.h"
|
2015-02-12 21:20:08 +00:00
|
|
|
|
2015-03-20 20:48:42 +00:00
|
|
|
// Tests use of const foreach(). map.count() is of course the better way to do this.
|
|
|
|
static int count(const SkTHashMap<int, double>& map) {
|
|
|
|
int n = 0;
|
|
|
|
map.foreach([&n](int, double) { n++; });
|
|
|
|
return n;
|
|
|
|
}
|
2015-02-12 21:20:08 +00:00
|
|
|
|
|
|
|
DEF_TEST(HashMap, r) {
|
2015-03-20 20:48:42 +00:00
|
|
|
SkTHashMap<int, double> map;
|
2015-02-12 21:20:08 +00:00
|
|
|
|
|
|
|
map.set(3, 4.0);
|
|
|
|
REPORTER_ASSERT(r, map.count() == 1);
|
|
|
|
|
2015-08-07 16:33:37 +00:00
|
|
|
REPORTER_ASSERT(r, map.approxBytesUsed() > 0);
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
double* found = map.find(3);
|
|
|
|
REPORTER_ASSERT(r, found);
|
|
|
|
REPORTER_ASSERT(r, *found == 4.0);
|
|
|
|
|
2015-03-20 20:48:42 +00:00
|
|
|
map.foreach([](int key, double* d){ *d = -key; });
|
|
|
|
REPORTER_ASSERT(r, count(map) == 1);
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
found = map.find(3);
|
|
|
|
REPORTER_ASSERT(r, found);
|
|
|
|
REPORTER_ASSERT(r, *found == -3.0);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(r, !map.find(2));
|
|
|
|
|
|
|
|
const int N = 20;
|
|
|
|
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
map.set(i, 2.0*i);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < N; i++) {
|
2015-04-27 14:01:44 +00:00
|
|
|
double* found = map.find(i);
|
2015-02-12 21:20:08 +00:00
|
|
|
REPORTER_ASSERT(r, found);
|
|
|
|
REPORTER_ASSERT(r, *found == i*2.0);
|
|
|
|
}
|
|
|
|
for (int i = N; i < 2*N; i++) {
|
|
|
|
REPORTER_ASSERT(r, !map.find(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
REPORTER_ASSERT(r, map.count() == N);
|
2015-02-20 20:35:32 +00:00
|
|
|
|
2015-04-21 13:53:56 +00:00
|
|
|
for (int i = 0; i < N/2; i++) {
|
|
|
|
map.remove(i);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
double* found = map.find(i);
|
|
|
|
REPORTER_ASSERT(r, (found == nullptr) == (i < N/2));
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(r, map.count() == N/2);
|
|
|
|
|
2015-02-20 20:35:32 +00:00
|
|
|
map.reset();
|
|
|
|
REPORTER_ASSERT(r, map.count() == 0);
|
2017-03-10 16:51:07 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Test that we don't leave dangling values in empty slots.
|
|
|
|
SkTHashMap<int, sk_sp<SkRefCnt>> refMap;
|
|
|
|
auto ref = sk_make_sp<SkRefCnt>();
|
|
|
|
REPORTER_ASSERT(r, ref->unique());
|
|
|
|
|
|
|
|
refMap.set(0, ref);
|
|
|
|
REPORTER_ASSERT(r, refMap.count() == 1);
|
|
|
|
REPORTER_ASSERT(r, !ref->unique());
|
|
|
|
|
|
|
|
refMap.remove(0);
|
|
|
|
REPORTER_ASSERT(r, refMap.count() == 0);
|
|
|
|
REPORTER_ASSERT(r, ref->unique());
|
|
|
|
}
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(HashSet, r) {
|
2015-03-20 20:48:42 +00:00
|
|
|
SkTHashSet<SkString> set;
|
2015-02-12 21:20:08 +00:00
|
|
|
|
|
|
|
set.add(SkString("Hello"));
|
|
|
|
set.add(SkString("World"));
|
|
|
|
|
|
|
|
REPORTER_ASSERT(r, set.count() == 2);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(r, set.contains(SkString("Hello")));
|
|
|
|
REPORTER_ASSERT(r, set.contains(SkString("World")));
|
|
|
|
REPORTER_ASSERT(r, !set.contains(SkString("Goodbye")));
|
2015-02-20 20:35:32 +00:00
|
|
|
|
2015-04-01 18:21:27 +00:00
|
|
|
REPORTER_ASSERT(r, set.find(SkString("Hello")));
|
|
|
|
REPORTER_ASSERT(r, *set.find(SkString("Hello")) == SkString("Hello"));
|
|
|
|
|
2015-04-21 13:53:56 +00:00
|
|
|
set.remove(SkString("Hello"));
|
|
|
|
REPORTER_ASSERT(r, !set.contains(SkString("Hello")));
|
|
|
|
REPORTER_ASSERT(r, set.count() == 1);
|
|
|
|
|
2015-02-20 20:35:32 +00:00
|
|
|
set.reset();
|
|
|
|
REPORTER_ASSERT(r, set.count() == 0);
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
2015-02-13 01:32:49 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CopyCounter {
|
|
|
|
public:
|
2015-08-27 14:41:13 +00:00
|
|
|
CopyCounter() : fID(0), fCounter(nullptr) {}
|
2015-02-13 01:32:49 +00:00
|
|
|
|
|
|
|
CopyCounter(uint32_t id, uint32_t* counter) : fID(id), fCounter(counter) {}
|
|
|
|
|
|
|
|
CopyCounter(const CopyCounter& other)
|
|
|
|
: fID(other.fID)
|
|
|
|
, fCounter(other.fCounter) {
|
|
|
|
SkASSERT(fCounter);
|
|
|
|
*fCounter += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator=(const CopyCounter& other) {
|
|
|
|
fID = other.fID;
|
|
|
|
fCounter = other.fCounter;
|
|
|
|
*fCounter += 1;
|
|
|
|
}
|
|
|
|
|
2016-12-13 17:46:05 +00:00
|
|
|
CopyCounter(CopyCounter&& other) { *this = std::move(other); }
|
|
|
|
void operator=(CopyCounter&& other) {
|
|
|
|
fID = other.fID;
|
|
|
|
fCounter = other.fCounter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-13 01:32:49 +00:00
|
|
|
bool operator==(const CopyCounter& other) const {
|
|
|
|
return fID == other.fID;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t fID;
|
|
|
|
uint32_t* fCounter;
|
|
|
|
};
|
|
|
|
|
2015-10-15 19:23:01 +00:00
|
|
|
struct HashCopyCounter {
|
|
|
|
uint32_t operator()(const CopyCounter&) const {
|
|
|
|
return 0; // let them collide, what do we care?
|
|
|
|
}
|
|
|
|
};
|
2015-02-13 01:32:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(HashSetCopyCounter, r) {
|
2015-10-15 19:23:01 +00:00
|
|
|
SkTHashSet<CopyCounter, HashCopyCounter> set;
|
2015-02-13 01:32:49 +00:00
|
|
|
|
|
|
|
uint32_t globalCounter = 0;
|
|
|
|
CopyCounter copyCounter1(1, &globalCounter);
|
|
|
|
CopyCounter copyCounter2(2, &globalCounter);
|
|
|
|
REPORTER_ASSERT(r, globalCounter == 0);
|
|
|
|
|
|
|
|
set.add(copyCounter1);
|
|
|
|
REPORTER_ASSERT(r, globalCounter == 1);
|
|
|
|
REPORTER_ASSERT(r, set.contains(copyCounter1));
|
|
|
|
REPORTER_ASSERT(r, globalCounter == 1);
|
|
|
|
set.add(copyCounter1);
|
|
|
|
// We allow copies for same-value adds for now.
|
|
|
|
REPORTER_ASSERT(r, globalCounter == 2);
|
|
|
|
|
|
|
|
set.add(copyCounter2);
|
|
|
|
REPORTER_ASSERT(r, globalCounter == 3);
|
|
|
|
REPORTER_ASSERT(r, set.contains(copyCounter1));
|
|
|
|
REPORTER_ASSERT(r, set.contains(copyCounter2));
|
|
|
|
REPORTER_ASSERT(r, globalCounter == 3);
|
|
|
|
set.add(copyCounter1);
|
|
|
|
set.add(copyCounter2);
|
|
|
|
// We allow copies for same-value adds for now.
|
|
|
|
REPORTER_ASSERT(r, globalCounter == 5);
|
|
|
|
}
|
2019-01-09 18:20:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
DEF_TEST(HashFindOrNull, r) {
|
|
|
|
struct Entry {
|
|
|
|
int key = 0;
|
|
|
|
int val = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HashTraits {
|
|
|
|
static int GetKey(const Entry* e) { return e->key; }
|
|
|
|
static uint32_t Hash(int key) { return key; }
|
|
|
|
};
|
|
|
|
|
|
|
|
SkTHashTable<Entry*, int, HashTraits> table;
|
|
|
|
|
|
|
|
REPORTER_ASSERT(r, nullptr == table.findOrNull(7));
|
|
|
|
|
|
|
|
Entry seven = { 7, 24 };
|
|
|
|
table.set(&seven);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(r, &seven == table.findOrNull(7));
|
|
|
|
}
|
2019-12-12 20:07:11 +00:00
|
|
|
|
2020-03-14 15:03:50 +00:00
|
|
|
DEF_TEST(HashTableGrowsAndShrinks, r) {
|
|
|
|
SkTHashSet<int> s;
|
|
|
|
auto check_count_cap = [&](int count, int cap) {
|
|
|
|
REPORTER_ASSERT(r, s.count() == count);
|
|
|
|
REPORTER_ASSERT(r, s.approxBytesUsed() == (sizeof(int) + sizeof(uint32_t)) * cap);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add and remove some elements to test basic growth and shrink patterns.
|
|
|
|
check_count_cap(0,0);
|
|
|
|
s.add(1); check_count_cap(1,4);
|
|
|
|
s.add(2); check_count_cap(2,4);
|
|
|
|
s.add(3); check_count_cap(3,4);
|
|
|
|
s.add(4); check_count_cap(4,8);
|
|
|
|
|
|
|
|
s.remove(4); check_count_cap(3,8);
|
|
|
|
s.remove(3); check_count_cap(2,4);
|
|
|
|
s.remove(2); check_count_cap(1,4);
|
|
|
|
s.remove(1); check_count_cap(0,4);
|
|
|
|
|
|
|
|
s.add(1); check_count_cap(1,4);
|
|
|
|
s.add(2); check_count_cap(2,4);
|
|
|
|
s.add(3); check_count_cap(3,4);
|
|
|
|
s.add(4); check_count_cap(4,8);
|
|
|
|
|
|
|
|
// Add and remove single elements repeatedly to test hysteresis
|
|
|
|
// avoids reallocating these small tables all the time.
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
s. add(5); check_count_cap(5,8);
|
|
|
|
s.remove(5); check_count_cap(4,8);
|
|
|
|
}
|
|
|
|
|
|
|
|
s.remove(4); check_count_cap(3,8);
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
s. add(4); check_count_cap(4,8);
|
|
|
|
s.remove(4); check_count_cap(3,8);
|
|
|
|
}
|
|
|
|
|
|
|
|
s.remove(3); check_count_cap(2,4);
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
s. add(4); check_count_cap(3,4);
|
|
|
|
s.remove(4); check_count_cap(2,4);
|
|
|
|
}
|
|
|
|
|
|
|
|
s.remove(2); check_count_cap(1,4);
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
s. add(2); check_count_cap(2,4);
|
|
|
|
s.remove(2); check_count_cap(1,4);
|
|
|
|
}
|
|
|
|
}
|