* as/link/lkar.h: sgetl and sputl are independent of endianness
[fw/sdcc] / support / regression / tests / malloc.c
index 1f19501d95a0c1c82c674a913bffefd919419c76..bbc36a0b4413dbdb19d7a38e89a0db1b5567afd3 100644 (file)
@@ -1,24 +1,35 @@
 /* Simple malloc tests.
  */
-#include <testfwk.h>
 #include <stdlib.h>
-
-/* PENDING */
-#if defined(__gbz80) || defined(__z80) || defined(__GNUC__)
-#define XDATA
-#else
-#define XDATA xdata
+#if defined(SDCC_pic16)
+#include <malloc.h>
 #endif
+#include <testfwk.h>
 
-XDATA char heap[100];
+#if defined(SDCC_pic16)
+xdata char heap[100];
+#endif
 
 void
 testMalloc(void)
 {
-  void XDATA *p1, *p2, *p3;
-  
-#if !defined(__gbz80) && !defined(__z80) && !defined(__GNUC__)
-  init_dynamic_memory((MEMHEADER xdata *)heap, sizeof(heap));
+  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);
@@ -37,6 +48,34 @@ testMalloc(void)
   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);