From e60275a234a625b3982b9744b5e9aefa2c1f7211 Mon Sep 17 00:00:00 2001 From: kartofen Date: Thu, 10 Apr 2025 21:23:08 +0300 Subject: testing changes --- builtfiles.dd.m4 | 19 ++++++++++----- headeronly.h.m4 | 5 ++++ msgpack.c | 36 ++++++++++++++++++---------- msgpack.h | 4 ---- ninja.m4 | 28 +++++++++++----------- test.c | 73 +++++++++++++++++++++++++++++++++++++++++--------------- 6 files changed, 110 insertions(+), 55 deletions(-) create mode 100644 headeronly.h.m4 diff --git a/builtfiles.dd.m4 b/builtfiles.dd.m4 index bbbf713..70d747a 100644 --- a/builtfiles.dd.m4 +++ b/builtfiles.dd.m4 @@ -1,7 +1,7 @@ - divert(-1) +divert(-1) changequote([,]) -define(iterate_files, [esyscmd([ +define(match_extension, [esyscmd([ for f in $(command ls $1/*.$2 2> /dev/null); do echo -n "$f "; done])]) @@ -10,13 +10,20 @@ define(substitue, [esyscmd([ echo -n "${f/$2/$3} "; done])]) -define(LIBS, [iterate_files([build], [so]) iterate_files([build], [a])]) -define(HEADERS, [iterate_files([build], [h])]) +ifdef([LIBDIR], [ + define([LIBS]) + define([LIBS_SOURCE], [match_extension([build], [so]) match_extension([build], [a])]) + define([LIBS_TARGET], [substitue(LIBS_SOURCE, BIN, LIBDIR)])]) + +ifdef([INCLUDEDIR], [ + define([HEADERS]) + define([HEADERS_SOURCE], [match_extension([build], [h])]) + define([HEADERS_TARGET], [substitue(HEADERS_SOURCE, BIN, INCLUDEDIR)])]) divert(0) ninja_dyndep_version = 1 -build libs | substitue(LIBS, BIN, LIBDIR): dyndep | LIBS +ifdef([LIBS], [build libs | LIBS_TARGET: dyndep | LIBS_SOURCE]) +ifdef([HEADERS], [build headers | HEADERS_TARGET: dyndep | HEADERS_SOURCE]) -build includes | substitue(HEADERS, BIN, INCLUDEDIR): dyndep | HEADERS diff --git a/headeronly.h.m4 b/headeronly.h.m4 new file mode 100644 index 0000000..47d67a1 --- /dev/null +++ b/headeronly.h.m4 @@ -0,0 +1,5 @@ +m4_include(msgpack.h) + +#ifdef MSGPACK_IMPLEMENTATION +m4_include(msgpack.c) +#endif diff --git a/msgpack.c b/msgpack.c index 2e6b57f..70cd463 100644 --- a/msgpack.c +++ b/msgpack.c @@ -2,7 +2,6 @@ #include // endianness #include // memcpy -// TODO: fix all these #define PINT8_H(p) (*(int8_t*)(p)) #define PINT16_H(p) ((int16_t)be16toh(*(uint16_t *)(p))) #define PINT32_H(p) ((int32_t)be32toh(*(uint32_t *)(p))) @@ -13,15 +12,26 @@ #define PUINT32_H(p) (be32toh(*(uint32_t *)(p))) #define PUINT64_H(p) (be64toh(*(uint64_t *)(p))) -#define H_PINT8(b) (*(int8_t*)(b)) -#define H_PINT16(b) ((int16_t)htobe16((int16_t)(b))) -#define H_PINT32(b) ((int32_t)htobe32((int32_t)(b))) -#define H_PINT64(b) ((int64_t)htobe64((int64_t)(b))) +#define H_UINT8(b) ((uint8_t)b) +#define H_UINT16(b) ((uint16_t)htobe16((uint16_t)(b))) +#define H_UINT32(b) ((uint32_t)htobe32((uint32_t)(b))) +#define H_UINT64(b) ((uint64_t)htobe64((uint64_t)(b))) -#define F32_H(b) (*(float *) &be32toh(*(uint32_t *)(b))) -#define F64_H(b) (*(double *)&be64toh(*(uint64_t *)(b))) -#define H_F32(b) (*(float *) &htobe32(*(uint32_t *)(b))) -#define H_F64(b) (*(double *)&htobe64(*(uint64_t *)(b))) +#define H_UINT8_CPY(b, p) (((uint8_t*)(p))[0] = H_UINT8(b)); +#define H_UINT16_CPY(b, p) (((uint16_t*)(p))[0] = H_UINT16(b)) +#define H_UINT32_CPY(b, p) (((uint32_t*)(p))[0] = H_UINT32(b)) +#define H_UINT64_CPY(b, p) (((uint64_t*)(p))[0] = H_UINT64(b)) + +#define H_INT8_CPY(b, p) H_UINT8_CPY(b, p) +#define H_INT16_CPY(b, p) H_UINT16_CPY(b, p) +#define H_INT32_CPY(b, p) H_UINT32_CPY(b, p) +#define H_INT64_CPY(b, p) H_UINT64_CPY(b, p) + +// fix these +// #define F32_H(b) (*(float *) &be32toh(*(uint32_t *)(b))) +// #define F64_H(b) (*(double *)&be64toh(*(uint64_t *)(b))) +// #define H_F32(b) (*(float *) &htobe32(*(uint32_t *)(b))) +// #define H_F64(b) (*(double *)&htobe64(*(uint64_t *)(b))) // range low, range high #define RANGES(X) \ @@ -174,7 +184,9 @@ const enum msgpack_type subtype; #define OP_INT_READ(_byte, ptr, value, x_conv) \ value = P##x_conv##_H(ptr) -#define OP_INT_WRITE(x_byte, ptr, value, x_conv) +#define OP_INT_WRITE(x_byte, ptr, value, x_conv) \ + (ptr-1)[0] = x_byte; /* temp fix*/ \ + H_##x_conv##_CPY((value), (ptr)) #define MAKE_X_INT(op, x_byte, x_fmt, x_type, x_subtype, x_offset, x_length, x_field, x_conv, ...) \ case x_fmt: \ @@ -211,7 +223,7 @@ int msgpack_read_int (msgpack_t *pack, union mp_int *m) COMPOSE_FUNCTION(RE int msgpack_write_nil (msgpack_t *pack); int msgpack_write_bool (msgpack_t *pack, const bool *m) COMPOSE_FUNCTION(WRITE, BOOL) -int msgpack_write_int (msgpack_t *pack, const union mp_int *m, enum msgpack_type subtype); +int msgpack_write_int (msgpack_t *pack, const union mp_int *m, enum msgpack_type subtype) COMPOSE_FUNCTION(WRITE, INT) 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); @@ -243,7 +255,7 @@ static enum msgpack_fmt pack_fmt(msgpack_t *pack) #define WRAP_FMT_INT(e) \ case MSGPACK_INT: e; break; -#define X_FMT_INT(_byte, x_fmt, x_type, x_subtype, _offset, _length, x_field, _conv, x_range_high, x_range_low) \ +#define X_FMT_INT(_byte, x_fmt, x_type, x_subtype, _offset, _length, x_field, _conv, x_range_low, x_range_high) \ if((x_subtype == subtype) && \ (((union mp_int *)m)->x_field >= x_range_low) && \ (((union mp_int *)m)->x_field <= x_range_high)) \ diff --git a/msgpack.h b/msgpack.h index ab3a643..6171664 100644 --- a/msgpack.h +++ b/msgpack.h @@ -242,8 +242,4 @@ static const char * const msgpack_error_string[] = { #define MSGPACK_CHECK2(call, arg, body_suc, body_err) \ __MSGPACK_CHECK_DEFER(call, body_suc, body_err, __EXPAND arg) -#ifdef MSGPACK_IMPLEMENTATION -// include(SOURCE) -#endif - #endif diff --git a/ninja.m4 b/ninja.m4 index 7d6c058..7181a00 100644 --- a/ninja.m4 +++ b/ninja.m4 @@ -5,7 +5,7 @@ define([getenv], [ define([$1], [esyscmd([echo -n $$1])]) ifelse($1, , [ ifelse($2, , [undefine([$1])], [define([$1], [$2])]) -])]) + ])]) # global vars getenv([PROD]) @@ -36,8 +36,8 @@ build $bin/msgpack.h: cpy msgpack.h build $bin/libmsgpack.a: ar $bin/msgpack.o build $bin/libmsgpack.so: cc $bin/msgpack.o cflags = $cflags -fPIC -shared -# build $bin/libmsgpack.h: m4 msgpack.h | msgpack.c -# m4flags = -D SOURCE=msgpack.c +build $bin/libmsgpack.h: m4 headeronly.h.m4 | msgpack.c msgpack.h + m4flags = -P # installing @@ -47,24 +47,24 @@ getenv([LIBDIR], [PREFIX/lib]) getenv([INCLUDEDIR], [PREFIX/include]) divert(0) -rule cpy_to_dir - command = cp $$(command ls $pattern 2> /dev/null) $dir +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_to_dir || $bin/files.dd - dir = LIBDIR - pattern = $bin/*.a $bin/*.so - dyndep = $bin/files.dd +build libs: cpy_pattern || $bin/files.dd description = Install library files to 'LIBDIR' -build includes: cpy_to_dir || $bin/files.dd - dir = INCLUDEDIR - pattern = $bin/*.h 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 # testing @@ -80,9 +80,9 @@ rule 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 header: phony $bin/libmsgpack.h -build install: phony libs includes +build install: phony libs headers build clean: ninja_clean build test: valgrind $bin/test diff --git a/test.c b/test.c index 96a4527..f7e821a 100644 --- a/test.c +++ b/test.c @@ -46,6 +46,7 @@ static int passed_test_counter; static int failed_test_counter; static int failed_asserts; +int test_internal(); int test_bool(); int test_int(); @@ -62,6 +63,7 @@ int test_int(); int main(void) { + TEST(test_internal()); TEST(test_bool()); TEST(test_int()); @@ -74,11 +76,23 @@ int main(void) return 0; } -#define BODY_SUCCESS_1(type) \ +int test_internal() +{ + 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(a == subtype, \ + logvar(a, msgpack_type_string[a], "%s")); \ + } #define BODY_FAILED_1(t, e, a) \ { \ @@ -94,24 +108,24 @@ int main(void) void bool_1(uint8_t *buf, size_t size, bool value) { - uint8_t b[1]; + uint8_t b[1] = {0}; bool v; MSGPACK_CHECK2(msgpack_read_bool(&msgpack_init(buf, size, NULL), &v), - (t, e, a), BODY_SUCCESS_1(MSGPACK_BOOL), BODY_FAILED_1(t, e, a)); + (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(MSGPACK_BOOL), BODY_FAILED_1(t, e, a)); + (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) \ - make_template(bool_##n(_buf, sizeof(_buf), _value), { \ - logbytes(buf, _buf, sizeof(_buf)); \ - logvar(value, _value, "%d"); \ - printf("\n"); \ - }); \ +#define bool_template(n, _buf, _value) \ + make_template(bool_##n((_buf), sizeof(_buf), (_value)), { \ + logbytes(buf, (_buf), sizeof(_buf)); \ + logvar(value, (_value), "%d"); \ + printf("\n"); \ + }); \ int test_bool() { @@ -123,19 +137,40 @@ int test_bool() return failed_asserts; } -#define int_template(n, _buf, _value) \ - make_template(bool_##n(_buf, sizeof(_buf), _value), { \ - logbytes(buf, _buf, sizeof(_buf)); \ - logvar(value, _value, "%d"); \ - printf("\n"); \ - }); \ +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.i == value.i, logvar(v.u, "%lld")); + + 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) \ + make_template(int_##n((_buf), sizeof(_buf), (_value), (_subtype)), { \ + logbytes(buf, (_buf), sizeof(_buf)); \ + if((_subtype) == MSGPACK_INT_SIGNED) \ + logvar(value.i, (_value).i, "%lld"); \ + else logvar(value.u, (_value).u, "%lld"); \ + logvar(subtype, msgpack_type_string[(_subtype)], "%s"); \ + printf("\n"); \ + }); \ int test_int() { failed_asserts = 0; - // char buf[] = {0xD3, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x01}; - // char buf[] = {0xCC, 0xFF}; + 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 []){0xFF}), + ((union mp_int){.i=-1}), MSGPACK_INT_SIGNED); return failed_asserts; } -- cgit v1.2.3