/
cunittests.c
executable file
·388 lines (312 loc) · 11.4 KB
/
cunittests.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <CUnit/Basic.h>
#include "beargit.h"
#include "util.h"
/* printf/fprintf calls in this tester will NOT go to file. */
#undef printf
#undef fprintf
/* The suite initialization function.
* You'll probably want to delete any leftover files in .beargit from previous
* tests, along with the .beargit directory itself.
*
* You'll most likely be able to share this across suites.
*/
int init_suite(void)
{
// preps to run tests by deleting the .beargit directory if it exists
fs_force_rm_beargit_dir();
unlink("TEST_STDOUT");
unlink("TEST_STDERR");
return 0;
}
/* You can also delete leftover files after a test suite runs, but there's
* no need to duplicate code between this and init_suite
*/
int clean_suite(void)
{
return 0;
}
/* Simple test of fread().
* Reads the data previously written by testFPRINTF()
* and checks whether the expected characters are present.
* Must be run after testFPRINTF().
*/
void simple_sample_test(void)
{
// This is a very basic test. Your tests should likely do more than this.
// We suggest checking the outputs of printfs/fprintfs to both stdout
// and stderr. To make this convenient for you, the tester replaces
// printf and fprintf with copies that write data to a file for you
// to access. To access all output written to stdout, you can read
// from the "TEST_STDOUT" file. To access all output written to stderr,
// you can read from the "TEST_STDERR" file.
int retval;
retval = beargit_init();
CU_ASSERT(0==retval);
retval = beargit_add("asdf.txt");
CU_ASSERT(0==retval);
}
struct commit {
char msg[MSG_SIZE];
struct commit* next;
};
void free_commit_list(struct commit** commit_list) {
if (*commit_list) {
free_commit_list(&((*commit_list)->next));
free(*commit_list);
}
*commit_list = NULL;
}
void run_commit(struct commit** commit_list, const char* msg) {
int retval = beargit_commit(msg);
CU_ASSERT(0==retval);
struct commit* new_commit = (struct commit*)malloc(sizeof(struct commit));
new_commit->next = *commit_list;
strcpy(new_commit->msg, msg);
*commit_list = new_commit;
}
void simple_log_test(void)
{
struct commit* commit_list = NULL;
int retval;
retval = beargit_init();
CU_ASSERT(0==retval);
FILE* asdf = fopen("asdf.txt", "w");
fclose(asdf);
retval = beargit_add("asdf.txt");
CU_ASSERT(0==retval);
run_commit(&commit_list, "THIS IS BEAR TERRITORY!1");
run_commit(&commit_list, "THIS IS BEAR TERRITORY!2");
run_commit(&commit_list, "THIS IS BEAR TERRITORY!3");
retval = beargit_log(10);
CU_ASSERT(0==retval);
struct commit* cur_commit = commit_list;
const int LINE_SIZE = 512;
char line[LINE_SIZE];
FILE* fstdout = fopen("TEST_STDOUT", "r");
CU_ASSERT_PTR_NOT_NULL(fstdout);
while (cur_commit != NULL) {
char refline[LINE_SIZE];
// First line is commit -- don't check the ID.
CU_ASSERT_PTR_NOT_NULL(fgets(line, LINE_SIZE, fstdout));
CU_ASSERT(!strncmp(line,"commit", strlen("commit")));
// Second line is msg
sprintf(refline, " %s\n", cur_commit->msg);
CU_ASSERT_PTR_NOT_NULL(fgets(line, LINE_SIZE, fstdout));
CU_ASSERT_STRING_EQUAL(line, refline);
// Third line is empty
CU_ASSERT_PTR_NOT_NULL(fgets(line, LINE_SIZE, fstdout));
CU_ASSERT(!strcmp(line,"\n"));
cur_commit = cur_commit->next;
}
CU_ASSERT_PTR_NULL(fgets(line, LINE_SIZE, fstdout));
// It's the end of output
CU_ASSERT(feof(fstdout));
fclose(fstdout);
free_commit_list(&commit_list);
}
//this test tests reset. It checks for the two erroring cases
//It checks if reset replaces the version of the file in the
//working directory with the specified file for reset.
//it also tests if index is update and file is sucessfully
//reset if the file is not in the working directory.
void reset_test(void){
struct commit* commit_list = NULL;
int retval;
retval = beargit_init();
CU_ASSERT(0==retval);
write_string_to_file("wug.txt", "Dis a wug");
write_string_to_file("wug2.txt", "Der two wugz");
write_string_to_file("wug3.txt", "Dis not a wug");
retval = beargit_add("wug.txt");
CU_ASSERT(0==retval);
run_commit(&commit_list, "THIS IS BEAR TERRITORY!1");
retval = beargit_add("wug2.txt");
CU_ASSERT(0==retval);
run_commit(&commit_list, "THIS IS BEAR TERRITORY!2");
retval = beargit_add("wug3.txt");
CU_ASSERT(0==retval);
run_commit(&commit_list, "THIS IS BEAR TERRITORY!3");
char commit_id[512] = "";
read_string_from_file(".beargit/.prev", commit_id, 512);
//Check for error if commit doens't exit
retval = beargit_reset("alskdfjlaskfj", "wug.txt");
CU_ASSERT(1==retval); //first line of stderr: "ERROR: Commit alskdfjlaskfj does not exist.\n"
//Check for error if file not in commit
retval = beargit_reset(commit_id, "wug6.txt");
CU_ASSERT(1==retval); //second line of stderr: "ERROR: wug6.txt is not in the index of commit [insert commit_id].\n"
//modifies wug.txt in working directory
write_string_to_file("wug.txt", "JK dis not a wug");
//Resets wug.txt from commit 3
retval = beargit_reset(commit_id, "wug.txt");
CU_ASSERT(0==retval);
char stuffinwug[512] = "";
read_string_from_file("wug.txt", stuffinwug, 512);
CU_ASSERT(strcmp(stuffinwug, "Dis a wug") == 0);
//remove wug3.txt
fs_rm("wug3.txt");
retval = beargit_rm("wug3.txt");
CU_ASSERT(retval == 0);
FILE* fstdout_wug = fopen("wug3.txt", "r");
CU_ASSERT_PTR_NULL(fstdout_wug);
//try to reset wug3.txt
retval = beargit_reset(commit_id, "wug3.txt");
FILE* check_wug_exist = fopen("wug3.txt", "r");
CU_ASSERT_PTR_NOT_NULL(check_wug_exist);
char check_index_4wug[512] = "";
read_string_from_file(".beargit/.index", check_index_4wug, 512);
char PostToBe[512] = "wug2.txt\nwug.txt\nwug3.txt\n";
CU_ASSERT(strcmp(check_index_4wug, PostToBe) == 0);
fclose(check_wug_exist);
//Read stderr file
char PostToBe4Err[512] = "";
strcat(PostToBe4Err, "ERROR: Commit alskdfjlaskfj does not exist.\nERROR: wug6.txt is not in the index of commit ");
strcat(PostToBe4Err, commit_id);
strcat(PostToBe4Err, ".\n");
char ActualErr[512] ="";
read_string_from_file("TEST_STDERR", ActualErr, 512);
CU_ASSERT(strcmp(PostToBe4Err, ActualErr) == 0);
free_commit_list(&commit_list);
}
//This suite tests merging multiple files. It first tests for error cases, then
//it tests for non-conflicting files then test for conflicted files.
void merge_test(void){
struct commit* commit_list = NULL;
int retval;
retval = beargit_init();
CU_ASSERT(0==retval);
write_string_to_file("wug.txt", "Dis a wug");
write_string_to_file("wug1.txt", "Dis a wug 1.5");
write_string_to_file("wug2.txt", "Der two wugz");
write_string_to_file("wug3.txt", "Dis not a wug");
retval = beargit_add("wug.txt");
CU_ASSERT(0==retval);
retval = beargit_add("wug1.txt");
CU_ASSERT(0==retval);
run_commit(&commit_list, "THIS IS BEAR TERRITORY!1");
//save commit1's ID
char commit1_id[512] = "";
read_string_from_file(".beargit/.prev", commit1_id, 512);
retval = beargit_add("wug2.txt");
CU_ASSERT(0==retval);
run_commit(&commit_list, "THIS IS BEAR TERRITORY!2");
//remove wug, wug1 and wug2
fs_rm("wug.txt");
retval = beargit_rm("wug.txt");
CU_ASSERT(0==retval);
fs_rm("wug1.txt");
retval = beargit_rm("wug1.txt");
CU_ASSERT(0==retval);
fs_rm("wug2.txt");
retval = beargit_rm("wug2.txt");
CU_ASSERT(0==retval);
//add wug3 & commit
retval = beargit_add("wug3.txt");
CU_ASSERT(0==retval);
run_commit(&commit_list, "THIS IS BEAR TERRITORY!3");
//Check for err msg when merging non existent commit ID / brench
retval = beargit_merge("alskdfjlaskfj");
CU_ASSERT(1==retval); //first line of stderr: "ERROR: No branch or commit alskdfjlaskfj exists."
//test merging non-conflicted files
retval = beargit_merge(commit1_id);
CU_ASSERT(0==retval); //first line of stdout: "wug.txt added\nwug1.txt added\n"
//check files actually moved
FILE* fstdout_wug = fopen("wug.txt", "r");
CU_ASSERT_PTR_NOT_NULL(fstdout_wug);
FILE* fstdout_wug1 = fopen("wug1.txt", "r");
CU_ASSERT_PTR_NOT_NULL(fstdout_wug1);
fclose(fstdout_wug);
fclose(fstdout_wug1);
//test merging conflicted files
retval = beargit_merge(commit1_id);
CU_ASSERT(0==retval); //first line of stdout: "wug.txt conflicted copy created \nwug1.txt conflicted copy created\n"
//check conflicted files created
char wugc[512] = "wug.txt.";
strcat(wugc, commit1_id);
char wugc1[512] = "wug1.txt.";
strcat(wugc1, commit1_id);
FILE* fstdout_wugc = fopen(wugc, "r");
CU_ASSERT_PTR_NOT_NULL(fstdout_wugc);
FILE* fstdout_wugc1 = fopen(wugc1, "r");
CU_ASSERT_PTR_NOT_NULL(fstdout_wugc1);
fclose(fstdout_wugc);
fclose(fstdout_wugc1);
//Read stderr file
char PostToBe4Err[512] = "ERROR: No branch or commit alskdfjlaskfj exists.\n";
char ActualErr[512] ="";
read_string_from_file("TEST_STDERR", ActualErr, 512);
CU_ASSERT(strcmp(PostToBe4Err, ActualErr) == 0);
// read stdout
char PostToBe[512] = "wug.txt added\nwug1.txt added\nwug.txt conflicted copy created\nwug1.txt conflicted copy created\n";
char Actual[512] ="";
read_string_from_file("TEST_STDOUT", Actual, 512);
CU_ASSERT(strcmp(PostToBe, Actual) == 0);
free_commit_list(&commit_list);
}
/* The main() function for setting up and running the tests.
* Returns a CUE_SUCCESS on successful running, another
* CUnit error code on failure.
*/
int cunittester()
{
CU_pSuite pSuite = NULL;
CU_pSuite pSuite2 = NULL;
CU_pSuite pSuite3 = NULL;
CU_pSuite pSuite4 = NULL;
/* initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
/* add a suite to the registry */
pSuite = CU_add_suite("Suite_1", init_suite, clean_suite);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* Add tests to the Suite #1 */
if (NULL == CU_add_test(pSuite, "Simple Test #1", simple_sample_test))
{
CU_cleanup_registry();
return CU_get_error();
}
pSuite2 = CU_add_suite("Suite_2", init_suite, clean_suite);
if (NULL == pSuite2) {
CU_cleanup_registry();
return CU_get_error();
}
/* Add tests to the Suite #2 */
if (NULL == CU_add_test(pSuite2, "Log output test", simple_log_test))
{
CU_cleanup_registry();
return CU_get_error();
}
pSuite3 = CU_add_suite("Suite_3", init_suite, clean_suite);
if (NULL == pSuite3) {
CU_cleanup_registry();
return CU_get_error();
}
/* Add tests to the Suite #3 */
if (NULL == CU_add_test(pSuite3, "Reset test", reset_test))
{
CU_cleanup_registry();
return CU_get_error();
}
pSuite4 = CU_add_suite("Suite_4", init_suite, clean_suite);
if (NULL == pSuite4) {
CU_cleanup_registry();
return CU_get_error();
}
/* Add tests to the Suite #4 */
if (NULL == CU_add_test(pSuite4, "Merge test", merge_test))
{
CU_cleanup_registry();
return CU_get_error();
}
/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}