From: Marko Kreen Date: Wed, 26 Jan 2011 12:51:12 +0000 (+0200) Subject: test: tests X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=89a80ecc2b00b5a92bdbed44f90a8f61a6ab7e97;p=libusual.git test: tests --- diff --git a/test/Makefile b/test/Makefile index 4a9d946..b228142 100644 --- a/test/Makefile +++ b/test/Makefile @@ -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 = diff --git a/test/test_common.c b/test/test_common.c index 7767461..cd6b3c5 100644 --- a/test/test_common.c +++ b/test/test_common.c @@ -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 }, diff --git a/test/test_common.h b/test/test_common.h index 16ece15..109c41e 100644 --- a/test/test_common.h +++ b/test/test_common.h @@ -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 index 0000000..3c341d0 --- /dev/null +++ b/test/test_endian.c @@ -0,0 +1,120 @@ +#include + +#include "test_common.h" + +#include +#include + +/* + * 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 +}; +