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 */
19 #define EXERCISE exercise_8
23 #define EXERCISE exercise_ccsds
26 #define EXERCISE exercise_int
29 #define EXERCISE exercise_char
33 #define PRINTPARM printf("(255,223):");
35 #define PRINTPARM printf("CCSDS (255,223):");
37 #define PRINTPARM printf("(%d,%d):",rs->nn,rs->nn-rs->nroots);
40 /* Exercise the RS codec passed as an argument */
42 #if !defined(CCSDS) && !defined(FIXED)
46 #if !defined(CCSDS) && !defined(FIXED)
47 struct rs *rs = (struct rs *)p;
49 DTYPE block[NN],tblock[NN];
57 int decoder_errors = 0;
60 /* Test up to the error correction capacity of the code */
61 for(errors=0;errors <= NROOTS/2;errors++){
63 /* Load block with random data and encode */
64 for(i=0;i<NN-NROOTS;i++)
65 block[i] = random() & NN;
67 #if defined(CCSDS) || defined(FIXED)
68 ENCODE_RS(&block[0],&block[NN-NROOTS]);
70 ENCODE_RS(rs,&block[0],&block[NN-NROOTS]);
73 /* Make temp copy, seed with errors */
74 memcpy(tblock,block,sizeof(tblock));
75 memset(errlocs,0,sizeof(errlocs));
76 memset(derrlocs,0,sizeof(derrlocs));
78 for(i=0;i<errors;i++){
80 errval = random() & NN;
81 } while(errval == 0); /* Error value must be nonzero */
84 errloc = random() % NN;
85 } while(errlocs[errloc] != 0); /* Must not choose the same location twice */
90 if(random() & 1) /* 50-50 chance */
91 derrlocs[erasures++] = errloc;
93 tblock[errloc] ^= errval;
96 /* Decode the errored block */
97 #if defined(CCSDS) || defined(FIXED)
98 derrors = DECODE_RS(tblock,derrlocs,erasures);
100 derrors = DECODE_RS(rs,tblock,derrlocs,erasures);
103 if(derrors != errors){
105 printf(" decoder says %d errors, true number is %d\n",derrors,errors);
108 for(i=0;i<derrors;i++){
109 if(errlocs[derrlocs[i]] == 0){
111 printf(" decoder indicates error in location %d without error\n",i);
115 if(memcmp(tblock,block,sizeof(tblock)) != 0){
117 printf(" uncorrected errors! output ^ input:");
120 printf(" %02x",tblock[i] ^ block[i]);
125 return decoder_errors;