aboutsummaryrefslogtreecommitdiff
path: root/test.c
diff options
context:
space:
mode:
Diffstat (limited to 'test.c')
-rw-r--r--test.c135
1 files changed, 81 insertions, 54 deletions
diff --git a/test.c b/test.c
index b45cdeb..37cb4be 100644
--- a/test.c
+++ b/test.c
@@ -50,16 +50,16 @@ static FILE *logfp;
static ssize_t fsendfile(FILE *dest, FILE *source, long *offset, ssize_t count)
{
fflush(dest);
- fflush(source);
+ fflush(source);
long cur = ftell(source);
fseek(source, 0, SEEK_END);
long end = ftell(source);
- if(!offset) offset = &cur;
+ if(!offset) offset = &cur;
if(count < 0)
count = end - *offset;
-
+
fseek(source, count + *offset, SEEK_SET);
return sendfile(fileno(dest), fileno(source), offset, count);
}
@@ -74,7 +74,7 @@ static int failed_asserts;
call; \
assert("Failed Test Condition", \
prev_failed_asserts == failed_asserts, \
- body; logprintf("\n")); \
+ body); \
} while(0)
#define TEST1(name) int name(void)
@@ -131,7 +131,7 @@ int main(void)
100.0f * passed_test_counter /
(failed_test_counter + passed_test_counter));
-
+
fclose(logfp);
return 0;
}
@@ -160,9 +160,11 @@ int main(void)
return; \
}
-TEST(test_internal,
+#define BRACED(...) {__VA_ARGS__}
+
+TEST(test_internal,
{
-
+
})
void bool_1(uint8_t *buf, size_t size, bool value)
@@ -179,19 +181,20 @@ void bool_1(uint8_t *buf, size_t size, bool value)
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"); \
- }); \
+#define bool_template(n, _buf, _value) ({ \
+ uint8_t buf[] = BRACED _buf; \
+ bool value = _value; \
+ check_condition(bool_##n(buf, sizeof(buf), value), \
+ { \
+ logbytes(buf, sizeof(buf)); \
+ logvar(value, "%d"); \
+ }); \
+ }); \
TEST(test_bool,
{
- bool_template(1, (uint8_t []){0xC2}, true);
- return;
- bool_template(1, (uint8_t []){0xC3}, false);
+ bool_template(1, (0xC2), true);
+ bool_template(1, (0xC3), false);
})
void int_1(uint8_t *buf, size_t size, union mp_int value, enum msgpack_type subtype)
@@ -202,7 +205,7 @@ void int_1(uint8_t *buf, size_t size, union mp_int value, enum msgpack_type subt
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));
+ assert(v.u == value.u, logvar(v.u, "%"PRIx64));
MSGPACK_CHECK2(msgpack_write_int(&msgpack_init(b, size, NULL), &value, subtype),
(t, e, a), BODY_SUCCESS_2(t, a, MSGPACK_INT, subtype),
@@ -210,25 +213,48 @@ void int_1(uint8_t *buf, size_t size, union mp_int value, enum msgpack_type subt
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,
+#define int_template(n, _buf, _value, _subtype) ({ \
+ uint8_t buf[] = BRACED _buf; \
+ union mp_int value = BRACED _value; \
+ check_condition(int_##n(buf, sizeof(buf), value, (_subtype)), \
+ { \
+ logbytes(buf, sizeof(buf)); \
+ if((_subtype) == MSGPACK_INT_SIGNED) \
+ logvar(value.i, "%"PRIi64); \
+ else logvar(value.u, "%"PRIu64); \
+ logvar(subtype, msgpack_type_string[(_subtype)], "%s"); \
+ }); \
+ }); \
+
+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);
+ // int64
+ int_template(1, (0xD3, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF), (.i=INT64_MAX), MSGPACK_INT_SIGNED);
+ int_template(1, (0xD3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), (.i=INT64_MIN), MSGPACK_INT_SIGNED);
+ // int32
+ int_template(1, (0xD2, 0x7F, 0xFF, 0xFF, 0xFF), (.i=INT32_MAX), MSGPACK_INT_SIGNED);
+ int_template(1, (0xD2, 0x80, 0x00, 0x00, 0x00), (.i=INT32_MIN), MSGPACK_INT_SIGNED);
+ // int16
+ int_template(1, (0xD1, 0x7F, 0xFF), (.i=INT16_MAX), MSGPACK_INT_SIGNED);
+ int_template(1, (0xD1, 0x80, 0x00), (.i=INT16_MIN), MSGPACK_INT_SIGNED);
+ // int8
+ int_template(1, (0xD0, 0x7F), (.i=INT8_MAX), MSGPACK_INT_SIGNED);
+ int_template(1, (0xD0, 0x80), (.i=INT8_MIN), MSGPACK_INT_SIGNED);
+
+ // uint64
+ int_template(1, (0xCF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF), (.u=UINT64_MAX), MSGPACK_INT_UNSIGNED);
+ // uint32
+ int_template(1, (0xCE, 0xFF, 0xFF, 0xFF, 0xFF), (.u=UINT32_MAX), MSGPACK_INT_UNSIGNED);
+ // uint16
+ int_template(1, (0xCD, 0xFF, 0xFF), (.u = UINT16_MAX), MSGPACK_INT_UNSIGNED);
+ // int8
+ int_template(1, (0xCC, 0xFF), (.u=UINT8_MAX), MSGPACK_INT_UNSIGNED);
+
+ // fixint
+ int_template(1, (0x00), (.u=0x00), MSGPACK_INT_UNSIGNED);
+ int_template(1, (0x7F), (.u=0x7F), MSGPACK_INT_UNSIGNED);
+ int_template(1, (0xE0), (.i= -32), MSGPACK_INT_SIGNED);
+ int_template(1, (0xFF), (.i= -1), MSGPACK_INT_SIGNED);
})
void float_2(union mp_float value, enum msgpack_type subtype)
@@ -254,24 +280,25 @@ void float_2(union mp_float value, enum msgpack_type subtype)
}
}
-#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"); \
- })
+#define float_template(n, _value, _subtype) ({ \
+ union mp_float value = BRACED _value; \
+ check_condition(float_##n(value, (_subtype)), \
+ { \
+ if((_subtype) == MSGPACK_FLOAT_32) \
+ logvar(value.f, "%f"); \
+ else logvar(value.d, "%d"); \
+ 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);
+ float_template(2, (.f=FLT_MAX), MSGPACK_FLOAT_32);
+ float_template(2, (.f=FLT_MIN), MSGPACK_FLOAT_32);
+ float_template(2, (.d=DBL_MAX), MSGPACK_FLOAT_64);
+ float_template(2, (.d=DBL_MIN), MSGPACK_FLOAT_64);
})
void raw_1(uint8_t *bin, size_t size, struct mp_bin r)
@@ -283,7 +310,7 @@ void raw_1(uint8_t *bin, size_t size, struct mp_bin r)
(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));
@@ -325,7 +352,7 @@ TEST(test_raw,
}
make_arrayormap_1(array, MSGPACK_ARRAY)
-
+
TEST(test_array,
{
check_condition(array_1("\x95", 1, 5),);
@@ -333,9 +360,9 @@ TEST(test_array,
})
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),);
})