2014-01-03 08:08:28 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
2015-01-31 14:54:08 +08:00
|
|
|
static rtree_node_elm_t *
|
|
|
|
node_alloc(size_t nelms)
|
|
|
|
{
|
|
|
|
|
2015-07-11 05:33:00 +08:00
|
|
|
return ((rtree_node_elm_t *)calloc(nelms, sizeof(rtree_node_elm_t)));
|
2015-01-31 14:54:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
node_dalloc(rtree_node_elm_t *node)
|
|
|
|
{
|
|
|
|
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
2014-01-03 08:08:28 +08:00
|
|
|
TEST_BEGIN(test_rtree_get_empty)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
|
2015-01-31 14:54:08 +08:00
|
|
|
rtree_t rtree;
|
|
|
|
assert_false(rtree_new(&rtree, i, node_alloc, node_dalloc),
|
|
|
|
"Unexpected rtree_new() failure");
|
2015-05-16 08:02:30 +08:00
|
|
|
assert_ptr_null(rtree_get(&rtree, 0, false),
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return NULL for empty tree");
|
2015-01-31 14:54:08 +08:00
|
|
|
rtree_delete(&rtree);
|
2014-01-03 08:08:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_BEGIN(test_rtree_extrema)
|
|
|
|
{
|
|
|
|
unsigned i;
|
2016-03-24 12:09:28 +08:00
|
|
|
extent_t extent_a, extent_b;
|
2014-01-03 08:08:28 +08:00
|
|
|
|
|
|
|
for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
|
2015-01-31 14:54:08 +08:00
|
|
|
rtree_t rtree;
|
|
|
|
assert_false(rtree_new(&rtree, i, node_alloc, node_dalloc),
|
|
|
|
"Unexpected rtree_new() failure");
|
2014-01-03 08:08:28 +08:00
|
|
|
|
2016-03-24 12:09:28 +08:00
|
|
|
assert_false(rtree_set(&rtree, 0, &extent_a),
|
2015-08-07 15:51:11 +08:00
|
|
|
"Unexpected rtree_set() failure");
|
2016-03-24 12:09:28 +08:00
|
|
|
assert_ptr_eq(rtree_get(&rtree, 0, true), &extent_a,
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return previously set value");
|
|
|
|
|
2016-03-24 12:09:28 +08:00
|
|
|
assert_false(rtree_set(&rtree, ~((uintptr_t)0), &extent_b),
|
2015-08-07 15:51:11 +08:00
|
|
|
"Unexpected rtree_set() failure");
|
2016-03-24 12:09:28 +08:00
|
|
|
assert_ptr_eq(rtree_get(&rtree, ~((uintptr_t)0), true),
|
|
|
|
&extent_b,
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return previously set value");
|
|
|
|
|
2015-01-31 14:54:08 +08:00
|
|
|
rtree_delete(&rtree);
|
2014-01-03 08:08:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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};
|
2016-03-24 12:09:28 +08:00
|
|
|
extent_t extent;
|
2015-01-31 14:54:08 +08:00
|
|
|
rtree_t rtree;
|
|
|
|
|
|
|
|
assert_false(rtree_new(&rtree, i, node_alloc, node_dalloc),
|
|
|
|
"Unexpected rtree_new() failure");
|
2014-01-03 08:08:28 +08:00
|
|
|
|
|
|
|
for (j = 0; j < sizeof(keys)/sizeof(uintptr_t); j++) {
|
2016-03-24 12:09:28 +08:00
|
|
|
assert_false(rtree_set(&rtree, keys[j], &extent),
|
2015-08-07 15:51:11 +08:00
|
|
|
"Unexpected rtree_set() failure");
|
2014-01-03 08:08:28 +08:00
|
|
|
for (k = 0; k < sizeof(keys)/sizeof(uintptr_t); k++) {
|
2015-05-16 08:02:30 +08:00
|
|
|
assert_ptr_eq(rtree_get(&rtree, keys[k], true),
|
2016-03-24 12:09:28 +08:00
|
|
|
&extent, "rtree_get() should return "
|
2015-05-16 08:02:30 +08:00
|
|
|
"previously set value and ignore "
|
|
|
|
"insignificant key bits; i=%u, j=%u, k=%u, "
|
2015-07-24 04:56:25 +08:00
|
|
|
"set key=%#"FMTxPTR", get key=%#"FMTxPTR, i,
|
2015-05-16 08:02:30 +08:00
|
|
|
j, k, keys[j], keys[k]);
|
2014-01-03 08:08:28 +08:00
|
|
|
}
|
2015-03-19 09:55:33 +08:00
|
|
|
assert_ptr_null(rtree_get(&rtree,
|
2015-05-16 08:02:30 +08:00
|
|
|
(((uintptr_t)1) << (sizeof(uintptr_t)*8-i)), false),
|
2014-01-03 08:08:28 +08:00
|
|
|
"Only leftmost rtree leaf should be set; "
|
|
|
|
"i=%u, j=%u", i, j);
|
2015-08-07 15:51:11 +08:00
|
|
|
assert_false(rtree_set(&rtree, keys[j], NULL),
|
|
|
|
"Unexpected rtree_set() failure");
|
2014-01-03 08:08:28 +08:00
|
|
|
}
|
|
|
|
|
2015-01-31 14:54:08 +08:00
|
|
|
rtree_delete(&rtree);
|
2014-01-03 08:08:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_BEGIN(test_rtree_random)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
sfmt_t *sfmt;
|
2015-01-31 14:54:08 +08:00
|
|
|
#define NSET 16
|
2014-01-03 08:08:28 +08:00
|
|
|
#define SEED 42
|
|
|
|
|
|
|
|
sfmt = init_gen_rand(SEED);
|
|
|
|
for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
|
|
|
|
uintptr_t keys[NSET];
|
2016-03-24 12:09:28 +08:00
|
|
|
extent_t extent;
|
2014-01-03 08:08:28 +08:00
|
|
|
unsigned j;
|
2015-01-31 14:54:08 +08:00
|
|
|
rtree_t rtree;
|
|
|
|
|
|
|
|
assert_false(rtree_new(&rtree, i, node_alloc, node_dalloc),
|
|
|
|
"Unexpected rtree_new() failure");
|
2014-01-03 08:08:28 +08:00
|
|
|
|
|
|
|
for (j = 0; j < NSET; j++) {
|
|
|
|
keys[j] = (uintptr_t)gen_rand64(sfmt);
|
2016-03-24 12:09:28 +08:00
|
|
|
assert_false(rtree_set(&rtree, keys[j], &extent),
|
2015-08-07 15:51:11 +08:00
|
|
|
"Unexpected rtree_set() failure");
|
2016-03-24 12:09:28 +08:00
|
|
|
assert_ptr_eq(rtree_get(&rtree, keys[j], true), &extent,
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return previously set value");
|
|
|
|
}
|
|
|
|
for (j = 0; j < NSET; j++) {
|
2016-03-24 12:09:28 +08:00
|
|
|
assert_ptr_eq(rtree_get(&rtree, keys[j], true), &extent,
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return previously set value");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < NSET; j++) {
|
2015-08-07 15:51:11 +08:00
|
|
|
assert_false(rtree_set(&rtree, keys[j], NULL),
|
|
|
|
"Unexpected rtree_set() failure");
|
2015-05-16 08:02:30 +08:00
|
|
|
assert_ptr_null(rtree_get(&rtree, keys[j], true),
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return previously set value");
|
|
|
|
}
|
|
|
|
for (j = 0; j < NSET; j++) {
|
2015-05-16 08:02:30 +08:00
|
|
|
assert_ptr_null(rtree_get(&rtree, keys[j], true),
|
2014-01-03 08:08:28 +08:00
|
|
|
"rtree_get() should return previously set value");
|
|
|
|
}
|
|
|
|
|
2015-01-31 14:54:08 +08:00
|
|
|
rtree_delete(&rtree);
|
2014-01-03 08:08:28 +08:00
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|