Merge branch 'dfsg-orig'
[debian/gnuradio] / gr-atsc / src / lib / qa_atsci_randomizer.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 <qa_atsci_randomizer.h>
24
25 #include <cppunit/TestAssert.h>
26 #include <string.h>
27
28 static unsigned int expected_initial_states[] = {
29   0x018f,
30   0xd3db,
31   0xbaf1,
32   0x8e64,
33   0x4732,
34   0x2399,
35   0xc2d0,
36   0x6168
37 };
38
39 static unsigned char expected_initial_values[] = {
40   0xc0,
41   0x6d,
42   0x3f,
43   0x99,
44   0x38,
45   0x6a,
46   0x29,
47   0x52
48 };
49
50 #define NELEMENTS(ary) ((sizeof (ary)) / sizeof (ary[0]))
51
52 void
53 qa_atsci_randomizer::t0_compare_output_maps ()
54 {
55   for (int i = 0; i < 0x10000; i++){
56     unsigned char slow = atsci_randomizer::slow_output_map(i);
57     unsigned char fast = atsci_randomizer::fast_output_map(i);
58     CPPUNIT_ASSERT_EQUAL (slow, fast);
59   }
60 }
61
62 void
63 qa_atsci_randomizer::t1_initial_states ()
64 {
65   // LFSR should start with expected states
66
67   for (unsigned int i = 0; i < NELEMENTS (expected_initial_values); i++){
68     unsigned int got = randomizer.state();
69     randomizer.clk ();
70     CPPUNIT_ASSERT_EQUAL (expected_initial_states[i], got);
71   }
72 }
73
74 void
75 qa_atsci_randomizer::t2_initial_values ()
76 {
77   // LFSR should start with expected values
78
79   for (unsigned int i = 0; i < NELEMENTS (expected_initial_values); i++){
80     unsigned char got = randomizer.output_and_clk ();
81     CPPUNIT_ASSERT_EQUAL (expected_initial_values[i], got);
82   }
83 }
84
85 void
86 qa_atsci_randomizer::t3_reset ()
87 {
88   // LFSR should start with expected values
89
90   for (unsigned int i = 0; i < NELEMENTS (expected_initial_values); i++){
91     unsigned char got = randomizer.output_and_clk ();
92     CPPUNIT_ASSERT_EQUAL (expected_initial_values[i], got);
93   }
94
95   randomizer.reset ();  // reset LFSR
96   
97   // and now should repeat expected values
98
99   for (unsigned int i = 0; i < NELEMENTS (expected_initial_values); i++){
100     unsigned char got = randomizer.output_and_clk ();
101     CPPUNIT_ASSERT_EQUAL (expected_initial_values[i], got);
102   }
103 }
104
105 void
106 qa_atsci_randomizer::t4_high_level ()
107 {
108   static const int      N = 5;
109
110   atsc_mpeg_packet              in[N];
111   atsc_mpeg_packet_no_sync      middle[N];
112   atsc_mpeg_packet              out[N];
113   
114   memset (in, 0, sizeof (in));
115   memset (middle, 0, sizeof (middle));
116   memset (out, 0, sizeof (out));
117
118   // setup input test data
119
120   int   counter = 0xff;
121   for (int n = 0; n < N; n++){
122     in[n].data[0] = MPEG_SYNC_BYTE;
123     for (int i = 0; i < ATSC_MPEG_DATA_LENGTH; i++)
124       in[n].data[i + 1] = counter++;
125   }
126
127   // randomize N packets
128
129   for (int n = 0; n < N; n++)
130     randomizer.randomize (middle[n], in[n]);
131
132   // reset LFSR and derandomize N packets
133
134   randomizer.reset ();  // reset LFSR
135
136   for (int n = 0; n < N; n++)
137     randomizer.derandomize (out[n], middle[n]);
138
139   // compare packets
140
141   for (int n = 0; n < N; n++){
142     CPPUNIT_ASSERT (in[n] == out[n]);
143   }
144 }
145