tralloc

A C library that adds size information to memory allocations.
git clone git://src.gearsix.net/tralloc
Log | Files | Refs | Atom | README

test.c (2934B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include "tralloc.h"
      5 
      6 void test_tralloc();
      7 void test_tralloc_wrap();
      8 
      9 void fail(const char *msg)
     10 {
     11 	fprintf(stderr, msg);
     12 	exit(EXIT_FAILURE);
     13 }
     14 
     15 int main ()
     16 {
     17 #ifdef TRALLOC_WRAP
     18 	#define malloc(n)       tr_malloc(n)
     19 	#define calloc(num, n)  tr_calloc(num, n)
     20 	#define realloc(ptr, n) tr_realloc(ptr, n)
     21 	#define free(ptr)       tr_free(ptr)
     22 	test_tralloc_wrap ();
     23 #else
     24 	test_tralloc();
     25 #endif
     26 
     27 	return EXIT_SUCCESS;
     28 }
     29 
     30 void test_tralloc()
     31 {
     32 	size_t siz;
     33 	char *str;
     34 
     35 	printf("tr_siz: %lu, tr_limit: %lu\n", tr_siz(), tr_limit());
     36 	if (tr_siz() != 0) fail("initial tr_siz not 0\n");
     37 	if (tr_limit() != 0) fail("initial tr_setlimit not 0\n");
     38 
     39 	siz = 6;
     40 	str = tr_malloc(siz);
     41 	printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
     42 	if (str == NULL) fail("tr_malloc failed\n");
     43 	if (tr_allocsiz(str) != (siz + sizeof(size_t))) fail("invalid tr_allocsiz value\n");
     44 	strcpy(str, "foobar");
     45 	if (strcmp(str, "foobar") != 0) fail("strcpy failed\n");
     46 	printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
     47 
     48 	siz = 12;
     49 	str = tr_realloc(str, siz);
     50 	printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
     51 	if (str == NULL) fail("tr_realloc failed\n");
     52 	if (tr_allocsiz(str) != (siz + sizeof(size_t))) fail("invalid tr_allocsiz value\n");
     53 	strcpy(str, "foobarfoobar");
     54 	if (strcmp(str, "foobarfoobar") != 0) fail("strcpy failed\n");
     55 	printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
     56 
     57 	tr_free(str);
     58 
     59 	siz = 6;
     60 	tr_setlimit(10);
     61 	puts("the following should cause an assertion failure:");
     62 	fflush(stdout);
     63 	str = tr_calloc(2, siz); /* should cause assertion failure */
     64 	printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
     65 	tr_free(str);
     66 }
     67 
     68 void test_tralloc_wrap()
     69 {
     70 	size_t siz;
     71 	char *str;
     72 
     73 	printf("tr_siz: %lu, tr_limit: %lu\n", tr_siz(), tr_limit());
     74 	if (tr_siz() != 0) fail("initial tr_siz not 0\n");
     75 	if (tr_limit() != 0) fail("initial tr_setlimit not 0\n");
     76 
     77 	siz = 6;
     78 	str = malloc(siz);
     79 	printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
     80 	if (str == NULL) fail("malloc failed\n");
     81 	if (tr_allocsiz(str) != (siz + sizeof(size_t))) fail("invalid tr_allocsiz value\n");
     82 	strcpy(str, "foobar");
     83 	if (strcmp(str, "foobar") != 0) fail("strcpy failed\n");
     84 	printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
     85 
     86 	siz = 12;
     87 	str = realloc(str, siz);
     88 	printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
     89 	if (str == NULL) fail("realloc failed\n");
     90 	if (tr_allocsiz(str) != (siz + sizeof(size_t))) fail("invalid tr_allocsiz value\n");
     91 	strcpy(str, "foobarfoobar");
     92 	if (strcmp(str, "foobarfoobar") != 0) fail("strcpy failed\n");
     93 	printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
     94 
     95 	free(str);
     96 
     97 	siz = 6;
     98 	tr_setlimit(10);
     99 	puts("the following should cause an assertion failure:");
    100 	fflush(stdout);
    101 	str = calloc(2, siz); /* should cause assertion failure */
    102 	printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
    103 	free(str);
    104 }