forked from VU-Programming/copp-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutil.h
73 lines (60 loc) · 1.6 KB
/
testutil.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
#ifndef TESTUTIL_H
#define TESTUTIL_H
#include <stdbool.h>
#include <stdlib.h>
// Disable abort on err using -DABORT_ON_ERR=0
// E.g. USERFLAGS=-DABORT_ON_ERR=0 make test1
#ifndef ABORT_ON_ERR
#define ABORT_ON_ERR 1
#endif
#define RUN_TEST(fun) __run_test(fun, #fun);
#define END_TEST() __print_test_result(__FILE__);
#define __assert(c) do { \
if (!(c)) { \
fprintf(stderr, "Assertion error '%s' in %s:%d\n", #c, __FILE__, __LINE__); \
__assert_errs++; \
if(ABORT_ON_ERR) abort(); \
} \
} while (0);
#define assert(c) __assert(c);
void steps(int n);
static int __passed_test_counter = 0;
static int __failed_test_counter = 0;
static int __assert_errs;
/*
* Test functions should follow the following format:
* void mytest(),
* calling assert() on all assertions.
*/
static void __run_test(void (*f)(void), char *tname) {
__assert_errs = 0;
f();
if (!__assert_errs) {
__passed_test_counter++;
return;
}
__failed_test_counter++;
fprintf(stderr, " [FAIL] Failed test %s\n", tname);
}
static int __print_test_result(char *test_name) {
int total = __passed_test_counter + __failed_test_counter;
char *msg = "[FAIL]";
if (total == __passed_test_counter) {
msg = "[Ok]";
}
fprintf(stderr, "========================================\n");
fprintf(stderr, "## %s passed %d/%d tests in %s##\n", msg, __passed_test_counter, total, test_name);
return total == __passed_test_counter ? 0 : 1;
}
void steps(int n)
{
for (int i = 0; i < n; i++)
step();
}
FILE *set_null_output(void)
{
FILE *fh = fopen("/dev/null", "w");
set_output(fh);
return fh;
}
#endif