Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / qa_message.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004 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
26 def all_counts ():
27     return (gr.block_ncurrently_allocated (),
28             gr.block_detail_ncurrently_allocated (),
29             gr.buffer_ncurrently_allocated (),
30             gr.buffer_reader_ncurrently_allocated (),
31             gr.message_ncurrently_allocated ())
32
33
34 class test_message (gr_unittest.TestCase):
35
36     def setUp (self):
37         self.msgq = gr.msg_queue ()
38
39     def tearDown (self):
40         self.msgq = None
41         
42     def leak_check (self, fct):
43         begin = all_counts ()
44         fct ()
45         # tear down early so we can check for leaks
46         self.tearDown ()
47         end = all_counts ()
48         self.assertEqual (begin, end)
49
50     def test_100 (self):
51         msg = gr.message (0, 1.5, 2.3)
52         self.assertEquals (0, msg.type())
53         self.assertAlmostEqual (1.5, msg.arg1())
54         self.assertAlmostEqual (2.3, msg.arg2())
55         self.assertEquals (0, msg.length())
56
57     def test_101 (self):
58         s = 'This is a test'
59         msg = gr.message_from_string(s)
60         self.assertEquals(s, msg.to_string())
61
62     def test_200 (self):
63         self.leak_check (self.body_200)
64         
65     def body_200 (self):
66         self.msgq.insert_tail (gr.message (0))
67         self.assertEquals (1, self.msgq.count())
68         self.msgq.insert_tail (gr.message (1))
69         self.assertEquals (2, self.msgq.count())
70         msg0 = self.msgq.delete_head ()
71         self.assertEquals (0, msg0.type())
72         msg1 = self.msgq.delete_head ()
73         self.assertEquals (1, msg1.type())
74         self.assertEquals (0, self.msgq.count())
75
76     def test_201 (self):
77         self.leak_check (self.body_201)
78         
79     def body_201 (self):
80         self.msgq.insert_tail (gr.message (0))
81         self.assertEquals (1, self.msgq.count())
82         self.msgq.insert_tail (gr.message (1))
83         self.assertEquals (2, self.msgq.count())
84
85     def test_202 (self):
86         self.leak_check (self.body_202)
87         
88     def body_202 (self):
89         # global msg
90         msg = gr.message (666)
91
92     def test_300(self):
93         input_data = (0,1,2,3,4,5,6,7,8,9)
94         src = gr.vector_source_b(input_data)
95         dst = gr.vector_sink_b()
96         tb = gr.top_block()
97         tb.connect(src, dst)
98         tb.run()
99         self.assertEquals(input_data, dst.data())
100
101     def test_301(self):
102         src = gr.message_source(gr.sizeof_char)
103         dst = gr.vector_sink_b()
104         tb = gr.top_block()
105         tb.connect(src, dst)
106         src.msgq().insert_tail(gr.message_from_string('01234'))
107         src.msgq().insert_tail(gr.message_from_string('5'))
108         src.msgq().insert_tail(gr.message_from_string(''))
109         src.msgq().insert_tail(gr.message_from_string('6789'))
110         src.msgq().insert_tail(gr.message(1))                  # send EOF
111         tb.run()
112         self.assertEquals(tuple(map(ord, '0123456789')), dst.data())
113         
114
115 if __name__ == '__main__':
116     gr_unittest.main ()