Commits

Martin Scharrer committed 0410789

Added tests.

  • Participants
  • Parent commits dad668a

Comments (0)

Files changed (2)

+
+CFLAGS=-O3 -Wall
 
 test: crc
 	./crc
 
 all: crc
 
+crc: | Makefile
 #include <string.h>
 #include <stdint.h>
 
-typedef uint16_t uint16;
-typedef uint8_t  uint8;
-typedef int16_t  int16;
-typedef int16_t  sint16;
-typedef int8_t   int8;
-typedef int8_t   sint8;
-
 #define CRC16_INIT      0xFFFF
 #define CRC16_POLY      0x1021
 #define CRC16_POLYREV   0x8408
     return crc;
 }
 
+typedef struct
+{
+    uint8_t* pdata;
+    //size_t   length;
+    uint16_t correct_crc;
+} testvector_t;
+
+#define B (uint8_t*)
+#define NUM_TESTVECTORS 3
+testvector_t testvectors [NUM_TESTVECTORS] = 
+{
+    {B"123456789", 0x29B1},
+    {B"test", 0x1FC6},
+    {B"This is a CRC test", 0x9973},
+    //{NULL, 0x0000},
+};
+
 int main(int argc, char* argv[])
 {
-    uint8_t  stdtext[10] = "123456789";
+    //uint8_t  stdtext[10] = "123456789";
     uint8_t* buffer;
     size_t   length;
     uint16_t crc;
+    int nerrors = 0;
+    int i;
 
-    buffer = (argc < 2) ? stdtext : (uint8_t*)argv[1];
-    length = strlen((char*)buffer);
+    if (argc > 1)
+    {
+        buffer = (uint8_t*)argv[1];
+        length = strlen(argv[1]);
+        crc = crc16(buffer, length);
+        printf("CRC: %s => %04X\n", buffer, crc);
+        exit (0);
+    }
 
-    crc = crc16(buffer, length);
+    for (i=0; i<NUM_TESTVECTORS; i++)
+    {
+        buffer = testvectors[i].pdata;
+        length = strlen((char*)buffer);
+        crc = crc16(buffer, length);
+        if (crc == testvectors[i].correct_crc)
+        {
+            printf("Correct CRC: '%s' => %04X\n", buffer, crc);
+        }
+        else
+        {
+            printf("Incorrect CRC: '%s' => %04X, correct: %04X\n", buffer, crc, testvectors[i].correct_crc);
+            nerrors++;
+        }
+    }
+    exit (nerrors ? 1 : 0);
 
-    printf("CRC: %04X\n", crc);
     return 0;
 }