diff options
-rw-r--r-- | builtfiles.dd.m4 | 31 | ||||
-rw-r--r-- | msgpack.c | 6 | ||||
-rw-r--r-- | msgpack.h | 9 | ||||
-rw-r--r-- | ninja.m4 | 58 | ||||
-rw-r--r-- | test.c | 141 |
5 files changed, 79 insertions, 166 deletions
diff --git a/builtfiles.dd.m4 b/builtfiles.dd.m4 deleted file mode 100644 index dd6b1e0..0000000 --- a/builtfiles.dd.m4 +++ /dev/null @@ -1,31 +0,0 @@ -divert(-1) -changequote([,]) - -define(match_extension, [esyscmd([ - for f in $(command ls $1/*.$2 2> /dev/null); do - echo -n "$f "; - done])]) -define(substitue, [esyscmd([ - for f in $1; do - echo -n "${f/$2/$3} "; - done])]) - -ifdef([BIN], [], [define([BIN], [build])]) - -ifdef([LIBDIR], [ - define([LIBS]) - define([LIBS_SOURCE], [match_extension(BIN, [so]) match_extension([build], [a])]) - define([LIBS_TARGET], [substitue(LIBS_SOURCE, BIN, LIBDIR)])]) - -ifdef([INCLUDEDIR], [ - define([HEADERS]) - define([HEADERS_SOURCE], [match_extension(BIN, [h])]) - define([HEADERS_TARGET], [substitue(HEADERS_SOURCE, BIN, INCLUDEDIR)])]) - -divert(0) - -ninja_dyndep_version = 1 - -ifdef([LIBS], [build libs | LIBS_TARGET: dyndep | LIBS_SOURCE]) -ifdef([HEADERS], [build headers | HEADERS_TARGET: dyndep | HEADERS_SOURCE]) - @@ -6,7 +6,6 @@ // are needlessly complicated, not good // handy macros for extra expansion -#define _(...) __VA_ARGS__ #define _CALL(f, ...) f(__VA_ARGS__) #define CAR(a, b) a #define CDR(a, b) b @@ -277,15 +276,10 @@ int msgpack_read_int (msgpack_t *pack, union mp_int *m) COMPOSE_FUNCTION(RE int msgpack_read_float (msgpack_t *pack, union mp_float *m) COMPOSE_FUNCTION(READ, FLOAT) int msgpack_read_raw (msgpack_t *pack, struct mp_bin *m) COMPOSE_FUNCTION(READ, RAW) -// int msgpack_read_raw_cpy (msgpack_t *pack, struct mp_bin *m); // int msgpack_read_ext (msgpack_t *pack, struct mp_bin *m) COMPOSE_FUNCTION(READ, EXT) -// int msgpack_read_ext_cpy (msgpack_t *pack, struct mp_bin *m); int msgpack_read_array (msgpack_t *pack, size_t *m) COMPOSE_FUNCTION(READ, ARRAY) int msgpack_read_map (msgpack_t *pack, size_t *m) COMPOSE_FUNCTION(READ, MAP) -// int msgpack_read_array2 (const msgpack_t *pack, struct mp_array *m); -// int msgpack_read_map2 (const msgpack_t *pack, struct mp_map *m); - // int msgpack_write_nil (msgpack_t *pack); int msgpack_write_bool (msgpack_t *pack, const bool *m) COMPOSE_FUNCTION(WRITE, BOOL) @@ -112,14 +112,14 @@ struct mp_timestamp { enum msgpack_type; -int msgpack_read_nil (msgpack_t *pack); +int msgpack_read_nil (msgpack_t *pack); // NOT IMPLEMENTED int msgpack_read_bool (msgpack_t *pack, bool *m); int msgpack_read_int (msgpack_t *pack, union mp_int *m); int msgpack_read_float (msgpack_t *pack, union mp_float *m); int msgpack_read_raw (msgpack_t *pack, struct mp_bin *m); // int msgpack_read_raw_cpy (msgpack_t *pack, struct mp_bin *m); -int msgpack_read_ext (msgpack_t *pack, struct mp_bin *m); +int msgpack_read_ext (msgpack_t *pack, struct mp_bin *m); // NOT IMPLEMENTED // int msgpack_read_ext_cpy (msgpack_t *pack, struct mp_bin *m); int msgpack_read_array (msgpack_t *pack, size_t *length); @@ -127,14 +127,13 @@ int msgpack_read_map (msgpack_t *pack, size_t *length); // int msgpack_read_array2 (const msgpack_t *pack, struct mp_array *m); // int msgpack_read_map2 (const msgpack_t *pack, struct mp_map *m); - int msgpack_write_nil (msgpack_t *pack); -int msgpack_write_bool (msgpack_t *pack, const bool *m); +int msgpack_write_bool (msgpack_t *pack, const bool *m); // NOT IMPLEMENTED int msgpack_write_int (msgpack_t *pack, const union mp_int *m, enum msgpack_type subtype); int msgpack_write_float (msgpack_t *pack, const union mp_float *m, enum msgpack_type subtype); int msgpack_write_raw (msgpack_t *pack, const struct mp_bin *m, enum msgpack_type subtype); -int msgpack_write_ext (msgpack_t *pack, const struct mp_bin *m, enum msgpack_type subtype); +int msgpack_write_ext (msgpack_t *pack, const struct mp_bin *m, enum msgpack_type subtype); // NOT IMPLEMENTED int msgpack_write_map (msgpack_t *pack, const size_t *m); int msgpack_write_array (msgpack_t *pack, const size_t *m); @@ -7,14 +7,12 @@ define([getenv], [ ifelse($2, , [undefine([$1])], [define([$1], [$2])]) ])]) -# global vars getenv([PROD]) - divert(0) # building -cflags = ifdef([PROD], [], [-Wall -Wextra -g]) -std=c99 -D_DEFAULT_SOURCE +cflags = ifdef([PROD], [-O3], [-Wall -Wextra -g]) -std=c99 -D_DEFAULT_SOURCE bin = build m4flags = @@ -42,52 +40,52 @@ build $bin/libmsgpack.h: m4 headeronly.h.m4 | msgpack.c msgpack.h # installing divert(-1) +getenv([TARGET], [header]) + getenv([PREFIX], [installdir]) getenv([LIBDIR], [PREFIX/lib]) getenv([INCLUDEDIR], [PREFIX/include]) + +define([install_file], [ +build $3/$1: install $2/$1 + dir = $3 +]) divert(0) -rule cpy_pattern - command = cp $$(command ls $pattern 2> /dev/null) $dest - -build force: phony -build $bin/files.dd: m4 builtfiles.dd.m4 | force - m4flags = -D BIN=$bin -D [LIBDIR]=LIBDIR -D [INCLUDEDIR]=INCLUDEDIR - description = Generate dynamic dependency from the built files - -build libs: cpy_pattern || $bin/files.dd - description = Install library files to 'LIBDIR' - dyndep = $bin/files.dd - dest = LIBDIR - pattern = $bin/*.a $bin/*.so -build headers: cpy_pattern || $bin/files.dd - description = Install header files to 'INCLUDEDIR' - dyndep = $bin/files.dd - dest = INCLUDEDIR - pattern = $bin/*.h +rule install + command = install -v -D -t $dir $in + +install_file(libmsgpack.h, $bin, INCLUDEDIR) +install_file(libmsgpack.so, $bin, LIBDIR) +install_file(libmsgpack.a, $bin, LIBDIR) +install_file(msgpack.h, $bin, INCLUDEDIR) # testing build $bin/test: cc msgpack.c test.c + cflags = $cflags -std=gnu99 rule valgrind command = valgrind -s --leak-check=full --show-leak-kinds=all $in # named targets +divert(-1) +define([target_table], [ifelse( + TARGET, [header], [$2/libmsgpack.h], + TARGET, [shared], [$1/libmsgpack.so $2/msgpack.h], + TARGET, [static], [$1/libmsgpack.a $2/msgpack.h] +)]) +divert(0) + rule ninja_clean command = ninja -t clean +build clean: ninja_clean -build static: phony $bin/libmsgpack.a $bin/msgpack.h -build shared: phony $bin/libmsgpack.so $bin/msgpack.h -build header: phony $bin/libmsgpack.h - -build install: phony libs headers -build clean: ninja_clean - -build test: valgrind $bin/test +build test: valgrind $bin/test -default ifdef([PROD], [static], [test]) +build install: phony target_table(LIBDIR, INCLUDEDIR) +default ifdef([PROD], [target_table($bin, $bin)], [test]) # regeneration @@ -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; -} +}) |