Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / qa_gri_lfsr.cc
1 /*
2  * Copyright 2008 Free Software Foundation, Inc.
3  * 
4  * This file is part of GNU Radio
5  * 
6  * GNU Radio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3, or (at your option)
9  * any later version.
10  * 
11  * GNU Radio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with GNU Radio; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include <gri_lfsr.h>
23 #include <qa_gri_lfsr.h>
24 #include <cppunit/TestAssert.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 void
29 qa_gri_lfsr::test_lfsr ()
30 {
31   int mask = 0x19;
32   int seed = 0x01;
33   int length = 5;
34
35   gri_lfsr lfsr1(mask,seed,length);
36   gri_lfsr lfsr2(mask,seed,length);
37   
38   unsigned char expected[] = {1, 0, 1, 1, 0, 1, 0, 1, 0, 0};
39
40   for(unsigned int i=0; i<31; i++){
41     lfsr1.next_bit();
42   }
43
44   // test that after one lfsr cycle we still match out uncycled lfsr
45   for (unsigned int i = 0; i < 41; i++) {
46     CPPUNIT_ASSERT_EQUAL((int) lfsr1.next_bit(), (int) lfsr2.next_bit());
47   }
48
49   // test the known correct values at the given shift offset
50   for(unsigned int i=0; i<10; i++){
51     CPPUNIT_ASSERT_EQUAL((int) lfsr1.next_bit(), (int) expected[i]);
52   }
53
54   // test for register length too long
55   CPPUNIT_ASSERT_THROW(gri_lfsr(mask, seed, 32), std::invalid_argument);
56 }
57
58 void
59 qa_gri_lfsr::test_scrambler()
60 {
61   // CCSDS 7-bit scrambler
62   int mask = 0x8A;
63   int seed = 0x7F;
64   int length = 7;
65
66   gri_lfsr scrambler(mask, seed, length);
67
68   // Impulse (1 and 126 more zeroes)
69   unsigned char src[] = 
70     { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
71       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
72       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
73       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
74       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
75       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
76       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
77       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
78       0, 0, 0, 0, 0, 0, 0 }; // flush bits
79   
80   // Impulse response (including leading bits)
81   unsigned char expected[] =
82     { 1, 1, 1, 1, 1, 1, 1, 
83       0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 
84       0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 
85       0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 
86       0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 
87       1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 
88       0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 
89       1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 
90       1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, };
91
92   int len = sizeof(src);
93   unsigned char actual[len];
94
95   for (int i = 0; i < len; i++)
96     actual[i] = scrambler.next_bit_scramble(src[i]);
97
98   CPPUNIT_ASSERT(memcmp(expected, actual, len) == 0);
99 }
100
101 void
102 qa_gri_lfsr::test_descrambler()
103 {
104   // CCSDS 7-bit scrambler
105   int mask = 0x8A;
106   int seed = 0x7F;
107   int length = 7;
108
109   gri_lfsr descrambler(mask, seed, length);
110
111   // Scrambled sequence (impulse response)
112   unsigned char src[] =
113     { 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 
114       0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 
115       0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 
116       0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 
117       1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 
118       0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 
119       1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 
120       1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0 }; 
121
122   // Original (garbage while synchronizing, them impulse)
123   unsigned char expected[] = 
124     { 0, 1, 0, 0, 1, 0,
125       1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
126       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
127       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
128       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
129       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
130       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
131       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
132       0, 0, 0, 0, 0, 0, 0, 0, 0 };
133   
134   int len = sizeof(src);
135   unsigned char actual[len];
136
137   for (int i = 0; i < len; i++)
138     actual[i] = descrambler.next_bit_descramble(src[i]);
139
140   CPPUNIT_ASSERT(memcmp(expected, actual, len) == 0);
141 }