e76a2f1f50cee44bc9511948aa7c74a0e65329cd
[debian/gnuradio] / gr-atsc / src / lib / qa_atsci_data_interleaver.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include <cppunit/TestAssert.h>
24 #include <qa_atsci_data_interleaver.h>
25 #include <string.h>
26
27
28 /*!
29  * write an easy to identify pattern into the packet
30  */
31 void
32 qa_atsci_data_interleaver::init_test_packet (int counter,
33                                             atsc_mpeg_packet_rs_encoded &out)
34 {
35   out.data[0] = 0xf0;
36   out.data[1] = 0xff;
37   out.data[2] = (counter >> 8) & 0xff;
38   out.data[3] = counter & 0xff;
39
40   for (int i = 4; i < 205; i++)
41     out.data[i] = i;
42
43   out.data[205] = 0xa0;
44   out.data[206] = 0xaf;
45 }
46
47 void
48 qa_atsci_data_interleaver::print_packet (FILE *fp,
49                                         int frame_number,
50                                         int field_number,
51                                         int segment_number,
52                                         const atsc_mpeg_packet_rs_encoded &in)
53 {
54   fprintf (fp, "%04d:%d:%03d  ", frame_number, field_number, segment_number);
55
56   const unsigned char *p = &in.data[0];
57   for (int i = 0; i < 8; i++)
58     fprintf (fp, "%02X", p[i]);
59
60   fprintf (fp, "  ");
61   p = &in.data[8];
62   for (int i = 0; i < 8; i++)
63     fprintf (fp, "%02X", p[i]);
64
65   
66   fprintf (fp, "  ...  ");
67   p = &in.data[191];
68   for (int i = 0; i < 8; i++)
69     fprintf (fp, "%02X", p[i]);
70
71   fprintf (fp, "  ");
72   p = &in.data[199];
73   for (int i = 0; i < 8; i++)
74     fprintf (fp, "%02X", p[i]);
75
76   fprintf (fp, "\n");
77 }
78
79 void
80 qa_atsci_data_interleaver::chk_assert (const atsc_mpeg_packet_rs_encoded &expected,
81                                       const atsc_mpeg_packet_rs_encoded &actual)
82 {
83   if (expected == actual)
84     return;
85
86   FILE *fp = stderr;
87
88   fprintf (fp, "Expected: ");
89   print_packet (fp, 0, 0, 0, expected);
90
91   fprintf (fp, "Actual:   ");
92   print_packet (fp, 0, 0, 0, actual);
93
94   CPPUNIT_ASSERT (expected == actual);
95 }
96
97 void
98 qa_atsci_data_interleaver::t0 ()
99 {
100   int   counter = 0;
101   atsc_mpeg_packet_rs_encoded   in, enc, out;
102   atsc_mpeg_packet_rs_encoded   zero;
103
104   memset (&zero, 0, sizeof (zero));
105   
106   for (int frame = 0; frame < 4; frame++){
107     for (int field = 0; field < 2; field++){
108       for (int segment = 0; segment < 312; segment++, counter++){
109
110         // build test packet
111         init_test_packet (counter, in);
112         in.pli.set_regular_seg (field == 1, segment);
113
114         // interleave it
115         outbound.interleave (enc, in);
116
117         // deinterleave it
118         inbound.deinterleave (out, enc);
119
120         if (counter < 52)
121           // CPPUNIT_ASSERT (zero == out);
122           chk_assert (zero, out);
123
124         else if (counter >= 52){
125           atsc_mpeg_packet_rs_encoded expected;
126           init_test_packet (counter - 52, expected);
127           // CPPUNIT_ASSERT (expected == out);
128           chk_assert (expected, out);
129         }
130       }
131     }
132   }
133 }
134
135 //
136 // Note, this assumes that the interleaver and deinterleaver
137 // have the state left by t0.
138 //
139 // This test pushes crap into the interleaver, and then confirms that
140 // the deinterleaver recovers.  This would be part of what you'd see
141 // on a channel change.
142 //
143
144 void
145 qa_atsci_data_interleaver::t1 ()
146 {
147   int   counter = 0;
148   atsc_mpeg_packet_rs_encoded   in, enc, out;
149   atsc_mpeg_packet_rs_encoded   zero;
150
151   memset (&zero, 0, sizeof (zero));
152   
153   static const int NCRAP = 13;
154   
155   // push NCRAP segments of crap into the interleaver,
156   // but don't change the deinterleaver state
157
158   for (int i = 0; i < NCRAP; i++){
159     init_test_packet (i + 2758, in);
160     in.pli.set_regular_seg (false, i + 5);
161
162     outbound.interleave (enc, in);
163   }
164
165   // now run the normal test.
166   // we should get good segments after NCRAP + 52
167   
168   int starting_counter = 3141;
169   counter = starting_counter;
170
171   for (int frame = 0; frame < 4; frame++){
172     for (int field = 0; field < 2; field++){
173       for (int segment = 0; segment < 312; segment++, counter++){
174
175         // build test packet
176         init_test_packet (counter, in);
177         in.pli.set_regular_seg (field == 1, segment);
178
179         // interleave it
180         outbound.interleave (enc, in);
181
182         // deinterleave it
183         inbound.deinterleave (out, enc);
184
185         if (counter < starting_counter + 52 + NCRAP){
186           // don't care...
187         }
188         else if (counter >= starting_counter + 52 + NCRAP){
189           atsc_mpeg_packet_rs_encoded expected;
190           init_test_packet (counter - 52, expected);
191           // CPPUNIT_ASSERT (expected == out);
192           chk_assert (expected, out);
193         }
194       }
195     }
196   }
197 }