Code refactoring of huge_test
authorIvan Maidanski <ivmai@mail.ru>
Tue, 27 Sep 2016 07:47:00 +0000 (10:47 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Tue, 27 Sep 2016 07:47:00 +0000 (10:47 +0300)
* tests/huge_test.c [!GC_MAXIMUM_HEAP_SIZE] (GC_MAXIMUM_HEAP_SIZE,
GC_INITIAL_HEAP_SIZE): Define (before include gc.h); move the comment
from main().
* tests/huge_test.c (CHECK_ALLOC_FAILED): New macro.
* tests/huge_test.c (main): Remove "r" local variable; do not call
GC_set_max_heap_size and GC_expand_hp explicitly (as it is done
by GC_INIT provided GC_MAXIMUM_HEAP_SIZE and GC_INITIAL_HEAP_SIZE are
defined); use CHECK_ALLOC_FAILED (instead of if/fprintf/exit).

tests/huge_test.c

index c245020..9c29af3 100644 (file)
 # define GC_IGNORE_WARN
 #endif
 
+#ifndef GC_MAXIMUM_HEAP_SIZE
+# define GC_MAXIMUM_HEAP_SIZE 100 * 1024 * 1024
+# define GC_INITIAL_HEAP_SIZE GC_MAXIMUM_HEAP_SIZE / 20
+    /* Otherwise heap expansion aborts when deallocating large block.   */
+    /* That's OK.  We test this corner case mostly to make sure that    */
+    /* it fails predictably.                                            */
+#endif
+
 #include "gc.h"
 
 /*
  * expected manner.
  */
 
+#define CHECK_ALLOC_FAILED(r, sz_str) \
+  do { \
+    if (NULL != (r)) { \
+        fprintf(stderr, \
+                "Size " sz_str " allocation unexpectedly succeeded\n"); \
+        exit(1); \
+    } \
+  } while (0)
+
 #define GC_SWORD_MAX ((GC_signed_word)(((GC_word)-1) >> 1))
 
 int main(void)
 {
-    void *r;
     GC_INIT();
 
-    GC_set_max_heap_size(100*1024*1024);
-        /* Otherwise heap expansion aborts when deallocating large block. */
-        /* That's OK.  We test this corner case mostly to make sure that  */
-        /* it fails predictably.                                          */
-    GC_expand_hp(1024*1024*5);
-    r = GC_MALLOC(GC_SWORD_MAX - 1024);
-    if (NULL != r) {
-        fprintf(stderr,
-                "Size SWORD_MAX-1024 allocation unexpectedly succeeded\n");
-        exit(1);
-    }
-    r = GC_MALLOC(GC_SWORD_MAX);
-    if (NULL != r) {
-        fprintf(stderr,
-                "Size SWORD_MAX allocation unexpectedly succeeded\n");
-        exit(1);
-    }
-    r = GC_MALLOC((GC_word)GC_SWORD_MAX + 1024);
-    if (NULL != r) {
-        fprintf(stderr,
-                "Size SWORD_MAX+1024 allocation unexpectedly succeeded\n");
-        exit(1);
-    }
+    CHECK_ALLOC_FAILED(GC_MALLOC(GC_SWORD_MAX - 1024), "SWORD_MAX-1024");
+    CHECK_ALLOC_FAILED(GC_MALLOC(GC_SWORD_MAX), "SWORD_MAX");
+    CHECK_ALLOC_FAILED(GC_MALLOC((GC_word)GC_SWORD_MAX + 1024),
+                       "SWORD_MAX+1024");
     return 0;
 }