]> git.gag.com Git - debian/gnuradio/blob - mblock/src/lib/qa_mblock_prims.cc
Merged latest pmt and mblock into trunk (eb/wip -r4262:4268)
[debian/gnuradio] / mblock / src / lib / qa_mblock_prims.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <qa_mblock_prims.h>
24 #include <cppunit/TestAssert.h>
25 #include <mb_mblock.h>
26 #include <mb_runtime.h>
27 #include <mb_protocol_class.h>
28 #include <mb_exception.h>
29 #include <stdio.h>
30
31 static pmt_t s_cs = pmt_intern("cs");
32 static pmt_t s_debug = pmt_intern("debug");
33 static pmt_t s_in = pmt_intern("in");
34 static pmt_t s_out = pmt_intern("out");
35   
36
37 // ================================================================
38
39 class dp_1 : public mb_mblock
40 {
41 public:
42   dp_1();
43   ~dp_1();
44 };
45
46 dp_1::dp_1()
47 {
48 }
49
50 dp_1::~dp_1(){}
51
52 // ----------------------------------------------------------------
53
54 class dp_2 : public mb_mblock
55 {
56 public:
57   dp_2();
58   ~dp_2();
59 };
60
61 dp_2::dp_2()
62 {
63   define_port("cs", "cs-protocol", false, mb_port::EXTERNAL);
64 }
65
66 dp_2::~dp_2(){}
67
68 // ----------------------------------------------------------------
69
70 class dp_3 : public mb_mblock
71 {
72 public:
73   dp_3();
74   ~dp_3();
75 };
76
77 dp_3::dp_3()
78 {
79   define_port("cs", "cs-protocol", false, mb_port::EXTERNAL);
80   define_port("cs", "cs-protocol", false, mb_port::EXTERNAL);   // duplicate def
81 }
82
83 dp_3::~dp_3(){}
84
85 // ----------------------------------------------------------------
86
87 void
88 qa_mblock_prims::test_define_ports()
89 {
90   mb_runtime_sptr       rt = mb_make_runtime();
91   // std::vector<mb_port_sptr>  intf;
92
93   mb_mblock_sptr        mb1 = mb_mblock_sptr(new dp_1());
94   // intf = mb1->peer_interface();
95   // CPPUNIT_ASSERT_EQUAL(size_t(0), intf.size());
96
97   // raises runtime_error because of unknown protocol "cs-protocol"
98   CPPUNIT_ASSERT_THROW(mb_mblock_sptr(new dp_2()), std::runtime_error);
99
100   // define the protocol class
101   pmt_t pc = mb_make_protocol_class(pmt_intern("cs-protocol"),
102                                     pmt_cons(pmt_intern("start"),
103                                              pmt_cons(pmt_intern("stop"),
104                                                       PMT_NIL)),
105                                     PMT_NIL);
106
107   // std::cout << "pc = " << pc << '\n';
108
109   mb_mblock_sptr mb2 = mb_mblock_sptr(new dp_2());
110
111   // intf = mb2->peer_interface();
112   // CPPUNIT_ASSERT_EQUAL(size_t(1), intf.size());
113   // CPPUNIT_ASSERT(pmt_eq(s_cs, intf[0]->port_name()));
114
115
116   // raises pmt_exception because of duplicate port definition of "cs"
117   CPPUNIT_ASSERT_THROW(mb_mblock_sptr(new dp_3()), mbe_duplicate_port);
118
119 #if 0
120   try {
121     mb_mblock_sptr mb2 = mb_mblock_sptr(new dp_2());
122   }
123   catch (pmt_exception &e){
124     std::cerr << e.msg() << ' ' << e.obj() << '\n';
125   }
126 #endif
127
128 }
129
130 // ================================================================
131
132 class dc_0 : public mb_mblock
133 {
134 public:
135   dc_0();
136   ~dc_0();
137 };
138
139 dc_0::dc_0()
140 {
141 }
142
143 dc_0::~dc_0() {}
144
145 // ----------------------------------------------------------------
146
147 class dc_ok : public mb_mblock
148 {
149 public:
150   dc_ok();
151   ~dc_ok();
152 };
153
154 dc_ok::dc_ok()
155 {
156   define_component("c0", mb_mblock_sptr(new dc_0()));
157   define_component("c1", mb_mblock_sptr(new dc_0()));
158   define_component("c2", mb_mblock_sptr(new dc_0()));
159 }
160
161 dc_ok::~dc_ok(){}
162
163 // ----------------------------------------------------------------
164
165 class dc_not_ok : public mb_mblock
166 {
167 public:
168   dc_not_ok();
169   ~dc_not_ok();
170 };
171
172 dc_not_ok::dc_not_ok()
173   : mb_mblock()
174 {
175   define_component("c0", mb_mblock_sptr(new dc_0()));
176   define_component("c0", mb_mblock_sptr(new dc_0()));   // duplicate name
177 }
178
179 dc_not_ok::~dc_not_ok(){}
180
181 // ----------------------------------------------------------------
182
183 void
184 qa_mblock_prims::test_define_components()
185 {
186   mb_runtime_sptr       rt = mb_make_runtime();
187   mb_mblock_sptr        mb1 = mb_mblock_sptr(new dc_ok());      // OK
188
189   // raises pmt_exception because of duplicate component definition of "c0"
190   CPPUNIT_ASSERT_THROW(mb_mblock_sptr(new dc_not_ok()), mbe_duplicate_component);
191 }
192
193 // ================================================================
194
195 class tc_norm : public mb_mblock
196 {
197 public:
198   tc_norm(){
199     define_port("data", "i/o", false, mb_port::EXTERNAL);
200     define_port("norm", "i/o", false, mb_port::EXTERNAL);
201     define_port("conj", "i/o", true,  mb_port::EXTERNAL);
202     define_port("int",  "i/o", false, mb_port::INTERNAL);
203   }
204
205   ~tc_norm();
206 };
207
208 tc_norm::~tc_norm(){}
209
210 ////////////////////////////////////////////////////////////////
211
212 class tc_0 : public mb_mblock
213 {
214 public:
215   tc_0(){
216     define_port("norm", "i/o", false, mb_port::EXTERNAL);
217     define_port("conj", "i/o", true,  mb_port::EXTERNAL);
218     define_port("int",  "i/o", false, mb_port::INTERNAL);
219
220     define_component("c0", mb_mblock_sptr(new tc_norm()));
221     define_component("c1", mb_mblock_sptr(new tc_norm()));
222     define_component("c2", mb_mblock_sptr(new tc_norm()));
223     define_component("c3", mb_mblock_sptr(new tc_norm()));
224     define_component("c4", mb_mblock_sptr(new tc_norm()));
225     define_component("c5", mb_mblock_sptr(new tc_norm()));
226
227     // OK
228     connect("c0", "norm", "c1", "conj");
229
230     // No:  No such component name
231     CPPUNIT_ASSERT_THROW(connect("foo", "data", "c1", "norm"), mbe_no_such_component);
232
233     // No:  No such port name
234     CPPUNIT_ASSERT_THROW(connect("c0", "data", "c1", "foo"), mbe_no_such_port);
235
236     // No:  already connected
237     CPPUNIT_ASSERT_THROW(connect("c0", "norm", "c2", "data"), mbe_already_connected);
238
239     // No:  already connected
240     CPPUNIT_ASSERT_THROW(connect("c2", "data", "c0", "norm"), mbe_already_connected);
241
242     // No: incompatible ports
243     CPPUNIT_ASSERT_THROW(connect("c1", "norm", "c2", "norm"), mbe_incompatible_ports);
244
245     // OK
246     connect("c1", "norm", "c2", "conj");
247
248     // No: No such port name
249     CPPUNIT_ASSERT_THROW(connect("c2", "norm", "self", "foo"), mbe_no_such_port);
250
251     // No: can't connect to child's internal port
252     CPPUNIT_ASSERT_THROW(connect("c0", "conj", "c2", "int"), mbe_no_such_port);
253
254     // No: can't connect to our own external port
255     CPPUNIT_ASSERT_THROW(connect("self", "norm", "c0", "conj"), mbe_invalid_port_type);
256
257     // OK:  connecting to one of our internal ports
258     connect("self", "int", "c3", "conj");
259
260     // =====  Now test disconnecting some stuff =====
261
262     // Confirm we're already connected
263     CPPUNIT_ASSERT_THROW(connect("self", "int", "c3", "conj"), mbe_already_connected);
264
265     int nc = nconnections();
266     disconnect("self", "int", "c3", "conj");    // disconnect
267     CPPUNIT_ASSERT_EQUAL(nc-1, nconnections());
268     
269     connect("self", "int", "c3", "conj");       // reconnect
270     CPPUNIT_ASSERT_EQUAL(nc, nconnections());
271
272     // confirm we're already connected
273     CPPUNIT_ASSERT_THROW(connect("self", "int", "c3", "conj"), mbe_already_connected);
274
275
276     connect("c0", "conj", "c5", "data");
277     connect("c4", "norm", "c5", "conj");
278     connect("c4", "conj", "c5", "norm");
279
280     nc = nconnections();
281     disconnect_component("c4");
282     CPPUNIT_ASSERT_EQUAL(nc-2, nconnections());
283
284     disconnect_component("c5");
285     CPPUNIT_ASSERT_EQUAL(nc-3, nconnections());
286
287     disconnect_all();
288     CPPUNIT_ASSERT_EQUAL(0, nconnections());
289
290   }
291
292   ~tc_0();
293 };
294
295 tc_0::~tc_0(){}
296
297 ////////////////////////////////////////////////////////////////
298
299 class tc_1 : public mb_mblock
300 {
301 public:
302   tc_1(){
303     define_component("c0", mb_mblock_sptr(new tc_norm()));
304     define_component("c1", mb_mblock_sptr(new tc_norm()));
305
306     connect("c0", "norm", "c1", "conj");
307   }
308
309   ~tc_1();
310 };
311
312 tc_1::~tc_1(){}
313
314 ////////////////////////////////////////////////////////////////
315
316 void
317 qa_mblock_prims::test_connect()
318 {
319   // define the protocol class
320   mb_make_protocol_class(pmt_intern("data"),                            // name of class
321                          pmt_cons(pmt_intern("data"), PMT_NIL),         // in
322                          PMT_NIL);                                      // out
323
324   mb_make_protocol_class(pmt_intern("i/o"),                             // name of class
325                          pmt_cons(pmt_intern("in"), PMT_NIL),           // in
326                          pmt_cons(pmt_intern("out"), PMT_NIL));         // out
327
328
329   mb_runtime_sptr       rt = mb_make_runtime();
330   mb_mblock_sptr        mb0 = mb_mblock_sptr(new tc_0());
331 }