-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_main.m
151 lines (115 loc) · 3.62 KB
/
test_main.m
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
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#define DEBUG 1
#define TEST 1
int _fileTestsPassCount = 0;
int _totalTestsPassCount = 0;
int _currentTestAsserted = 0;
const char *_currentTestCaseName;
jmp_buf _returnToPerformTestCase;
void colorBold() {
#ifndef NO_COLOR
printf("\033[1m");
#endif
}
void colorGreen() {
#ifndef NO_COLOR
printf("\033[32m");
#endif
}
void colorRed() {
#ifndef NO_COLOR
printf("\033[31m");
#endif
}
void colorGray() {
#ifndef NO_COLOR
printf("\033[90m");
#endif
}
void colorReset() {
#ifndef NO_COLOR
printf("\033[0m");
#endif
}
void handleSignal(int sig, siginfo_t *info, void *where) {
longjmp(_returnToPerformTestCase, 1);
}
#define ASSERT_MSG_VA(cond, msg, ...) \
do { \
if (!(cond)) { \
assertHandler(__FILE__, (i32)__LINE__, "(" #cond ") " msg, __VA_ARGS__); \
fprintf(stderr, "\n"); \
_currentTestAsserted = 1; \
longjmp(_returnToPerformTestCase, 1); \
} \
} while(0)
#define ASSERT_MSG(cond, msg) ASSERT_MSG_VA(cond, msg, 0)
#define ASSERT(cond) ASSERT_MSG_VA(cond, "", 0)
#define PANIC(msg) ASSERT_MSG_VA(0, msg, 0)
#define UNIMPLEMENTED() ASSERT_MSG_VA(0, "unimplemented", 0);
#include "unity.c"
void setSignalHandlerCheckingError(int sig) {
struct sigaction sa_new = {0};
sa_new.sa_sigaction = handleSignal;
sa_new.sa_flags = SA_SIGINFO;
if (sigaction(sig, &sa_new, NULL) == -1) {
perror("failed to set handler for signal");
exit(1);
}
}
void _performTestCaseReportingResults(void (*testCase)(void), const char *name) {
_currentTestCaseName = name;
if (setjmp(_returnToPerformTestCase) == 0)
testCase();
int success = _currentTestAsserted ? 0 : 1;
size_t width = strlen(name);
int approxWidth = 60;
if (!success) approxWidth -= 4;
// Pad if it's shorter than desired.
// Then output the actual thing.
printf(" %s", name);
putchar(' ');
while (width++ <= approxWidth)
putchar('.');
putchar(' ');
success ? colorGreen() : colorRed();
printf("%s\n", success ? "OK" : "FAILED");
colorReset();
_fileTestsPassCount += _currentTestAsserted ? 0 : 1;
_totalTestsPassCount += _currentTestAsserted ? 0 : 1;
_currentTestAsserted = 0;
}
int main() {
setSignalHandlerCheckingError(SIGINT);
setSignalHandlerCheckingError(SIGABRT);
setSignalHandlerCheckingError(SIGILL);
setSignalHandlerCheckingError(SIGSEGV);
setSignalHandlerCheckingError(SIGFPE);
setSignalHandlerCheckingError(SIGBUS);
setSignalHandlerCheckingError(SIGPIPE);
printf("src/string.c:\n");
_performTestCaseReportingResults(test_stringInterning, "test_stringInterning");
colorGray();
printf("%3.1f%% success (%d out of 1)\n\n", (double) _fileTestsPassCount / 1.f * 100, _fileTestsPassCount);
colorReset();
_fileTestsPassCount = 0;
printf("src/queue.c:\n");
_performTestCaseReportingResults(test_queue, "test_queue");
colorGray();
printf("%3.1f%% success (%d out of 1)\n\n", (double) _fileTestsPassCount / 1.f * 100, _fileTestsPassCount);
colorReset();
_fileTestsPassCount = 0;
printf("src/compiler.c:\n");
_performTestCaseReportingResults(test_flagParsingAndDefaults, "test_flagParsingAndDefaults");
colorGray();
printf("%3.1f%% success (%d out of 1)\n\n", (double) _fileTestsPassCount / 1.f * 100, _fileTestsPassCount);
colorReset();
_fileTestsPassCount = 0;
// All tests finished
colorBold();
printf("%3.1f%% success (%d out of 3)\n", (double) _totalTestsPassCount / 3.f * 100, _totalTestsPassCount);
colorReset();
return !(_totalTestsPassCount == 3);
}