Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / qa_diff_encoder.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2006,2007 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 from gnuradio import gr, gr_unittest
24 import math
25 import random
26
27 def make_random_int_tuple(L, min, max):
28     result = []
29     for x in range(L):
30         result.append(random.randint(min, max))
31     return tuple(result)
32
33     
34 class test_encoder (gr_unittest.TestCase):
35
36     def setUp (self):
37         self.tb = gr.top_block ()
38
39     def tearDown (self):
40         self.tb = None
41
42     def test_diff_encdec_000(self):
43         random.seed(0)
44         modulus = 2
45         src_data = make_random_int_tuple(1000, 0, modulus-1)
46         expected_result = src_data
47         src = gr.vector_source_b(src_data)
48         enc = gr.diff_encoder_bb(modulus)
49         dec = gr.diff_decoder_bb(modulus)
50         dst = gr.vector_sink_b()
51         self.tb.connect(src, enc, dec, dst)
52         self.tb.run()               # run the graph and wait for it to finish
53         actual_result = dst.data()  # fetch the contents of the sink
54         self.assertEqual(expected_result, actual_result)
55
56     def test_diff_encdec_001(self):
57         random.seed(0)
58         modulus = 4
59         src_data = make_random_int_tuple(1000, 0, modulus-1)
60         expected_result = src_data
61         src = gr.vector_source_b(src_data)
62         enc = gr.diff_encoder_bb(modulus)
63         dec = gr.diff_decoder_bb(modulus)
64         dst = gr.vector_sink_b()
65         self.tb.connect(src, enc, dec, dst)
66         self.tb.run()               # run the graph and wait for it to finish
67         actual_result = dst.data()  # fetch the contents of the sink
68         self.assertEqual(expected_result, actual_result)
69
70     def test_diff_encdec_002(self):
71         random.seed(0)
72         modulus = 8
73         src_data = make_random_int_tuple(40000, 0, modulus-1)
74         expected_result = src_data
75         src = gr.vector_source_b(src_data)
76         enc = gr.diff_encoder_bb(modulus)
77         dec = gr.diff_decoder_bb(modulus)
78         dst = gr.vector_sink_b()
79         self.tb.connect(src, enc, dec, dst)
80         self.tb.run()               # run the graph and wait for it to finish
81         actual_result = dst.data()  # fetch the contents of the sink
82         self.assertEqual(expected_result, actual_result)
83
84 if __name__ == '__main__':
85     gr_unittest.main ()
86