Home | History | Annotate | Download | only in tests
      1 
      2 /* Test for correct functioning of the --malloc-fill and --free-fill
      3    flags.  Needs --malloc-fill=0x55 and --free-fill=0x77. */
      4 
      5 #include <stdio.h>
      6 #include <assert.h>
      7 #include <stdlib.h>
      8 #include "../memcheck.h"
      9 
     10 int main ( void )
     11 {
     12   int *r, *oldr, *a;
     13 
     14 #define TEST(x, exp_x, desc) \
     15   (void)VALGRIND_MAKE_MEM_DEFINED(&x, sizeof(int));     \
     16   if (x == exp_x) { \
     17      fprintf(stderr, "PASSED: " desc "\n"); \
     18   } else { \
     19      fprintf(stderr, "FAILED: " desc "\n"); \
     20   }
     21 
     22   //-------------------------------------------------------------
     23   fprintf(stderr, "test simple malloc/free:\n");
     24 
     25   a = malloc(10 * sizeof(int)); assert(a);
     26   TEST(a[4], 0x55555555, "malloc-filled");
     27 
     28   free(a);
     29   TEST(a[5], 0x77777777, "  free-filled");
     30 
     31   //-------------------------------------------------------------
     32   fprintf(stderr, "\ntest realloc-larger:\n");
     33 
     34   r = malloc(30 * sizeof(int)); assert(r);
     35   TEST(r[25], 0x55555555, "malloc-filled");
     36 
     37   /* Make larger */
     38   oldr = r;
     39   r = realloc(r, 40 * sizeof(int)); assert(r);
     40 
     41   TEST(oldr[26], 0x77777777, "  free-filled");
     42   TEST(   r[35], 0x55555555, "malloc-filled");
     43 
     44   free(r);
     45 
     46   //-------------------------------------------------------------
     47   fprintf(stderr, "\ntest realloc-smaller:\n");
     48 
     49   r = malloc(30 * sizeof(int)); assert(r);
     50   TEST(r[25], 0x55555555,   "malloc-filled");
     51 
     52   /* Make smaller */
     53   oldr = r;
     54   r = realloc(r, 20 * sizeof(int)); assert(r);
     55 
     56   TEST(oldr[26], 0x77777777, "  free-filled");
     57 
     58   free(r);
     59 
     60   //-------------------------------------------------------------
     61   fprintf(stderr, "\ntest calloc:\n");
     62   a = calloc(100, sizeof(int)); assert(r);
     63 
     64   TEST(a[42], 0x00000000, "zero");
     65 
     66   free(a);
     67 
     68   return 0;
     69 }
     70