From bcfcf4a9ccd9ef6f21fab5f2cb9cb05bc1afdb19 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Tue, 27 Sep 2016 10:47:00 +0300 Subject: [PATCH] Code refactoring of huge_test * 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 | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/tests/huge_test.c b/tests/huge_test.c index c245020..9c29af3 100644 --- a/tests/huge_test.c +++ b/tests/huge_test.c @@ -10,6 +10,14 @@ # 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" /* @@ -19,35 +27,24 @@ * 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; } -- 2.7.4