aboutsummaryrefslogtreecommitdiff
path: root/mempool/test/mempool_test.c
blob: 6c48a2ab9d5be9ce9aef5af0fac11cdbbe910649 (plain)
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
#include "mempool.h"

#include "test.h"

#define NUM_BLOCKS 10

DEF_MEMPOOL(test_pool, int, NUM_BLOCKS)

static int count(test_pool* pool) {
  int count = 0;
  mempool_foreach(pool, n, { count++; });
  return count;
}

static int sum(test_pool* pool) {
  int sum = 0;
  mempool_foreach(pool, n, { sum += *n; });
  return sum;
}

// Create a statically-backed pool.
TEST_CASE(mempool_create) {
  test_pool pool;
  mempool_make(&pool);
}

// Create a dynamically-backed pool.
TEST_CASE(mem_create_dyn) {
  DEF_MEMPOOL_DYN(dyn_pool, int);

  dyn_pool pool;
  mempool_make_dyn(&pool, NUM_BLOCKS, sizeof(int));
}

// Allocate all N blocks.
TEST_CASE(mempool_allocate_until_full) {
  test_pool pool;
  mempool_make(&pool);

  for (int i = 0; i < NUM_BLOCKS; ++i) {
    const int* block = mempool_alloc(&pool);
    TEST_TRUE(block != 0);
  }
}

// Allocate all N blocks, then free them.
TEST_CASE(mempool_fill_then_free) {
  test_pool pool;
  mempool_make(&pool);

  int* blocks[NUM_BLOCKS] = {0};
  for (int i = 0; i < NUM_BLOCKS; ++i) {
    blocks[i] = mempool_alloc(&pool);
    TEST_TRUE(blocks[i] != 0);
  }

  for (int i = 0; i < NUM_BLOCKS; ++i) {
    mempool_free(&pool, &blocks[i]);
    TEST_EQUAL(blocks[i], 0); // Pointer should be set to 0 on free.
  }

  TEST_EQUAL(count(&pool), 0);
}

// Attempt to allocate blocks past the maximum pool size.
// The pool should handle the failed allocations gracefully.
TEST_CASE(mempool_allocate_beyond_max_size) {
  test_pool pool;
  mempool_make(&pool);
  mempool_enable_traps(&pool, false);

  // Fully allocate the pool.
  for (int i = 0; i < NUM_BLOCKS; ++i) {
    TEST_TRUE(mempool_alloc(&pool) != 0);
  }

  // Past the end.
  for (int i = 0; i < NUM_BLOCKS; ++i) {
    TEST_EQUAL(mempool_alloc(&pool), 0);
  }
}

// Free blocks should always remain zeroed out.
// This tests the invariant right after creating the pool.
TEST_CASE(mempool_zero_free_blocks_after_creation) {
  test_pool pool;
  mempool_make(&pool);

  const int zero = 0;
  for (int i = 0; i < NUM_BLOCKS; ++i) {
    const int* block = (const int*)(pool.blocks) + i;
    TEST_EQUAL(memcmp(block, &zero, sizeof(int)), 0);
  }
}

// Free blocks should always remain zeroed out.
// This tests the invariant after freeing a block.
TEST_CASE(mempool_zero_free_block_after_free) {
  test_pool pool;
  mempool_make(&pool);

  int* val = mempool_alloc(&pool);
  TEST_TRUE(val != 0);
  *val = 177;

  int* old_val = val;
  mempool_free(&pool, &val); // val pointer is set to 0.
  TEST_EQUAL(*old_val, 0);   // Block is zeroed out after free.
}

// Traverse an empty pool.
TEST_CASE(mempool_traverse_empty) {
  test_pool pool;
  mempool_make(&pool);

  TEST_EQUAL(count(&pool), 0);
}

// Traverse a partially full pool.
TEST_CASE(mempool_traverse_partially_full) {
  const int N = NUM_BLOCKS / 2;

  test_pool pool;
  mempool_make(&pool);

  for (int i = 0; i < N; ++i) {
    int* val = mempool_alloc(&pool);
    TEST_TRUE(val != 0);
    *val = i + 1;
  }

  TEST_EQUAL(sum(&pool), N * (N + 1) / 2);
}

// Traverse a full pool.
TEST_CASE(mempool_traverse_full) {
  test_pool pool;
  mempool_make(&pool);

  for (int i = 0; i < NUM_BLOCKS; ++i) {
    int* val = mempool_alloc(&pool);
    TEST_TRUE(val != 0);
    *val = i + 1;
  }

  TEST_EQUAL(sum(&pool), NUM_BLOCKS * (NUM_BLOCKS + 1) / 2);
}

// Get the ith (allocated) block.
TEST_CASE(mempool_get_block) {
  test_pool pool;
  mempool_make(&pool);

  for (int i = 0; i < NUM_BLOCKS; ++i) {
    int* block = mempool_alloc(&pool);
    TEST_TRUE(block != 0);
    *block = i;
    TEST_EQUAL(mempool_get_block_index(&pool, block), (size_t)i);
  }

  for (int i = 0; i < NUM_BLOCKS; ++i) {
    TEST_EQUAL(*mempool_get_block(&pool, i), i);
  }
}

// Clear and re-use an allocator.
TEST_CASE(mem_clear_then_reuse) {
  test_pool mem;
  mempool_make(&mem);

  // Allocate chunks, contents not important.
  for (int i = 0; i < NUM_BLOCKS; ++i) {
    int* chunk = mempool_alloc(&mem);
    TEST_TRUE(chunk != 0);
  }

  mempool_clear(&mem);

  // Allocate chunks and assign values 0..N.
  for (int i = 0; i < NUM_BLOCKS; ++i) {
    int* chunk = mempool_alloc(&mem);
    TEST_TRUE(chunk != 0);
    *chunk = i + 1;
  }

  TEST_EQUAL(sum(&mem), NUM_BLOCKS * (NUM_BLOCKS + 1) / 2);
}

int main() { return 0; }