-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1_tester.cc
257 lines (232 loc) · 6.89 KB
/
part1_tester.cc
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* You need not go through or edit this file to complete this lab.
*
* */
/*
* By Yutao Liu
*
* for CSP-2013-Fall
* ---------------
*
* By Zhe Qiu
*
* for CSE-2014-Undergraduate
*
* -----------------
*
* By Fuqian Huang
*
* for CSE-2018-Autumn
*
* -----------------
*
* By Jiahuan Shen
*
* for CSE-2021-Fall
*
* */
/* part1 tester.
* Test whether extent_client -> extent_server -> inode_manager behave correctly
*/
#include "extent_client.h"
#include <stdio.h>
#define FILE_NUM 50
#define LARGE_FILE_SIZE_MIN 512*10
#define LARGE_FILE_SIZE_MAX 512*200
#define iprint(msg) \
printf("[TEST_ERROR]: %s\n", msg);
extent_client *ec;
int total_score = 0;
int test_create_and_getattr()
{
int i, rnum;
extent_protocol::extentid_t id;
extent_protocol::attr a;
printf("========== begin test create and getattr ==========\n");
srand((unsigned)time(NULL));
for (i = 0; i < FILE_NUM; i++) {
rnum = rand() % 10;
memset(&a, 0, sizeof(a));
if (rnum < 3) {
ec->create(extent_protocol::T_DIR, id);
if ((int)id == 0) {
iprint("error creating dir\n");
return 1;
}
if (ec->getattr(id, a) != extent_protocol::OK) {
iprint("error getting attr, return not OK\n");
return 2;
}
if (a.type != extent_protocol::T_DIR) {
iprint("error getting attr, type is wrong");
return 3;
}
} else {
ec->create(extent_protocol::T_FILE, id);
if ((int)id == 0) {
iprint("error creating dir\n");
return 1;
}
if (ec->getattr(id, a) != extent_protocol::OK) {
iprint("error getting attr, return not OK\n");
return 2;
}
if (a.type != extent_protocol::T_FILE) {
iprint("error getting attr, type is wrong");
return 3;
}
}
}
total_score += 40;
printf("========== pass test create and getattr ==========\n");
return 0;
}
int test_indirect()
{
int i, j, k, rnum, size;
char *temp = (char *)malloc(LARGE_FILE_SIZE_MAX);
extent_protocol::extentid_t id_list[FILE_NUM];
std::string content[FILE_NUM];
printf("begin test indirect\n");
srand((unsigned)time(NULL));
for (i = 0; i < FILE_NUM; i++) {
if (ec->create(extent_protocol::T_FILE, id_list[i]) != extent_protocol::OK) {
printf("error create, return not OK\n");
return 1;
}
}
for (j = 0; j < 10; j++) {
for (i = 0; i < FILE_NUM; i++) {
memset(temp, 0, LARGE_FILE_SIZE_MAX);
size = (rand() % (LARGE_FILE_SIZE_MAX - LARGE_FILE_SIZE_MIN)) + LARGE_FILE_SIZE_MIN;
for (k = 0; k < size; k++) {
rnum = rand() % 26;
temp[k] = 97 + rnum;
}
content[i] = std::string(temp);
if (ec->put(id_list[i], content[i]) != extent_protocol::OK) {
printf("error put, return not OK\n");
return 1;
}
}
for (i = 0; i < FILE_NUM; i++) {
std::string buf;
if (ec->get(id_list[i], buf) != extent_protocol::OK) {
printf("error get, return not OK\n");
return 2;
}
if (buf.compare(content[i]) != 0) {
std::cout << "error get large file, not consistent with put large file : " <<
buf << " <-> " << content[i] << "\n";
return 3;
}
}
}
for (i = 0; i < FILE_NUM; i++) {
if (ec->remove(id_list[i]) != extent_protocol::OK) {
printf("error remove, return not OK\n");
return 4;
}
}
total_score += 10;
printf("end test indirect\n");
return 0;
}
int test_put_and_get()
{
int i, rnum;
extent_protocol::extentid_t id;
extent_protocol::attr a;
int contents[FILE_NUM];
char *temp = (char *)malloc(10);
printf("========== begin test put and get ==========\n");
srand((unsigned)time(NULL));
for (i = 0; i < FILE_NUM; i++) {
memset(&a, 0, sizeof(a));
id = (extent_protocol::extentid_t)(i+2);
if (ec->getattr(id, a) != extent_protocol::OK) {
iprint("error getting attr, return not OK\n");
return 1;
}
if (a.type == extent_protocol::T_FILE) {
rnum = rand() % 10000;
memset(temp, 0, 10);
sprintf(temp, "%d", rnum);
std::string buf(temp);
if (ec->put(id, buf) != extent_protocol::OK) {
iprint("error put, return not OK\n");
return 2;
}
contents[i] = rnum;
}
}
for (i = 0; i < FILE_NUM; i++) {
memset(&a, 0, sizeof(a));
id = (extent_protocol::extentid_t)(i+2);
if (ec->getattr(id, a) != extent_protocol::OK) {
iprint("error getting attr, return not OK\n");
return 3;
}
if (a.type == extent_protocol::T_FILE) {
std::string buf;
if (ec->get(id, buf) != extent_protocol::OK) {
iprint("error get, return not OK\n");
return 4;
}
memset(temp, 0, 10);
sprintf(temp, "%d", contents[i]);
std::string buf2(temp);
if (buf.compare(buf2) != 0) {
std::cout << "[TEST_ERROR] : error get, not consistent with put " <<
buf << " <-> " << buf2 << "\n\n";
return 5;
}
}
}
total_score += 30;
printf("========== pass test put and get ==========\n");
return 0;
}
int test_remove()
{
int i;
extent_protocol::extentid_t id;
extent_protocol::attr a;
printf("========== begin test remove ==========\n");
for (i = 0; i < FILE_NUM; i++) {
memset(&a, 0, sizeof(a));
id = (extent_protocol::extentid_t)(i+2);
if (ec->remove(id) != extent_protocol::OK) {
iprint("error removing, return not OK\n");
return 1;
}
ec->getattr(id, a);
if (a.type != 0) {
iprint("error removing, type is still positive\n");
return 2;
}
}
total_score += 20;
printf("========== pass test remove ==========\n");
return 0;
}
int main(int argc, char *argv[])
{
if (argc != 1) {
printf("Usage: ./part1_tester\n");
return 1;
}
ec = new extent_client();
if (test_create_and_getattr() != 0)
goto test_finish;
if (test_put_and_get() != 0)
goto test_finish;
if (test_remove() != 0)
goto test_finish;
if (test_indirect() != 0)
goto test_finish;
test_finish:
printf("---------------------------------\n");
printf("Part1 score is : %d/100\n", total_score);
return 0;
}