-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.c
82 lines (71 loc) · 1.7 KB
/
tests.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
//
// This is the entry file into all the tests
//
#include "r2_unit.h"
#include <stdio.h>
#define R2_MATHS_IMPLEMENTATION
#include "r2_maths.h"
#define R2_STRINGS_IMPLEMENTATION
#include "r2_strings.h"
///////////////////////////////////////////////
// ADD TESTS HERE
// List C includes here and conventionally call the
// function <whatever>_test() in the all tests() method
///////////////////////////////////////////////
#include "tests/r2_maths.c"
#include "tests/r2_strings.c"
#include "tests/r2_termui.c"
///////////////////////////////////////////////
// Add suites here...
// Defined in the tests files above
const char *(*s[3])(void) = {r2_termui_test, r2_maths_test, r2_strings_test};
///////////////////////////////////////////////
//
// Running
//
int r2_tests_run = 0;
static const char *all_tests(void)
{
int c = sizeof s / sizeof(s)[0];
for (int x = 0; x < c; x++)
{
const char *error = (*s[x])();
if (error != 0)
{
return error;
}
}
return 0;
}
static void test_error(const char *str)
{
fprintf(stderr, "%s\n", str);
fflush(stderr);
}
static void test_debug(const char *str)
{
fprintf(stdout, "%s\n", str);
fflush(stdout);
}
int main(int argc, char **argv)
{
const char *result = all_tests();
test_debug("\n");
if (result != 0)
{
char f[100];
snprintf(f, 100, "FAIL: %s", result);
test_error(f);
return 0;
}
else
{
test_debug("ALL TESTS PASSED");
}
test_debug("-------------------------");
char p[20];
snprintf(p, 20, "Tests run: %d\n", r2_tests_run);
test_debug(p);
// To have Make not get mad about error state
return !(NULL == result);
}