diff options
| author | 3gg <3gg@shellblade.net> | 2021-12-04 16:01:12 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2021-12-04 16:01:12 -0800 |
| commit | f8217d240d598f39f70047f7a623dd46312542c6 (patch) | |
| tree | 4e40843d665e388416c1226f739c2b8c0b8da736 /listpool/test/listpool_test.c | |
| parent | 5f6ea503cdb6ad4a95b679672a1ad324d96c89a5 (diff) | |
Initial commit.
Diffstat (limited to 'listpool/test/listpool_test.c')
| -rw-r--r-- | listpool/test/listpool_test.c | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/listpool/test/listpool_test.c b/listpool/test/listpool_test.c new file mode 100644 index 0000000..cb54d00 --- /dev/null +++ b/listpool/test/listpool_test.c | |||
| @@ -0,0 +1,166 @@ | |||
| 1 | #include "listpool.h" | ||
| 2 | |||
| 3 | #include "test.h" | ||
| 4 | |||
| 5 | #define NUM_BLOCKS 10 | ||
| 6 | |||
| 7 | DEF_LISTPOOL(test_pool, int, NUM_BLOCKS); | ||
| 8 | |||
| 9 | static int count(test_pool* pool) { | ||
| 10 | int count = 0; | ||
| 11 | listpool_foreach(pool, n, { count++; }); | ||
| 12 | return count; | ||
| 13 | } | ||
| 14 | |||
| 15 | static int sum(test_pool* pool) { | ||
| 16 | int sum = 0; | ||
| 17 | listpool_foreach(pool, n, { sum += *n; }); | ||
| 18 | return sum; | ||
| 19 | } | ||
| 20 | |||
| 21 | // Create a pool. | ||
| 22 | TEST_CASE(listpool_create) { | ||
| 23 | test_pool pool; | ||
| 24 | listpool_make(&pool); | ||
| 25 | } | ||
| 26 | |||
| 27 | // Allocate all N blocks. | ||
| 28 | TEST_CASE(listpool_fully_allocate) { | ||
| 29 | test_pool pool; | ||
| 30 | listpool_make(&pool); | ||
| 31 | |||
| 32 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 33 | const int* block = listpool_alloc(&pool); | ||
| 34 | TEST_TRUE(block != 0); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | // Allocate all N blocks, then free them. | ||
| 39 | TEST_CASE(listpool_fill_then_free) { | ||
| 40 | test_pool pool; | ||
| 41 | listpool_make(&pool); | ||
| 42 | |||
| 43 | int* blocks[NUM_BLOCKS] = {0}; | ||
| 44 | for (int i = 0; i < NUM_BLOCKS; i++) { | ||
| 45 | blocks[i] = listpool_alloc(&pool); | ||
| 46 | TEST_TRUE(blocks[i] != 0); | ||
| 47 | } | ||
| 48 | |||
| 49 | for (int i = 0; i < NUM_BLOCKS; i++) { | ||
| 50 | listpool_free(&pool, &blocks[i]); | ||
| 51 | TEST_EQUAL(blocks[i], 0); // Pointer should be set to 0 on free. | ||
| 52 | } | ||
| 53 | |||
| 54 | TEST_EQUAL(count(&pool), 0); | ||
| 55 | } | ||
| 56 | |||
| 57 | // Attempt to allocate blocks past the maximum pool size. | ||
| 58 | // The pool should handle the failed allocations gracefully. | ||
| 59 | TEST_CASE(listpool_allocate_beyond_max_size) { | ||
| 60 | test_pool pool; | ||
| 61 | listpool_make(&pool); | ||
| 62 | |||
| 63 | // Fully allocate the pool. | ||
| 64 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 65 | TEST_TRUE(listpool_alloc(&pool) != 0); | ||
| 66 | } | ||
| 67 | |||
| 68 | // Past the end. | ||
| 69 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 70 | TEST_EQUAL(listpool_alloc(&pool), 0); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | // Free blocks should always remain zeroed out. | ||
| 75 | // This tests the invariant right after creating the pool. | ||
| 76 | TEST_CASE(listpool_zero_free_blocks_after_creation) { | ||
| 77 | test_pool pool; | ||
| 78 | listpool_make(&pool); | ||
| 79 | |||
| 80 | const int zero = 0; | ||
| 81 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 82 | const int* block = (const int*)(pool.blocks) + i; | ||
| 83 | TEST_EQUAL(memcmp(block, &zero, sizeof(int)), 0); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | // Free blocks should always remain zeroed out. | ||
| 88 | // This tests the invariant after freeing a block. | ||
| 89 | TEST_CASE(listpool_zero_free_block_after_free) { | ||
| 90 | test_pool pool; | ||
| 91 | listpool_make(&pool); | ||
| 92 | |||
| 93 | int* val = listpool_alloc(&pool); | ||
| 94 | TEST_TRUE(val != 0); | ||
| 95 | *val = 177; | ||
| 96 | |||
| 97 | int* old_val = val; | ||
| 98 | listpool_free(&pool, &val); // val pointer is set to 0. | ||
| 99 | TEST_EQUAL(*old_val, 0); // Block is zeroed out after free. | ||
| 100 | } | ||
| 101 | |||
| 102 | // Traverse an empty pool. | ||
| 103 | TEST_CASE(listpool_traverse_empty) { | ||
| 104 | test_pool pool; | ||
| 105 | listpool_make(&pool); | ||
| 106 | |||
| 107 | TEST_EQUAL(count(&pool), 0); | ||
| 108 | } | ||
| 109 | |||
| 110 | // Traverse a partially full pool. | ||
| 111 | TEST_CASE(listpool_traverse_partially_full) { | ||
| 112 | const int N = NUM_BLOCKS / 2; | ||
| 113 | |||
| 114 | test_pool pool; | ||
| 115 | listpool_make(&pool); | ||
| 116 | |||
| 117 | for (int i = 0; i < N; ++i) { | ||
| 118 | int* val = listpool_alloc(&pool); | ||
| 119 | TEST_TRUE(val != 0); | ||
| 120 | *val = i + 1; | ||
| 121 | } | ||
| 122 | |||
| 123 | TEST_EQUAL(sum(&pool), (N) * (N + 1) / 2); | ||
| 124 | } | ||
| 125 | |||
| 126 | // Traverse a full pool. | ||
| 127 | TEST_CASE(listpool_traverse_full) { | ||
| 128 | test_pool pool; | ||
| 129 | listpool_make(&pool); | ||
| 130 | |||
| 131 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 132 | int* val = listpool_alloc(&pool); | ||
| 133 | TEST_TRUE(val != 0); | ||
| 134 | *val = i + 1; | ||
| 135 | } | ||
| 136 | |||
| 137 | TEST_EQUAL(sum(&pool), (NUM_BLOCKS) * (NUM_BLOCKS + 1) / 2); | ||
| 138 | } | ||
| 139 | |||
| 140 | // Remove a value from the list. | ||
| 141 | TEST_CASE(listpool_remove_value) { | ||
| 142 | test_pool pool; | ||
| 143 | listpool_make(&pool); | ||
| 144 | |||
| 145 | int* x = listpool_alloc(&pool); | ||
| 146 | int* y = listpool_alloc(&pool); | ||
| 147 | TEST_TRUE(x != 0); | ||
| 148 | TEST_TRUE(y != 0); | ||
| 149 | |||
| 150 | *x = 155; | ||
| 151 | *y = 177; | ||
| 152 | |||
| 153 | listpool_remove(&pool, 155); // x | ||
| 154 | |||
| 155 | TEST_EQUAL(count(&pool), 1); | ||
| 156 | TEST_EQUAL(sum(&pool), *y); | ||
| 157 | } | ||
| 158 | |||
| 159 | // Stress test. | ||
| 160 | // | ||
| 161 | // 1. Allocate the pool, either fully or partially. If fully, attempt to | ||
| 162 | // allocate some items past the end. | ||
| 163 | // | ||
| 164 | // 2. Free all allocated items in some random order. | ||
| 165 | |||
| 166 | int main() { return 0; } | ||
