-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_test.c
178 lines (150 loc) · 3.77 KB
/
unit_test.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
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
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "includes/unit_test.h"
#include "sh_colors.h"
static int success_tests = 0;
static int total_tests = 0;
static int context_tests = 0;
static int context_success = 0;
static int active_test_suite = 0;
static char current_context[64];
static char current_test[128];
static int test_status = -1;
static void assert_active_test_suite() {
if(active_test_suite != 1) {
fprintf(stderr, "No active test suite! Call begin_test_suite() first!\n");
assert(0);
}
}
static void assert_active_test() {
if(test_status == -1) {
fprintf(stderr, "Assert called, but no test was active\n");
assert(0);
}
}
void begin_test_suite() {
if(active_test_suite == 1) {
fprintf(stderr, "Test suite already active!\n");
assert(0);
} else {
printf("============= BEGIN TEST SUITE ===============\n\n");
}
success_tests = 0;
total_tests = 0;
active_test_suite = 1;
}
int end_test_suite() {
assert_active_test_suite();
active_test_suite = 0;
const char * status;
printf("============= END TEST SUITE ===============\n");
if(success_tests == total_tests) {
status = "succeeded";
color(SH_FG_GREEN);
} else {
status = "failed";
color(SH_FG_RED);
}
printf("Test suite %s. Succesive tests: %d/%d\n", status, success_tests, total_tests);
reset_color();
return !(success_tests == total_tests);
}
void begin_context(const char * context) {
assert_active_test_suite();
context_tests = 0;
context_success = 0;
test_status = -1;
strcpy(current_context, context);
printf("==== %s ====\n", context);
}
int end_context() {
const char * status;
end_test();
printf("==============================\n");
printf("Result for %s: %d/%d successfull tests\n\n", current_context, context_success, context_tests);
return !(context_success == context_tests);
}
void begin_test(const char * test_name) {
assert_active_test_suite();
end_test();
test_status = 0;
strcpy(current_test, test_name);
}
int end_test() {
if(test_status == -1) {
return 0;
} else {
++context_tests;
++total_tests;
if(test_status == 0) { //Previous test succeeded
++context_success;
++success_tests;
test_status = -1; //Inactivate
color(SH_FG_GREEN);
printf("Test %s: OK\n", current_test);
reset_color();
return 0;
} else {
test_status = -1;
return 1;
}
}
}
/***
* Assertions
*/
static void core_assert(int expr, const char * error_msg) {
assert_active_test();
if(test_status == 0) {
if(expr == 0) {
color(SH_FG_RED);
printf("Test %s: FAIL. Expected %s\n", current_test, error_msg);
test_status=1;
reset_color();
}
}
}
void assert_custom(int expr, const char * msg) {
core_assert(expr, msg);
}
void assert_true(int expr) {
core_assert(expr, "expression to be true");
}
void assert_false(int expr) {
core_assert((expr==0), "expression to be false");
}
void assert_gt(int v1,int v2) {
char msg[128];
sprintf(msg, "%d to be greater than %d", v1, v2);
core_assert((v1>v2), msg);
}
void assert_lt(int v1, int v2) {
char msg[128];
sprintf(msg, "%d to be less than %d", v1, v2);
core_assert((v1<v2), msg);
}
void assert_strings_equal(const char * str1, const char * str2) {
char msg[256];
if(strlen(str1)+strlen(str2) < 240) {
sprintf(msg, "\"%s\" to be equal to \"%s\"", str1, str2);
} else {
sprintf(msg, "strings to be equal");
}
core_assert((strcmp(str1, str2)==0), msg);
}
void assert_strings_equal_n(const char * str1, const char * str2, int n) {
char msg[256];
if(2*n < 240) {
sprintf(msg, "\"%.*s\" to be equal to \"%.*s\"", n,str1,n, str2);
} else {
sprintf(msg, "strings to be equal");
}
core_assert((strncmp(str1, str2, n)==0), msg);
}
void assert_ints_equal(int i1, int i2) {
char msg[128];
sprintf(msg, "%d to be equal to %d", i1, i2);
core_assert((i1==i2), msg);
}