| 1 | #include "structures/tests/test_internal.h" |
| 2 | |
| 3 | TEST_GROUP_DECLARE(id_space); |
| 4 | |
| 5 | TEST_DECLARE_UNIT(id_space, alloc_and_free_coalesce) { |
| 6 | struct id_space *is = id_space_init(max_id: 100); |
| 7 | TEST_ASSERT_NONNULL(is); |
| 8 | |
| 9 | /* Allocate sequential IDs */ |
| 10 | uint64_t id1 = id_space_alloc(is); |
| 11 | uint64_t id2 = id_space_alloc(is); |
| 12 | uint64_t id3 = id_space_alloc(is); |
| 13 | |
| 14 | TEST_ASSERT_EQ(id1, 1); |
| 15 | TEST_ASSERT_EQ(id2, 2); |
| 16 | TEST_ASSERT_EQ(id3, 3); |
| 17 | |
| 18 | /* Free middle ID (id2) */ |
| 19 | id_space_free(is, id: id2); |
| 20 | |
| 21 | /* Next allocation should reuse id2 */ |
| 22 | uint64_t reused = id_space_alloc(is); |
| 23 | TEST_ASSERT_EQ(reused, id2); |
| 24 | |
| 25 | /* Free all in non-sequential order and verify coalescing */ |
| 26 | id_space_free(is, id: id1); |
| 27 | id_space_free(is, id: id3); |
| 28 | id_space_free(is, id: reused); |
| 29 | |
| 30 | /* Allocate range */ |
| 31 | uint64_t range_start = id_space_alloc_range(is, count: 10); |
| 32 | TEST_ASSERT_EQ(range_start, 1); |
| 33 | |
| 34 | id_space_free_range(is, start: range_start, count: 10); |
| 35 | |
| 36 | /* Verify after full free that ID 1 is available again */ |
| 37 | uint64_t id_again = id_space_alloc(is); |
| 38 | TEST_ASSERT_EQ(id_again, 1); |
| 39 | id_space_free(is, id: id_again); |
| 40 | |
| 41 | id_space_destroy(is); |
| 42 | return TEST_SUCCESS; |
| 43 | } |
| 44 | |