-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.c
297 lines (215 loc) · 7.93 KB
/
tester.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "svc.h"
#include "helper.h"
int test_svc_init(void* helper) {
struct head* h = (struct head*) helper;
struct branch* master = h->cur_branch;
assert(strcmp(master->name, "master") == 0);
assert(master->active_commit == NULL);
assert(master == master->next_branch);
assert(((struct head*)helper)->cur_branch == master);
assert(((struct head*)helper)->tracked_files == NULL);
return 0;
}
int test_svc_branch(void* helper) {
struct head* h = (struct head*) helper;
struct branch* master = h->cur_branch;
assert(svc_branch(helper, "example_branch/branch-10") == 0);
assert(svc_branch(helper, "example") == 0);
assert(svc_branch(helper, "good-branch") == 0);
assert(svc_branch(helper, "example") == -2);
assert(svc_branch(helper, "example_branch/branch-10") == -2);
assert(svc_branch(helper, "good-branch") == -2);
return 0;
}
int test_list_branches(void* helper) {
svc_add(helper, "hello.py");
svc_add(helper, "Tests/test1.in");
svc_add(helper, "COMP2017/svc.c");
int* num_branches = malloc(sizeof(int));
char** list = list_branches(helper, num_branches);
assert(*num_branches == 4);
for (int i = 0; i < *num_branches; i++) {
printf("From list: %s\n", list[i]);
assert(strcmp(list[i], "master") == 0 ||
strcmp(list[i], "example_branch/branch-10") == 0 ||
strcmp(list[i], "example") == 0 ||
strcmp(list[i], "good-branch") == 0);
}
return 0;
}
int test_svc_add_example_1(void* helper) {
assert(svc_add(helper, "hello.py") == 2027);
assert(svc_add(helper, "hello.py") == -2);
// Do checks on helper itself.
struct head* h = (struct head*) helper;
assert(strcmp(h->tracked_files->name, "hello.py") == 0);
printf("%s", h->tracked_files->contents);
assert(h->tracked_files->stat == ADDED);
assert(h->tracked_files->prev_file == NULL);
assert(h->tracked_files->next_file == NULL);
assert(svc_add(helper, "Tests/test1.in") == 564);
// Do checks again after adding Tests/test1.in
assert(strcmp(h->tracked_files->name, "Tests/test1.in") == 0);
printf("%s", h->tracked_files->contents);
assert(h->tracked_files->stat == ADDED);
// assert(h->tracked_files->prev_file == h->tracked_files);
assert(h->tracked_files->next_file == NULL);
struct file* hello = h->tracked_files->prev_file;
assert(strcmp(hello->name, "hello.py") == 0);
printf("%s", hello->contents);
assert(hello->stat == ADDED);
assert(hello->prev_file == NULL);
assert(hello->next_file == h->tracked_files);
// assert(strcmp(h->tracked_files->name, "hello.py") == 0);
// printf("%s", h->tracked_files->contents);
// assert(h->tracked_files->stat == ADDED);
// assert(h->tracked_files->prev_file == NULL);
// assert(h->tracked_files->next_file != NULL);
// Get Tests/test1.in
// struct file* test1 = h->tracked_files->next_file;
return 0;
}
int test_svc_add_example_2(void* helper) {
assert(svc_add(helper, "COMP2017/svc.h") == 5007);
assert(svc_add(helper, "COMP2017/svc.c") == 5217);
struct head* h = (struct head*) helper;
struct file* files = h->tracked_files;
while (files != NULL) {
printf("In test: %s\n", files->name);
printf("In test: %s\n", files->contents);
files = files->prev_file;
}
return 0;
}
int test_svc_remove(void* helper) {
svc_add(helper, "hello.py");
svc_add(helper, "Tests/test1.in");
svc_add(helper, "COMP2017/svc.c");
svc_add(helper, "COMP2017/svc.h");
// assert(svc_rm(helper, "COMP2017/svc.h") == 5007);
assert(svc_rm(helper, "Tests/test1.in") == 564);
// Test file !being tracked.
assert(svc_rm(helper, "whatever.c") == -2);
struct head* h = (struct head*) helper;
struct file* files = h->tracked_files;
while (files != NULL) {
printf("In test svc remove: %s\n", files->name);
printf("In test svc remove: %s", files->contents);
files = files->prev_file;
}
return 0;
}
int test_svc_checkout(void* helper) {
assert(svc_checkout(helper, "example") == 0);
struct head* h = (struct head*) helper;
struct branch* b = h->cur_branch;
assert(strcmp(b->name, "example") == 0);
printf("Checkout branch: %s\n", b->name);
return 0;
}
int test_example_1(void* helper) {
// printf("%d\n", hash_file(helper, "hello.py"));
assert(hash_file(helper, "hello.py") == 2027);
assert(hash_file(helper, "fake.c") == -2);
assert(svc_commit(helper, "No changes") == NULL);
// Malloc hello and test strings.
char* hello = malloc(20);
strcpy(hello, "hello.py");
char* test = malloc(20);
strcpy(test, "Tests/test1.in");
// End of malloc
// Hello.py
assert(svc_add(helper, hello) == 2027);
strcpy(hello, "it");
// Tests\test1.in
assert(svc_add(helper, test) == 564);
strcpy(test, "it");
assert(svc_add(helper, test) == -3);
char* id = svc_commit(helper, "Initial commit");
assert(strcmp(id, "74cde7") == 0);
void* commit = get_commit(helper, "74cde7");
int n_prev;
char** prev_commits = get_prev_commits(helper, commit, &n_prev);
assert(prev_commits == NULL);
assert(n_prev == 0);
print_commit(helper, "74cde7");
int n;
char** branches = list_branches(helper, &n);
assert(n == 1);
printf("%s\n", branches[n - 1]);
return 0;
}
int test_example_2(void* helper) {
// Starting from a blank project, create two files and add them.
// Hash files are correct.
assert(svc_add(helper, "COMP2017/svc.c") == 5217);
assert(svc_add(helper, "COMP2017/svc.h") == 5007);
char* commid = svc_commit(helper, "Initial commit");
printf("%s\n", commid);
assert(strcmp(commid, "7b3e30") == 0);
assert(svc_branch(helper, "random_branch") == 0);
assert(svc_checkout(helper, "random_branch") == 0);
FILE * f = fopen("COMP2017/svc.c", "w");
fputs("#include \"svc.h\"\nvoid *svc_init(void) {\n return NULL;\n}\n", f);
fclose(f);
assert(svc_rm(helper, "COMP2017/svc.h") == 5007);
char* init_id = svc_commit(helper, "Implemented svc_init");
printf("Init_id: %s\n", init_id);
assert(strcmp(init_id, "73eacd") == 0);
// You realise you accidentally deleted svc.h and want to revert to initial commit.
remove("COMP2017/svc.h");
assert(svc_reset(helper, "7b3e30") == 0);
// Then the file COMP2017/svc.c is changed again to have the contents shown above.
FILE * fileptr = fopen("COMP2017/svc.c", "w");
fputs("#include \"svc.h\"\nvoid *svc_init(void) {\n return NULL;\n}\n", fileptr);
fclose(fileptr);
char* after_reset = svc_commit(helper, "Implemented svc_init");
printf("After reset: %s\n", after_reset);
assert(strcmp(after_reset, "24829b") == 0);
void* commit = get_commit(helper, "24829b");
int n_prev;
char** prev_commits = get_prev_commits(helper, commit, &n_prev);
assert(n_prev == 1);
for (int i = 0; i < n_prev; i++) {
printf("Get prev commits: %s\n", prev_commits[i]);
assert(strcmp(prev_commits[i], "7b3e30") == 0);
}
assert(svc_checkout(helper, "master") == 0);
free(prev_commits);
return 0;
}
int test_uncommitted(void* helper) {
assert(svc_add(helper, "COMP2017/svc.c") == 5217);
assert(svc_add(helper, "COMP2017/svc.h") == 5007);
char* commid = svc_commit(helper, "Initial commit");
printf("%s\n", commid);
assert(strcmp(commid, "7b3e30") == 0);
FILE * f = fopen("COMP2017/svc.c", "w");
fputs("#include \"svc.h\"\nvoid *svc_init(void) {\n return NULL;\n}\n", f);
fclose(f);
assert(svc_branch(helper, "random_branch") == -3);
return 0;
}
void rewrite() {
// Sleep before regenerating files.
// sleep(10);
FILE * svc_cfp = fopen("COMP2017/svc.c", "w");
fputs("#include \"svc.h\"\nvoid *svc_init(void) {\n // TODO: implement\n}\n", svc_cfp);
fclose(svc_cfp);
FILE* svc_hfp = fopen("COMP2017/svc.h", "w");
fputs("#ifndef svc_h\n#define svc_h\nvoid *svc_init(void);\n#endif\n", svc_hfp);
fclose(svc_hfp);
}
int main() {
void *helper = svc_init();
// assert(test_example_1(helper) == 0);
assert(test_example_2(helper) == 0);
// assert(test_uncommitted(helper) == 0);
rewrite();
cleanup(helper);
return 0;
}