1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
#include "test/jemalloc_test.h"
#include "jemalloc/internal/ph.h"
typedef struct node_s node_t;
ph_structs(heap, node_t);
struct node_s {
#define NODE_MAGIC 0x9823af7e
uint32_t magic;
heap_link_t link;
uint64_t key;
};
static int
node_cmp(const node_t *a, const node_t *b) {
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));
}
return ret;
}
static int
node_cmp_magic(const node_t *a, const node_t *b) {
expect_u32_eq(a->magic, NODE_MAGIC, "Bad magic");
expect_u32_eq(b->magic, NODE_MAGIC, "Bad magic");
return node_cmp(a, b);
}
ph_gen(static, heap, node_t, link, node_cmp_magic);
static node_t *
node_next_get(const node_t *node) {
return phn_next_get((node_t *)node, offsetof(node_t, link));
}
static node_t *
node_prev_get(const node_t *node) {
return phn_prev_get((node_t *)node, offsetof(node_t, link));
}
static node_t *
node_lchild_get(const node_t *node) {
return phn_lchild_get((node_t *)node, offsetof(node_t, link));
}
static void
node_print(const node_t *node, unsigned depth) {
unsigned i;
node_t *leftmost_child, *sibling;
for (i = 0; i < depth; i++) {
malloc_printf("\t");
}
malloc_printf("%2"FMTu64"\n", node->key);
leftmost_child = node_lchild_get(node);
if (leftmost_child == NULL) {
return;
}
node_print(leftmost_child, depth + 1);
for (sibling = node_next_get(leftmost_child); sibling !=
NULL; sibling = node_next_get(sibling)) {
node_print(sibling, depth + 1);
}
}
static void
heap_print(const heap_t *heap) {
node_t *auxelm;
malloc_printf("vvv heap %p vvv\n", heap);
if (heap->ph.root == NULL) {
goto label_return;
}
node_print(heap->ph.root, 0);
for (auxelm = node_next_get(heap->ph.root); auxelm != NULL;
auxelm = node_next_get(auxelm)) {
expect_ptr_eq(node_next_get(node_prev_get(auxelm)), auxelm,
"auxelm's prev doesn't link to auxelm");
node_print(auxelm, 0);
}
label_return:
malloc_printf("^^^ heap %p ^^^\n", heap);
}
static unsigned
node_validate(const node_t *node, const node_t *parent) {
unsigned nnodes = 1;
node_t *leftmost_child, *sibling;
if (parent != NULL) {
expect_d_ge(node_cmp_magic(node, parent), 0,
"Child is less than parent");
}
leftmost_child = node_lchild_get(node);
if (leftmost_child == NULL) {
return nnodes;
}
expect_ptr_eq(node_prev_get(leftmost_child),
(void *)node, "Leftmost child does not link to node");
nnodes += node_validate(leftmost_child, node);
for (sibling = node_next_get(leftmost_child); sibling !=
NULL; sibling = node_next_get(sibling)) {
expect_ptr_eq(node_next_get(node_prev_get(sibling)), sibling,
"sibling's prev doesn't link to sibling");
nnodes += node_validate(sibling, node);
}
return nnodes;
}
static unsigned
heap_validate(const heap_t *heap) {
unsigned nnodes = 0;
node_t *auxelm;
if (heap->ph.root == NULL) {
goto label_return;
}
nnodes += node_validate(heap->ph.root, NULL);
for (auxelm = node_next_get(heap->ph.root); auxelm != NULL;
auxelm = node_next_get(auxelm)) {
expect_ptr_eq(node_next_get(node_prev_get(auxelm)), auxelm,
"auxelm's prev doesn't link to auxelm");
nnodes += node_validate(auxelm, NULL);
}
label_return:
if (false) {
heap_print(heap);
}
return nnodes;
}
TEST_BEGIN(test_ph_empty) {
heap_t heap;
heap_new(&heap);
expect_true(heap_empty(&heap), "Heap should be empty");
expect_ptr_null(heap_first(&heap), "Unexpected node");
expect_ptr_null(heap_any(&heap), "Unexpected node");
}
TEST_END
static void
node_remove(heap_t *heap, node_t *node) {
heap_remove(heap, node);
node->magic = 0;
}
static node_t *
node_remove_first(heap_t *heap) {
node_t *node = heap_remove_first(heap);
node->magic = 0;
return node;
}
static node_t *
node_remove_any(heap_t *heap) {
node_t *node = heap_remove_any(heap);
node->magic = 0;
return node;
}
TEST_BEGIN(test_ph_random) {
#define NNODES 25
#define NBAGS 250
#define SEED 42
sfmt_t *sfmt;
uint64_t bag[NNODES];
heap_t heap;
node_t nodes[NNODES];
unsigned i, j, k;
sfmt = init_gen_rand(SEED);
for (i = 0; i < NBAGS; i++) {
switch (i) {
case 0:
/* Insert in order. */
for (j = 0; j < NNODES; j++) {
bag[j] = j;
}
break;
case 1:
/* Insert in reverse order. */
for (j = 0; j < NNODES; j++) {
bag[j] = NNODES - j - 1;
}
break;
default:
for (j = 0; j < NNODES; j++) {
bag[j] = gen_rand64_range(sfmt, NNODES);
}
}
for (j = 1; j <= NNODES; j++) {
/* Initialize heap and nodes. */
heap_new(&heap);
expect_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];
}
/* Insert nodes. */
for (k = 0; k < j; k++) {
heap_insert(&heap, &nodes[k]);
if (i % 13 == 12) {
expect_ptr_not_null(heap_any(&heap),
"Heap should not be empty");
/* Trigger merging. */
expect_ptr_not_null(heap_first(&heap),
"Heap should not be empty");
}
expect_u_eq(heap_validate(&heap), k + 1,
"Incorrect node count");
}
expect_false(heap_empty(&heap),
"Heap should not be empty");
/* Remove nodes. */
switch (i % 6) {
case 0:
for (k = 0; k < j; k++) {
expect_u_eq(heap_validate(&heap), j - k,
"Incorrect node count");
node_remove(&heap, &nodes[k]);
expect_u_eq(heap_validate(&heap), j - k
- 1, "Incorrect node count");
}
break;
case 1:
for (k = j; k > 0; k--) {
node_remove(&heap, &nodes[k-1]);
expect_u_eq(heap_validate(&heap), k - 1,
"Incorrect node count");
}
break;
case 2: {
node_t *prev = NULL;
for (k = 0; k < j; k++) {
node_t *node = node_remove_first(&heap);
expect_u_eq(heap_validate(&heap), j - k
- 1, "Incorrect node count");
if (prev != NULL) {
expect_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);
expect_u_eq(heap_validate(&heap), j - k,
"Incorrect node count");
if (prev != NULL) {
expect_d_ge(node_cmp(node,
prev), 0,
"Bad removal order");
}
node_remove(&heap, node);
expect_u_eq(heap_validate(&heap), j - k
- 1, "Incorrect node count");
prev = node;
}
break;
} case 4: {
for (k = 0; k < j; k++) {
node_remove_any(&heap);
expect_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);
expect_u_eq(heap_validate(&heap), j - k,
"Incorrect node count");
node_remove(&heap, node);
expect_u_eq(heap_validate(&heap), j - k
- 1, "Incorrect node count");
}
break;
} default:
not_reached();
}
expect_ptr_null(heap_first(&heap),
"Heap should be empty");
expect_ptr_null(heap_any(&heap),
"Heap should be empty");
expect_true(heap_empty(&heap), "Heap should be empty");
}
}
fini_gen_rand(sfmt);
#undef NNODES
#undef SEED
}
TEST_END
int
main(void) {
return test(
test_ph_empty,
test_ph_random);
}
|