Skip to content

Commit

Permalink
test: improve coverage of memory functions
Browse files Browse the repository at this point in the history
  • Loading branch information
flatcap committed Jun 10, 2023
1 parent adab4c3 commit 4cb07a6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 11 deletions.
12 changes: 6 additions & 6 deletions mutt/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void *mutt_mem_calloc(size_t nmemb, size_t size)
void *p = calloc(nmemb, size);
if (!p)
{
mutt_error(_("Out of memory"));
mutt_exit(1);
mutt_error(_("Out of memory")); // LCOV_EXCL_LINE
mutt_exit(1); // LCOV_EXCL_LINE
}
return p;
}
Expand Down Expand Up @@ -95,8 +95,8 @@ void *mutt_mem_malloc(size_t size)
void *p = malloc(size);
if (!p)
{
mutt_error(_("Out of memory"));
mutt_exit(1);
mutt_error(_("Out of memory")); // LCOV_EXCL_LINE
mutt_exit(1); // LCOV_EXCL_LINE
}
return p;
}
Expand Down Expand Up @@ -131,8 +131,8 @@ void mutt_mem_realloc(void *ptr, size_t size)
void *r = realloc(*p, size);
if (!r)
{
mutt_error(_("Out of memory"));
mutt_exit(1);
mutt_error(_("Out of memory")); // LCOV_EXCL_LINE
mutt_exit(1); // LCOV_EXCL_LINE
}

*p = r;
Expand Down
36 changes: 36 additions & 0 deletions test/memory/mutt_mem_calloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,44 @@
#define TEST_NO_MAIN
#include "config.h"
#include "acutest.h"
#include "mutt/lib.h"

void test_mutt_mem_calloc(void)
{
// void *mutt_mem_calloc(size_t nmemb, size_t size);

{
void *ptr = mutt_mem_calloc(0, 0);
TEST_CHECK(ptr == NULL);
}

{
void *ptr = mutt_mem_calloc(0, 1024);
TEST_CHECK(ptr == NULL);
}

{
void *ptr = mutt_mem_calloc(1024, 0);
TEST_CHECK(ptr == NULL);
}

{
size_t num = 64;
size_t size = 128;

void *ptr = mutt_mem_calloc(num, size);
TEST_CHECK(ptr != NULL);

unsigned char *cp = ptr;
for (size_t i = 0; i < (num * size); i++)
{
if (cp[i] != '\0')
{
TEST_CHECK_(false, "mem[%zu] = 0x%02x\n", cp[i]);
break;
}
}

FREE(&ptr);
}
}
8 changes: 8 additions & 0 deletions test/memory/mutt_mem_free.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,13 @@ void test_mutt_mem_free(void)
void *ptr = NULL;
mutt_mem_free(&ptr);
TEST_CHECK_(1, "mutt_mem_free(&ptr)");
TEST_CHECK(ptr == NULL);
}

{
void *ptr = mutt_mem_malloc(128);
mutt_mem_free(&ptr);
TEST_CHECK_(1, "mutt_mem_free(&ptr)");
TEST_CHECK(ptr == NULL);
}
}
17 changes: 12 additions & 5 deletions test/memory/mutt_mem_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ void test_mutt_mem_malloc(void)
// void *mutt_mem_malloc(size_t size);

MuttLogger = log_disp_null;
size_t big = 1024 * 1024; // ~0;
void *ptr = mutt_mem_malloc(big);
TEST_CHECK(ptr != NULL);
TEST_CHECK(exit_called == false);

FREE(&ptr);
{
void *ptr = mutt_mem_malloc(0);
TEST_CHECK(ptr == NULL);
}

{
size_t big = 1024 * 1024; // ~0;
void *ptr = mutt_mem_malloc(big);
TEST_CHECK(ptr != NULL);
TEST_CHECK(exit_called == false);
FREE(&ptr);
}
}
16 changes: 16 additions & 0 deletions test/memory/mutt_mem_realloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,20 @@ void test_mutt_mem_realloc(void)
mutt_mem_realloc(NULL, 10);
TEST_CHECK_(1, "mutt_mem_realloc(NULL, 10)");
}

{
void *ptr = mutt_mem_malloc(128);
mutt_mem_realloc(&ptr, 256);
TEST_CHECK_(1, "mutt_mem_realloc(&ptr, 256)");
TEST_CHECK(ptr != NULL);
FREE(&ptr);
}

{
void *ptr = mutt_mem_malloc(128);
mutt_mem_realloc(&ptr, 0);
TEST_CHECK_(1, "mutt_mem_realloc(&ptr, 0)");
TEST_CHECK(ptr == NULL);
FREE(&ptr);
}
}

0 comments on commit 4cb07a6

Please sign in to comment.