summaryrefslogtreecommitdiff
path: root/gr-blocks/python/qa_boolean_operators.py
blob: 5572f60acb0c5da3316e00ff2d1268c4e31978ee (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
#!/usr/bin/env python
#
# Copyright 2004,2007,2008,2010 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_boolean_operators (gr_unittest.TestCase):

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

    def tearDown (self):
        self.tb = None

    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_bb (self, src_data, exp_data, op):
        for s in zip (range (len (src_data)), src_data):
            src = gr.vector_source_b (s[1])
            self.tb.connect (src, (op, s[0]))
        dst = gr.vector_sink_b ()
        self.tb.connect (op, dst)
        self.tb.run ()
        result_data = dst.data ()
        self.assertEqual (exp_data, result_data)

    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 test_xor_ss (self):
        src1_data =       (1,  2,  3,  0x5004,   0x1150)
        src2_data =       (8,  2,  1 , 0x0508,   0x1105)
        expected_result = (9,  0,  2,  0x550C,   0x0055)
        op = blocks_swig.xor_ss ()
        self.help_ss ((src1_data, src2_data),
                      expected_result, op)

    def test_xor_bb (self):
        src1_data =       (1,  2,  3,  4,   0x50)
        src2_data =       (8,  2,  1 , 8,   0x05)
        expected_result = (9,  0,  2,  0xC, 0x55)
        op = blocks_swig.xor_bb ()
        self.help_bb ((src1_data, src2_data),
                      expected_result, op)


    def test_xor_ii (self):
        src1_data =       (1,  2,  3,  0x5000004,   0x11000050)
        src2_data =       (8,  2,  1 , 0x0500008,   0x11000005)
        expected_result = (9,  0,  2,  0x550000C,   0x00000055)
        op = blocks_swig.xor_ii ()
        self.help_ii ((src1_data, src2_data),
                      expected_result, op)


    def test_and_ss (self):
        src1_data =       (1,  2,  3,  0x5004,   0x1150)
        src2_data =       (8,  2,  1 , 0x0508,   0x1105)
        expected_result = (0,  2,  1,  0x0000,   0x1100)
        op = blocks_swig.and_ss ()
        self.help_ss ((src1_data, src2_data),
                      expected_result, op)

    def test_and_bb (self):
        src1_data =       (1,  2, 2,  3,  0x04,   0x50)
        src2_data =       (8,  2, 2,  1,  0x08,   0x05)
        src3_data =       (8,  2, 1,  1,  0x08,   0x05)
        expected_result = (0,  2, 0,  1,  0x00,   0x00)
        op = blocks_swig.and_bb ()
        self.help_bb ((src1_data, src2_data, src3_data),
                      expected_result, op)

    def test_and_ii (self):
        src1_data =       (1,  2,  3,  0x50005004,   0x11001150)
        src2_data =       (8,  2,  1 , 0x05000508,   0x11001105)
        expected_result = (0,  2,  1,  0x00000000,   0x11001100)
        op = blocks_swig.and_ii ()
        self.help_ii ((src1_data, src2_data),
                      expected_result, op)

    def test_and_const_ss (self):
        src_data =        (1,  2,  3,  0x5004,   0x1150)
        expected_result = (0,  2,  2,  0x5000,   0x1100)
        src = gr.vector_source_s(src_data)
        op = blocks_swig.and_const_ss (0x55AA)
        dst = gr.vector_sink_s()
        self.tb.connect(src, op, dst)
        self.tb.run()
        self.assertEqual(dst.data(), expected_result)

    def test_and_const_bb (self):
        src_data =        (1,  2, 3,  0x50,   0x11)
        expected_result = (0,  2, 2,  0x00,   0x00)
        src = gr.vector_source_b(src_data)
        op = blocks_swig.and_const_bb (0xAA)
        dst = gr.vector_sink_b()
        self.tb.connect(src, op, dst)
        self.tb.run()
        self.assertEqual(dst.data(), expected_result)


    def test_and_const_ii (self):
        src_data =        (1,  2,  3,  0x5004,   0x1150)
        expected_result = (0,  2,  2,  0x5000,   0x1100)
        src = gr.vector_source_i(src_data)
        op = blocks_swig.and_const_ii (0x55AA)
        dst = gr.vector_sink_i()
        self.tb.connect(src, op, dst)
        self.tb.run()
        self.assertEqual(dst.data(), expected_result)


    def test_or_ss (self):
        src1_data =       (1,  2,  3,  0x5004,   0x1150)
        src2_data =       (8,  2,  1 , 0x0508,   0x1105)
        expected_result = (9,  2,  3,  0x550C,   0x1155)
        op = blocks_swig.or_ss ()
        self.help_ss ((src1_data, src2_data),
                      expected_result, op)

    def test_or_bb (self):
        src1_data =       (1,  2, 2,  3,  0x04,   0x50)
        src2_data =       (8,  2, 2,  1 , 0x08,   0x05)
        src3_data =       (8,  2, 1,  1 , 0x08,   0x05)
        expected_result = (9,  2, 3,  3,  0x0C,   0x55)
        op = blocks_swig.or_bb ()
        self.help_bb ((src1_data, src2_data, src3_data),
                      expected_result, op)

    def test_or_ii (self):
        src1_data =       (1,  2,  3,  0x50005004,   0x11001150)
        src2_data =       (8,  2,  1 , 0x05000508,   0x11001105)
        expected_result = (9,  2,  3,  0x5500550C,   0x11001155)
        op = blocks_swig.or_ii ()
        self.help_ii ((src1_data, src2_data),
                      expected_result, op)

    def test_not_ss (self):
        src1_data =       (1,      2,      3,       0x5004,   0x1150)
        expected_result = (~1,     ~2,      ~3,       ~0x5004,   ~0x1150)
        op = blocks_swig.not_ss ()
        self.help_ss ((((src1_data),)),
                      expected_result, op)

    def test_not_bb (self):
        src1_data =       (1,     2,    2,     3,     0x04,   0x50)
        expected_result = (0xFE,  0xFD, 0xFD,  0xFC,  0xFB,   0xAF)
        op = blocks_swig.not_bb ()
        self.help_bb (((src1_data), ),
                      expected_result, op)

    def test_not_ii (self):
        src1_data =       (1,    2,  3,  0x50005004,   0x11001150)
        expected_result = (~1 , ~2, ~3, ~0x50005004,  ~0x11001150)
        op = blocks_swig.not_ii ()
        self.help_ii (((src1_data),),
                      expected_result, op)


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