mirror of
https://github.com/MaJerle/lwmem.git
synced 2025-02-07 14:14:10 +08:00
Add test file
This commit is contained in:
parent
e293bd5291
commit
9e941aa9df
@ -145,6 +145,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="..\..\lwmem\src\lwmem\lwmem.c" />
|
<ClCompile Include="..\..\lwmem\src\lwmem\lwmem.c" />
|
||||||
<ClCompile Include="..\..\lwmem\src\system\lwmem_sys_win32.c" />
|
<ClCompile Include="..\..\lwmem\src\system\lwmem_sys_win32.c" />
|
||||||
|
<ClCompile Include="..\..\tests\lwmem_test.c" />
|
||||||
<ClCompile Include="main.c" />
|
<ClCompile Include="main.c" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
@ -8,6 +8,9 @@
|
|||||||
<Filter Include="Source Files\LWMEM">
|
<Filter Include="Source Files\LWMEM">
|
||||||
<UniqueIdentifier>{a9bad49b-d114-4596-8fe8-162c60f482ee}</UniqueIdentifier>
|
<UniqueIdentifier>{a9bad49b-d114-4596-8fe8-162c60f482ee}</UniqueIdentifier>
|
||||||
</Filter>
|
</Filter>
|
||||||
|
<Filter Include="Source Files\Tests">
|
||||||
|
<UniqueIdentifier>{87f67bb1-45c3-4724-b7de-f1e8551453e3}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="main.c">
|
<ClCompile Include="main.c">
|
||||||
@ -19,5 +22,8 @@
|
|||||||
<ClCompile Include="..\..\lwmem\src\system\lwmem_sys_win32.c">
|
<ClCompile Include="..\..\lwmem\src\system\lwmem_sys_win32.c">
|
||||||
<Filter>Source Files\LWMEM</Filter>
|
<Filter>Source Files\LWMEM</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\tests\lwmem_test.c">
|
||||||
|
<Filter>Source Files\Tests</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -3,97 +3,12 @@
|
|||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "stdint.h"
|
#include "stdint.h"
|
||||||
|
|
||||||
#define ARRSIZE(x) (sizeof(x) / (sizeof((x)[0])))
|
extern void lwmem_test_run(void);
|
||||||
|
extern void lwmem_test_memory_structure(void);
|
||||||
/* Define regions in increasing order */
|
|
||||||
uint8_t mem1[1024];
|
|
||||||
lwmem_region_t
|
|
||||||
regions[] = {
|
|
||||||
{ mem1, sizeof(mem1) / 2 },
|
|
||||||
{ mem1 + sizeof(mem1) / 2, sizeof(mem1) / 2 },
|
|
||||||
};
|
|
||||||
|
|
||||||
#define ASSERT(x) do { \
|
|
||||||
if (!(x)) { \
|
|
||||||
printf("Assert failed with condition (" # x ")\r\n"); \
|
|
||||||
} else {\
|
|
||||||
printf("Assert passed with condition (" # x ")\r\n"); \
|
|
||||||
}\
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
/* For debug purposes */
|
|
||||||
lwmem_region_t* regions_used;
|
|
||||||
size_t regions_count = 4; /* Use only 1 region for debug purposes of non-free areas */
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(void) {
|
main(void) {
|
||||||
uint8_t* ptr1, *ptr2, *ptr3, *ptr4;
|
lwmem_test_memory_structure();
|
||||||
uint8_t* rptr1, *rptr2, *rptr3, *rptr4;
|
//lwmem_test_run();
|
||||||
|
|
||||||
/* Create regions for debug purpose */
|
|
||||||
if (!lwmem_debug_create_regions(®ions_used, regions_count, 128)) {
|
|
||||||
printf("Cannot allocate memory for regions for debug purpose!\r\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
lwmem_assignmem(regions_used, regions_count);
|
|
||||||
printf("Manager is ready!\r\n");
|
|
||||||
lwmem_debug_print(1, 1);
|
|
||||||
|
|
||||||
/* Test case 1, allocate 3 blocks, each of different size */
|
|
||||||
/* We know that sizeof internal metadata block is 8 bytes on win32 */
|
|
||||||
printf("\r\n\r\nAllocating 4 pointers and freeing first and third..\r\n");
|
|
||||||
ptr1 = lwmem_malloc(8);
|
|
||||||
ptr2 = lwmem_malloc(4);
|
|
||||||
ptr3 = lwmem_malloc(4);
|
|
||||||
ptr4 = lwmem_malloc(16);
|
|
||||||
lwmem_free(ptr1); /* Free but keep value for future comparison */
|
|
||||||
lwmem_free(ptr3); /* Free but keep value for future comparison */
|
|
||||||
lwmem_debug_print(1, 1);
|
|
||||||
printf("Debug above is effectively state 3\r\n");
|
|
||||||
lwmem_debug_save_state(); /* Every restore operations rewinds here */
|
|
||||||
|
|
||||||
ptr1 = lwmem_malloc_from(®ions_used[2], 16);
|
|
||||||
lwmem_debug_print(1, 1);
|
|
||||||
ptr1 = lwmem_realloc_from(®ions_used[2], ptr1, 24);
|
|
||||||
lwmem_debug_print(1, 1);
|
|
||||||
lwmem_free(ptr1);
|
|
||||||
lwmem_debug_print(1, 1);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
/* We always try to reallocate pointer ptr2 */
|
|
||||||
|
|
||||||
/* Create 3a case */
|
|
||||||
printf("\r\n------------------------------------------------------------------------\r\n");
|
|
||||||
lwmem_debug_restore_to_saved();
|
|
||||||
printf("State 3a\r\n");
|
|
||||||
rptr1 = lwmem_realloc(ptr2, 8);
|
|
||||||
lwmem_debug_print(1, 1);
|
|
||||||
ASSERT(rptr1 == ptr2);
|
|
||||||
|
|
||||||
/* Create 3b case */
|
|
||||||
printf("\r\n------------------------------------------------------------------------\r\n");
|
|
||||||
lwmem_debug_restore_to_saved();
|
|
||||||
printf("State 3b\r\n");
|
|
||||||
rptr2 = lwmem_realloc(ptr2, 20);
|
|
||||||
lwmem_debug_print(1, 1);
|
|
||||||
ASSERT(rptr2 == ptr2);
|
|
||||||
|
|
||||||
/* Create 3c case */
|
|
||||||
printf("\r\n------------------------------------------------------------------------\r\n");
|
|
||||||
lwmem_debug_restore_to_saved();
|
|
||||||
printf("State 3c\r\n");
|
|
||||||
rptr3 = lwmem_realloc(ptr2, 24);
|
|
||||||
lwmem_debug_print(1, 1);
|
|
||||||
ASSERT(rptr3 == ptr1);
|
|
||||||
|
|
||||||
/* Create 3d case */
|
|
||||||
printf("\r\n------------------------------------------------------------------------\r\n");
|
|
||||||
lwmem_debug_restore_to_saved();
|
|
||||||
printf("State 3d\r\n");
|
|
||||||
rptr4 = lwmem_realloc(ptr2, 36);
|
|
||||||
lwmem_debug_print(1, 1);
|
|
||||||
ASSERT(rptr4 != ptr1 && rptr4 != ptr2 && rptr4 != ptr3 && rptr4 != ptr4);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
185
tests/lwmem_test.c
Normal file
185
tests/lwmem_test.c
Normal file
@ -0,0 +1,185 @@
|
|||||||
|
#include "lwmem/lwmem.h"
|
||||||
|
|
||||||
|
/* Assert check */
|
||||||
|
#define ASSERT(x) do { \
|
||||||
|
if (!(x)) { \
|
||||||
|
printf("Assert on line %d failed with condition (" # x ")\r\n", (int)__LINE__); \
|
||||||
|
} else {\
|
||||||
|
printf("Assert on line %d passed with condition (" # x ")\r\n", (int)__LINE__); \
|
||||||
|
}\
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
/********************************************/
|
||||||
|
/* Test case helpers */
|
||||||
|
#define IS_ALLOC_IN_REGION(ptr, region) ASSERT((unsigned char *)(ptr) >= (region)->start_addr && (unsigned char *)(ptr) < ((unsigned char *)(region)->start_addr + (region)->size))
|
||||||
|
|
||||||
|
/********************************************/
|
||||||
|
/* Configuration for default lwmem instance */
|
||||||
|
|
||||||
|
/* Region memory declaration */
|
||||||
|
uint8_t lw_mem1[1024], lw_mem2[256], lw_mem3[128];
|
||||||
|
|
||||||
|
/* Regions descriptor */
|
||||||
|
lwmem_region_t
|
||||||
|
lw_regions[] = {
|
||||||
|
{ lw_mem3, sizeof(lw_mem3) },
|
||||||
|
{ lw_mem2, sizeof(lw_mem2) },
|
||||||
|
{ lw_mem1, sizeof(lw_mem1) },
|
||||||
|
};
|
||||||
|
|
||||||
|
/********************************************/
|
||||||
|
/********************************************/
|
||||||
|
/* Configuration for custom lwmem instance */
|
||||||
|
/* LwMEM instance */
|
||||||
|
lwmem_t lw_c;
|
||||||
|
|
||||||
|
/* Region memory declaration */
|
||||||
|
uint8_t lw_c_mem1[1024], lw_c_mem2[256], lw_c_mem3[128];
|
||||||
|
|
||||||
|
/* Regions descriptor */
|
||||||
|
lwmem_region_t
|
||||||
|
lw_c_regions[] = {
|
||||||
|
{ lw_c_mem3, sizeof(lw_c_mem3) },
|
||||||
|
{ lw_c_mem2, sizeof(lw_c_mem2) },
|
||||||
|
{ lw_c_mem1, sizeof(lw_c_mem1) },
|
||||||
|
};
|
||||||
|
/********************************************/
|
||||||
|
|
||||||
|
void
|
||||||
|
lwmem_test_run(void) {
|
||||||
|
void* ptr_1, * ptr_2, * ptr_3, * ptr_4;
|
||||||
|
void* ptr_c_1, * ptr_c_2, * ptr_c_3, * ptr_c_4;
|
||||||
|
|
||||||
|
/* Initialize default lwmem instance */
|
||||||
|
/* Use one of 2 possible function calls: */
|
||||||
|
lwmem_assignmem(lw_regions, sizeof(lw_regions) / sizeof(lw_regions[0]));
|
||||||
|
//lwmem_assignmem_ex(NULL, lw_regions, sizeof(lw_regions) / sizeof(lw_regions[0]));
|
||||||
|
|
||||||
|
/* Initialize another, custom instance */
|
||||||
|
lwmem_assignmem_ex(&lw_c, lw_c_regions, sizeof(lw_c_regions) / sizeof(lw_c_regions[0]));
|
||||||
|
|
||||||
|
/* Regions initialized... */
|
||||||
|
|
||||||
|
/********************************************/
|
||||||
|
/* Run tests on default region */
|
||||||
|
/********************************************/
|
||||||
|
|
||||||
|
/* Allocation of 64 bytes must in in first region */
|
||||||
|
ptr_1 = lwmem_malloc(64);
|
||||||
|
IS_ALLOC_IN_REGION(ptr_1, &lw_regions[0]);
|
||||||
|
|
||||||
|
/* Allocation of 256 bytes can only be in 3rd region */
|
||||||
|
ptr_2 = lwmem_malloc(256);
|
||||||
|
IS_ALLOC_IN_REGION(ptr_2, &lw_regions[2]);
|
||||||
|
|
||||||
|
/* Allocation of 128 bytes can be in second or third region (depends on memory availability),
|
||||||
|
but in case of these tests it can be (and should be) in second region */
|
||||||
|
ptr_3 = lwmem_malloc(128);
|
||||||
|
IS_ALLOC_IN_REGION(ptr_3, &lw_regions[1]);
|
||||||
|
|
||||||
|
/* Free all pointers to default state */
|
||||||
|
lwmem_free(ptr_1);
|
||||||
|
lwmem_free(ptr_2);
|
||||||
|
lwmem_free(ptr_3);
|
||||||
|
|
||||||
|
/* Force allocation region to be used */
|
||||||
|
/* Allocation of 16-bytes forced to 2nd region */
|
||||||
|
ptr_1 = lwmem_malloc_ex(NULL, &lw_regions[1], 16);
|
||||||
|
IS_ALLOC_IN_REGION(ptr_1, &lw_regions[1]);
|
||||||
|
|
||||||
|
/* Allocate ptr 2 in any region of default lwmem, the first available must be 1st region */
|
||||||
|
ptr_2 = lwmem_malloc_ex(NULL, NULL, 16);
|
||||||
|
IS_ALLOC_IN_REGION(ptr_2, &lw_regions[0]);
|
||||||
|
|
||||||
|
/* Free pointers */
|
||||||
|
lwmem_free(ptr_1);
|
||||||
|
lwmem_free(ptr_2);
|
||||||
|
|
||||||
|
/********************************************/
|
||||||
|
/* Run tests on custom region */
|
||||||
|
/********************************************/
|
||||||
|
|
||||||
|
/* Allocation of 64 bytes must in in first region */
|
||||||
|
ptr_c_1 = lwmem_malloc_ex(&lw_c, NULL, 64);
|
||||||
|
IS_ALLOC_IN_REGION(ptr_c_1, &lw_c_regions[0]);
|
||||||
|
|
||||||
|
/* Allocation of 256 bytes can only be in 3rd region */
|
||||||
|
ptr_c_2 = lwmem_malloc_ex(&lw_c, NULL, 256);
|
||||||
|
IS_ALLOC_IN_REGION(ptr_c_2, &lw_c_regions[2]);
|
||||||
|
|
||||||
|
/* Allocation of 128 bytes can be in second or third region (depends on memory availability),
|
||||||
|
but in case of these tests it can be (and should be) in second region */
|
||||||
|
ptr_c_3 = lwmem_malloc_ex(&lw_c, NULL, 128);
|
||||||
|
IS_ALLOC_IN_REGION(ptr_c_3, &lw_c_regions[1]);
|
||||||
|
|
||||||
|
/* Free all pointers to default state */
|
||||||
|
lwmem_free(ptr_c_1);
|
||||||
|
lwmem_free(ptr_c_2);
|
||||||
|
lwmem_free(ptr_c_3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* For debug purposes */
|
||||||
|
lwmem_region_t* regions_used;
|
||||||
|
size_t regions_count = 4; /* Use only 1 region for debug purposes of non-free areas */
|
||||||
|
|
||||||
|
void
|
||||||
|
lwmem_test_memory_structure(void) {
|
||||||
|
uint8_t* ptr1, *ptr2, *ptr3, *ptr4;
|
||||||
|
uint8_t* rptr1, *rptr2, *rptr3, *rptr4;
|
||||||
|
|
||||||
|
/* Create regions for debug purpose */
|
||||||
|
if (!lwmem_debug_create_regions(®ions_used, regions_count, 128)) {
|
||||||
|
printf("Cannot allocate memory for regions for debug purpose!\r\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lwmem_assignmem(regions_used, regions_count);
|
||||||
|
printf("Manager is ready!\r\n");
|
||||||
|
lwmem_debug_print(1, 1);
|
||||||
|
|
||||||
|
/* Test case 1, allocate 3 blocks, each of different size */
|
||||||
|
/* We know that sizeof internal metadata block is 8 bytes on win32 */
|
||||||
|
printf("\r\n\r\nAllocating 4 pointers and freeing first and third..\r\n");
|
||||||
|
ptr1 = lwmem_malloc(8);
|
||||||
|
ptr2 = lwmem_malloc(4);
|
||||||
|
ptr3 = lwmem_malloc(4);
|
||||||
|
ptr4 = lwmem_malloc(16);
|
||||||
|
lwmem_free(ptr1); /* Free but keep value for future comparison */
|
||||||
|
lwmem_free(ptr3); /* Free but keep value for future comparison */
|
||||||
|
lwmem_debug_print(1, 1);
|
||||||
|
printf("Debug above is effectively state 3\r\n");
|
||||||
|
lwmem_debug_save_state(); /* Every restore operations rewinds here */
|
||||||
|
|
||||||
|
/* We always try to reallocate pointer ptr2 */
|
||||||
|
|
||||||
|
/* Create 3a case */
|
||||||
|
printf("\r\n------------------------------------------------------------------------\r\n");
|
||||||
|
lwmem_debug_restore_to_saved();
|
||||||
|
printf("State 3a\r\n");
|
||||||
|
rptr1 = lwmem_realloc(ptr2, 8);
|
||||||
|
lwmem_debug_print(1, 1);
|
||||||
|
ASSERT(rptr1 == ptr2);
|
||||||
|
|
||||||
|
/* Create 3b case */
|
||||||
|
printf("\r\n------------------------------------------------------------------------\r\n");
|
||||||
|
lwmem_debug_restore_to_saved();
|
||||||
|
printf("State 3b\r\n");
|
||||||
|
rptr2 = lwmem_realloc(ptr2, 20);
|
||||||
|
lwmem_debug_print(1, 1);
|
||||||
|
ASSERT(rptr2 == ptr2);
|
||||||
|
|
||||||
|
/* Create 3c case */
|
||||||
|
printf("\r\n------------------------------------------------------------------------\r\n");
|
||||||
|
lwmem_debug_restore_to_saved();
|
||||||
|
printf("State 3c\r\n");
|
||||||
|
rptr3 = lwmem_realloc(ptr2, 24);
|
||||||
|
lwmem_debug_print(1, 1);
|
||||||
|
ASSERT(rptr3 == ptr1);
|
||||||
|
|
||||||
|
/* Create 3d case */
|
||||||
|
printf("\r\n------------------------------------------------------------------------\r\n");
|
||||||
|
lwmem_debug_restore_to_saved();
|
||||||
|
printf("State 3d\r\n");
|
||||||
|
rptr4 = lwmem_realloc(ptr2, 36);
|
||||||
|
lwmem_debug_print(1, 1);
|
||||||
|
ASSERT(rptr4 != ptr1 && rptr4 != ptr2 && rptr4 != ptr3 && rptr4 != ptr4);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user