test: <endian.h> tests
authorMarko Kreen <markokr@gmail.com>
Wed, 26 Jan 2011 12:51:12 +0000 (14:51 +0200)
committerMarko Kreen <markokr@gmail.com>
Wed, 26 Jan 2011 12:51:12 +0000 (14:51 +0200)
test/Makefile
test/test_common.c
test/test_common.h
test/test_endian.c [new file with mode: 0644]

index 4a9d946fab3e92d5e63284551ac08219d4014ea6..b228142c49cafed12e159f8f75005962b2fdf505 100644 (file)
@@ -4,7 +4,7 @@ SRCS = test_string.c test_crypto.c test_aatree.c test_heap.c \
        test_common.c test_list.c tinytest.c test_cbtree.c \
        test_utf8.c test_strpool.c test_pgutil.c test_regex.c \
        test_cxalloc.c test_bits.c test_base.c test_netdb.c \
-       test_cfparser.c
+       test_cfparser.c test_endian.c
 OBJS = $(addprefix obj/, $(SRCS:.c=.o))
 HDRS = test_common.h test_config.h tinytest.h tinytest_macros.h
 LIBS =
index 77674617c866453dff367bd3141f5bae8e9791aa..cd6b3c5c90df1bdf39b6c0a506ccf7b54157f9fa 100644 (file)
@@ -9,6 +9,7 @@ struct testgroup_t groups[] = {
        { "cxalloc/", cxalloc_tests },
        { "cbtree/", cbtree_tests },
        { "crypto/", crypto_tests },
+       { "endian/", endian_tests },
        { "string/", string_tests },
        { "heap/", heap_tests },
        { "list/", list_tests },
index 16ece15ddf7dbbe1ec7255fdaf73db04cad208ac..109c41ecd73d8162d268a1dbfa0d25e8e1bc67a7 100644 (file)
@@ -23,4 +23,5 @@ extern struct testcase_t bits_tests[];
 extern struct testcase_t base_tests[];
 extern struct testcase_t netdb_tests[];
 extern struct testcase_t cfparser_tests[];
+extern struct testcase_t endian_tests[];
 
diff --git a/test/test_endian.c b/test/test_endian.c
new file mode 100644 (file)
index 0000000..3c341d0
--- /dev/null
@@ -0,0 +1,120 @@
+#include <usual/endian.h>
+
+#include "test_common.h"
+
+#include <stdarg.h>
+#include <string.h>
+
+/*
+ * bswap*()
+ */
+
+static void test_bswap(void *p)
+{
+       int_check(bswap16(0xff01), 0x01ff);
+       int_check(bswap32(0x01020304), 0x04030201);
+       int_check(bswap64(0x0102030405060708ULL), 0x0807060504030201ULL);
+end:;
+}
+
+/*
+ * *enc(), *dec()
+ */
+
+static uint64_t tdecode(int t, ...)
+{
+       uint8_t buf[16];
+       bool be = t > 0;
+       va_list ap;
+       uint64_t val = 777;
+       int i;
+
+       if (t < 0) t = -t;
+
+       va_start(ap, t);
+       memset(buf, 0xC1, sizeof(buf));
+       for (i = 0; i < t; i++)
+               buf[i] = va_arg(ap, int);
+       va_end(ap);
+
+       if (be) {
+               switch (t) {
+               case 2: val = be16dec(buf); break;
+               case 4: val = be32dec(buf); break;
+               case 8: val = be64dec(buf); break;
+               }
+       } else {
+               switch (t) {
+               case 2: val = le16dec(buf); break;
+               case 4: val = le32dec(buf); break;
+               case 8: val = le64dec(buf); break;
+               }
+       }
+       return val;
+}
+
+static const char *tencode(int t, uint64_t val)
+{
+       static char res[64];
+       uint8_t buf[16];
+       bool be = t > 0;
+       int i;
+
+       if (t < 0) t = -t;
+
+       memset(buf, 0xFC, sizeof(buf));
+
+       if (be) {
+               switch (t) {
+               case 2: be16enc(buf, val); break;
+               case 4: be32enc(buf, val); break;
+               case 8: be64enc(buf, val); break;
+               }
+       } else {
+               switch (t) {
+               case 2: le16enc(buf, val); break;
+               case 4: le32enc(buf, val); break;
+               case 8: le64enc(buf, val); break;
+               }
+       }
+
+       for (i = t; i < sizeof(buf); i++) {
+               if (buf[i] != 0xFC)
+                       return "OVER";
+       }
+
+       snprintf(res, sizeof(res), "%02X %02X %02X %02X %02X %02X %02X %02X ",
+                buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
+       res[t*3 - 1] = 0;
+       return res;
+}
+
+static void test_encdec(void *p)
+{
+       ull_check(tdecode( 2, 1,2), 0x0102);
+       ull_check(tdecode(-2, 1,2), 0x0201);
+       ull_check(tdecode( 4, 1,2,3,4), 0x01020304);
+       ull_check(tdecode(-4, 1,2,3,4), 0x04030201);
+       ull_check(tdecode( 8, 1,2,3,4,5,6,7,8), 0x0102030405060708);
+       ull_check(tdecode(-8, 1,2,3,4,5,6,7,8), 0x0807060504030201);
+
+       str_check(tencode( 2, 0x0102), "01 02");
+       str_check(tencode(-2, 0x0102), "02 01");
+       str_check(tencode( 4, 0x01020304), "01 02 03 04");
+       str_check(tencode(-4, 0x01020304), "04 03 02 01");
+       str_check(tencode( 8, 0x0102030405060708ULL), "01 02 03 04 05 06 07 08");
+       str_check(tencode(-8, 0x0102030405060708ULL), "08 07 06 05 04 03 02 01");
+end:;
+}
+
+
+/*
+ * Describe
+ */
+
+struct testcase_t endian_tests[] = {
+       { "bswap", test_bswap },
+       { "encdec", test_encdec },
+       END_OF_TESTCASES
+};
+