2014-01-03 08:08:28 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
|
|
|
TEST_BEGIN(test_rtree_get_empty)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
|
|
|
|
rtree_t *rtree = rtree_new(i, imalloc, idalloc);
|
2014-01-03 09:36:38 +08:00
|
|
|
assert_u_eq(rtree_get(rtree, 0), 0,
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return NULL for empty tree");
|
|
|
|
rtree_delete(rtree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_BEGIN(test_rtree_extrema)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
|
|
|
|
rtree_t *rtree = rtree_new(i, imalloc, idalloc);
|
|
|
|
|
2014-01-03 09:36:38 +08:00
|
|
|
rtree_set(rtree, 0, 1);
|
|
|
|
assert_u_eq(rtree_get(rtree, 0), 1,
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return previously set value");
|
|
|
|
|
2014-01-03 09:36:38 +08:00
|
|
|
rtree_set(rtree, ~((uintptr_t)0), 1);
|
|
|
|
assert_u_eq(rtree_get(rtree, ~((uintptr_t)0)), 1,
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return previously set value");
|
|
|
|
|
|
|
|
rtree_delete(rtree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_BEGIN(test_rtree_bits)
|
|
|
|
{
|
|
|
|
unsigned i, j, k;
|
|
|
|
|
|
|
|
for (i = 1; i < (sizeof(uintptr_t) << 3); i++) {
|
|
|
|
uintptr_t keys[] = {0, 1,
|
|
|
|
(((uintptr_t)1) << (sizeof(uintptr_t)*8-i)) - 1};
|
|
|
|
rtree_t *rtree = rtree_new(i, imalloc, idalloc);
|
|
|
|
|
|
|
|
for (j = 0; j < sizeof(keys)/sizeof(uintptr_t); j++) {
|
2014-01-03 09:36:38 +08:00
|
|
|
rtree_set(rtree, keys[j], 1);
|
2014-01-03 08:08:28 +08:00
|
|
|
for (k = 0; k < sizeof(keys)/sizeof(uintptr_t); k++) {
|
2014-01-03 09:36:38 +08:00
|
|
|
assert_u_eq(rtree_get(rtree, keys[k]), 1,
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return previously set "
|
|
|
|
"value and ignore insignificant key bits; "
|
|
|
|
"i=%u, j=%u, k=%u, set key=%#x, "
|
|
|
|
"get key=%#x", i, j, k, keys[j], keys[k]);
|
|
|
|
}
|
2014-01-03 09:36:38 +08:00
|
|
|
assert_u_eq(rtree_get(rtree,
|
|
|
|
(((uintptr_t)1) << (sizeof(uintptr_t)*8-i))), 0,
|
2014-01-03 08:08:28 +08:00
|
|
|
"Only leftmost rtree leaf should be set; "
|
|
|
|
"i=%u, j=%u", i, j);
|
2014-01-03 09:36:38 +08:00
|
|
|
rtree_set(rtree, keys[j], 0);
|
2014-01-03 08:08:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rtree_delete(rtree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_BEGIN(test_rtree_random)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
sfmt_t *sfmt;
|
|
|
|
#define NSET 100
|
|
|
|
#define SEED 42
|
|
|
|
|
|
|
|
sfmt = init_gen_rand(SEED);
|
|
|
|
for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
|
|
|
|
rtree_t *rtree = rtree_new(i, imalloc, idalloc);
|
|
|
|
uintptr_t keys[NSET];
|
|
|
|
unsigned j;
|
|
|
|
|
|
|
|
for (j = 0; j < NSET; j++) {
|
|
|
|
keys[j] = (uintptr_t)gen_rand64(sfmt);
|
2014-01-03 09:36:38 +08:00
|
|
|
rtree_set(rtree, keys[j], 1);
|
|
|
|
assert_u_eq(rtree_get(rtree, keys[j]), 1,
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return previously set value");
|
|
|
|
}
|
|
|
|
for (j = 0; j < NSET; j++) {
|
2014-01-03 09:36:38 +08:00
|
|
|
assert_u_eq(rtree_get(rtree, keys[j]), 1,
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return previously set value");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < NSET; j++) {
|
2014-01-03 09:36:38 +08:00
|
|
|
rtree_set(rtree, keys[j], 0);
|
|
|
|
assert_u_eq(rtree_get(rtree, keys[j]), 0,
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return previously set value");
|
|
|
|
}
|
|
|
|
for (j = 0; j < NSET; j++) {
|
2014-01-03 09:36:38 +08:00
|
|
|
assert_u_eq(rtree_get(rtree, keys[j]), 0,
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return previously set value");
|
|
|
|
}
|
|
|
|
|
|
|
|
rtree_delete(rtree);
|
|
|
|
}
|
|
|
|
fini_gen_rand(sfmt);
|
|
|
|
#undef NSET
|
|
|
|
#undef SEED
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (test(
|
|
|
|
test_rtree_get_empty,
|
|
|
|
test_rtree_extrema,
|
|
|
|
test_rtree_bits,
|
|
|
|
test_rtree_random));
|
|
|
|
}
|