-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.c
More file actions
132 lines (102 loc) · 3.06 KB
/
tests.c
File metadata and controls
132 lines (102 loc) · 3.06 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "vmemalloc.h"
#define NUM_TO_ALLOC 65
void checkBlock(unsigned char* block, unsigned char value, size_t size) {
for (int i = 0; i < (int)size; i++) {
assert(block[0] == value);
}
}
void testLarge() {
printf("Testing large allocations\n");
assert(allocatedSpace == 0);
assert(allocatedChunkCount == 0);
assert(freeChunkCount == 0);
assert(regionsUsed == 0);
unsigned char* allocd[NUM_TO_ALLOC];
allocd[0] = vmemalloc(70);
assert(allocd[0] != NULL);
memset(allocd[0], 1, 70);
assert(allocatedSpace >= 70);
assert(allocatedChunkCount == 1);
assert(freeChunkCount <= 1);
assert(regionsUsed == 1);
vmemfree(allocd[0]);
assert(allocatedSpace == 0);
assert(allocatedChunkCount == 0);
assert(freeChunkCount == 0);
assert(regionsUsed == 0);
int sizeToAlloc = 70;
for (int i = 0; i < NUM_TO_ALLOC; i++) {
allocd[i] = vmemalloc(sizeToAlloc);
assert(allocd[i] != NULL);
memset(allocd[i], i, sizeToAlloc);
sizeToAlloc += 70;
}
sizeToAlloc = 70;
for (int i = 0; i < NUM_TO_ALLOC; i++) {
// Check that nothing has changed.
for (int j = 0; j < sizeToAlloc; j++) {
assert(allocd[i][j] == i);
}
vmemfree(allocd[i]);
sizeToAlloc += 70;
}
assert(allocatedSpace == 0);
assert(allocatedChunkCount == 0);
assert(freeChunkCount == 0);
assert(regionsUsed == 0);
}
void testSmall() {
printf("Testing small allocations\n");
char *chars[NUM_TO_ALLOC];
long *longs[NUM_TO_ALLOC];
chars[0] = vmemalloc(sizeof(char));
assert(chars[0] != NULL);
*chars[0] = (char)~0;
assert(allocatedSpace >= (int)sizeof(char));
assert(allocatedChunkCount == 1);
assert(freeChunkCount <= 1);
assert(regionsUsed == 1);
vmemfree(chars[0]);
assert(allocatedSpace == 0);
assert(allocatedChunkCount == 0);
assert(regionsUsed == 0);
assert(freeChunkCount == 0);
for (int i = 0; i < NUM_TO_ALLOC; i++) {
chars[i] = vmemalloc(sizeof(char));
*chars[i] = (char)i;
longs[i] = vmemalloc(sizeof(long));
*longs[i] = (long)i;
}
assert(allocatedSpace >= NUM_TO_ALLOC * (int)(sizeof(char) + sizeof(long)));
assert(allocatedChunkCount > 0);
assert(regionsUsed > 0);
int middle = NUM_TO_ALLOC / 2;
for (int i = middle; i < NUM_TO_ALLOC; i++) {
assert(*chars[i] == (char)i);
vmemfree(chars[i]);
assert(*longs[i] == (long)i);
vmemfree(longs[i]);
}
for (int i = middle - 1; i >= 0; i--) {
assert(*chars[i] == (char)i);
vmemfree(chars[i]);
assert(*longs[i] == (long)i);
vmemfree(longs[i]);
}
assert(allocatedSpace == 0);
assert(allocatedChunkCount == 0);
assert(regionsUsed == 0);
assert(freeChunkCount == 0);
}
int main(){
setupTimer();
setTraceFile("experiment2.csv");
testLarge();
testSmall();
closeTraceFile();
printf("Test succeeded.\n");
return 0;
}