2010-09-28 20:26:55 +04:00
|
|
|
#include "util/probing_hash_table.hh"
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2013-01-24 16:07:46 +04:00
|
|
|
#include "util/murmur_hash.hh"
|
|
|
|
#include "util/scoped.hh"
|
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
#define BOOST_TEST_MODULE ProbingHashTableTest
|
|
|
|
#include <boost/test/unit_test.hpp>
|
2012-05-05 08:55:46 +04:00
|
|
|
#include <boost/scoped_array.hpp>
|
2010-09-10 04:36:07 +04:00
|
|
|
#include <boost/functional/hash.hpp>
|
2012-08-06 13:37:59 +04:00
|
|
|
#include <stdio.h>
|
2013-01-24 16:07:46 +04:00
|
|
|
#include <stdlib.h>
|
2012-08-06 16:16:34 +04:00
|
|
|
#include <string.h>
|
2012-05-05 08:55:46 +04:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
namespace util {
|
|
|
|
namespace {
|
|
|
|
|
2011-11-17 23:12:19 +04:00
|
|
|
struct Entry {
|
|
|
|
unsigned char key;
|
|
|
|
typedef unsigned char Key;
|
|
|
|
|
|
|
|
unsigned char GetKey() const {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2013-01-24 16:07:46 +04:00
|
|
|
void SetKey(unsigned char to) {
|
|
|
|
key = to;
|
|
|
|
}
|
|
|
|
|
2011-11-17 23:12:19 +04:00
|
|
|
uint64_t GetValue() const {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t value;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef ProbingHashTable<Entry, boost::hash<unsigned char> > Table;
|
2010-09-15 01:33:11 +04:00
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
BOOST_AUTO_TEST_CASE(simple) {
|
2012-08-03 15:45:16 +04:00
|
|
|
size_t size = Table::Size(10, 1.2);
|
|
|
|
boost::scoped_array<char> mem(new char[size]);
|
|
|
|
memset(mem.get(), 0, size);
|
2010-09-15 01:33:11 +04:00
|
|
|
|
2012-08-03 15:45:16 +04:00
|
|
|
Table table(mem.get(), size);
|
2011-11-17 23:12:19 +04:00
|
|
|
const Entry *i = NULL;
|
2010-09-15 01:33:11 +04:00
|
|
|
BOOST_CHECK(!table.Find(2, i));
|
2011-11-17 23:12:19 +04:00
|
|
|
Entry to_ins;
|
|
|
|
to_ins.key = 3;
|
|
|
|
to_ins.value = 328920;
|
|
|
|
table.Insert(to_ins);
|
2010-09-15 01:33:11 +04:00
|
|
|
BOOST_REQUIRE(table.Find(3, i));
|
|
|
|
BOOST_CHECK_EQUAL(3, i->GetKey());
|
|
|
|
BOOST_CHECK_EQUAL(static_cast<uint64_t>(328920), i->GetValue());
|
|
|
|
BOOST_CHECK(!table.Find(2, i));
|
2010-09-10 04:36:07 +04:00
|
|
|
}
|
|
|
|
|
2013-01-24 16:07:46 +04:00
|
|
|
struct Entry64 {
|
|
|
|
uint64_t key;
|
|
|
|
typedef uint64_t Key;
|
|
|
|
|
|
|
|
Entry64() {}
|
|
|
|
|
|
|
|
explicit Entry64(uint64_t key_in) {
|
|
|
|
key = key_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
Key GetKey() const { return key; }
|
|
|
|
void SetKey(uint64_t to) { key = to; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MurmurHashEntry64 {
|
|
|
|
std::size_t operator()(uint64_t value) const {
|
|
|
|
return util::MurmurHash64A(&value, 8);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef ProbingHashTable<Entry64, MurmurHashEntry64> Table64;
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(Double) {
|
|
|
|
for (std::size_t initial = 19; initial < 30; ++initial) {
|
|
|
|
size_t size = Table64::Size(initial, 1.2);
|
|
|
|
scoped_malloc mem(MallocOrThrow(size));
|
|
|
|
Table64 table(mem.get(), size, std::numeric_limits<uint64_t>::max());
|
|
|
|
table.Clear();
|
|
|
|
for (uint64_t i = 0; i < 19; ++i) {
|
|
|
|
table.Insert(Entry64(i));
|
|
|
|
}
|
|
|
|
table.CheckConsistency();
|
|
|
|
mem.call_realloc(table.DoubleTo());
|
|
|
|
table.Double(mem.get());
|
|
|
|
table.CheckConsistency();
|
|
|
|
for (uint64_t i = 20; i < 40 ; ++i) {
|
|
|
|
table.Insert(Entry64(i));
|
|
|
|
}
|
|
|
|
mem.call_realloc(table.DoubleTo());
|
|
|
|
table.Double(mem.get());
|
|
|
|
table.CheckConsistency();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
} // namespace
|
|
|
|
} // namespace util
|