Add memory pool test, fix bugs

This commit is contained in:
Rickey Fehr
2025-10-17 18:05:36 -07:00
parent 16059ab632
commit 138792df48
11 changed files with 171 additions and 25 deletions
+17
View File
@@ -0,0 +1,17 @@
POOLS_DEF_FILE := \"test_pools.def\"
CC := gcc
CFLAGS := -I../../include -I. \
-g -O3 -Wall -Werror -DPOOLS_DEF_FILE=$(POOLS_DEF_FILE)
SRC := pool_test.c ../../source/pool.c
OUT := build/pool_test
$(OUT): $(SRC) | build
$(CC) $(CFLAGS) -o $@ $^
build:
mkdir -p build
clean:
rm -f $(OUT)
+87
View File
@@ -0,0 +1,87 @@
#include "pool.h"
#include "test_structures.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int get_random(int low, int high)
{
srand(time(NULL));
int n = low + rand() % (high - low + 1);
return n;
}
bool test_fill(ChunkOfData* myPtrs[], int check_size)
{
int itr = 0;
ChunkOfData* test_chunk = NULL;
do
{
test_chunk = POOL_GET(ChunkOfData);
if(test_chunk != NULL) myPtrs[itr++] = test_chunk;
} while(test_chunk != NULL);
if(itr != check_size)
{
fprintf(stderr, "Error: failed to get expected number of valid pointers\n"
" expected: %d, actual %d\n", check_size, itr);
return false;
}
return true;
}
bool test_fill_and_empty(void)
{
ChunkOfData* myPtrs[TEST_SIZE];
if(!test_fill(myPtrs, TEST_SIZE)) return false;
for(int itr = TEST_SIZE; itr >= 0; --itr)
{
POOL_FREE(ChunkOfData, myPtrs[itr]);
}
return true;
}
bool test_fill_and_remove_at_random_and_refill_and_empty(void)
{
ChunkOfData* myPtrs[TEST_SIZE];
if(!test_fill(myPtrs, TEST_SIZE)) return false;
// remove between 10 and TEST_SIZE
int number_to_remove = get_random(100, TEST_SIZE);
for(int itr = 0; itr < number_to_remove; itr++)
{
POOL_FREE(ChunkOfData, myPtrs[itr]);
}
if(!test_fill(myPtrs, number_to_remove)) return false;
for(int itr = 0; itr < TEST_SIZE; itr++)
{
POOL_FREE(ChunkOfData, myPtrs[itr]);
}
return true;
}
int main(void)
{
// Test it twice to make sure empty works, kinda hacky.
if(!test_fill_and_empty()) return -1;
if(!test_fill_and_empty()) return -1;
// Similarly here, verify that fill, random num removal, refill, and empty
// by refilling and emptying again
if(!test_fill_and_remove_at_random_and_refill_and_empty()) return -1;
if(!test_fill_and_empty()) return -1;
printf("Pool Tests Passed\n");
return 0;
}
+6
View File
@@ -0,0 +1,6 @@
#include "test_structures.h"
#define TEST_SIZE 240
POOL_ENTRY(ChunkOfData, TEST_SIZE);
+18
View File
@@ -0,0 +1,18 @@
#ifndef POOL_TEST_STRUCTURES
#define POOL_TEST_STRUCTURES
#include <stddef.h>
typedef struct
{
union
{
int my_int;
float my_float;
char my_char;
} GenericData;
int my_type;
} ChunkOfData;
#endif // POOL_TEST_STRUCTURES
+13
View File
@@ -0,0 +1,13 @@
#!/bin/bash
set -eu
run_pool_test() {
cd pool
make clean
make
./build/pool_test
cd - > /dev/null
}
run_pool_test