aboutsummaryrefslogtreecommitdiffstats
path: root/test.c
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2020-09-12 00:53:08 +0100
committerTomasz Kramkowski <tk@the-tk.com>2020-09-12 00:53:08 +0100
commit62963a856d3e712b2e583feef8866844ec321cf2 (patch)
tree38af3c4715712aa8d4454b8da86c56ff92c6da79 /test.c
parent23f2ddf2695fe4180538c940ccc6d368fc5ed1f6 (diff)
downloadpack-62963a856d3e712b2e583feef8866844ec321cf2.tar.gz
pack-62963a856d3e712b2e583feef8866844ec321cf2.tar.xz
pack-62963a856d3e712b2e583feef8866844ec321cf2.zip
Implement a basic pack function
Diffstat (limited to 'test.c')
-rw-r--r--test.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/test.c b/test.c
index 35813d0..adc20a1 100644
--- a/test.c
+++ b/test.c
@@ -3,6 +3,7 @@
* SPDX-License-Identifier: MIT
*/
#include <stdint.h>
+#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdbool.h>
@@ -32,8 +33,47 @@ struct test {
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 { \
+ enum pack_status CHECK_PACK_s = pack(dest, sizeof (dest), fmt, __VA_ARGS__); \
+ if (CHECK_PACK_s != PACK_OK) { \
+ printf(__FILE__ ":%d pack(%p, %zu, " #fmt ", ...) -> %s (%d)\n", __LINE__, dest, sizeof (dest), pack_strerror(CHECK_PACK_s), CHECK_PACK_s); \
+ return false; \
+ } \
+} while (0)
+
+#define CHECK_BUFEQUAL(a, b) \
+ static_assert(sizeof (a) == sizeof (b), "CHECK_BUFEQUAL - buffer sizes don't match"); \
+ if (!do_check_bufequal(__FILE__, __LINE__, (a), (b), sizeof (a))) return false;
+
+static bool do_check_bufequal(const char *file, int line, void *a_, void *b_, size_t s)
+{
+ unsigned char *a = a_, *b = b_;
+ bool mismatch = false;
+ size_t start, end;
+ for (size_t i = 0; i < s; i++) {
+ if (a[i] == b[i]) continue;
+ if (!mismatch) {
+ start = i;
+ mismatch = true;
+ }
+ end = i + 1;
+ }
+ if (!mismatch) return true;
+ if (start > 0) start--;
+ if (end < s) end++;
+ printf("%s:%d buffers unequal\n idx", file, line);
+ for (size_t i = start; i < end; i++) printf(" %02zu", i);
+ printf("\n a =");
+ for (size_t i = start; i < end; i++) printf(" %02x", a[i]);
+ printf("\n b =");
+ for (size_t i = start; i < end; i++) printf(" %02x", b[i]);
+ putchar('\n');
+ return false;
+}
+
#include "test.inc"
TEST(unpack_simple0_float, "unpack simple float")
@@ -85,6 +125,20 @@ TEST(unpack_simple_padding, "unpack simple padding")
return true;
}
+TEST(pack_simple_general, "pack simple general")
+{
+ unsigned char b[12] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
+
+ CHECK_PACK(b, ">ixxxBxxl", -123, 22, -12648430l);
+ CHECK_BUFEQUAL(b, DATA(0xff, 0x85,
+ 0x00, 0x00, 0x00,
+ 0x16,
+ 0x00, 0x00,
+ 0xff, 0x3f, 0x00, 0x12));
+
+ return true;
+}
+
int main(void)
{
extern struct test __start_tests, __stop_tests;