/
cunittests.c
executable file
·256 lines (207 loc) · 6.59 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
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <CUnit/Basic.h>
#include <limits.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;
};
//tests the add command
void add_test(void)
{
int retval;
retval = beargit_init();
CU_ASSERT(0==retval);
retval = beargit_add("asdf.txt"); //checks that a normal add will not error
CU_ASSERT(0==retval);
retval = beargit_add("asdf.txt"); //check that add will error if the file has already been added
CU_ASSERT(3==retval);
FILE* findex = fopen(".beargit/index", "r");
char line[512];
fgets(line, sizeof(line), findex);
CU_ASSERT(line == "asdf.txt"); //check add properly added filename to index
CU_ASSERT(fgets(line, sizeof(line), findex) == NULL); //check that second add didn't still add to index
fclose(findex);
}
struct commit {
char msg[MSG_SIZE];
struct commit* next;
};
//tests the remove command
void rm_test(void)
{
int retval;
retval = beargit_init();
CU_ASSERT(0==retval);
retval = beargit_add("asdf.txt");
CU_ASSERT(0==retval);
retval = beargit_rm("asdf.txt"); //tests a normal remove will not error
CU_ASSERT(0==retval);
retval = beargit_rm("asdf.txt"); //tests that a remove on a nonexistent/previously removed file does error
CU_ASSERT(1==retval);
FILE* findex = fopen(".beargit/index", "r");
char line[512];
fgets(line, sizeof(line), findex);
CU_ASSERT(line != "asdf.txt"); //check that a removed file is removed from the index
fclose(findex);
}
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, "GO BEARS!1");
run_commit(&commit_list, "GO BEARS!2");
run_commit(&commit_list, "GO BEARS!3");
retval = beargit_log(INT_MAX);
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 empty
CU_ASSERT_PTR_NOT_NULL(fgets(line, LINE_SIZE, fstdout));
CU_ASSERT(!strcmp(line,"\n"));
// Second 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")));
// Third 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);
cur_commit = cur_commit->next;
}
// Last line is empty
CU_ASSERT_PTR_NOT_NULL(fgets(line, LINE_SIZE, fstdout));
CU_ASSERT(!strcmp(line,"\n"));
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);
}
/* 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;
/* 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();
}
if (NULL == CU_add_test(pSuiteN, "Add Test", add_test))
{
CU_cleanup_registry();
return CU_get_error();
}
if (NULL == CU_add_test(pSuiteN, "Remove Test", rm_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();
}