summaryrefslogtreecommitdiff
path: root/libmisc/tests/test_endian.c
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-11-15 15:12:08 -0700
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-11-15 21:40:37 -0700
commit5704de985cff1d40359ecd15211cece0fbe79067 (patch)
tree5c172a6ea91716f4f8023e58d580e4b08fbd7fc1 /libmisc/tests/test_endian.c
parentf753128b22b61d4f85a74ba2694b8f9a576fc238 (diff)
Add tests to libmisc
Diffstat (limited to 'libmisc/tests/test_endian.c')
-rw-r--r--libmisc/tests/test_endian.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/libmisc/tests/test_endian.c b/libmisc/tests/test_endian.c
new file mode 100644
index 0000000..d0b547c
--- /dev/null
+++ b/libmisc/tests/test_endian.c
@@ -0,0 +1,32 @@
+/* libmisc/tests/test_endian.c - Tests for <libmisc/endian.h>
+ *
+ * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <string.h> /* for memcmp() */
+
+#include <libmisc/endian.h>
+
+#include "test.h"
+
+int main() {
+ uint8_t act[12] = {0};
+ uint16be_encode(&act[0], UINT16_C(0x1234));
+ uint32be_encode(&act[2], UINT32_C(0x56789ABC));
+ uint16le_encode(&act[6], UINT16_C(0x1234));
+ uint32le_encode(&act[8], UINT32_C(0x56789ABC));
+
+ uint8_t exp[12] = { 0x12, 0x34,
+ 0x56, 0x78, 0x9A, 0xBC,
+ 0x34, 0x12,
+ 0xBC, 0x9A, 0x78, 0x56 };
+ test_assert(memcmp(act, exp, 12) == 0);
+
+ test_assert(uint16be_decode(&act[0]) == UINT16_C(0x1234));
+ test_assert(uint32be_decode(&act[2]) == UINT32_C(0x56789ABC));
+ test_assert(uint16le_decode(&act[6]) == UINT16_C(0x1234));
+ test_assert(uint32le_decode(&act[8]) == UINT32_C(0x56789ABC));
+
+ return 0;
+}