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