summaryrefslogtreecommitdiffstats
path: root/tests/bits/bitrev_test.c
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2011-09-01 14:47:31 +0200
committerHarald Welte <laforge@gnumonks.org>2011-09-01 14:47:31 +0200
commit712691d8d8abbeb97f21df8c1d923d8694996ee7 (patch)
tree9114a0b62186b35333dbb17b067d068bba8e02b4 /tests/bits/bitrev_test.c
parent36c5a3e2857b049e1e979beb7d476426ae475831 (diff)
add functions for bit-reversal
Sometimes we need stuff like reversing every bit in each byte (but not the byte-order).
Diffstat (limited to 'tests/bits/bitrev_test.c')
-rw-r--r--tests/bits/bitrev_test.c36
1 files changed, 36 insertions, 0 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/bits.h>
+
+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;
+}