2016-03-01 03:30:34 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
2017-04-11 07:54:25 +08:00
|
|
|
#include "jemalloc/internal/ph.h"
|
|
|
|
|
2016-03-01 03:30:34 +08:00
|
|
|
typedef struct node_s node_t;
|
|
|
|
|
|
|
|
struct node_s {
|
2017-01-20 13:41:41 +08:00
|
|
|
#define NODE_MAGIC 0x9823af7e
|
2016-03-27 08:30:37 +08:00
|
|
|
uint32_t magic;
|
|
|
|
phn(node_t) link;
|
|
|
|
uint64_t key;
|
2016-03-01 03:30:34 +08:00
|
|
|
};
|
|
|
|
|
2016-03-27 08:30:37 +08:00
|
|
|
static int
|
2017-01-16 08:56:30 +08:00
|
|
|
node_cmp(const node_t *a, const node_t *b) {
|
2016-03-27 08:30:37 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = (a->key > b->key) - (a->key < b->key);
|
|
|
|
if (ret == 0) {
|
|
|
|
/*
|
|
|
|
* Duplicates are not allowed in the heap, so force an
|
|
|
|
* arbitrary ordering for non-identical items with equal keys.
|
|
|
|
*/
|
|
|
|
ret = (((uintptr_t)a) > ((uintptr_t)b))
|
|
|
|
- (((uintptr_t)a) < ((uintptr_t)b));
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2016-03-27 08:30:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
node_cmp_magic(const node_t *a, const node_t *b) {
|
|
|
|
|
|
|
|
assert_u32_eq(a->magic, NODE_MAGIC, "Bad magic");
|
|
|
|
assert_u32_eq(b->magic, NODE_MAGIC, "Bad magic");
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return node_cmp(a, b);
|
2016-03-27 08:30:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef ph(node_t) heap_t;
|
|
|
|
ph_gen(static, heap_, heap_t, node_t, link, node_cmp_magic);
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
node_print(const node_t *node, unsigned depth) {
|
2016-03-27 08:30:37 +08:00
|
|
|
unsigned i;
|
|
|
|
node_t *leftmost_child, *sibling;
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
for (i = 0; i < depth; i++) {
|
2016-03-27 08:30:37 +08:00
|
|
|
malloc_printf("\t");
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-03-27 08:30:37 +08:00
|
|
|
malloc_printf("%2"FMTu64"\n", node->key);
|
|
|
|
|
|
|
|
leftmost_child = phn_lchild_get(node_t, link, node);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (leftmost_child == NULL) {
|
2016-03-27 08:30:37 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-03-27 08:30:37 +08:00
|
|
|
node_print(leftmost_child, depth + 1);
|
|
|
|
|
|
|
|
for (sibling = phn_next_get(node_t, link, leftmost_child); sibling !=
|
|
|
|
NULL; sibling = phn_next_get(node_t, link, sibling)) {
|
|
|
|
node_print(sibling, depth + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
heap_print(const heap_t *heap) {
|
2016-03-27 08:30:37 +08:00
|
|
|
node_t *auxelm;
|
|
|
|
|
|
|
|
malloc_printf("vvv heap %p vvv\n", heap);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (heap->ph_root == NULL) {
|
2016-03-27 08:30:37 +08:00
|
|
|
goto label_return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-03-27 08:30:37 +08:00
|
|
|
|
|
|
|
node_print(heap->ph_root, 0);
|
|
|
|
|
|
|
|
for (auxelm = phn_next_get(node_t, link, heap->ph_root); auxelm != NULL;
|
|
|
|
auxelm = phn_next_get(node_t, link, auxelm)) {
|
|
|
|
assert_ptr_eq(phn_next_get(node_t, link, phn_prev_get(node_t,
|
|
|
|
link, auxelm)), auxelm,
|
|
|
|
"auxelm's prev doesn't link to auxelm");
|
|
|
|
node_print(auxelm, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
label_return:
|
|
|
|
malloc_printf("^^^ heap %p ^^^\n", heap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
node_validate(const node_t *node, const node_t *parent) {
|
2016-03-27 08:30:37 +08:00
|
|
|
unsigned nnodes = 1;
|
|
|
|
node_t *leftmost_child, *sibling;
|
|
|
|
|
|
|
|
if (parent != NULL) {
|
|
|
|
assert_d_ge(node_cmp_magic(node, parent), 0,
|
|
|
|
"Child is less than parent");
|
|
|
|
}
|
|
|
|
|
|
|
|
leftmost_child = phn_lchild_get(node_t, link, node);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (leftmost_child == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return nnodes;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-03-27 08:30:37 +08:00
|
|
|
assert_ptr_eq((void *)phn_prev_get(node_t, link, leftmost_child),
|
|
|
|
(void *)node, "Leftmost child does not link to node");
|
|
|
|
nnodes += node_validate(leftmost_child, node);
|
|
|
|
|
|
|
|
for (sibling = phn_next_get(node_t, link, leftmost_child); sibling !=
|
|
|
|
NULL; sibling = phn_next_get(node_t, link, sibling)) {
|
|
|
|
assert_ptr_eq(phn_next_get(node_t, link, phn_prev_get(node_t,
|
|
|
|
link, sibling)), sibling,
|
|
|
|
"sibling's prev doesn't link to sibling");
|
|
|
|
nnodes += node_validate(sibling, node);
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return nnodes;
|
2016-03-27 08:30:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
heap_validate(const heap_t *heap) {
|
2016-03-27 08:30:37 +08:00
|
|
|
unsigned nnodes = 0;
|
|
|
|
node_t *auxelm;
|
2016-03-01 03:30:34 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (heap->ph_root == NULL) {
|
2016-03-27 08:30:37 +08:00
|
|
|
goto label_return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-03-01 03:30:34 +08:00
|
|
|
|
2016-03-27 08:30:37 +08:00
|
|
|
nnodes += node_validate(heap->ph_root, NULL);
|
|
|
|
|
|
|
|
for (auxelm = phn_next_get(node_t, link, heap->ph_root); auxelm != NULL;
|
|
|
|
auxelm = phn_next_get(node_t, link, auxelm)) {
|
|
|
|
assert_ptr_eq(phn_next_get(node_t, link, phn_prev_get(node_t,
|
|
|
|
link, auxelm)), auxelm,
|
|
|
|
"auxelm's prev doesn't link to auxelm");
|
|
|
|
nnodes += node_validate(auxelm, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
label_return:
|
2017-01-16 08:56:30 +08:00
|
|
|
if (false) {
|
2016-03-27 08:30:37 +08:00
|
|
|
heap_print(heap);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return nnodes;
|
2016-03-27 08:30:37 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_ph_empty) {
|
2016-03-27 08:30:37 +08:00
|
|
|
heap_t heap;
|
|
|
|
|
|
|
|
heap_new(&heap);
|
|
|
|
assert_true(heap_empty(&heap), "Heap should be empty");
|
|
|
|
assert_ptr_null(heap_first(&heap), "Unexpected node");
|
2017-03-04 14:51:21 +08:00
|
|
|
assert_ptr_null(heap_any(&heap), "Unexpected node");
|
2016-03-01 03:30:34 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2016-03-27 08:30:37 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
node_remove(heap_t *heap, node_t *node) {
|
2016-03-27 08:30:37 +08:00
|
|
|
heap_remove(heap, node);
|
|
|
|
|
|
|
|
node->magic = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static node_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
node_remove_first(heap_t *heap) {
|
2016-03-27 08:30:37 +08:00
|
|
|
node_t *node = heap_remove_first(heap);
|
|
|
|
node->magic = 0;
|
2017-01-20 10:15:45 +08:00
|
|
|
return node;
|
2016-03-27 08:30:37 +08:00
|
|
|
}
|
|
|
|
|
2017-03-04 14:51:21 +08:00
|
|
|
static node_t *
|
|
|
|
node_remove_any(heap_t *heap) {
|
|
|
|
node_t *node = heap_remove_any(heap);
|
|
|
|
node->magic = 0;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_ph_random) {
|
2017-01-20 13:41:41 +08:00
|
|
|
#define NNODES 25
|
|
|
|
#define NBAGS 250
|
|
|
|
#define SEED 42
|
2016-03-01 03:30:34 +08:00
|
|
|
sfmt_t *sfmt;
|
2016-03-27 08:30:37 +08:00
|
|
|
uint64_t bag[NNODES];
|
|
|
|
heap_t heap;
|
2016-03-01 03:30:34 +08:00
|
|
|
node_t nodes[NNODES];
|
|
|
|
unsigned i, j, k;
|
|
|
|
|
|
|
|
sfmt = init_gen_rand(SEED);
|
2016-03-27 08:30:37 +08:00
|
|
|
for (i = 0; i < NBAGS; i++) {
|
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
/* Insert in order. */
|
2017-01-16 08:56:30 +08:00
|
|
|
for (j = 0; j < NNODES; j++) {
|
2016-03-27 08:30:37 +08:00
|
|
|
bag[j] = j;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-03-27 08:30:37 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
/* Insert in reverse order. */
|
2017-01-16 08:56:30 +08:00
|
|
|
for (j = 0; j < NNODES; j++) {
|
2016-03-27 08:30:37 +08:00
|
|
|
bag[j] = NNODES - j - 1;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-03-27 08:30:37 +08:00
|
|
|
break;
|
|
|
|
default:
|
2017-01-16 08:56:30 +08:00
|
|
|
for (j = 0; j < NNODES; j++) {
|
2016-03-27 08:30:37 +08:00
|
|
|
bag[j] = gen_rand64_range(sfmt, NNODES);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-03-27 08:30:37 +08:00
|
|
|
}
|
|
|
|
|
2016-03-01 03:30:34 +08:00
|
|
|
for (j = 1; j <= NNODES; j++) {
|
|
|
|
/* Initialize heap and nodes. */
|
2016-03-27 08:30:37 +08:00
|
|
|
heap_new(&heap);
|
|
|
|
assert_u_eq(heap_validate(&heap), 0,
|
|
|
|
"Incorrect node count");
|
|
|
|
for (k = 0; k < j; k++) {
|
|
|
|
nodes[k].magic = NODE_MAGIC;
|
|
|
|
nodes[k].key = bag[k];
|
|
|
|
}
|
2016-03-01 03:30:34 +08:00
|
|
|
|
|
|
|
/* Insert nodes. */
|
|
|
|
for (k = 0; k < j; k++) {
|
2016-03-27 08:30:37 +08:00
|
|
|
heap_insert(&heap, &nodes[k]);
|
|
|
|
if (i % 13 == 12) {
|
2017-03-04 14:51:21 +08:00
|
|
|
assert_ptr_not_null(heap_any(&heap),
|
|
|
|
"Heap should not be empty");
|
2016-03-27 08:30:37 +08:00
|
|
|
/* Trigger merging. */
|
|
|
|
assert_ptr_not_null(heap_first(&heap),
|
|
|
|
"Heap should not be empty");
|
|
|
|
}
|
|
|
|
assert_u_eq(heap_validate(&heap), k + 1,
|
|
|
|
"Incorrect node count");
|
2016-03-01 03:30:34 +08:00
|
|
|
}
|
|
|
|
|
2016-03-27 08:30:37 +08:00
|
|
|
assert_false(heap_empty(&heap),
|
|
|
|
"Heap should not be empty");
|
|
|
|
|
2016-03-01 03:30:34 +08:00
|
|
|
/* Remove nodes. */
|
2017-03-04 14:51:21 +08:00
|
|
|
switch (i % 6) {
|
2016-03-01 03:30:34 +08:00
|
|
|
case 0:
|
2016-03-27 08:30:37 +08:00
|
|
|
for (k = 0; k < j; k++) {
|
|
|
|
assert_u_eq(heap_validate(&heap), j - k,
|
|
|
|
"Incorrect node count");
|
|
|
|
node_remove(&heap, &nodes[k]);
|
|
|
|
assert_u_eq(heap_validate(&heap), j - k
|
|
|
|
- 1, "Incorrect node count");
|
|
|
|
}
|
2016-03-01 03:30:34 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2016-03-27 08:30:37 +08:00
|
|
|
for (k = j; k > 0; k--) {
|
|
|
|
node_remove(&heap, &nodes[k-1]);
|
|
|
|
assert_u_eq(heap_validate(&heap), k - 1,
|
|
|
|
"Incorrect node count");
|
|
|
|
}
|
2016-03-01 03:30:34 +08:00
|
|
|
break;
|
2016-03-27 08:30:37 +08:00
|
|
|
case 2: {
|
|
|
|
node_t *prev = NULL;
|
|
|
|
for (k = 0; k < j; k++) {
|
|
|
|
node_t *node = node_remove_first(&heap);
|
|
|
|
assert_u_eq(heap_validate(&heap), j - k
|
|
|
|
- 1, "Incorrect node count");
|
|
|
|
if (prev != NULL) {
|
|
|
|
assert_d_ge(node_cmp(node,
|
|
|
|
prev), 0,
|
|
|
|
"Bad removal order");
|
|
|
|
}
|
|
|
|
prev = node;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} case 3: {
|
|
|
|
node_t *prev = NULL;
|
|
|
|
for (k = 0; k < j; k++) {
|
|
|
|
node_t *node = heap_first(&heap);
|
|
|
|
assert_u_eq(heap_validate(&heap), j - k,
|
|
|
|
"Incorrect node count");
|
|
|
|
if (prev != NULL) {
|
|
|
|
assert_d_ge(node_cmp(node,
|
|
|
|
prev), 0,
|
|
|
|
"Bad removal order");
|
|
|
|
}
|
|
|
|
node_remove(&heap, node);
|
|
|
|
assert_u_eq(heap_validate(&heap), j - k
|
|
|
|
- 1, "Incorrect node count");
|
|
|
|
prev = node;
|
|
|
|
}
|
|
|
|
break;
|
2017-03-04 14:51:21 +08:00
|
|
|
} case 4: {
|
|
|
|
for (k = 0; k < j; k++) {
|
|
|
|
node_remove_any(&heap);
|
|
|
|
assert_u_eq(heap_validate(&heap), j - k
|
|
|
|
- 1, "Incorrect node count");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} case 5: {
|
|
|
|
for (k = 0; k < j; k++) {
|
|
|
|
node_t *node = heap_any(&heap);
|
|
|
|
assert_u_eq(heap_validate(&heap), j - k,
|
|
|
|
"Incorrect node count");
|
|
|
|
node_remove(&heap, node);
|
|
|
|
assert_u_eq(heap_validate(&heap), j - k
|
|
|
|
- 1, "Incorrect node count");
|
|
|
|
}
|
|
|
|
break;
|
2016-03-27 08:30:37 +08:00
|
|
|
} default:
|
2016-03-01 03:30:34 +08:00
|
|
|
not_reached();
|
|
|
|
}
|
|
|
|
|
2016-03-27 08:30:37 +08:00
|
|
|
assert_ptr_null(heap_first(&heap),
|
|
|
|
"Heap should be empty");
|
2017-03-04 14:51:21 +08:00
|
|
|
assert_ptr_null(heap_any(&heap),
|
|
|
|
"Heap should be empty");
|
2016-03-27 08:30:37 +08:00
|
|
|
assert_true(heap_empty(&heap), "Heap should be empty");
|
2016-03-01 03:30:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fini_gen_rand(sfmt);
|
|
|
|
#undef NNODES
|
|
|
|
#undef SEED
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
main(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return test(
|
2016-03-01 03:30:34 +08:00
|
|
|
test_ph_empty,
|
2017-01-20 10:15:45 +08:00
|
|
|
test_ph_random);
|
2016-03-01 03:30:34 +08:00
|
|
|
}
|