-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutest.c
342 lines (292 loc) · 8.4 KB
/
utest.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
#define _UTEST_IMPL
#include "utest.h"
#undef _UTEST_IMPL
#include <stdarg.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
int n_Tests;
UTestCase *_current_test;
UTestCase **AllTests;
__attribute__((constructor(101)))
void __setup(void)
{
AllTests = (UTestCase**)malloc(0);
n_Tests = 0;
}
__attribute__((destructor))
void __cleanup(void)
{
for (int i = 0; i < n_Tests; i++) {
free(AllTests[i]);
}
free(AllTests);
}
static void RunnerInit(UTestRunner*);
static int RunTest(UTestRunner*);
static int PrintIgnored(void);
static size_t pipe_read_util(int fd, char** buffer);
#define COL_OK "\x1b[1;32m"
#define COL_WARNING "\x1b[1;35m"
#define COL_ERROR "\x1b[1;31m"
#define COL_RESET "\x1b[0m"
#define MSG_OK COL_OK "Ok" COL_RESET
#define MSG_FAIL COL_ERROR "Fail" COL_RESET
int RunTests(void)
{
int status = 0, ignored;
int n = n_Tests;
UTestRunner runner;
RunnerInit(&runner);
ignored = PrintIgnored();
for (int i = 0; i < n; i++)
{
if (AllTests[i]->ignore) {
continue;
}
_current_test = AllTests[i];
runner.test = _current_test;
status += RunTest(&runner);
}
_current_test = NULL;
n -= ignored;
if (status == 0)
printf("\n" MSG_OK);
else
printf("\n" MSG_FAIL);
printf(": %d of %d tests passed\n", n - status, n);
return status;
}
static int RunTest(UTestRunner* r) {
if (r->test->ignore)
return 0;
if (r->test->setup != NULL)
r->test->setup();
r->test->test(r);
if (r->test->teardown != NULL)
r->test->teardown();
if (r->test->capture_output)
free(r->test->output);
if (r->test->status == 0)
printf(".");
else
printf("x");
if (r->test->status > 0)
return 1;
return 0;
}
static int RunnerFail(const char* fmt, ...)
{
char fmtbuf[256];
va_list args;
snprintf(fmtbuf, sizeof(fmtbuf), COL_ERROR "Assertion Failure:" COL_RESET " %s", fmt);
va_start(args, fmt);
vfprintf(stderr, fmtbuf, args);
va_end(args);
abort();
}
static void RunnerInit(UTestRunner* runner)
{
runner->fail = RunnerFail;
runner->warning = utest_warning;
}
void utest_build_testcase(UTestCase opt, TestMethod tst, char *name) {
UTestCase* newtest = malloc(sizeof(UTestCase));
newtest->name = name;
newtest->test = tst;
newtest->status = 0;
newtest->ignore = opt.ignore;
newtest->capture_output = opt.capture_output;
newtest->output = NULL;
if (opt.setup != NULL)
newtest->setup = opt.setup;
else
newtest->setup = NULL;
if (opt.teardown != NULL)
newtest->teardown = opt.teardown;
else
newtest->teardown = NULL;
AllTests = (UTestCase**)realloc(
AllTests, (n_Tests + 1) * sizeof(UTestCase*));
AllTests[n_Tests++] = newtest;
}
static int PrintIgnored(void)
{
int i, n = 0;
for (i = 0; i < n_Tests; i++) {
if (AllTests[i]->ignore) {
printf(COL_WARNING "Ignoring testcase: " COL_RESET "'%s'\n", AllTests[i]->name);
n++;
}
}
return n;
}
int assertion_failure(const char* fmt, ...)
{
char fmtbuf[256];
va_list args;
snprintf(fmtbuf, sizeof(fmtbuf), COL_ERROR "Assertion Failure:" COL_RESET " %s", fmt);
va_start(args, fmt);
vfprintf(stderr, fmtbuf, args);
va_end(args);
return 1;
}
int utest_warning(const char* fmt, ...)
{
char fmtbuf[256];
va_list args;
snprintf(fmtbuf, sizeof(fmtbuf), COL_WARNING "Test Warning:" COL_RESET " %s", fmt);
va_start(args, fmt);
vfprintf(stdout, fmtbuf, args);
va_end(args);
return 1;
}
int utest_capture_output(char **buf, size_t* len)
{
static int init = 1;
static int stdout_save = -1;
static int outpipe[2] = {-1, -1};
fflush(stdout);
if (init) // initialize output capture
{
if (pipe(outpipe) != 0) {
fprintf(stderr, "couldn't create output capture pipe\n");
exit(1);
}
if ((stdout_save = dup(STDOUT_FILENO)) == -1)
fprintf(stderr, "couldn't copy stdout\n");
if (dup2(outpipe[1], STDOUT_FILENO) == -1)
fprintf(stderr, "couldn't rediect stdout to pipe\n");
init = 0; // done with initialization
return 1;
}
else // end output capture
{
write(outpipe[1], "", 1);
close(outpipe[1]);
dup2(stdout_save, STDOUT_FILENO);
*len = pipe_read_util(outpipe[0], buf);
_current_test->output = *buf;
close(outpipe[0]);
init = 1; // should run init stage next time capture_output is run
outpipe[1] = -1; outpipe[0] = -1;
stdout_save = -1;
return 0;
}
}
static size_t pipe_read_util(int fd, char** buffer) {
char buf[256];
size_t buffer_len = 0;
size_t read_count = read(fd, buf, sizeof(buf)-1);
if (read_count > 0) {
if (*buffer == NULL) {
*buffer = malloc(read_count + 1);
}
memcpy(*buffer, buf, read_count + 1);
}
buffer_len = read_count;
while (read_count == sizeof(buf) - 1) {
read_count = read(fd, buf, sizeof(buf) - 1);
buffer_len += read_count;
*buffer = realloc(*buffer, buffer_len + 1);
memcpy(*buffer + buffer_len - read_count, buf, read_count + 1);
}
return buffer_len;
}
/**
* Return: 1 for a match, 0 for not the same.
*/
int binary_compare(byte_t* left, byte_t* right, size_t len) {
for (size_t i = 0; i < len; i++) {
if (left[i] != right[i]) {
return 0;
}
}
return 1;
}
#define _ARR_EQ_IMPL(SUFFIX, TYPE) \
int arr_unordered_eq_##SUFFIX(TYPE *a1, TYPE *a2, size_t len) { \
for (size_t i = 0; i < len; i++) { \
for (size_t k = 0; k < len; k++) { \
if (a1[i] == a2[k]) { \
goto Found; \
} \
} \
return 0; \
Found:; \
} \
return 1; \
} \
int arr_eq_##SUFFIX(TYPE *arr1, TYPE *arr2, size_t len) { \
for (size_t i = 0; i < len; i++) \
if (arr1[i] != arr2[i]) \
return 0; \
return 1; \
}
#undef _ARR_EQ_IMPL // Not currently using this
int arr_unordered_eq_s(char** a1, char** a2, size_t len)
{
size_t i, k;
for (i = 0; i < len; i++) {
for (k = 0; k < len; k++) {
if (strcmp(a1[i], a2[k]) == 0) {
goto Found;
}
}
return 0;
Found:;
}
return 1;
}
int arr_eq_s(char** arr1, char** arr2, size_t len) {
for (size_t i = 0; i < len; i++)
if (strcmp(arr1[i], arr2[i]) != 0)
return 0;
return 1;
}
int strcomp(char* one, char* two) {
while (*one)
if (*one++ != *two++)
return 1;
return 0;
}
int str_arr_contains(char** arr, size_t len, char* str)
{
size_t i;
for (i = 0; i < len; i++)
{
if (strcmp(str, arr[i]) == 0) {
return 1;
}
}
return 0;
}
static char character_set[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";
char** random_strings(int n_strings, int str_length)
{
srand(time(0));
char** list = malloc(sizeof(char*) * n_strings);
int i, k;
for (i = 0; i < n_strings; i++)
{
list[i] = malloc((str_length + 1) * sizeof(char));
for (k = 0; k < str_length; k++)
list[i][k] = character_set[rand() % (sizeof(character_set) - 1)];
list[i][k] = '\0';
}
return list;
}
void ut_timer_start(struct utest_timer* timer)
{
gettimeofday(&timer->start, NULL);
}
void ut_timer_end(struct utest_timer* timer)
{
gettimeofday(&timer->end, NULL);
}
double ut_timer_sec(struct utest_timer timer)
{
long sec = (timer.end.tv_sec) - (timer.start.tv_sec);
long usec = (timer.end.tv_usec) - (timer.start.tv_usec);
return sec + (usec / 1e6);
}