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