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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <sys/sendfile.h>
#include "msgpack.h"
// fprintf to logfp, but doesn't
// move the file position indicator
static FILE *logfp;
#define logprintf(...) \
do { \
long pos = ftell(logfp); \
fseek(logfp, 0, SEEK_END); \
fprintf(logfp, __VA_ARGS__); \
fseek(logfp, pos, SEEK_SET); \
} while(0)
#define _IN " "
#define _assert1(c) _assert2(c, ;)
#define _assert2(c, b) _assert3(#c, c, b)
#define _assert3(n, c, b) \
do { \
if(!(c)) { \
logprintf("Assertion error '%s' in %s:%d\n", n, __FILE__, __LINE__); \
b; \
failed_asserts++; \
} \
} while (0);
#define _logvar2(v, fmt) _logvar3(v, v, fmt)
#define _logvar3(n, v, fmt) logprintf(_IN #n" = "fmt"\n", v)
#define _logbuf3(b, l, fmt) _logbuf4(b, b, l, fmt)
#define _logbuf4(n, b, l, fmt) \
do { \
logprintf(_IN #n" = { "fmt, b[0]); \
for(size_t i = 1; i < l; i++) \
logprintf(", "fmt, b[i]); \
logprintf(" }\n"); \
} while(0)
#define OVERLOAD_MACRO_4(_1, _2, _3, _4, NAME, ...) NAME
#define assert(...) OVERLOAD_MACRO_4(__VA_ARGS__, _, _assert3, _assert2, _assert1) (__VA_ARGS__)
#define logvar(...) OVERLOAD_MACRO_4(__VA_ARGS__, _, _logvar3, _logvar2, _) (__VA_ARGS__)
#define logbuf(...) OVERLOAD_MACRO_4(__VA_ARGS__, _logbuf4, _logbuf3, _, _) (__VA_ARGS__)
#define logbytes(...) logbuf(__VA_ARGS__, "0x%hhx")
// sendfile for file pointers
// when count is -1, copy to the end
static ssize_t fsendfile(FILE *dest, FILE *source, long *offset, ssize_t count)
{
fflush(dest);
fflush(source);
long cur = ftell(source);
fseek(source, 0, SEEK_END);
long end = ftell(source);
if(!offset) offset = &cur;
if(count < 0)
count = end - *offset;
fseek(source, count + *offset, SEEK_SET);
return sendfile(fileno(dest), fileno(source), offset, count);
}
static int passed_test_counter;
static int failed_test_counter;
static int failed_asserts;
#define check_condition(call, body) \
do { \
int prev_failed_asserts = failed_asserts; \
call; \
assert("Failed Test Condition", \
prev_failed_asserts == failed_asserts, \
body; logprintf("\n")); \
} while(0)
#define TEST1(name) int name(void)
#define TEST2(name, ...) TEST1(name) \
{ \
failed_asserts = 0; \
({void f(void) __VA_ARGS__ f();}); \
return failed_asserts; \
}
#define TEST(...) OVERLOAD_MACRO_4(__VA_ARGS__, _, _, TEST2, TEST1) (__VA_ARGS__)
#define RUN_TEST(test) \
do { \
int r = 0; \
if((r = test())) { \
printf("[FAILED] "#test \
" with %d errors\n", r); \
failed_test_counter++; \
} else { \
printf("[PASSED] "#test"\n"); \
passed_test_counter++; \
} \
fsendfile(stdout, logfp, NULL, -1) \
? printf("\n") : 0; \
} while(0)
TEST(test_internal);
TEST(test_bool);
TEST(test_int);
TEST(test_float);
TEST(test_raw);
TEST(test_array);
TEST(test_map);
TEST(test_compound);
int main(void)
{
logfp = tmpfile();
RUN_TEST(test_internal);
RUN_TEST(test_bool);
RUN_TEST(test_int);
RUN_TEST(test_float);
RUN_TEST(test_raw);
RUN_TEST(test_array);
RUN_TEST(test_map);
RUN_TEST(test_compound);
printf("------------------------------\n");
printf("PASSED %d/%d tests, (%.1f%%)\n",
passed_test_counter,
failed_test_counter + passed_test_counter,
100.0f * passed_test_counter /
(failed_test_counter + passed_test_counter));
fclose(logfp);
return 0;
}
#define BODY_SUCCESS_1(t, type) \
{ \
assert(t == type, \
logvar(t, msgpack_type_string[t], "%s")); \
}
#define BODY_SUCCESS_2(t, a, type, subtype) \
{ \
BODY_SUCCESS_1(t, type); \
assert((int)a == (int)subtype, \
logvar(a, msgpack_type_string[a], "%s")); \
}
#define BODY_FAILED_1(t, e, a) \
{ \
assert("Failed Call", 0, ;); \
logvar(t, msgpack_type_string[t], "%s"); \
logvar(e, msgpack_error_string[e], "%s"); \
if(e == MSGPACK_ERROR_WRONG_TYPE) \
logvar(a, msgpack_type_string[a], "%s"); \
else logvar(a, "0x%hhx"); \
return; \
}
TEST(test_internal,
{
})
void bool_1(uint8_t *buf, size_t size, bool value)
{
uint8_t b[1] = {0};
bool v;
MSGPACK_CHECK2(msgpack_read_bool(&msgpack_init(buf, size, NULL), &v),
(t, e, a), BODY_SUCCESS_1(t, MSGPACK_BOOL), BODY_FAILED_1(t, e, a));
assert(v == value, logvar(v, "%d"));
MSGPACK_CHECK2(msgpack_write_bool(&msgpack_init(b, size, NULL), &value),
(t, e, a), BODY_SUCCESS_1(t, MSGPACK_BOOL), BODY_FAILED_1(t, e, a));
assert(memcmp(buf, b, size) == 0, logbytes(b, size));
}
#define bool_template(n, _buf, _value) \
check_condition(bool_##n((_buf), sizeof(_buf), (_value)), \
{ \
logbytes(buf, (_buf), sizeof(_buf)); \
logvar(value, (_value), "%d"); \
printf("\n"); \
}); \
TEST(test_bool,
{
bool_template(1, (uint8_t []){0xC2}, true);
return;
bool_template(1, (uint8_t []){0xC3}, false);
})
void int_1(uint8_t *buf, size_t size, union mp_int value, enum msgpack_type subtype)
{
uint8_t b[9] = {0};
union mp_int v;
MSGPACK_CHECK2(msgpack_read_int(&msgpack_init(buf, size, NULL), &v),
(t, e, a), BODY_SUCCESS_2(t, a, MSGPACK_INT, subtype),
BODY_FAILED_1(t, e, a));
assert(v.u == value.u, logvar(v.u, "%"PRIu64));
MSGPACK_CHECK2(msgpack_write_int(&msgpack_init(b, size, NULL), &value, subtype),
(t, e, a), BODY_SUCCESS_2(t, a, MSGPACK_INT, subtype),
BODY_FAILED_1(t, e, a));
assert(memcmp(buf, b, size) == 0, logbytes(b, size));
}
#define int_template(n, _buf, _value, _subtype) \
check_condition(int_##n((_buf), sizeof(_buf), (_value), (_subtype)), \
{ \
logbytes(buf, (_buf), sizeof(_buf)); \
if((_subtype) == MSGPACK_INT_SIGNED) \
logvar(value.i, (_value).i, "%"PRIi64); \
else logvar(value.u, (_value).u, "%"PRIu64); \
logvar(subtype, msgpack_type_string[(_subtype)], "%s"); \
printf("\n"); \
}); \
TEST(test_int,
{
int_template(1, ((uint8_t []){0xD3, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01}),
((union mp_int){.i=0x0100000000000201}), MSGPACK_INT_SIGNED);
int_template(1, ((uint8_t []){0xCF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01}),
((union mp_int){.u=0x0100000000000201}), MSGPACK_INT_UNSIGNED);
int_template(1, ((uint8_t []){0xFF}),
((union mp_int){.i=-1}), MSGPACK_INT_SIGNED);
})
void float_2(union mp_float value, enum msgpack_type subtype)
{
uint8_t b[9] = {0};
size_t size = 9;
union mp_float v;
MSGPACK_CHECK2(msgpack_write_float(&msgpack_init(b, size, NULL), &value, subtype),
(t, e, a), BODY_SUCCESS_2(t, a, MSGPACK_FLOAT, subtype),
BODY_FAILED_1(t, e, a));
MSGPACK_CHECK2(msgpack_read_float(&msgpack_init(b, size, NULL), &v),
(t, e, a), BODY_SUCCESS_2(t, a, MSGPACK_FLOAT, subtype),
BODY_FAILED_1(t, e, a));
if(subtype == MSGPACK_FLOAT_32) {
assert(v.f == value.f, logvar(v.f, "%f"));
logbytes(b, 5);
} else {
assert(v.d == value.d, logvar(v.d, "%f"));
logbytes(b, 9);
}
}
#define float_template(n, _value, _subtype) \
check_condition(float_##n((_value), (_subtype)), \
{ \
if((_subtype) == MSGPACK_FLOAT_32) \
logvar(value.f, (_value).f, "%f"); \
else logvar(value.d, (_value).d, "%f"); \
logvar(subtype, \
msgpack_type_string[(_subtype)], "%s"); \
})
#include <float.h>
TEST(test_float,
{
float_template(2, ((union mp_float){.f=-1.7}), MSGPACK_FLOAT_32);
float_template(2, ((union mp_float){.f=FLT_MAX}), MSGPACK_FLOAT_32);
float_template(2, ((union mp_float){.f=FLT_MIN}), MSGPACK_FLOAT_32);
float_template(2, ((union mp_float){.d=DBL_MAX}), MSGPACK_FLOAT_64);
float_template(2, ((union mp_float){.d=DBL_MIN}), MSGPACK_FLOAT_64);
})
void raw_1(uint8_t *bin, size_t size, struct mp_bin r)
{
uint8_t b[64] = {0};
struct mp_bin v = {0};
MSGPACK_CHECK2(msgpack_read_raw(&msgpack_init(bin, size, NULL), &v),
(t, e, a),
BODY_SUCCESS_2(t, a, MSGPACK_RAW, MSGPACK_RAW_STRING),
BODY_FAILED_1(t, e, a));
assert(v.size == r.size, logvar(v.size, "%zu"));
assert(memcmp(v.bin, r.bin, v.size) == 0, logbytes(v.bin, v.size));
MSGPACK_CHECK2(msgpack_write_raw(&msgpack_init(b, sizeof(b), NULL), &r, MSGPACK_RAW_STRING),
(t, e, a),
BODY_SUCCESS_2(t, a, MSGPACK_RAW, MSGPACK_RAW_STRING),
BODY_FAILED_1(t, e, a));
assert(memcmp(b, bin, size) == 0, logbytes(b, size));
}
TEST(test_raw,
{
check_condition(raw_1("\xa5hello", 6,
(struct mp_bin){.size = 5, .bin = "hello"}), );
check_condition(raw_1("\xd9\x24hello_______________________________", 38,
(struct mp_bin){.size = 36, .bin = "hello_______________________________"}), );
})
#define make_arrayormap_1(stype, btype) \
void stype##_1(uint8_t *bin, size_t size, size_t value) \
{ \
uint8_t b[64] = {0}; \
size_t v = {0}; \
\
MSGPACK_CHECK2(msgpack_read_##stype(&msgpack_init(bin, size, NULL), &v), \
(t, e, a), \
BODY_SUCCESS_1(t, btype), \
BODY_FAILED_1(t, e, a)); \
\
assert(value == v, logvar(v, "%zu")); \
\
MSGPACK_CHECK2(msgpack_write_##stype(&msgpack_init(b, sizeof(b), NULL), &value), \
(t, e, a), \
BODY_SUCCESS_1(t, btype), \
BODY_FAILED_1(t, e, a)); \
\
assert(memcmp(b, bin, size) == 0, logbytes(b, size)); \
}
make_arrayormap_1(array, MSGPACK_ARRAY)
TEST(test_array,
{
check_condition(array_1("\x95", 1, 5),);
check_condition(array_1("\xdc\x00\x11", 3, 17),);
})
make_arrayormap_1(map, MSGPACK_MAP)
TEST(test_map,
{
check_condition(map_1("\x85", 1, 5),);
check_condition(map_1("\xde\x00\x11", 3, 17),);
})
void compound_1()
{
uint8_t bin[] = {0x0A, 0xCF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01};
msgpack_t pack = msgpack_init(bin, sizeof(bin), NULL);
union mp_int v = {0};
MSGPACK_CHECK2(msgpack_read_int(&pack, &v),
(t, e, a), BODY_SUCCESS_2(t, a, MSGPACK_INT, MSGPACK_INT_UNSIGNED),
BODY_FAILED_1(t, e, a));
assert(v.i == 10, logvar(v.u, "%"PRIi64));
MSGPACK_CHECK2(msgpack_read_int(&pack, &v),
(t, e, a), BODY_SUCCESS_2(t, a, MSGPACK_INT, MSGPACK_INT_UNSIGNED),
BODY_FAILED_1(t, e, a));
assert(v.i == 0x0100000000000201, logvar(v.u, "%"PRIi64));
}
TEST(test_compound,
{
compound_1();
})
|