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