summaryrefslogtreecommitdiff
path: root/gr-blocks/python/qa_add_mult_div_sub.py
blob: 0aca03d3fd1df4c9c14cc65b55a2f04825fa3a2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python
#
# Copyright 2004,2007,2010,2012 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#

from gnuradio import gr, gr_unittest
import blocks_swig

class test_add_mult_div_sub(gr_unittest.TestCase):

    def setUp(self):
        self.tb = gr.top_block()

    def tearDown(self):
        self.tb = None

    def help_ii(self, src_data, exp_data, op):
        for s in zip(range(len(src_data)), src_data):
            src = gr.vector_source_i(s[1])
            self.tb.connect(src, (op, s[0]))
        dst = gr.vector_sink_i()
        self.tb.connect(op, dst)
        self.tb.run()
        result_data = dst.data()
        self.assertEqual(exp_data, result_data)

    def help_ss(self, src_data, exp_data, op):
        for s in zip(range(len(src_data)), src_data):
            src = gr.vector_source_s(s[1])
            self.tb.connect(src, (op, s[0]))
        dst = gr.vector_sink_s()
        self.tb.connect(op, dst)
        self.tb.run()
        result_data = dst.data()
        self.assertEqual(exp_data, result_data)

    def help_ff(self, src_data, exp_data, op):
        for s in zip(range(len(src_data)), src_data):
            src = gr.vector_source_f(s[1])
            self.tb.connect(src, (op, s[0]))
        dst = gr.vector_sink_f()
        self.tb.connect(op, dst)
        self.tb.run()
        result_data = dst.data()
        self.assertEqual(exp_data, result_data)

    def help_cc(self, src_data, exp_data, op):
        for s in zip(range(len(src_data)), src_data):
            src = gr.vector_source_c(s[1])
            self.tb.connect(src, (op, s[0]))
        dst = gr.vector_sink_c()
        self.tb.connect(op, dst)
        self.tb.run()
        result_data = dst.data()
        self.assertEqual(exp_data, result_data)

    # add_XX

    def test_add_ss(self):
        src1_data = (1,  2, 3, 4, 5)
        src2_data = (8, -3, 4, 8, 2)
        expected_result = (9, -1, 7, 12, 7)
        op = blocks_swig.add_ss()
        self.help_ss((src1_data, src2_data), expected_result, op)

    def test_add_ii(self):
        src1_data = (1,  2, 3, 4, 5)
        src2_data = (8, -3, 4, 8, 2)
        expected_result = (9, -1, 7, 12, 7)
        op = blocks_swig.add_ii()
        self.help_ii((src1_data, src2_data), expected_result, op)

    def test_add_ff(self):
        src1_data = (1.0,  2.0, 3.0, 4.0, 5.0)
        src2_data = (8.0, -3.0, 4.0, 8.0, 2.0)
        expected_result = (9.0, -1.0, 7.0, 12.0, 7.0)
        op = blocks_swig.add_ff()
        self.help_ff((src1_data, src2_data), expected_result, op)

    def test_add_cc(self):
        src1_data = (1+1j,  2+2j, 3+3j, 4+4j, 5+5j)
        src2_data = (8+8j, -3-3j, 4+4j, 8+8j, 2+2j)
        expected_result = (9+9j, -1-1j, 7+7j, 12+12j, 7+7j)
        op = blocks_swig.add_cc()
        self.help_cc((src1_data, src2_data), expected_result, op)

    # add_const_XX

    def test_add_const_ss(self):
        src_data = (1, 2, 3, 4, 5)
        expected_result = (6, 7, 8, 9, 10)
        op = blocks_swig.add_const_ss(5)
        self.help_ss((src_data,), expected_result, op)

    def test_add_const_ii(self):
        src_data = (1, 2, 3, 4, 5)
        expected_result = (6, 7, 8, 9, 10)
        op = blocks_swig.add_const_ii(5)
        self.help_ii((src_data,), expected_result, op)

    def test_add_const_ff(self):
        src_data = (1, 2, 3, 4, 5)
        expected_result = (6, 7, 8, 9, 10)
        op = blocks_swig.add_const_ff(5)
        self.help_ff((src_data,), expected_result, op)

    def test_add_const_cc(self):
        src_data = (1, 2, 3, 4, 5)
        expected_result = (1+5j, 2+5j, 3+5j, 4+5j, 5+5j)
        op = blocks_swig.add_const_cc(5j)
        self.help_cc((src_data,), expected_result, op)

    # multiply_XX

    def test_multiply_ss(self):
        src1_data = (1,  2, 3, 4, 5)
        src2_data = (8, -3, 4, 8, 2)
        expected_result = (8, -6, 12, 32, 10)
        op = blocks_swig.multiply_ss()
        self.help_ss((src1_data, src2_data),
                      expected_result, op)

    def test_multiply_ii(self):
        src1_data = (1,  2, 3, 4, 5)
        src2_data = (8, -3, 4, 8, 2)
        expected_result = (8, -6, 12, 32, 10)
        op = blocks_swig.multiply_ii()
        self.help_ii((src1_data, src2_data),
                      expected_result, op)

    def test_multiply_ff(self):
        src1_data = (1,  2, 3, 4, 5)
        src2_data = (8, -3, 4, 8, 2)
        expected_result = (8, -6, 12, 32, 10)
        op = blocks_swig.multiply_ff()
        self.help_ff((src1_data, src2_data),
                      expected_result, op)

    def test_multiply_cc(self):
        src1_data = (1+1j,  2+2j, 3+3j, 4+4j, 5+5j)
        src2_data = (8, -3, 4, 8, 2)
        expected_result = (8+8j, -6-6j, 12+12j, 32+32j, 10+10j)
        op = blocks_swig.multiply_cc()
        self.help_cc((src1_data, src2_data),
                      expected_result, op)

    # multiply_const_XX

    def test_multiply_const_ss(self):
        src_data = (-1, 0, 1, 2, 3)
        expected_result = (-5, 0, 5, 10, 15)
        op = blocks_swig.multiply_const_ss(5)
        self.help_ss((src_data,), expected_result, op)

    def test_multiply_const_ii(self):
        src_data = (-1, 0, 1, 2, 3)
        expected_result = (-5, 0, 5, 10, 15)
        op = blocks_swig.multiply_const_ii(5)
        self.help_ii((src_data,), expected_result, op)

    def test_multiply_const_ff(self):
        src_data = (-1, 0, 1, 2, 3)
        expected_result = (-5, 0, 5, 10, 15)
        op = blocks_swig.multiply_const_ff(5)
        self.help_ff((src_data,), expected_result, op)

    def test_multiply_const_cc(self):
        src_data = (-1-1j, 0+0j, 1+1j, 2+2j, 3+3j)
        expected_result = (-5-5j, 0+0j, 5+5j, 10+10j, 15+15j)
        op = blocks_swig.multiply_const_cc(5)
        self.help_cc((src_data,), expected_result, op)

    def test_multiply_const_cc2(self):
        src_data = (-1-1j, 0+0j, 1+1j, 2+2j, 3+3j)
        expected_result = (-3-7j, 0+0j, 3+7j, 6+14j, 9+21j)
        op = blocks_swig.multiply_const_cc(5+2j)
        self.help_cc((src_data,), expected_result, op)

    def test_sub_ii(self):
        src1_data = (1,  2, 3, 4, 5)
        src2_data = (8, -3, 4, 8, 2)
        expected_result = (-7, 5, -1, -4, 3)
        op = blocks_swig.sub_ii()
        self.help_ii((src1_data, src2_data),
                      expected_result, op)

    def test_div_ff(self):
        src1_data       = ( 5,  9, -15, 1024)
        src2_data       = (10,  3,  -5,   64)
        expected_result = (0.5, 3,   3,   16)
        op = blocks_swig.divide_ff()
        self.help_ff((src1_data, src2_data), expected_result, op)

if __name__ == '__main__':
    gr_unittest.run(test_add_mult_div_sub, "test_add_mult_div_sub.xml")