1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
/* Simple malloc tests.
*/
#include <stdlib.h>
#if defined(SDCC_pic16)
#include <malloc.h>
#endif
#include <testfwk.h>
#if defined(SDCC_pic16)
xdata char heap[100];
#endif
void
testMalloc(void)
{
void xdata *p1, *p2, *p3;
char *p;
unsigned char i;
#if !defined(__GNUC__) && !defined(SDCC_gbz80) && !defined(SDCC_z80)
#if defined(SDCC_pic16)
_initHeap(heap, sizeof heap);
#endif
p1 = malloc(2000);
ASSERT(p1 == NULL);
LOG(("p1 == NULL when out of memory\n"));
#ifdef PORT_HOST
LOG(("p1: %p\n", p1));
#else
LOG(("p1: %u\n", (unsigned) p1));
#endif
#endif
p1 = malloc(5);
ASSERT(p1 != NULL);
#ifdef PORT_HOST
LOG(("p1: %p\n", p1));
#else
LOG(("p1: %u\n", (unsigned) p1));
#endif
p2 = malloc(20);
ASSERT(p2 != NULL);
#ifdef PORT_HOST
LOG(("p2: %p\n", p2));
#else
LOG(("p2: %u\n", (unsigned) p2));
#endif
p = (char*)p2;
for (i=0; i<20; i++, p++)
*p = i;
p2 = realloc(p2, 25);
ASSERT(p2 != NULL);
#ifdef PORT_HOST
LOG(("p2, after expanding realloc: %p\n", p2));
#else
LOG(("p2, after expanding realloc: %u\n", (unsigned) p2));
#endif
p = (char*)p2;
for (i=0; i<20; i++, p++)
ASSERT(*p == i);
p2 = realloc(p2, 15);
ASSERT(p2 != NULL);
#ifdef PORT_HOST
LOG(("p2, after shrinking realloc: %p\n", p2));
#else
LOG(("p2, after shrinking realloc: %u\n", (unsigned) p2));
#endif
p = (char*)p2;
for (i=0; i<15; i++, p++)
ASSERT(*p == i);
free(p2);
p3 = malloc(10);
ASSERT(p3 != NULL);
#ifdef PORT_HOST
LOG(("p3, after freeing p2: %p\n", p3));
#else
LOG(("p3, after freeing p2: %u\n", (unsigned) p3));
#endif
}
|