diff options
Diffstat (limited to 'mempool/test/mempool_test.c')
-rw-r--r-- | mempool/test/mempool_test.c | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/mempool/test/mempool_test.c b/mempool/test/mempool_test.c new file mode 100644 index 0000000..d257922 --- /dev/null +++ b/mempool/test/mempool_test.c | |||
@@ -0,0 +1,157 @@ | |||
1 | #include "mempool.h" | ||
2 | |||
3 | #include "test.h" | ||
4 | |||
5 | #define NUM_BLOCKS 10 | ||
6 | |||
7 | DEF_MEMPOOL(test_pool, int, NUM_BLOCKS); | ||
8 | |||
9 | static int count(test_pool* pool) { | ||
10 | int count = 0; | ||
11 | mempool_foreach(pool, n, { count++; }); | ||
12 | return count; | ||
13 | } | ||
14 | |||
15 | static int sum(test_pool* pool) { | ||
16 | int sum = 0; | ||
17 | mempool_foreach(pool, n, { sum += *n; }); | ||
18 | return sum; | ||
19 | } | ||
20 | |||
21 | // Create a pool. | ||
22 | TEST_CASE(mempool_create) { | ||
23 | test_pool pool; | ||
24 | mempool_make(&pool); | ||
25 | } | ||
26 | |||
27 | // Allocate all N blocks. | ||
28 | TEST_CASE(mempool_allocate_until_full) { | ||
29 | test_pool pool; | ||
30 | mempool_make(&pool); | ||
31 | |||
32 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
33 | const int* block = mempool_alloc(&pool); | ||
34 | TEST_TRUE(block != 0); | ||
35 | } | ||
36 | } | ||
37 | |||
38 | // Allocate all N blocks, then free them. | ||
39 | TEST_CASE(mempool_fill_then_free) { | ||
40 | test_pool pool; | ||
41 | mempool_make(&pool); | ||
42 | |||
43 | int* blocks[NUM_BLOCKS] = {0}; | ||
44 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
45 | blocks[i] = mempool_alloc(&pool); | ||
46 | TEST_TRUE(blocks[i] != 0); | ||
47 | } | ||
48 | |||
49 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
50 | mempool_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(mempool_allocate_beyond_max_size) { | ||
60 | test_pool pool; | ||
61 | mempool_make(&pool); | ||
62 | |||
63 | // Fully allocate the pool. | ||
64 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
65 | TEST_TRUE(mempool_alloc(&pool) != 0); | ||
66 | } | ||
67 | |||
68 | // Past the end. | ||
69 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
70 | TEST_EQUAL(mempool_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(mempool_zero_free_blocks_after_creation) { | ||
77 | test_pool pool; | ||
78 | mempool_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(mempool_zero_free_block_after_free) { | ||
90 | test_pool pool; | ||
91 | mempool_make(&pool); | ||
92 | |||
93 | int* val = mempool_alloc(&pool); | ||
94 | TEST_TRUE(val != 0); | ||
95 | *val = 177; | ||
96 | |||
97 | int* old_val = val; | ||
98 | mempool_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(mempool_traverse_empty) { | ||
104 | test_pool pool; | ||
105 | mempool_make(&pool); | ||
106 | |||
107 | TEST_EQUAL(count(&pool), 0); | ||
108 | } | ||
109 | |||
110 | // Traverse a partially full pool. | ||
111 | TEST_CASE(mempool_traverse_partially_full) { | ||
112 | const int N = NUM_BLOCKS / 2; | ||
113 | |||
114 | test_pool pool; | ||
115 | mempool_make(&pool); | ||
116 | |||
117 | for (int i = 0; i < N; ++i) { | ||
118 | int* val = mempool_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(mempool_traverse_full) { | ||
128 | test_pool pool; | ||
129 | mempool_make(&pool); | ||
130 | |||
131 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
132 | int* val = mempool_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 | // Get the ith (allocated) block. | ||
141 | TEST_CASE(mempool_get_block) { | ||
142 | test_pool pool; | ||
143 | mempool_make(&pool); | ||
144 | |||
145 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
146 | int* block = mempool_alloc(&pool); | ||
147 | TEST_TRUE(block != 0); | ||
148 | *block = i; | ||
149 | TEST_EQUAL(mempool_get_block_index(&pool, block), (size_t)i); | ||
150 | } | ||
151 | |||
152 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
153 | TEST_EQUAL(*mempool_get_block(&pool, i), i); | ||
154 | } | ||
155 | } | ||
156 | |||
157 | int main() { return 0; } | ||