-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptest.c
187 lines (156 loc) · 4.07 KB
/
ptest.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
#include "ptest.h"
#include "stdio.h"
#include "stdint.h"
#include "stdbool.h"
#include "stdlib.h"
#include "setjmp.h"
#include "string.h"
extern const char *__progname;
typedef struct {
const char* test_name;
bool has_fixture;
void* test_func;
CREATE_FUNC create_func;
TEARDOWN_FUNC teardown_func;
void* data;
} PTestInfo;
static PTestInfo *test_funcs = NULL;
static size_t num_tests = 0;
static jmp_buf fail_jump;
static const char* failure_message = NULL;
void P_Fail() {
#ifdef PTEST_ENABLE_TRACE
void *trace[PTEST_TRACE_DEPTH + 1];
backtrace(&trace, PTEST_TRACE_DEPTH + 1);
printf("Trace:\n");
static const int buflen = 1024;
int i = 1;
for (; trace[i] && i < PTEST_TRACE_DEPTH + 1; ++i) {
char syscom[buflen];
snprintf(syscom, buflen, "addr2line -e %s -f -s -C -p %p", __progname, trace[i]);
FILE *f = popen(syscom, "r");
if (f != NULL)
{
char buffer[buflen];
memset(buffer, 0, buflen * sizeof(char));
while (fgets(buffer, sizeof(buffer), f) != NULL)
{
printf("\t%s", buffer);
}
pclose(f);
if (strstr(buffer, "main at") == buffer) {
break;
}
}
}
#endif
longjmp(fail_jump, 1);
}
void P_ClearFailMessage() {
failure_message = NULL;
}
void P_SetFailMessage(const char* fail_msg) {
failure_message = fail_msg;
}
void P_AddTest(const char* test_name, TEST_FUNC testfunc) {
num_tests++;
test_funcs = realloc(test_funcs, num_tests * sizeof(PTestInfo));
PTestInfo *info = &test_funcs[num_tests - 1];
info->has_fixture = false;
info->test_func = testfunc;
info->test_name = test_name;
info->create_func = NULL;
info->teardown_func = NULL;
}
void P_AddTestWithFixture(const char* test_name, TEST_FIXTURE_FUNC test_func, CREATE_FUNC create_func, TEARDOWN_FUNC teardown_func, void* data) {
num_tests++;
test_funcs = realloc(test_funcs, num_tests * sizeof(PTestInfo));
PTestInfo *info = &test_funcs[num_tests - 1];
info->has_fixture = true;
info->test_func = test_func;
info->test_name = test_name;
info->create_func = create_func;
info->teardown_func = teardown_func;
info->data = data;
}
static bool P_RunTest(PTestInfo* info) {
bool success = true;
failure_message = NULL;
int i = strlen(info->test_name);
putchar('\n');
for (; i > 0; i--) {
putchar('=');
}
printf("\n%s\n", info->test_name);
i = strlen(info->test_name);
for (; i > 0; i--) {
putchar('=');
}
putchar('\n');
if (!setjmp(fail_jump)) {
if (info->create_func && info->data == NULL) {
info->data = info->create_func();
}
int result = 0;
if (info->has_fixture) {
TEST_FIXTURE_FUNC test_func = (TEST_FIXTURE_FUNC)(info->test_func);
result = test_func(info->data);
}
else {
TEST_FUNC test_func = (TEST_FUNC)(info->test_func);
result = test_func();
}
if (!result) {
printf("\n[OK]\n");
}
else {
printf("\n[FAILURE] Test returned TEST_FAIL\n");
if (failure_message) {
printf("%s\n", failure_message);
success = false;
}
}
}
else {
if (failure_message) {
printf("%s\n", failure_message);
}
printf("\n[FAILURE]\n");
success = false;
}
// Allow teardown to assert if necessary. Probably shouldn't allow this tbh.
if (!setjmp(fail_jump)) {
if (info->teardown_func) {
info->teardown_func(info->data);
}
}
return success;
}
int P_RunTests() {
size_t tests_succeeded = 0;
putchar('\n');
size_t i = 0;
for (; i < num_tests; ++i) {
if (P_RunTest(&test_funcs[i])) {
tests_succeeded++;
}
}
printf("\n=====================================\n\nTests run: %zu\tTests Succeeded: %zu\tTests Failed: %zu\n\n", i, tests_succeeded, i - tests_succeeded);
return (tests_succeeded == i) ? 0 : -1;
}
int P_RunTestsWith(const char *partial_match) {
size_t tests_run = 0;
size_t tests_succeeded = 0;
putchar('\n');
size_t i = 0;
for (; i < num_tests; ++i) {
if (strstr(test_funcs[i].test_name, partial_match)) {
if (P_RunTest(&test_funcs[i])) {
tests_succeeded++;
}
tests_run++;
}
}
printf("=====================================\n\nTests run: %zu\tTests Succeeded: %zu\tTests Failed: %zu\n\n", tests_run, tests_succeeded, tests_run - tests_succeeded);
return (tests_succeeded == i) ? 0 : -1;
}