aboutsummaryrefslogtreecommitdiff
path: root/test.c
diff options
context:
space:
mode:
Diffstat (limited to 'test.c')
-rw-r--r--test.c141
1 files changed, 47 insertions, 94 deletions
diff --git a/test.c b/test.c
index 8ed77a5..b45cdeb 100644
--- a/test.c
+++ b/test.c
@@ -77,10 +77,19 @@ static int failed_asserts;
body; logprintf("\n")); \
} while(0)
-#define TEST(test) \
+#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)) { \
+ if((r = test())) { \
printf("[FAILED] "#test \
" with %d errors\n", r); \
failed_test_counter++; \
@@ -92,27 +101,28 @@ static int failed_asserts;
? printf("\n") : 0; \
} while(0)
-int test_internal();
-int test_bool();
-int test_int();
-int test_float();
-int test_raw();
-int test_array();
-int test_map();
-int test_compound();
+
+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();
- TEST(test_internal());
- TEST(test_bool());
- TEST(test_int());
- TEST(test_float());
- TEST(test_raw());
- TEST(test_array());
- TEST(test_map());
- TEST(test_compound());
+ 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",
@@ -150,12 +160,10 @@ int main(void)
return; \
}
-int test_internal()
+TEST(test_internal,
{
- failed_asserts = 0;
-
- return failed_asserts;
-}
+
+})
void bool_1(uint8_t *buf, size_t size, bool value)
{
@@ -179,15 +187,12 @@ void bool_1(uint8_t *buf, size_t size, bool value)
printf("\n"); \
}); \
-int test_bool()
+TEST(test_bool,
{
- failed_asserts = 0;
-
bool_template(1, (uint8_t []){0xC2}, true);
+ return;
bool_template(1, (uint8_t []){0xC3}, false);
-
- return failed_asserts;
-}
+})
void int_1(uint8_t *buf, size_t size, union mp_int value, enum msgpack_type subtype)
{
@@ -216,51 +221,15 @@ void int_1(uint8_t *buf, size_t size, union mp_int value, enum msgpack_type subt
printf("\n"); \
}); \
-int test_int()
+TEST(test_int,
{
- failed_asserts = 0;
-
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);
-
- return failed_asserts;
-}
-
-// void float_1(uint8_t *buf, size_t size, union mp_float value, enum msgpack_type subtype)
-// {
-// uint8_t b[9] = {0};
-// union mp_float v;
-
-// MSGPACK_CHECK2(msgpack_read_float(&msgpack_init(buf, 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"));
-// } else {
-// assert(v.d == value.d, logvar(v.d, "%f"));
-// }
-
-// 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));
-// assert(memcmp(buf, b, size) == 0, logbytes(b, size));
-// }
-
-// #define float_template(n, _buf, _value, _subtype) \
-// check_condition(float_##n((_buf), sizeof(_buf), (_value), (_subtype)), \
-// { \
-// logbytes(buf, (_buf), sizeof(_buf)); \
-// 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"); \
-// printf("\n"); \
-// });
+})
void float_2(union mp_float value, enum msgpack_type subtype)
{
@@ -296,18 +265,14 @@ void float_2(union mp_float value, enum msgpack_type subtype)
})
#include <float.h>
-int test_float()
+TEST(test_float,
{
- failed_asserts = 0;
-
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);
-
- return failed_asserts;
-}
+})
void raw_1(uint8_t *bin, size_t size, struct mp_bin r)
{
@@ -330,15 +295,13 @@ void raw_1(uint8_t *bin, size_t size, struct mp_bin r)
assert(memcmp(b, bin, size) == 0, logbytes(b, size));
}
-int test_raw()
+TEST(test_raw,
{
- failed_asserts = 0;
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_______________________________"}), );
- return failed_asserts;
-}
+})
#define make_arrayormap_1(stype, btype) \
void stype##_1(uint8_t *bin, size_t size, size_t value) \
@@ -363,27 +326,19 @@ int test_raw()
make_arrayormap_1(array, MSGPACK_ARRAY)
-int test_array()
+TEST(test_array,
{
- failed_asserts = 0;
-
check_condition(array_1("\x95", 1, 5),);
check_condition(array_1("\xdc\x00\x11", 3, 17),);
-
- return failed_asserts;
-}
+})
make_arrayormap_1(map, MSGPACK_MAP)
-int test_map()
-{
- failed_asserts = 0;
-
+TEST(test_map,
+{
check_condition(map_1("\x85", 1, 5),);
check_condition(map_1("\xde\x00\x11", 3, 17),);
-
- return failed_asserts;
-}
+})
void compound_1()
{
@@ -402,9 +357,7 @@ void compound_1()
assert(v.i == 0x0100000000000201, logvar(v.u, "%"PRIi64));
}
-int test_compound()
+TEST(test_compound,
{
- failed_asserts = 0;
compound_1();
- return failed_asserts;
-}
+})