@@ -41,6 +41,87 @@ void decode(DSDcc::Golay_20_8& Golay_20_8, unsigned char *codeword)
4141 }
4242}
4343
44+ void rand_test ()
45+ {
46+ unsigned char msg[8 ];
47+ unsigned char codeword[20 ], xcodeword[20 ];
48+ int idx1, idx2, idx3;
49+ int dataIn, dataOut;
50+ int passCount = 0 , failCount = 0 , parityFailCount = 0 ;
51+ DSDcc::Golay_20_8 golay_20_8;
52+
53+ // Run multiple times, to randomly corrupt different bits
54+ // Takes about 10 seconds on a fast PC
55+ for (int repeat = 0 ; repeat < 100000 ; repeat++)
56+ {
57+ // Exhaustively test all 8-bit inputs
58+ for (int dataIn = 0 ; dataIn < 256 ; dataIn++)
59+ {
60+ // Convert to array of bits
61+ for (int j = 0 ; j < 8 ; j++) {
62+ msg[j] = (dataIn >> j) & 1 ;
63+ }
64+
65+ // Encode
66+ golay_20_8.encode (msg, codeword);
67+
68+ // Save copy of uncorrupted codeword
69+ std::copy (codeword, codeword + 20 , xcodeword);
70+
71+ // Randomly corrupt up to 3 bits
72+ idx1 = rand () % 20 ;
73+ idx2 = rand () % 20 ;
74+ idx3 = rand () % 20 ;
75+ codeword[idx1] ^= codeword[idx1];
76+ codeword[idx2] ^= codeword[idx2];
77+ codeword[idx3] ^= codeword[idx3];
78+
79+ bool fail = false ;
80+ // Decode and correct errors
81+ dataOut = 0 ;
82+ if (golay_20_8.decode (codeword))
83+ {
84+ // Check data is corrected
85+ for (int j = 0 ; j < 8 ; j++) {
86+ dataOut |= codeword[j] << j;
87+ }
88+ if (dataIn != dataOut) {
89+ fail = true ;
90+ }
91+
92+ // Check also that parity has been corrected, as we previously had a bug with this
93+ if (memcmp (codeword, xcodeword, 20 )) {
94+ parityFailCount++;
95+ }
96+ }
97+ else
98+ {
99+ fail = true ;
100+ }
101+ if (fail)
102+ {
103+ std::cout << " Decode failed:"
104+ << " dataIn=" << dataIn
105+ << " dataOut=" << dataOut
106+ << " idx1=" << idx1
107+ << " idx2=" << idx2
108+ << " idx3=" << idx3
109+ << " \n " ;
110+ failCount++;
111+ }
112+ else
113+ {
114+ passCount++;
115+ }
116+ }
117+ }
118+
119+ std::cout << " rand_test:"
120+ << " Passcount=" << passCount
121+ << " Failcount=" << failCount
122+ << " parityFailCount=" << parityFailCount << " \n " ;
123+ }
124+
44125int main (int argc, char *argv[])
45126{
46127 unsigned char msg[8 ] = {1 , 0 , 0 , 1 , 0 , 1 , 0 , 0 };
@@ -106,6 +187,8 @@ int main(int argc, char *argv[])
106187 xcodeword[19 ] ^= 1 ;
107188 decode (golay_20_8, xcodeword);
108189
190+ rand_test ();
191+
109192 return 0 ;
110193}
111194
0 commit comments