| 1 | #include "structures/tests/test_internal.h" |
| 2 | |
| 3 | TEST_GROUP_DECLARE(minheap, .intensity_desc = { |
| 4 | .curve = SCALE_PIECEWISE_LOG, |
| 5 | .unit = "nodes" , |
| 6 | }); |
| 7 | |
| 8 | static void mhtest_do_inserts(struct minheap *mh, struct minheap_node **nodes, |
| 9 | size_t count) { |
| 10 | for (size_t i = 0; i < count; i++) { |
| 11 | struct minheap_node *mhn = |
| 12 | kmalloc(sizeof(struct minheap_node), ALLOC_FLAGS_ZERO); |
| 13 | mhn->key = count - i; |
| 14 | nodes[i] = mhn; |
| 15 | minheap_insert(heap: mh, node: mhn, key: mhn->key); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | TEST_DECLARE_UNIT(minheap, basic_ops, TEST_INTENSITY(10, 50, 1024)) { |
| 20 | size_t count = ctx->intensity_val ? ctx->intensity_val : 50; |
| 21 | struct minheap_node **nodes = |
| 22 | kmalloc(sizeof(struct minheap_node *) * count, ALLOC_FLAGS_ZERO); |
| 23 | TEST_ASSERT_NONNULL(nodes); |
| 24 | |
| 25 | struct minheap *mh = minheap_create(); |
| 26 | mhtest_do_inserts(mh, nodes, count); |
| 27 | TEST_ASSERT_EQ(mh->size, count); |
| 28 | |
| 29 | for (size_t i = 0; i < count; i++) { |
| 30 | struct minheap_node *mhn = nodes[i]; |
| 31 | minheap_remove(heap: mh, node: mhn); |
| 32 | kfree(mhn); |
| 33 | nodes[i] = NULL; |
| 34 | } |
| 35 | |
| 36 | TEST_ASSERT_EQ(mh->size, 0); |
| 37 | mhtest_do_inserts(mh, nodes, count); |
| 38 | |
| 39 | TEST_ASSERT_EQ(minheap_peek(mh)->key, 1); |
| 40 | struct minheap_node *popped = minheap_pop(heap: mh); |
| 41 | TEST_ASSERT_EQ(popped->key, 1); |
| 42 | |
| 43 | kfree(nodes); |
| 44 | return TEST_SUCCESS; |
| 45 | } |
| 46 | |