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
|
#include <assert.h>
#include <setjmp.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "test_lib.h"
#define DO_ALLOC(TYPE) ((TYPE*) malloc(sizeof(TYPE)))
#define GIT_MAX_TEST_CASES 64
struct git_test {
char *name;
git_testfunc function;
int failed;
int ran;
const char *message;
jmp_buf *jump;
};
struct git_testsuite {
char *name;
int count, fail_count;
git_test *list[GIT_MAX_TEST_CASES];
};
static void test_init(git_test *t, const char *name, git_testfunc function)
{
t->name = strdup(name);
t->failed = 0;
t->ran = 0;
t->message = NULL;
t->function = function;
t->jump = NULL;
}
static void test_free(git_test *t)
{
if (t) {
free(t->name);
free(t);
}
}
void test_run(git_test *tc)
{
jmp_buf buf;
tc->jump = &buf;
if (setjmp(buf) == 0) {
tc->ran = 1;
(tc->function)(tc);
}
tc->jump = 0;
}
git_test *git_test_new(const char *name, git_testfunc function)
{
git_test *tc = DO_ALLOC(git_test);
test_init(tc, name, function);
return tc;
}
/*-------------------------------------------------------------------------*
* Public assert methods
*-------------------------------------------------------------------------*/
static void fail_test(git_test *tc, const char *file, int line, const char *message)
{
char buf[1024];
snprintf(buf, 1024, "%s @ %s:%d", message, file, line);
tc->failed = 1;
tc->message = strdup(buf);
if (tc->jump != 0)
longjmp(*(tc->jump), 0);
}
void git_test__fail(git_test *tc, const char *file, int line, const char *message)
{
fail_test(tc, file, line, message);
}
void git_test__assert(git_test *tc, const char *file, int line, const char *message, int condition)
{
if (condition == 0)
fail_test(tc, file, line, message);
}
/*-------------------------------------------------------------------------*
* Test Suite
*-------------------------------------------------------------------------*/
static void testsuite_init(git_testsuite *ts)
{
ts->count = 0;
ts->fail_count = 0;
memset(ts->list, 0, sizeof(ts->list));
}
git_testsuite *git_testsuite_new(const char *name)
{
git_testsuite *ts = DO_ALLOC(git_testsuite);
testsuite_init(ts);
ts->name = strdup(name);
return ts;
}
void git_testsuite_free(git_testsuite *ts)
{
unsigned int n;
for (n = 0; n < GIT_MAX_TEST_CASES; n++)
if (ts->list[n])
test_free(ts->list[n]);
free(ts);
}
void git_testsuite_add(git_testsuite *ts, git_test *tc)
{
assert(ts->count < GIT_MAX_TEST_CASES);
ts->list[ts->count++] = tc;
}
void git_testsuite_addsuite(git_testsuite *ts, git_testsuite *ts2)
{
int i;
for (i = 0 ; i < ts2->count ; ++i)
git_testsuite_add(ts, ts2->list[i]);
}
static void print_details(git_testsuite *ts)
{
int i;
int failCount = 0;
if (ts->fail_count == 0) {
const char *testWord = ts->count == 1 ? "test" : "tests";
printf("OK (%d %s)\n", ts->count, testWord);
} else {
printf("Failed (%d failures):\n", ts->fail_count);
for (i = 0 ; i < ts->count ; ++i) {
git_test *tc = ts->list[i];
if (tc->failed) {
failCount++;
printf(" %d) %s: %s\n", failCount, tc->name, tc->message);
}
}
}
}
int git_testsuite_run(git_testsuite *ts)
{
int i;
printf("Suite \"%s\": ", ts->name);
for (i = 0 ; i < ts->count ; ++i) {
git_test *tc = ts->list[i];
test_run(tc);
if (tc->failed) {
ts->fail_count++;
putchar('F');
} else
putchar('.');
}
printf("\n ");
print_details(ts);
return ts->fail_count;
}
|