-
Notifications
You must be signed in to change notification settings - Fork 0
/
attractor.c
272 lines (197 loc) · 6.94 KB
/
attractor.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* 2024-02-16
*
* The attractor unit test library
*/
#include "attractor.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define ATT_ERROR_MESSAGE(RESULT, FORMAT, EXPECTED) \
if(att_verbose >= 1 && att_show_error) { \
fputs(att_show_colors ? "Expected \x1B[32m" : "Expected ", stdout); \
printf(FORMAT, EXPECTED); \
fputs(att_show_colors ? "\x1B[0m, got \x1B[31m" : ", got ", stdout); \
printf(FORMAT, RESULT); \
fputs(att_show_colors ? "\x1B[0m\n\n" : "\n\n", stdout); \
}
static unsigned int att_valid_tests = 0;
static unsigned int att_total_tests = 0;
static unsigned int att_verbose = ATT_VERBOSE;
static unsigned int att_show_error = ATT_SHOW_ERROR;
static unsigned int att_columns = 80;
static int att_show_colors = 0;
unsigned int att_get_valid_tests(void) {
return att_valid_tests;
}
unsigned int att_get_total_tests(void) {
return att_total_tests;
}
void att_set_verbose(unsigned int verbose) {
att_verbose = verbose;
}
void att_set_show_error(unsigned int show_error) {
att_show_error = show_error;
}
int att_assert(const char *type, int test, const char *description);
ATT_API unsigned int att_assert_c(char result, char expected, const char *description) {
int test = att_assert("char", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%c", expected);
}
return test;
}
ATT_API unsigned int att_assert_u_c(unsigned char result, unsigned char expected, const char *description) {
int test = att_assert("char", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%c", expected);
}
return test;
}
ATT_API unsigned int att_assert_p_c(char* result, char* expected, const char *description) {
int test = att_assert("char*", ((result == expected) || ((result && expected) ? strcmp(result, expected) == 0 : 0)), description);
if(!test) {
ATT_ERROR_MESSAGE(result, ATT_STRING_AS_POINTERS == 1 ? "%p" : "\"%s\"", expected);
}
return test;
}
ATT_API unsigned int att_assert_hd(short result, short expected, const char *description) {
int test = att_assert("short", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%hd", expected);
}
return test;
}
ATT_API unsigned int att_assert_u_hu(unsigned short result, unsigned short expected, const char *description) {
int test = att_assert("short", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%hu", expected);
}
return test;
}
ATT_API unsigned int att_assert_d(int result, int expected, const char *description) {
int test = att_assert("int", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%d", expected);
}
return test;
}
ATT_API unsigned int att_assert_u_u(unsigned int result, unsigned int expected, const char *description) {
int test = att_assert("int", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%u", expected);
}
return test;
}
ATT_API unsigned int att_assert_ld(long result, long expected, const char *description) {
int test = att_assert("long", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%ld", expected);
}
return test;
}
ATT_API unsigned int att_assert_u_lu(unsigned long result, unsigned long expected, const char *description) {
int test = att_assert("long", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%lu", expected);
}
return test;
}
ATT_API unsigned int att_assert_lld(long long result, long long expected, const char *description) {
int test = att_assert("long long", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%lld", expected);
}
return test;
}
ATT_API unsigned int att_assert_u_llu(unsigned long long result, unsigned long long expected, const char *description) {
int test = att_assert("long long", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%llu", expected);
}
return test;
}
ATT_API unsigned int att_assert_f(float result, float expected, const char *description) {
int test = att_assert("float", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%f", expected);
}
return test;
}
ATT_API unsigned int att_assert_lf(double result, double expected, const char *description) {
int test = att_assert("double", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%lf", expected);
}
return test;
}
ATT_API unsigned int att_assert_Lf(long double result, long double expected, const char *description) {
int test = att_assert("long double", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%Lf", expected);
}
return test;
}
ATT_API unsigned int att_assert_p_p(void* result, void* expected, const char *description) {
int test = att_assert("void*", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%p", expected);
}
return test;
}
ATT_API unsigned int att_assert_b(_Bool result, _Bool expected, const char *description) {
int test = att_assert("_Bool", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%d", expected);
}
return test;
}
ATT_API unsigned int att_assert_unknown(int result, int expected, const char *description) {
int test = att_assert("default", result == expected, description);
if(!test) {
ATT_ERROR_MESSAGE(result, "%d", expected);
}
return test;
}
int att_assert(const char *format, int test, const char *description) {
++att_total_tests;
// Initialize the library
if(att_total_tests == 1) {
if(isatty(STDOUT_FILENO)) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
if(w.ws_col > 0) {
att_columns = w.ws_col;
}
att_show_colors = 1;
}
}
if(test) {
++att_valid_tests;
}
if(att_verbose == 0) {
return test;
} else if(att_verbose == 1) {
fputs(test ? "." : (att_show_colors ? "\x1B[31mF\x1B[0m" : "F"), stdout);
if(!test) {
fputs("\n", stdout);
}
} else {
const char *ok = att_show_colors ? "\x1B[32mOK\x1B[0m" : "OK";
const char *fail = att_show_colors ? "\x1B[31mFAIL\x1B[0m" : "FAIL";
int length = att_columns - (strlen(format) + strlen(description) + (test ? 2 : 4) + 5);
if(length <= 0) {
length = 2;
}
char spaces[length + 1];
spaces[length] = '\0';
for(int i = 0; i < length; i++) {
spaces[i] = ' ';
}
printf(att_show_colors ? "[%s] \x1b[34m%s\x1b[0m: %s%s\n" : "[%s] %s: %s%s\n",
format, description, spaces, test ? ok : fail);
}
return test;
}