From e89bda0c5b1577ea1cdeb619a4af390ae4d423cc Mon Sep 17 00:00:00 2001
From: Christian Vogel <vogelchr@vogel.cx>
Date: Sun, 23 Jan 2011 13:19:38 +0100
Subject: [PATCH 2/2] Test for osmo_crc16 routines (based on Harald's crc_test in Tetra).

---
 configure.in          |    1 +
 tests/Makefile.am     |    2 +-
 tests/crc/Makefile.am |    6 +++
 tests/crc/crc_test.c  |   90 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 98 insertions(+), 1 deletions(-)
 create mode 100644 tests/crc/Makefile.am
 create mode 100644 tests/crc/crc_test.c

diff --git a/configure.in b/configure.in
index 309aa03..937f2f6 100644
--- a/configure.in
+++ b/configure.in
@@ -115,6 +115,7 @@ AC_OUTPUT(
 	tests/timer/Makefile
 	tests/sms/Makefile
 	tests/msgfile/Makefile
+	tests/crc/Makefile
 	tests/ussd/Makefile
 	tests/smscb/Makefile
 	Makefile)
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 2b4ac6e..222a112 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 crc
 if ENABLE_MSGFILE
 SUBDIRS += msgfile
 endif
diff --git a/tests/crc/Makefile.am b/tests/crc/Makefile.am
new file mode 100644
index 0000000..2b5bd44
--- /dev/null
+++ b/tests/crc/Makefile.am
@@ -0,0 +1,6 @@
+INCLUDES = $(all_includes) -I$(top_srcdir)/include
+
+noinst_PROGRAMS = crc_test
+
+crc_test_SOURCES = crc_test.c
+crc_test_LDADD = $(top_builddir)/src/libosmocore.la
diff --git a/tests/crc/crc_test.c b/tests/crc/crc_test.c
new file mode 100644
index 0000000..0ee1231
--- /dev/null
+++ b/tests/crc/crc_test.c
@@ -0,0 +1,90 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <osmocore/osmo_crc16.h>
+
+#define POLY 0x1021
+uint16_t crc_itut_table[256];
+
+int main(int argc,char **argv){
+	uint8_t input1[] = { 0x01 };
+	pbit_t packedbits[10]; /* 80 bits should be enough for everybody */
+        uint16_t crc;
+	int i;
+
+	osmo_crc16_table_init(crc_itut_table,POLY);
+	printf("First elements of table for CRC with poly 0x%04x:\n",POLY);
+	for(i=0;i<16;i++)
+		printf(" %04x",crc_itut_table[i]);
+	printf("\n");
+
+	crc = osmo_crc16(crc_itut_table,0x0,(pbit_t*)input1,8);
+        // crc = crc16_itut_bytes(0x0, input1, 8);
+        printf("The CRC is now: %u/0x%x\n", crc, crc);
+
+	crc = osmo_crc16_ubits(crc_itut_table,0x0,(ubit_t *)input1,1);
+        // crc = crc16_itut_bits(0x0, input1, 1);
+        printf("The CRC is now: %d/0x%x\n", crc, crc);
+
+	crc = osmo_crc16(crc_itut_table,0xffff,(pbit_t*)input1,8);
+        // crc = crc16_itut_bytes(0xFFFF, input1, 8);
+        printf("The CRC is now: %u/0x%x\n", crc, crc);
+
+	crc = osmo_crc16_ubits(crc_itut_table,0xffff,(ubit_t*)input1,1);
+        // crc = crc16_itut_bits(0xFFFF, input1, 1);
+        printf("The CRC is now: %d/0x%x\n", crc, crc);
+
+        /* actual CRC-CCIT usage */
+        ubit_t input2[] = {
+		0, 0, 0, 1, 0, 0, 0, 0, /* 0x10 */
+		1, 0, 1, 1, 0, 0, 0, 0, /* 0xb0 */
+		1, 0, 1, 1, 1, 1, 1, 0, /* 0xbe */
+		0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
+		1, 0, 0, 0, 0, 0, 1, 1, /* 0x83 */
+		0, 0, 0, 0, 0, 1, 1, 1, /* 0x07 */
+		1, 1, 0, 1, 0, 0, 1, 1, /* 0xd3 */
+		0, 0, 1, 1,             /* 0x3_       | 60 */
+		/* checksum */
+		1, 1, 0, 1, 1, 1, 1, 0, /* 0x_d 0xe_  | 16  (..76) */
+		1, 1, 1, 1, 0, 0, 0, 1, /* 0x_f 0x1_
+		/* tail bits */
+		0, 0, 0, 0 };            /*  4  (..80) */
+
+	osmo_ubit2pbit(packedbits,input2,80);
+	printf("packed bits:");
+	for(i=0;i<10;i++)
+		printf(" %02x",packedbits[i]);
+	printf("\n");
+
+	crc = ~osmo_crc16_ubits(crc_itut_table,0xffff,(ubit_t*)input2,60);
+        // crc = ~crc16_itut_bits(0xffff, input2, 60);
+        printf("The CRC is now: %d/0x%x\n", crc, crc);
+
+
+	crc = ~osmo_crc16(crc_itut_table,0xffff,packedbits,60);
+	printf("    ...packed: %d/0x%x\n",crc,crc);
+
+        /* swap the bytes */
+        crc = (crc << 8) | (crc >> 8);
+        osmo_pbit2ubit(&input2[60], (uint8_t *)&crc, 16);
+
+	/* update packed bits (again) */
+	osmo_ubit2pbit(packedbits,input2,80);
+	printf("packed bits:");
+	for(i=0;i<10;i++)
+		printf(" %02x",packedbits[i]);
+	printf("\n");
+
+
+	crc = osmo_crc16(crc_itut_table,0xffff,packedbits,60+16);
+        // crc = crc16_itut_bits(0xFFFF, input2, 60 + 16);
+        printf("The CRC is now: %d/0x%x\n", crc, crc);
+        if (crc == 0x1d0f)
+                printf("Decoded successfully.\n");
+        else
+                printf("Failed to decode.\n");
+
+        return 0;
+
+}
-- 
1.6.3.3

