struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / malloc.c
blob34f2ebf0bc1b9a0723b568fb5c23fdd743716fbd
1 /* Simple malloc tests.
2 */
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdint.h>
6 #if defined(__SDCC_pic16)
7 #include <malloc.h>
8 #endif
9 #include <testfwk.h>
11 #if defined(__SDCC_pic16)
12 __xdata char heap[100];
13 #endif
15 void mallocfree(void)
17 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
18 char *a, *b, *c;
19 char d[25];
21 LOG (("mallocfree()\n"));
23 a = malloc(16);
24 ASSERT(a);
25 memset(a, 2, 16);
26 b = malloc(16);
27 ASSERT(b);
28 memset(b, 1, 16);
29 c = calloc(16, 1);
30 ASSERT(c);
31 #ifndef PORT_HOST
32 LOG (("1 a %u b %u c %u\n", (unsigned)a, (unsigned)b, (unsigned)c));
33 #endif
34 memset(d, 2, 16);
35 ASSERT(!memcmp(d, a, 16));
36 memset(d, 1, 16);
37 ASSERT(!memcmp(d, b, 16));
38 memset(d, 0, 16);
39 ASSERT(!memcmp(d, c, 16));
41 free(b);
42 b = malloc(20);
43 memset(b, 3, 20);
44 #ifndef PORT_HOST
45 LOG (("2 a %u b %u c %u\n", (unsigned)a, (unsigned)b, (unsigned)c));
46 #endif
47 memset(d, 2, 16);
48 ASSERT(!memcmp(d, a, 16));
49 memset(d, 3, 20);
50 ASSERT(!memcmp(d, b, 20));
51 memset(d, 0, 16);
52 ASSERT(!memcmp(d, c, 16));
54 free(b);
55 b = malloc(10);
56 memset(b, 4, 10);
57 #ifndef PORT_HOST
58 LOG (("3 a %u b %u c %u\n", (unsigned)a, (unsigned)b, (unsigned)c));
59 #endif
60 memset(d, 2, 16);
61 ASSERT(!memcmp(d, a, 16));
62 memset(d, 4, 20);
63 ASSERT(!memcmp(d, b, 10));
64 memset(d, 0, 16);
65 ASSERT(!memcmp(d, c, 16));
67 free(b);
68 b = malloc(8);
69 memset(b, 5, 8);
70 b = realloc(b, 4);
71 memset(d, 5, 8);
72 ASSERT(!memcmp(d, b, 4));
73 #ifndef PORT_HOST
74 LOG (("4 a %u b %u c %u\n", (unsigned)a, (unsigned)b, (unsigned)c));
75 #endif
77 free(a);
78 b = realloc(b, 8);
79 ASSERT(!memcmp(d, b, 4));
81 #ifndef PORT_HOST
82 LOG (("5 a %u b %u c %u\n", (unsigned)a, (unsigned)b, (unsigned)c));
83 #endif
84 free(b);
85 #ifndef PORT_HOST
86 LOG (("6 a %u b %u c %u\n", (unsigned)a, (unsigned)b, (unsigned)c));
87 #endif
88 free(c);
89 #ifndef PORT_HOST
90 LOG (("7 a %u b %u c %u\n", (unsigned)a, (unsigned)b, (unsigned)c));
91 #endif
93 a = malloc(10);
94 memset(a, 6, 10);
95 b = malloc(10);
96 memset(b, 7, 10);
97 c = malloc(10);
98 memset(c, 8, 10);
99 #ifndef PORT_HOST
100 LOG (("8 a %u b %u c %u\n", (unsigned)a, (unsigned)b, (unsigned)c));
101 #endif
103 free(b);
104 a = realloc(a, 25);
105 memset(a + 10, 6, 15);
106 #ifndef PORT_HOST
107 LOG (("9 a %u b %u c %u\n", (unsigned)a, (unsigned)b, (unsigned)c));
108 #endif
110 memset(d, 6, 25);
111 ASSERT(!memcmp(d, a, 25));
112 memset(d, 8, 10);
113 ASSERT(!memcmp(d, c, 10));
115 free(a);
116 free(c);
118 /* Check that we can allocate at least 256 bytes at once. */
119 #if defined(PORT_HOST) || defined(__SDCC_z80) || defined(__SDCC_z80n) || defined(__SDCC_z180) || defined(__SDCC_r2k) || defined(__SDCC_r2ka) || defined(__SDCC_r3ka)
120 a = malloc(256);
121 ASSERT(a);
122 free(a);
123 #endif
125 /* Check for overflow in calloc() memory size calculation, bug #2650. */
126 #ifndef PORT_HOST
127 c = calloc(SIZE_MAX / 256, 258);
128 ASSERT(!c);
129 #endif
130 #endif
133 void
134 testMalloc (void)
136 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
137 #ifndef __SDCC_pdk14
138 void __xdata *p1, *p2, *p3;
139 char *p;
140 unsigned char i;
143 #if defined(__SDCC_pic16)
144 _initHeap (heap, sizeof heap);
145 #endif
147 #ifndef PORT_HOST
148 p1 = malloc (0xFFFF);
149 ASSERT (p1 == NULL);
150 LOG (("p1 == NULL when out of memory\n"));
151 #endif
153 #if !defined(PORT_HOST) && !defined(__SDCC_sm83) && !defined(__SDCC_z80) && !defined(__SDCC_z80n)
154 p1 = malloc (2000);
155 ASSERT (p1 == NULL);
156 LOG (("p1 == NULL when out of memory\n"));
157 #ifdef PORT_HOST
158 LOG (("p1: %p\n", p1));
159 #else
160 LOG (("p1: %u\n", (unsigned) p1));
161 #endif
162 #endif
164 p1 = malloc (5);
165 ASSERT (p1 != NULL);
166 #ifdef PORT_HOST
167 LOG (("p1: %p\n", p1));
168 #else
169 LOG (("p1: %u\n", (unsigned) p1));
170 #endif
172 p2 = malloc (20);
173 ASSERT (p2 != NULL);
174 #ifdef PORT_HOST
175 LOG (("p2: %p\n", p2));
176 #else
177 LOG (("p2: %u\n", (unsigned) p2));
178 #endif
180 p = (char*)p2;
181 for (i = 0; i < 20; i++, p++)
182 *p = i;
184 p2 = realloc (p2, 25);
185 ASSERT (p2 != NULL);
186 #ifdef PORT_HOST
187 LOG (("p2, after expanding realloc: %p\n", p2));
188 #else
189 LOG (("p2, after expanding realloc: %u\n", (unsigned) p2));
190 #endif
192 p = (char*)p2;
193 for (i = 0; i < 20; i++, p++)
194 ASSERT (*p == i);
196 p2 = realloc (p2, 15);
197 ASSERT (p2 != NULL);
198 #ifdef PORT_HOST
199 LOG (("p2, after shrinking realloc: %p\n", p2));
200 #else
201 LOG (("p2, after shrinking realloc: %u\n", (unsigned) p2));
202 #endif
204 p = (char*)p2;
205 for (i = 0; i < 15; i++, p++)
206 ASSERT (*p == i);
208 free (p2);
210 p3 = malloc (10);
211 ASSERT (p3 != NULL);
212 #ifdef PORT_HOST
213 LOG (("p3, after freeing p2: %p\n", p3));
214 #else
215 LOG (("p3, after freeing p2: %u\n", (unsigned) p3));
216 #endif
218 free (p1);
219 free (p3);
220 mallocfree();
221 #endif
222 #endif