Switch from GPLv2 to GPLv2+
[fw/altos] / src / test / ao_fec_test.c
index 671fcafcba4d27dc4e7be8fcaf9257a25e1bc50a..cbced6ae31435fb870ee8c599c02d88f9d564d5a 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -100,6 +101,7 @@ ao_fuzz (uint8_t *in, int in_len, uint8_t *out, double dev)
 {
        int     i;
        int     errors = 0;
+       int     s;
        
        for (i = 0; i < in_len; i++) {
                double  error = gaussian_random(0, dev);
@@ -115,6 +117,24 @@ ao_fuzz (uint8_t *in, int in_len, uint8_t *out, double dev)
                        else
                                byte -= error;
                }
+
+               /* abcd efgh    8
+                * abcd efga    7
+                * abcd efab    6
+                * abcd eabc    5
+                * abcd abcd    4
+                * abca bcab    3
+                * abab abab    2
+                * aaaa aaaa    1
+                */
+
+#define SAVE           8
+#define SAVE_MASK      (((1 << SAVE) - 1) << (8 - SAVE))
+
+               byte &= SAVE_MASK;
+               for (s = SAVE; s < 8; s += SAVE)
+                       byte |= byte >> s;
+
                out[i] = byte;
        }
        return errors;
@@ -283,6 +303,11 @@ ao_real_packet(void)
        return ok;
 }
 
+#define EXPECT_DECODE_FAIL     0
+#define EXPECT_CRC_MISMATCH    6386
+#define EXPECT_DATA_MISMATCH   0
+#define NOISE_AMOUNT           0x50
+
 int
 main(int argc, char **argv)
 {
@@ -298,13 +323,15 @@ main(int argc, char **argv)
        int             transmit_len;
 
        uint8_t         receive[EXPAND_LEN(sizeof(original))];
-       int             receive_len, receive_errors;
+       int             receive_len;
 
        uint8_t         decode[DECODE_LEN(sizeof(original))];
        int             decode_ok;
 
        int             errors = 0;
-       int             error;
+       int             decode_fail = 0;
+       int             crc_mismatch = 0;
+       int             data_mismatch = 0;
 
        if (!ao_real_packet())
                errors++;
@@ -322,37 +349,38 @@ main(int argc, char **argv)
                transmit_len = ao_expand(encode, encode_len, transmit);
 
                /* Add gaussian noise to the signal */
-               receive_errors = ao_fuzz(transmit, transmit_len, receive, 0x38);
+               (void) ao_fuzz(transmit, transmit_len, receive, NOISE_AMOUNT);
                receive_len = transmit_len;
                
                /* Decode it */
                decode_ok = ao_fec_decode(receive, receive_len, decode, original_len + 2, NULL);
 
                /* Check to see if we received the right data */
-               error = 0;
 
-               if (!decode_ok) {
-                       printf ("decode failed\n");
-                       error++;
-               }
+               if (!decode_ok)
+                       decode_fail++;
+               else if (decode[original_len +1] != AO_FEC_DECODE_CRC_OK)
+                       crc_mismatch++;
+               else if (memcmp(original, decode, original_len) != 0)
+                       data_mismatch++;
+       }
 
-               if (decode[original_len +1] != AO_FEC_DECODE_CRC_OK) {
-                       printf ("crc mis-match\n");
-                       error++;
-               }
 
-               if (memcmp(original, decode, original_len) != 0) {
-                       printf ("data mis-match\n");
-                       error++;
-               }
-               if (error) {
-                       printf ("Errors: %d\n", receive_errors);
-                       ao_fec_dump_bytes(original, original_len, "Input");
-                       ao_fec_dump_bytes(decode, original_len, "Decode");
-                       errors += error;
-               }
-       }
        printf ("%d packets coded\n", trial);
+       printf ("decode_fail %d crc_mismatch %d data_mismatch %d\n",
+               decode_fail, crc_mismatch, data_mismatch);
+       if (decode_fail != EXPECT_DECODE_FAIL) {
+               printf ("expected %d decode failures\n", EXPECT_DECODE_FAIL);
+               errors++;
+       }
+       if (crc_mismatch != EXPECT_CRC_MISMATCH) {
+               printf ("expected %d crc mismatch\n", EXPECT_CRC_MISMATCH);
+               errors++;
+       }
+       if (data_mismatch != EXPECT_DATA_MISMATCH) {
+               printf ("expected %d data mismatch\n", EXPECT_DATA_MISMATCH);
+               errors++;
+       }
        return errors;
 }