use scoped_refptr to new HashIndex (#3818)

This commit is contained in:
HuangWei
2020-06-10 23:47:10 +08:00
committed by GitHub
parent cd402a6827
commit 8caedadb67

View File

@ -32,14 +32,14 @@ inline uint64_t HashCode(uint64_t v) {
TEST(HashIndex, findset) {
size_t sz = 20;
HashIndex hi(sz);
scoped_refptr<HashIndex> hi(new HashIndex(sz));
std::vector<uint32_t> entries;
entries.reserve(10);
// add all
for (size_t i = 0; i < sz * 2; i += 2) {
uint64_t keyHash = HashCode(i);
entries.clear();
uint64_t slot = hi.find(keyHash, &entries);
uint64_t slot = hi->find(keyHash, &entries);
bool found = false;
for (auto& e : entries) {
uint64_t keyHashVerify = HashCode(e);
@ -53,13 +53,13 @@ TEST(HashIndex, findset) {
}
EXPECT_FALSE(found);
EXPECT_NE(slot, HashIndex::npos);
hi.set(slot, keyHash, i);
hi->set(slot, keyHash, i);
}
// search
for (size_t i = 0; i < sz * 2; i += 2) {
uint64_t keyHash = HashCode(i);
entries.clear();
hi.find(keyHash, &entries);
hi->find(keyHash, &entries);
uint64_t fslot = HashIndex::npos;
for (auto& e : entries) {
uint64_t keyHashVerify = HashCode(e);
@ -73,24 +73,24 @@ TEST(HashIndex, findset) {
}
EXPECT_NE(fslot, HashIndex::npos);
}
LOG(INFO) << hi.dump();
LOG(INFO) << hi->dump();
}
TEST(HashIndex, add) {
srand(1);
size_t N = 1000;
HashIndex hi(N);
scoped_refptr<HashIndex> hi(new HashIndex(N));
std::vector<int64_t> keys(N);
for (size_t i = 0; i < N; ++i) {
keys[i] = rand();
uint64_t hashcode = HashCode(keys[i]);
EXPECT_TRUE(hi.add(hashcode, i));
EXPECT_TRUE(hi->add(hashcode, i));
}
std::vector<uint32_t> entries;
entries.reserve(10);
for (size_t i = 0; i < N; ++i) {
uint64_t hashcode = HashCode(keys[i]);
hi.find(hashcode, &entries);
hi->find(hashcode, &entries);
bool found = false;
for (size_t ei = 0; ei < entries.size(); ++ei) {
int64_t v = keys[entries[ei]];
@ -101,7 +101,7 @@ TEST(HashIndex, add) {
}
EXPECT_TRUE(found);
}
LOG(INFO) << hi.dump();
LOG(INFO) << hi->dump();
}
} // namespace memory