From 712691d8d8abbeb97f21df8c1d923d8694996ee7 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Thu, 1 Sep 2011 14:47:31 +0200 Subject: add functions for bit-reversal Sometimes we need stuff like reversing every bit in each byte (but not the byte-order). --- tests/Makefile.am | 2 +- tests/bits/Makefile.am | 6 ++++++ tests/bits/bitrev_test.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/bits/Makefile.am create mode 100644 tests/bits/bitrev_test.c (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index 2b4ac6e6..6c3cb337 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,5 +1,5 @@ if ENABLE_TESTS -SUBDIRS = timer sms ussd smscb +SUBDIRS = timer sms ussd smscb bits if ENABLE_MSGFILE SUBDIRS += msgfile endif diff --git a/tests/bits/Makefile.am b/tests/bits/Makefile.am new file mode 100644 index 00000000..dd03e83e --- /dev/null +++ b/tests/bits/Makefile.am @@ -0,0 +1,6 @@ +INCLUDES = $(all_includes) -I$(top_srcdir)/include +noinst_PROGRAMS = bitrev_test + +bitrev_test_SOURCES = bitrev_test.c +bitrev_test_LDADD = $(top_builddir)/src/libosmocore.la + diff --git a/tests/bits/bitrev_test.c b/tests/bits/bitrev_test.c new file mode 100644 index 00000000..5eca990a --- /dev/null +++ b/tests/bits/bitrev_test.c @@ -0,0 +1,36 @@ + +#include +#include +#include +#include + +#include +#include + +static const uint8_t input[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; +static const uint8_t exp_out[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; + +int main(int argc, char **argv) +{ + uint8_t out[ARRAY_SIZE(input)]; + unsigned int offs; + + for (offs = 0; offs < sizeof(out); offs++) { + uint8_t *start = out + offs; + uint8_t len = sizeof(out) - offs; + + memcpy(out, input, sizeof(out)); + + printf("INORDER: %s\n", osmo_hexdump(start, len)); + osmo_revbytebits_buf(start, len); + printf("REVERSED: %s\n", osmo_hexdump(start, len)); + if (memcmp(start, exp_out + offs, len)) { + printf("EXPECTED: %s\n", osmo_hexdump(exp_out+offs, len)); + fprintf(stderr, "REVERSED != EXPECTED!\n"); + exit(1); + } + printf("\n"); + } + + return 0; +} -- cgit v1.2.3