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