This file provides implementation of pool_test(). It tests the functionality of the memory pool.
This file is pjlib-test/pool.c
#include <pj/pool.h>
#include <pj/pool_buf.h>
#include <pj/rand.h>
#include <pj/log.h>
#include <pj/except.h>
#include "test.h"
#if INCLUDE_POOL_TEST
#define SIZE 4096
static void null_callback(pj_pool_t *pool, pj_size_t size)
{
PJ_UNUSED_ARG(pool);
PJ_UNUSED_ARG(size);
}
#define GET_FREE(p) (pj_pool_get_capacity(p)-pj_pool_get_used_size(p))
static int capacity_test(void)
{
pj_pool_t *pool = pj_pool_create(mem, NULL, SIZE, 0, &null_callback);
pj_size_t freesize;
PJ_LOG(3,("test", "...capacity_test()"));
if (!pool)
return -200;
freesize = GET_FREE(pool);
if (pj_pool_alloc(pool, freesize) == NULL) {
PJ_LOG(3,("test", "...error: wrong freesize %u reported",
freesize));
pj_pool_release(pool);
return -210;
}
pj_pool_release(pool);
return 0;
}
static int pool_alignment_test(void)
{
pj_pool_t *pool;
void *ptr;
enum { MEMSIZE = 64, LOOP = 100 };
unsigned i;
PJ_LOG(3,("test", "...alignment test"));
pool = pj_pool_create(mem, NULL, PJ_POOL_SIZE+MEMSIZE, MEMSIZE, NULL);
if (!pool)
return -300;
#define IS_ALIGNED(p) ((((unsigned long)p) & (PJ_POOL_ALIGNMENT-1)) == 0)
for (i=0; i<LOOP; ++i) {
ptr = pj_pool_alloc(pool, 1);
if (!IS_ALIGNED(ptr)) {
pj_pool_release(pool);
return -310;
}
ptr = pj_pool_alloc(pool, 1);
if (!IS_ALIGNED(ptr)) {
pj_pool_release(pool);
return -320;
}
ptr = pj_pool_alloc(pool, MEMSIZE*2+1);
if (!IS_ALIGNED(ptr)) {
pj_pool_release(pool);
return -330;
}
pj_pool_reset(pool);
}
pj_pool_release(pool);
return 0;
}
static int pool_buf_alignment_test(void)
{
pj_pool_t *pool;
char buf[512];
void *ptr;
enum { LOOP = 100 };
unsigned i;
PJ_LOG(3,("test", "...pool_buf alignment test"));
pool = pj_pool_create_on_buf(NULL, buf, sizeof(buf));
if (!pool)
return -400;
for (i=0; i<LOOP; ++i) {
ptr = pj_pool_alloc(pool, 1);
if (!IS_ALIGNED(ptr)) {
pj_pool_release(pool);
return -410;
}
ptr = pj_pool_alloc(pool, 1);
if (!IS_ALIGNED(ptr)) {
pj_pool_release(pool);
return -420;
}
pj_pool_reset(pool);
}
return 0;
}
static int drain_test(pj_size_t size, pj_size_t increment)
{
pj_pool_t *pool = pj_pool_create(mem, NULL, size, increment,
&null_callback);
pj_size_t freesize;
void *p;
int status = 0;
PJ_LOG(3,("test", "...drain_test(%d,%d)", size, increment));
if (!pool)
return -10;
freesize = GET_FREE(pool);
if (freesize < 1) {
status=-15;
goto on_error;
}
while (freesize > 0) {
int size;
if (freesize > 255)
size = ((pj_rand() & 0x000000FF) + PJ_POOL_ALIGNMENT) &
~(PJ_POOL_ALIGNMENT - 1);
else
size = freesize;
p = pj_pool_alloc(pool, size);
if (!p) {
status=-20; goto on_error;
}
freesize -= size;
}
if (GET_FREE(pool) != 0) {
PJ_LOG(3,("test", "....error: returned free=%u (expecting 0)",
GET_FREE(pool)));
status=-30; goto on_error;
}
p = pj_pool_alloc(pool, 257);
if (!p) {
status=-40; goto on_error;
}
if (GET_FREE(pool) == 0) {
status=-50; goto on_error;
}
on_error:
pj_pool_release(pool);
return status;
}
static int pool_buf_test(void)
{
enum { STATIC_BUF_SIZE = 40 };
static char buf[ STATIC_BUF_SIZE + sizeof(pj_pool_t) +
sizeof(pj_pool_block) + 2 * PJ_POOL_ALIGNMENT];
pj_pool_t *pool;
void *p;
PJ_USE_EXCEPTION;
PJ_LOG(3,("test", "...pool_buf test"));
pool = pj_pool_create_on_buf("no name", buf, sizeof(buf));
if (!pool)
return -70;
PJ_TRY {
if ((p=pj_pool_alloc(pool, STATIC_BUF_SIZE/2)) == NULL)
return -75;
if ((p=pj_pool_alloc(pool, STATIC_BUF_SIZE/2)) == NULL)
return -76;
}
PJ_CATCH_ANY {
return -77;
}
PJ_END;
PJ_TRY {
p = pj_pool_alloc(pool, STATIC_BUF_SIZE);
if (p != NULL) {
return -78;
}
}
PJ_CATCH_ANY {
}
PJ_END;
return 0;
}
int pool_test(void)
{
enum { LOOP = 2 };
int loop;
int rc;
rc = capacity_test();
if (rc) return rc;
rc = pool_alignment_test();
if (rc) return rc;
rc = pool_buf_alignment_test();
if (rc) return rc;
for (loop=0; loop<LOOP; ++loop) {
rc = drain_test(SIZE, SIZE);
if (rc != 0) return rc;
rc = drain_test(SIZE, 0);
if (rc != -40) return rc;
}
rc = pool_buf_test();
if (rc != 0)
return rc;
return 0;
}
#else
int dummy_pool_test;
#endif