diff options
author | kartofen <mladenovnasko0@gmail.com> | 2025-05-25 14:38:20 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2025-05-25 14:38:20 +0300 |
commit | 37982d9665dd57374984d1fe7cd0dfadc35bb03b (patch) | |
tree | 38bce80bd4f309c86de6f521ffeb546726403515 | |
parent | bc87cea97a02af805e26b00d23c876be5fbfb0ca (diff) |
-rw-r--r-- | README.md | 9 | ||||
-rw-r--r-- | ninja.m4 | 4 | ||||
-rw-r--r-- | test.c | 135 |
3 files changed, 88 insertions, 60 deletions
@@ -15,14 +15,15 @@ See the api in `msgpack.h`, it is self-explanatory. This project use ninja and m4 macros (so both need to be installed), use `ninja` to build, with options like: -* `shared`, `static`, `header` for bulding to a usable library and -* `test`to run the unit tests. +* `"no options"` to build to the current target (when `PROD=1` it runs the test) +* `test` to run the the unit tests. * `install` to install the built files in output directories set by `ninja configure` To change paths and presets using `ninja configure` with an appropriate environment variable: * `PROD=1` to use the non-debug flags -* `PREFIX=<path>` used for other paths, default `installdir` +* `TARGET=header/static/shared` to set the target type +* `PREFIX=<path>` used for other paths, default `./installdir` * `LIBDIR=<path>` used for libraries (.so and .a), default `PREFIX/lib` -* `INCLUDEDIR=<path>` used for c headers, default `PREFIX/include` +* `INCLUDEDIR=<path>` used for C headers, default `PREFIX/include` @@ -40,8 +40,6 @@ 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]) @@ -71,6 +69,8 @@ rule valgrind # named targets divert(-1) +getenv([TARGET], [header]) + define([target_table], [ifelse( TARGET, [header], [$2/libmsgpack.h], TARGET, [shared], [$1/libmsgpack.so $2/msgpack.h], @@ -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),); }) |