forked from metthal/IFJ-Projekt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.h
194 lines (167 loc) · 6.82 KB
/
test.h
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
/*
* Project name:
* Implementace interpretu imperativního jazyka IFJ13.
*
* Codename:
* INI: Ni Interpreter
*
* Description:
* https://wis.fit.vutbr.cz/FIT/st/course-files-st.php/course/IFJ-IT/projects/ifj2013.pdf
*
* Project's GitHub repository:
* https://github.com/metthal/IFJ-Projekt
*
* Team:
* Marek Milkovič (xmilko01)
* Lukáš Vrabec (xvrabe07)
* Ján Spišiak (xspisi03)
* Ivan Ševčík (xsevci50)
* Marek Bertovič (xberto00)
*/
#ifndef TEST_H
#define TEST_H
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <assert.h>
#include <signal.h>
typedef void (*TestEP)();
static const uint8_t TestNameLen = 48;
static const uint8_t TestResultPos = 50;
extern uint32_t testCountOk, testCountFailed, testFlags;
typedef enum
{
TestOk,
TestFailed
} TestResult;
typedef enum
{
None = 0,
NotPassed = 1,
VerboseOut = 2,
OnlyFailed = 4,
BreakOnFail = 8
} TestFlags;
static inline void printfc(uint8_t color, uint8_t style, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
if (!isatty(fileno(stdout)))
vprintf(fmt, args);
else {
char buffer[256];
vsprintf(buffer, fmt, args);
printf("\033[%u;%um%s\033[00m", style, color, buffer);
}
va_end(args);
}
static inline void testResult(uint8_t result, const char *testName, const char *file, uint32_t line, const char *expr)
{
char buffer[TestNameLen + 1];
memset(buffer, 0, TestNameLen + 1);
if (strlen(testName) >= TestNameLen) {
strncpy(buffer, testName, TestNameLen - 1);
buffer[TestNameLen] = '\0';
testName = buffer;
}
if (!(testFlags & NotPassed) || result == TestFailed) {
printf("%s", testName);
uint8_t spaceCount = TestResultPos - strlen(testName);
for (uint8_t i = 0; i < spaceCount; ++i)
putchar(' ');
}
if (result == TestOk) {
if (!(testFlags & NotPassed)) {
printf("[ ");
printfc(1, 32, "PASS");
printf(" ]\n");
}
testCountOk++;
}
else {
printf("[ ");
printfc(1, 31, "FAIL");
printf(" ]\n");
if (testFlags & OnlyFailed) {
printf("%s:%u %s\n", file, line, expr);
} else {
printf("Test at %s:%u failed!\n\t%s\n\n", file, line, expr);
}
testCountFailed++;
if (testFlags & BreakOnFail) {
raise(SIGINT);
}
}
}
#define REGISTER_TEST_SUITE(testSuiteName) testSuites[testSuiteCount++] = &testSuiteName;
#define RUN_TEST_SUITES for (uint8_t i = 0; i < testSuiteCount; ++i) (*testSuites[i])();
#define TEST_SUITE(testSuiteName) extern void testSuiteName();
#define TEST_SUITE_START(testSuiteName) \
void testSuiteName() { \
if (!(testFlags & OnlyFailed)) { \
printfc(1, 33, "\n%s:\n", #testSuiteName); \
printf("Starting test suite in %s...\n", __FILE__); \
} \
uint32_t testCountOkStart = testCountOk, testCountFailedStart = testCountFailed;
#define TEST_SUITE_END \
if (!(testFlags & OnlyFailed)) { \
printf("Suite tests passed: "); \
printfc(1, 32, "%38u\n", testCountOk - testCountOkStart); \
printf("Suite tests failed: "); \
printfc(1, 31, "%38u\n", testCountFailed - testCountFailedStart); \
} \
}
#define SHOULD_EQUAL(TestName, val1, val2) \
if (val1 == val2) \
testResult(TestOk, TestName, __FILE__, __LINE__, NULL); \
else \
testResult(TestFailed, TestName, __FILE__, __LINE__, "SHOULD_EQUAL("#val1 " == " #val2 ")");
#define SHOULD_NOT_EQUAL(TestName, val1, val2) \
if (val1 != val2) \
testResult(TestOk, TestName, __FILE__, __LINE__, NULL); \
else \
testResult(TestFailed, TestName, __FILE__, __LINE__, "SHOULD_NOT_EQUAL("#val1 " != " #val2 ")");
#define SHOULD_BE_GRT(TestName, val1, val2) \
if (val1 > val2) \
testResult(TestOk, TestName, __FILE__, __LINE__, NULL); \
else \
testResult(TestFailed, TestName, __FILE__, __LINE__, "SHOULD_BE_GRT("#val1 " > " #val2 ")");
#define SHOULD_BE_GRT_EQ(TestName, val1, val2) \
if (val1 >= val2) \
testResult(TestOk, TestName, __FILE__, __LINE__, NULL); \
else \
testResult(TestFailed, TestName, __FILE__, __LINE__, "SHOULD_BE_GRT_EQ("#val1 " >= " #val2 ")");
#define SHOULD_BE_LESS(TestName, val1, val2) \
if (val1 < val2) \
testResult(TestOk, TestName, __FILE__, __LINE__, NULL); \
else \
testResult(TestFailed, TestName, __FILE__, __LINE__, "SHOULD_BE_LESS("#val1 " < " #val2 ")");
#define SHOULD_BE_LESS_EQ(TestName, val1, val2) \
if (val1 <= val2) \
testResult(TestOk, TestName, __FILE__, __LINE__, NULL); \
else \
testResult(TestFailed, TestName, __FILE__, __LINE__, "SHOULD_BE_LESS_EQ("#val1 " <= " #val2 ")");
#define SHOULD_BE_TRUE(TestName, expr) \
if (expr) \
testResult(TestOk, TestName, __FILE__, __LINE__, NULL); \
else \
testResult(TestFailed, TestName, __FILE__, __LINE__, "SHOULD_BE_TRUE("#expr")");
#define SHOULD_BE_FALSE(TestName, expr) \
if (!(expr)) \
testResult(TestOk, TestName, __FILE__, __LINE__, NULL); \
else \
testResult(TestFailed, TestName, __FILE__, __LINE__, "SHOULD_BE_FALSE("#expr")");
#define SHOULD_EQUAL_STR(TestName, str1, str2) \
if (strcmp(str1, str2) == 0) \
testResult(TestOk, TestName, __FILE__, __LINE__, NULL); \
else \
testResult(TestFailed, TestName, __FILE__, __LINE__, "SHOULD_EQUAL_STR(" #str1 " == " #str2 ")");
#define SHOULD_NOT_EQUAL_STR(TestName, str1, str2) \
if (strcmp(str1, str2) != 0) \
testResult(TestOk, TestName, __FILE__, __LINE__, NULL); \
else \
testResult(TestFailed, TestName, __FILE__, __LINE__, "SHOULD_NOT_EQUAL_STR(" #str1 " != " #str2 ")");
#endif