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
|
#include "./pke-test-types.h"
#include "./pke-test-dummy.h"
#include "unistd.h"
#include <stdio.h>
#define CLR_WHITE "\033[0m"
#define CLR_GREEN "\033[92m"
#define CLR_RED "\033[31m"
// https://stackoverflow.com/questions/64190847/how-to-catch-a-call-to-exit-for-unit-testing
struct pke_test_long_jump lj;
void exit(int code) {
if (lj.expected_exit) {
lj.caught = 1;
longjmp(lj.jump_env, 1);
}
_exit(code);
}
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
int i = 0;
uint32_t k, pass_count;
pke_test_get_group *group_fns[] = {
pke_test_dummy_get_group,
NULL,
};
pke_test_get_group *fn = group_fns[i];
while (fn != NULL) {
pass_count = 0;
struct pke_test_group *group = (fn)();
fprintf(stdout, "[pke-test]:[%s] Begin.\n", group->title);
for (k = 0; k < group->n_tests; ++k) {
fprintf(stdout, "[pke-test]:[%s]:[%s] Begin.\n", group->title, group->tests[k].title);
lj.expected_exit = 0;
lj.caught = 0;
if (group->tests[k].expected_result == (group->tests[k].func)()){
pass_count += 1;
fprintf(stdout, "[pke-test]:[%s]:[%s] %sPassed.%s\n", group->title, group->tests[k].title, CLR_GREEN, CLR_WHITE);
} else {
fprintf(stdout, "[pke-test]:[%s]:[%s] %sFailed.%s\n", group->title, group->tests[k].title, CLR_RED, CLR_WHITE);
}
}
fprintf(stdout, "[pke-test]:[%s] End. ( %s%03d%s / %03d ) Tests Completed.\n\n", group->title, pass_count == group->n_tests ? CLR_GREEN : CLR_RED, pass_count, CLR_WHITE, group->n_tests);
i += 1;
fn = group_fns[i];
}
return 0;
}
|