From 2e1ff80dab0cee0f197dccc9c5dd2f3b9afdb168 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Thu, 20 Aug 2020 21:34:34 +0100 Subject: init --- unpack_test.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 unpack_test.c (limited to 'unpack_test.c') diff --git a/unpack_test.c b/unpack_test.c new file mode 100644 index 0000000..8eee6b3 --- /dev/null +++ b/unpack_test.c @@ -0,0 +1,110 @@ +#include +#include + +#include "pack.h" + +typedef bool test_func(void); + +struct test { + test_func *func; + char *desc; +}; + +#define TEST(name) bool test_##name(void) +#define TEST_ENTRY(name, description) { test_##name, description } +#define TEST_ENTRY_END { NULL, NULL } +#define DATA(...) (unsigned char []){ __VA_ARGS__ }, sizeof (unsigned char []){ __VA_ARGS__ } +#define CHECK(test) if (!(test)) { puts("! " #test); return false; } + +#define CHECK_UNPACK(data, fmt, ...) do { \ + enum pack_status s = unpack(data, fmt, __VA_ARGS__); \ + if (s != PACK_OK) { \ + printf(__FILE__ ":%d unpack(" #data ", " #fmt ", ...) -> %s (%d)\n", __LINE__, pack_strerror(s), s); \ + return false; \ + } \ +} while (0) +#define CHECK_EQUAL(a, b) if (a != b) { printf(__FILE__ ":%d %d != %d\n", __LINE__, a, b); return false; } + +TEST(schar) +{ + signed char c = __LINE__; + fprintf(stderr, "Address of c: %p\n", (void *)&c); + CHECK_UNPACK(DATA(0), "b", &c); + CHECK_EQUAL(c, 0); + CHECK_UNPACK(DATA(1), "b", &c); + CHECK_EQUAL(c, 1); + CHECK_UNPACK(DATA(127), "b", &c); + CHECK_EQUAL(c, 127); + CHECK_UNPACK(DATA(255), "b", &c); + CHECK_EQUAL(c, -1); + CHECK_UNPACK(DATA(128), "b", &c); + CHECK_EQUAL(c, -128); + return true; +} + +TEST(uchar) +{ + unsigned char c = __LINE__; + fprintf(stderr, "Address of c: %p\n", (void *)&c); + CHECK_UNPACK(DATA(0), "B", &c); + CHECK_EQUAL(c, 0); + CHECK_UNPACK(DATA(1), "B", &c); + CHECK_EQUAL(c, 1); + CHECK_UNPACK(DATA(255), "B", &c); + CHECK_EQUAL(c, 255); + return true; +} + +TEST(sshort) +{ + short s = __LINE__; + fprintf(stderr, "Address of s: %p\n", (void *)&s); + CHECK_UNPACK(DATA(0, 0), "h", &s); + CHECK_EQUAL(s, 0); + CHECK_UNPACK(DATA(0, 0), ">h", &s); + CHECK_EQUAL(s, 0); + CHECK_UNPACK(DATA(0, 0), "h", &s); + CHECK_EQUAL(s, 1); + CHECK_UNPACK(DATA(1, 0), "h", &s); + CHECK_EQUAL(s, 32767); + CHECK_UNPACK(DATA(0xff, 0x7f), "h", &s); + CHECK_EQUAL(s, -1); + CHECK_UNPACK(DATA(0xff, 0xff), "h", &s); + CHECK_EQUAL(s, -32768); + CHECK_UNPACK(DATA(0x00, 0x80), "