1 /* Exercise an RS codec a specified number of times using random
2 * data and error patterns
4 * Copyright 2002 Phil Karn, KA9Q
5 * May be used under the terms of the GNU General Public License (GPL)
7 #define FLAG_ERASURE 1 /* Randomly flag 50% of errors as erasures */
20 #define EXERCISE exercise_8
24 #define EXERCISE exercise_ccsds
27 #define EXERCISE exercise_int
30 #define EXERCISE exercise_char
34 #define PRINTPARM printf("(255,223):");
36 #define PRINTPARM printf("CCSDS (255,223):");
38 #define PRINTPARM printf("(%d,%d):",rs->nn,rs->nn-rs->nroots);
41 /* Exercise the RS codec passed as an argument */
43 #if !defined(CCSDS) && !defined(FIXED)
47 #if !defined(CCSDS) && !defined(FIXED)
48 struct rs *rs = (struct rs *)p;
50 DTYPE block[NN],tblock[NN];
58 int decoder_errors = 0;
61 /* Test up to the error correction capacity of the code */
62 for(errors=0;errors <= NROOTS/2;errors++){
64 /* Load block with random data and encode */
65 for(i=0;i<NN-NROOTS;i++)
66 block[i] = random() & NN;
68 #if defined(CCSDS) || defined(FIXED)
69 ENCODE_RS(&block[0],&block[NN-NROOTS]);
71 ENCODE_RS(rs,&block[0],&block[NN-NROOTS]);
74 /* Make temp copy, seed with errors */
75 memcpy(tblock,block,sizeof(tblock));
76 memset(errlocs,0,sizeof(errlocs));
77 memset(derrlocs,0,sizeof(derrlocs));
79 for(i=0;i<errors;i++){
81 errval = random() & NN;
82 } while(errval == 0); /* Error value must be nonzero */
85 errloc = random() % NN;
86 } while(errlocs[errloc] != 0); /* Must not choose the same location twice */
91 if(random() & 1) /* 50-50 chance */
92 derrlocs[erasures++] = errloc;
94 tblock[errloc] ^= errval;
97 /* Decode the errored block */
98 #if defined(CCSDS) || defined(FIXED)
99 derrors = DECODE_RS(tblock,derrlocs,erasures);
101 derrors = DECODE_RS(rs,tblock,derrlocs,erasures);
104 if(derrors != errors){
106 printf(" decoder says %d errors, true number is %d\n",derrors,errors);
109 for(i=0;i<derrors;i++){
110 if(errlocs[derrlocs[i]] == 0){
112 printf(" decoder indicates error in location %d without error\n",i);
116 if(memcmp(tblock,block,sizeof(tblock)) != 0){
118 printf(" uncorrected errors! output ^ input:");
121 printf(" %02x",tblock[i] ^ block[i]);
126 return decoder_errors;