Update easy_pool_cleanup_new (easy_pool.c)

fixed bug that alloc memory of incorrect size and add test
This commit is contained in:
握猫猫 2023-04-27 16:46:29 +08:00 committed by 好鱼 ing
parent 7e1a48d9dc
commit 0afe7ce669
2 changed files with 33 additions and 1 deletions

View File

@ -243,7 +243,7 @@ char *easy_pool_strdup(easy_pool_t *pool, const char *str)
easy_pool_cleanup_t *easy_pool_cleanup_new(easy_pool_t *pool, const void *data, easy_pool_cleanup_pt *handler)
{
easy_pool_cleanup_t *cl;
cl = easy_pool_alloc(pool, sizeof(easy_pool_t));
cl = easy_pool_alloc(pool, sizeof(easy_pool_cleanup_t));
if (cl) {
cl->handler = handler;

View File

@ -44,6 +44,13 @@ static void *test_realloc_2 (void *ptr, size_t size)
return NULL;
}
static void test_cleanup(const void *data)
{
int *p;
p = (int *) data;
*p += 1;
}
TEST(easy_pool, create)
{
@ -281,3 +288,28 @@ TEST(easy_pool, strdup)
easy_pool_destroy(pool);
}
TEST(easy_pool, cleanup)
{
easy_pool_t *pool;
easy_pool_cleanup_t *cl;
int i, cnt, size;
i = 0;
cnt = 0;
size = 111;
cl = NULL;
pool = easy_pool_create(0);
for (i = 0; i < size; ++ i)
{
cl = easy_pool_cleanup_new(pool, &cnt, test_cleanup);
EXPECT_TRUE(cl != NULL);
easy_pool_cleanup_reg(pool, cl);
}
cl = NULL;
easy_pool_destroy(pool);
EXPECT_TRUE(cnt == size);
}