-
Notifications
You must be signed in to change notification settings - Fork 0
/
mminline-tests.c
212 lines (177 loc) · 7.3 KB
/
mminline-tests.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "mminline.h"
#include "mm.h"
#define USAGE \
"./run_tests <all | " \
"names of tests to run>" \
"\n Ex. \"./inline_tests all\" runs all tests" \
"\n Ex. \"./inline_tests set_flink set_blink\" runs the set_flink and set_blink " \
"\n Ex. \"./inline_tests pull_free_block\" runs the pull_free_block test" \
"\n Possible tests: 'set_flink', 'set_blink', 'pull_free_block'"
void assert_flink(block_t *expected, block_t *actual, const char *message);
void assert_blink(block_t *expected, block_t *actual, const char *message);
void assert_pull_free_block(block_t *expected, block_t *actual, const char *message);
void print_test_summary();
static block_t *flist_first;
block_t* prologue;
block_t* epilogue;
void set_flink_test() {
prologue = malloc(16);
epilogue = malloc(16);
block_t *cur_block = (block_t *)malloc(8*2 + 32);
block_t *new_flink = (block_t *)malloc(8*2 + 64);
block_set_size_and_allocated(cur_block, 32, 0);
block_set_size_and_allocated(new_flink, 64, 0);
block_set_flink(cur_block, new_flink);
assert(block_flink(cur_block) == new_flink);
assert(block_size(cur_block) == 32);
assert(block_size(new_flink) == 64);
assert(!block_allocated(cur_block));
assert(!block_allocated(new_flink));
free(prologue);
free(epilogue);
free(cur_block);
free(new_flink);
}
void set_blink_test() {
prologue = malloc(16);
epilogue = malloc(16);
block_t *cur_block = (block_t *)malloc(8*2 + 32);
block_t *new_blink = (block_t *)malloc(8*2 + 64);
block_set_size_and_allocated(cur_block, 32, 0);
block_set_size_and_allocated(new_blink, 64, 0);
block_set_blink(cur_block, new_blink);
assert(block_blink(cur_block) == new_blink);
assert(block_size(cur_block) == 32);
assert(block_size(new_blink) == 64);
assert(!block_allocated(cur_block));
assert(!block_allocated(new_blink));
free(prologue);
free(epilogue);
free(cur_block);
free(new_blink);
}
void pull_free_block_test() {
prologue = malloc(16);
epilogue = malloc(16);
block_t *block_one = (block_t *)malloc(8*2 + 32);
block_t *block_two = (block_t *)malloc(8*2 + 64);
block_t *block_three = (block_t *)malloc(8*2 + 48);
flist_first = NULL;
block_set_size_and_allocated(block_one, 32, 0);
block_set_size_and_allocated(block_two, 64, 0);
block_set_size_and_allocated(block_three, 48, 0);
insert_free_block(block_three);
insert_free_block(block_two);
insert_free_block(block_one);
pull_free_block(block_two);
sleep(1);
assert(block_one->payload[0] == (int)((char *)block_three - (char *)prologue));
assert(block_one->payload[1] == (int)((char *)block_three - (char *)prologue));
assert(block_three->payload[0] == (int)((char *)block_one - (char *)prologue));
assert(block_three->payload[1] == (int)((char *)block_one - (char *)prologue));
assert(flist_first != NULL);
assert((block_t *)flist_first == (block_t *) block_one);
pull_free_block(block_three);
sleep(1);
assert(block_one->payload[0] == (int)((char *)block_one - (char *)prologue));
assert(block_one->payload[1] == (int)((char *)block_one - (char *)prologue));
assert(flist_first != NULL);
assert((block_t *)flist_first == (block_t *) block_one);
pull_free_block(block_one);
sleep(1);
assert(flist_first == NULL);
block_t *block_four = (block_t *)malloc(8*2 + 32);
block_t *block_five = (block_t *)malloc(8*2 + 64);
block_t *block_six = (block_t *)malloc(8*2 + 48);
block_t *block_seven = (block_t *)malloc(8*2 + 96);
block_set_size_and_allocated(block_four, 32, 0);
block_set_size_and_allocated(block_five, 64, 0);
block_set_size_and_allocated(block_six, 48, 0);
block_set_size_and_allocated(block_seven, 96, 0);
insert_free_block(block_seven);
insert_free_block(block_six);
insert_free_block(block_five);
insert_free_block(block_four);
pull_free_block(block_four);
sleep(1);
assert(block_five->payload[0] == (int)((char *)block_six - (char *)prologue));
assert(block_five->payload[1] == (int)((char *)block_seven - (char *)prologue));
assert(block_seven->payload[0] == (int)((char *)block_five - (char *)prologue));
assert(block_seven->payload[1] == (int)((char *)block_six - (char *)prologue));
assert(flist_first != NULL);
assert((block_t *)flist_first == (block_t *) block_five);
pull_free_block(block_five);
sleep(1);
assert(block_six->payload[1] == (int)((char *)block_seven - (char *)prologue));
assert(block_six->payload[0] == (int)((char *)block_seven - (char *)prologue));
assert(block_seven->payload[1] == (int)((char *)block_six - (char *)prologue));
assert(block_seven->payload[0] == (int)((char *)block_six - (char *)prologue));
assert(flist_first != NULL);
assert((block_t *)flist_first == (block_t *) block_six);
pull_free_block(block_six);
sleep(1);
assert(block_seven->payload[1] == (int)((char *)block_seven - (char *)prologue));
assert(block_seven->payload[0] == (int)((char *)block_seven - (char *)prologue));
assert(flist_first != NULL);
assert((block_t *)flist_first == (block_t *) block_seven);
pull_free_block(block_seven);
sleep(1);
assert(flist_first == NULL);
free(prologue);
free(epilogue);
free(block_one);
free(block_two);
free(block_three);
free(block_four);
free(block_five);
free(block_six);
free(block_seven);
}
int total_tests, num_correct, num_incorrect;
int run_test_in_separate_process(void (*func)(), int num_tests, const char *message) {
printf("running test: ");
printf("%s\n", message);
func();
num_tests--;
return 0;
}
void foreach_test(int num_tests, char const *test_names[],
int (*wrapper)(void (*)(), int, const char *)){
int functions_passed = 0;
int dummy;
if (!strcmp(test_names[1], "all")){
functions_passed += wrapper(&set_blink_test,5, "set_blink");
functions_passed += wrapper(&set_flink_test, 6, "set_flink");
functions_passed += wrapper(&pull_free_block_test, 4, "pull_free_block");
return;
}
for (int i = 0; i < num_tests; ++i){
if (i == 0)
continue; // file name
const char *test_name = test_names[i];
if (!strcmp(test_name, "set_blink"))
functions_passed += wrapper(&set_blink_test,5, "set_blink");
else if (!strcmp(test_name, "set_flink"))
functions_passed += wrapper(&set_flink_test, 6, "set_flink");
else if (!strcmp(test_name, "pull_free_block"))
functions_passed += wrapper(&pull_free_block_test, 4, "pull_free_block");
else if (sscanf(test_name, "%d", &dummy) != 1)
printf("Unknown test: %s\n", test_name);
}
}
int main(int argc, char const *argv[]){
printf("Testing for correctness...\n");
if (argc < 2){
printf("USAGE: %s\n", USAGE);
return 1;
}
foreach_test(argc, argv, &run_test_in_separate_process);
printf("Passed all tests!\n");
return 0;
}