2010-01-18 17:45:41 +00:00
|
|
|
#include "test_mem.h"
|
|
|
|
|
|
|
|
#include "lwip/mem.h"
|
|
|
|
#include "lwip/stats.h"
|
|
|
|
|
|
|
|
#if !LWIP_STATS || !MEM_STATS
|
|
|
|
#error "This tests needs MEM-statistics enabled"
|
|
|
|
#endif
|
2010-01-27 20:37:02 +00:00
|
|
|
#if LWIP_DNS
|
2011-09-26 20:09:27 +00:00
|
|
|
#error "This test needs DNS turned off (as it mallocs on init)"
|
2010-01-18 17:45:41 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Setups/teardown functions */
|
|
|
|
|
|
|
|
static void
|
|
|
|
mem_setup(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mem_teardown(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Test functions */
|
|
|
|
|
2010-02-19 16:23:46 +00:00
|
|
|
/** Call mem_malloc, mem_free and mem_trim and check stats */
|
2010-01-18 17:45:41 +00:00
|
|
|
START_TEST(test_mem_one)
|
|
|
|
{
|
|
|
|
#define SIZE1 16
|
|
|
|
#define SIZE1_2 12
|
|
|
|
#define SIZE2 16
|
2010-01-27 20:37:02 +00:00
|
|
|
void *p1, *p2;
|
2010-01-18 17:45:41 +00:00
|
|
|
mem_size_t s1, s2;
|
|
|
|
LWIP_UNUSED_ARG(_i);
|
|
|
|
|
|
|
|
fail_unless(lwip_stats.mem.used == 0);
|
|
|
|
|
|
|
|
p1 = mem_malloc(SIZE1);
|
|
|
|
fail_unless(p1 != NULL);
|
|
|
|
fail_unless(lwip_stats.mem.used >= SIZE1);
|
|
|
|
s1 = lwip_stats.mem.used;
|
|
|
|
|
|
|
|
p2 = mem_malloc(SIZE2);
|
|
|
|
fail_unless(p2 != NULL);
|
|
|
|
fail_unless(lwip_stats.mem.used >= SIZE2 + s1);
|
|
|
|
s2 = lwip_stats.mem.used;
|
|
|
|
|
2010-02-19 16:23:46 +00:00
|
|
|
mem_trim(p1, SIZE1_2);
|
2010-01-18 17:45:41 +00:00
|
|
|
|
|
|
|
mem_free(p2);
|
|
|
|
fail_unless(lwip_stats.mem.used <= s2 - SIZE2);
|
|
|
|
|
|
|
|
mem_free(p1);
|
|
|
|
fail_unless(lwip_stats.mem.used == 0);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2014-12-10 08:48:50 +00:00
|
|
|
static void malloc_keep_x(int x, int num, int size, int freestep)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
void* p[16];
|
2015-08-03 18:41:49 +00:00
|
|
|
LWIP_ASSERT("invalid size", size >= 0 && size < (mem_size_t)-1);
|
2014-12-10 08:48:50 +00:00
|
|
|
memset(p, 0, sizeof(p));
|
|
|
|
for(i = 0; i < num && i < 16; i++) {
|
2015-08-03 18:41:49 +00:00
|
|
|
p[i] = mem_malloc((mem_size_t)size);
|
2014-12-10 08:48:50 +00:00
|
|
|
fail_unless(p[i] != NULL);
|
|
|
|
}
|
|
|
|
for(i = 0; i < num && i < 16; i += freestep) {
|
|
|
|
if (i == x) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mem_free(p[i]);
|
|
|
|
p[i] = NULL;
|
|
|
|
}
|
|
|
|
for(i = 0; i < num && i < 16; i++) {
|
|
|
|
if (i == x) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (p[i] != NULL) {
|
|
|
|
mem_free(p[i]);
|
|
|
|
p[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fail_unless(p[x] != NULL);
|
|
|
|
mem_free(p[x]);
|
|
|
|
}
|
|
|
|
|
|
|
|
START_TEST(test_mem_random)
|
|
|
|
{
|
|
|
|
const int num = 16;
|
|
|
|
int x;
|
|
|
|
int size;
|
|
|
|
int freestep;
|
2015-03-06 06:39:06 +00:00
|
|
|
LWIP_UNUSED_ARG(_i);
|
2014-12-10 08:48:50 +00:00
|
|
|
|
|
|
|
fail_unless(lwip_stats.mem.used == 0);
|
|
|
|
|
|
|
|
for (x = 0; x < num; x++) {
|
|
|
|
for (size = 1; size < 32; size++) {
|
|
|
|
for (freestep = 1; freestep <= 3; freestep++) {
|
|
|
|
fail_unless(lwip_stats.mem.used == 0);
|
|
|
|
malloc_keep_x(x, num, size, freestep);
|
|
|
|
fail_unless(lwip_stats.mem.used == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TEST
|
2010-01-18 17:45:41 +00:00
|
|
|
|
|
|
|
/** Create the suite including all tests for this module */
|
|
|
|
Suite *
|
|
|
|
mem_suite(void)
|
|
|
|
{
|
2014-02-12 20:52:21 +00:00
|
|
|
testfunc tests[] = {
|
2014-12-10 08:48:50 +00:00
|
|
|
TESTFUNC(test_mem_one),
|
|
|
|
TESTFUNC(test_mem_random)
|
2010-01-18 17:45:41 +00:00
|
|
|
};
|
2014-02-12 20:52:21 +00:00
|
|
|
return create_suite("MEM", tests, sizeof(tests)/sizeof(testfunc), mem_setup, mem_teardown);
|
2010-01-18 17:45:41 +00:00
|
|
|
}
|