From 5b31a09c18ee399869590d9b44eab0665b13c3ab Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Wed, 11 Aug 2021 01:23:10 +0100 Subject: test.c: add simple unpack_struct test --- test.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/test.c b/test.c index c06efe5..c5ce521 100644 --- a/test.c +++ b/test.c @@ -35,6 +35,17 @@ struct test { } \ } while (0) +#define CHECK_UNPACK_STRUCT(data, args, dest) do { \ + enum pack_status CHECK_UNPACK_STRUCT_s = \ + unpack_struct(data, sizeof (data), args, dest); \ + if (CHECK_UNPACK_STRUCT_s != PACK_OK) { \ + printf(__FILE__ ":%d unpack_struct(" #data ", " #args ", " #dest ") -> %s (%d)\n", \ + __LINE__, pack_strerror(CHECK_UNPACK_STRUCT_s), \ + CHECK_UNPACK_STRUCT_s); \ + return false; \ + } \ +} while(0) + #define CHECK_EQUAL(f, a, b) if (a != b) { printf(__FILE__ ":%d %"f" != %"f"\n", __LINE__, a, b); return false; } #define CHECK_PACK(dest, fmt, ...) do { \ @@ -141,6 +152,41 @@ TEST(pack_simple_general, "pack simple general") return true; } +TEST(unpack_struct_simple, "unpack_struct simple") +{ + struct dest { + int i; + unsigned char B; + float f; + } v = { __LINE__, __LINE__ + 1, __LINE__ + 2 }; + struct pack_args args = { + .endian = PACK_ENDIAN_BIG, + .fields = (struct pack_field []){ + { PACK_TYPE_INT, 1, offsetof(struct dest, i) }, + { PACK_TYPE_PADDING, 3, 0 }, + { PACK_TYPE_UCHAR, 1, offsetof(struct dest, B) }, + { PACK_TYPE_PADDING, 2, 0 }, + { PACK_TYPE_FLOAT, 1, offsetof(struct dest, f) }, + }, + .num_fields = 5, + }; + unsigned char d[] = { + 0xff, 0x85, + 0x00, 0x00, 0x00, + 0x16, + 0x00, 0x00, + 0x40, 0x44, 0x58, 0x00 + }; + + CHECK_UNPACK_STRUCT(d, &args, &v); + + CHECK_EQUAL("d", v.i, -123); + CHECK_EQUAL("d", v.B, 22); + CHECK_EQUAL("f", v.f, 0x1.88bp+1); + + return true; +} + int main(void) { extern struct test __start_tests, __stop_tests; -- cgit v1.2.3-54-g00ecf