Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / qa_add_and_friends.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004,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
25 class test_head (gr_unittest.TestCase):
26
27     def setUp (self):
28         self.tb = gr.top_block ()
29
30     def tearDown (self):
31         self.tb = None
32
33     def help_ii (self, src_data, exp_data, op):
34         for s in zip (range (len (src_data)), src_data):
35             src = gr.vector_source_i (s[1])
36             self.tb.connect (src, (op, s[0]))
37         dst = gr.vector_sink_i ()
38         self.tb.connect (op, dst)
39         self.tb.run ()
40         result_data = dst.data ()
41         self.assertEqual (exp_data, result_data)
42
43     def help_ff (self, src_data, exp_data, op):
44         for s in zip (range (len (src_data)), src_data):
45             src = gr.vector_source_f (s[1])
46             self.tb.connect (src, (op, s[0]))
47         dst = gr.vector_sink_f ()
48         self.tb.connect (op, dst)
49         self.tb.run ()
50         result_data = dst.data ()
51         self.assertEqual (exp_data, result_data)
52
53     def help_cc (self, src_data, exp_data, op):
54         for s in zip (range (len (src_data)), src_data):
55             src = gr.vector_source_c (s[1])
56             self.tb.connect (src, (op, s[0]))
57         dst = gr.vector_sink_c ()
58         self.tb.connect (op, dst)
59         self.tb.run ()
60         result_data = dst.data ()
61         self.assertEqual (exp_data, result_data)
62
63     def test_add_const_ii (self):
64         src_data = (1, 2, 3, 4, 5)
65         expected_result = (6, 7, 8, 9, 10)
66         op = gr.add_const_ii (5)
67         self.help_ii ((src_data,), expected_result, op)
68
69     def test_add_const_cc (self):
70         src_data = (1, 2, 3, 4, 5)
71         expected_result = (1+5j, 2+5j, 3+5j, 4+5j, 5+5j)
72         op = gr.add_const_cc (5j)
73         self.help_cc ((src_data,), expected_result, op)
74
75     def test_mult_const_ii (self):
76         src_data = (-1, 0, 1, 2, 3)
77         expected_result = (-5, 0, 5, 10, 15)
78         op = gr.multiply_const_ii (5)
79         self.help_ii ((src_data,), expected_result, op)
80
81     def test_add_ii (self):
82         src1_data = (1,  2, 3, 4, 5)
83         src2_data = (8, -3, 4, 8, 2)
84         expected_result = (9, -1, 7, 12, 7)
85         op = gr.add_ii ()
86         self.help_ii ((src1_data, src2_data),
87                       expected_result, op)
88
89     def test_mult_ii (self):
90         src1_data = (1,  2, 3, 4, 5)
91         src2_data = (8, -3, 4, 8, 2)
92         expected_result = (8, -6, 12, 32, 10)
93         op = gr.multiply_ii ()
94         self.help_ii ((src1_data, src2_data),
95                       expected_result, op)
96
97     def test_sub_ii_1 (self):
98         src1_data = (1,  2, 3, 4, 5)
99         expected_result = (-1, -2, -3, -4, -5)
100         op = gr.sub_ii ()
101         self.help_ii ((src1_data,),
102                       expected_result, op)
103
104     def test_sub_ii_2 (self):
105         src1_data = (1,  2, 3, 4, 5)
106         src2_data = (8, -3, 4, 8, 2)
107         expected_result = (-7, 5, -1, -4, 3)
108         op = gr.sub_ii ()
109         self.help_ii ((src1_data, src2_data),
110                       expected_result, op)
111
112     def test_div_ff_1 (self):
113         src1_data       = (1,  2,  4,    -8)
114         expected_result = (1, 0.5, 0.25, -.125)
115         op = gr.divide_ff ()
116         self.help_ff ((src1_data,),
117                       expected_result, op)
118
119     def test_div_ff_2 (self):
120         src1_data       = ( 5,  9, -15, 1024)
121         src2_data       = (10,  3,  -5,   64)
122         expected_result = (0.5, 3,   3,   16)
123         op = gr.divide_ff ()
124         self.help_ff ((src1_data, src2_data),
125                       expected_result, op)
126
127
128 if __name__ == '__main__':
129     gr_unittest.main ()