forked from lc-soft/LCUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibtest.c
180 lines (165 loc) · 4.76 KB
/
libtest.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
179
180
#include "libtest.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <LCUI/util.h>
#ifdef _WIN32
#define COLOR_NONE
#define COLOR_RED
#define COLOR_GREEN
#else
#define COLOR_NONE "\e[0m"
#define COLOR_RED "\e[0;31m"
#define COLOR_GREEN "\e[0;32m"
#endif
#define RED(TEXT) COLOR_RED TEXT COLOR_NONE
#define GREEN(TEXT) COLOR_GREEN TEXT COLOR_NONE
static size_t tests_passed = 0;
static size_t tests_total = 0;
static int test_msg_indent = 0;
static int64_t test_start_time = 0;
static int test_msg(const char *fmt, ...)
{
int ret;
char str[1024];
va_list args;
va_start(args, fmt);
ret = vsnprintf(str, 1023, fmt, args);
va_end(args);
printf("%*s%s", test_msg_indent * 2, "", str);
return ret;
}
void test_begin(void)
{
if (test_start_time == 0) {
LCUITime_Init();
test_start_time = LCUI_GetTime();
}
test_msg_indent++;
}
void test_end(void)
{
test_msg_indent--;
}
int test_result(void)
{
if (tests_total > tests_passed) {
return (int)(tests_total - tests_passed);
}
return 0;
}
int print_test_result(void)
{
printf(GREEN(" %zu passing") " (%ums)\n", tests_passed,
(unsigned)LCUI_GetTimeDelta(test_start_time));
if (tests_total > tests_passed) {
printf(RED(" %zu faling\n\n"), tests_total - tests_passed);
return (int)(tests_total - tests_passed);
}
printf("\n");
return 0;
}
void describe(const char *name, void (*func)())
{
test_msg("%s\n", name);
test_begin();
func();
test_end();
test_msg("\n");
}
void it_i(const char *name, int actual, int expected)
{
tests_total++;
if (actual == expected) {
test_msg(GREEN("√ ") "%s == %d\n", name, expected);
tests_passed++;
return;
}
test_msg(RED("× %s == %d\n"), name, expected);
test_msg(RED(" AssertionError: %d == %d\n"), actual, expected);
test_msg(GREEN(" + expected ") RED("- actual\n\n"));
test_msg(RED(" - %d\n"), actual);
test_msg(GREEN(" + %d\n\n"), expected);
}
void it_b(const char *name, int actual, int expected)
{
const char *actual_str = actual ? "true" : "false";
const char *expected_str = expected ? "true" : "false";
tests_total++;
if (!actual == !expected) {
test_msg(GREEN("√ ") "%s\n", name);
tests_passed++;
return;
}
test_msg(RED("× %s\n"), name);
test_msg(RED(" AssertionError: %s == %s\n"), actual_str, expected_str);
test_msg(GREEN(" + expected ") RED("- actual\n\n"));
test_msg(RED(" - %s\n"), actual_str);
test_msg(GREEN(" + %s\n\n"), expected_str);
}
void it_s(const char *name, const char *actual, const char *expected)
{
tests_total++;
if ((actual && expected && strcmp(actual, expected) == 0) ||
actual == expected) {
test_msg(GREEN("√ ") "%s == '%s'\n", name, expected);
tests_passed++;
return;
}
test_msg(RED("× %s == '%s'\n"), name, expected);
if (expected) {
test_msg(RED(" AssertionError: '%s' == '%s'\n"), actual,
expected);
} else {
test_msg(RED(" AssertionError: '%s' == null\n"), actual);
}
test_msg(GREEN(" + expected ") RED("- actual\n\n"));
test_msg(RED(" - %s\n"), actual);
test_msg(GREEN(" + %s\n\n"), expected);
}
void it_rectf(const char *name, const LCUI_RectF *actual,
const LCUI_RectF *expected)
{
tests_total++;
if (LCUIRectF_IsEquals(actual, expected)) {
test_msg(GREEN("√ ") "%s == (%g, %g, %g, %g)\n", name,
expected->x, expected->y, expected->width,
expected->height);
tests_passed++;
return;
}
test_msg(RED("× %s == (%g, %g, %g, %g)\n"), name, expected->x,
expected->y, expected->width, expected->height);
test_msg(
RED(" AssertionError: (%g, %g, %g, %g) == (%g, %g, %g, %g)\n"),
actual->x, actual->y, actual->width, actual->height, expected->x,
expected->y, expected->width, expected->height);
test_msg(GREEN(" + expected ") RED("- actual\n\n"));
test_msg(RED(" - (%g, %g, %g, %g)\n"), actual->x, actual->y,
actual->width, actual->height);
test_msg(GREEN(" + (%g, %g, %g, %g)\n\n"), expected->x, expected->y,
expected->width, expected->height);
}
void it_rect(const char *name, const LCUI_Rect *actual,
const LCUI_Rect *expected)
{
tests_total++;
if (LCUIRect_IsEquals(actual, expected)) {
test_msg(GREEN("√ ") "%s == (%d, %d, %d, %d)\n", name,
expected->x, expected->y, expected->width,
expected->height);
tests_passed++;
return;
}
test_msg(RED("× %s == (%d, %d, %d, %d)\n"), name, expected->x,
expected->y, expected->width, expected->height);
test_msg(
RED(" AssertionError: (%d, %g, %d, %d) == (%d, %d, %d, %d)\n"),
actual->x, actual->y, actual->width, actual->height, expected->x,
expected->y, expected->width, expected->height);
test_msg(GREEN(" + expected ") RED("- actual\n\n"));
test_msg(RED(" - (%d, %d, %d, %d)\n"), actual->x, actual->y,
actual->width, actual->height);
test_msg(GREEN(" + (%d, %d, %d, %d)\n\n"), expected->x, expected->y,
expected->width, expected->height);
}