Fibonacci Linear Feedback Shift Register
[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
27 void
28 qa_gri_lfsr::test_lfsr ()
29 {
30   int mask = 0x19;
31   int seed = 0x01;
32   int length = 5;
33
34   gri_lfsr lfsr1(mask,seed,length);
35   gri_lfsr lfsr2(mask,seed,length);
36   
37   unsigned char expected[] = {1, 0, 1, 1, 0, 1, 0, 1, 0, 0};
38
39   for(unsigned int i=0; i<31; i++){
40     lfsr1.next_bit();
41   }
42
43   // test that after one lfsr cycle we still match out uncycled lfsr
44   for (unsigned int i = 0; i < 41; i++) {
45     CPPUNIT_ASSERT_EQUAL((int) lfsr1.next_bit(), (int) lfsr2.next_bit());
46   }
47
48   // test the known correct values at the given shift offset
49   for(unsigned int i=0; i<10; i++){
50     CPPUNIT_ASSERT_EQUAL((int) lfsr1.next_bit(), (int) expected[i]);
51   }
52
53   // test for register length too long
54   CPPUNIT_ASSERT_THROW(gri_lfsr(mask, seed, 32), std::invalid_argument);
55 }
56