3 * Copyright 2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
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>
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");
37 // ================================================================
39 class dp_1 : public mb_mblock
52 // ----------------------------------------------------------------
54 class dp_2 : public mb_mblock
63 define_port("cs", "cs-protocol", false, mb_port::EXTERNAL);
68 // ----------------------------------------------------------------
70 class dp_3 : public mb_mblock
79 define_port("cs", "cs-protocol", false, mb_port::EXTERNAL);
80 define_port("cs", "cs-protocol", false, mb_port::EXTERNAL); // duplicate def
85 // ----------------------------------------------------------------
88 qa_mblock_prims::test_define_ports()
90 mb_runtime_sptr rt = mb_make_runtime();
91 // std::vector<mb_port_sptr> intf;
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());
97 // raises runtime_error because of unknown protocol "cs-protocol"
98 CPPUNIT_ASSERT_THROW(mb_mblock_sptr(new dp_2()), std::runtime_error);
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"),
107 // std::cout << "pc = " << pc << '\n';
109 mb_mblock_sptr mb2 = mb_mblock_sptr(new dp_2());
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()));
116 // raises pmt_exception because of duplicate port definition of "cs"
117 CPPUNIT_ASSERT_THROW(mb_mblock_sptr(new dp_3()), mbe_duplicate_port);
121 mb_mblock_sptr mb2 = mb_mblock_sptr(new dp_2());
123 catch (pmt_exception &e){
124 std::cerr << e.msg() << ' ' << e.obj() << '\n';
130 // ================================================================
132 class dc_0 : public mb_mblock
145 // ----------------------------------------------------------------
147 class dc_ok : public mb_mblock
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()));
163 // ----------------------------------------------------------------
165 class dc_not_ok : public mb_mblock
172 dc_not_ok::dc_not_ok()
175 define_component("c0", mb_mblock_sptr(new dc_0()));
176 define_component("c0", mb_mblock_sptr(new dc_0())); // duplicate name
179 dc_not_ok::~dc_not_ok(){}
181 // ----------------------------------------------------------------
184 qa_mblock_prims::test_define_components()
186 mb_runtime_sptr rt = mb_make_runtime();
187 mb_mblock_sptr mb1 = mb_mblock_sptr(new dc_ok()); // OK
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);
193 // ================================================================
195 class tc_norm : public mb_mblock
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);
208 tc_norm::~tc_norm(){}
210 ////////////////////////////////////////////////////////////////
212 class tc_0 : public mb_mblock
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);
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()));
228 connect("c0", "norm", "c1", "conj");
230 // No: No such component name
231 CPPUNIT_ASSERT_THROW(connect("foo", "data", "c1", "norm"), mbe_no_such_component);
233 // No: No such port name
234 CPPUNIT_ASSERT_THROW(connect("c0", "data", "c1", "foo"), mbe_no_such_port);
236 // No: already connected
237 CPPUNIT_ASSERT_THROW(connect("c0", "norm", "c2", "data"), mbe_already_connected);
239 // No: already connected
240 CPPUNIT_ASSERT_THROW(connect("c2", "data", "c0", "norm"), mbe_already_connected);
242 // No: incompatible ports
243 CPPUNIT_ASSERT_THROW(connect("c1", "norm", "c2", "norm"), mbe_incompatible_ports);
246 connect("c1", "norm", "c2", "conj");
248 // No: No such port name
249 CPPUNIT_ASSERT_THROW(connect("c2", "norm", "self", "foo"), mbe_no_such_port);
251 // No: can't connect to child's internal port
252 CPPUNIT_ASSERT_THROW(connect("c0", "conj", "c2", "int"), mbe_no_such_port);
254 // No: can't connect to our own external port
255 CPPUNIT_ASSERT_THROW(connect("self", "norm", "c0", "conj"), mbe_invalid_port_type);
257 // OK: connecting to one of our internal ports
258 connect("self", "int", "c3", "conj");
260 // ===== Now test disconnecting some stuff =====
262 // Confirm we're already connected
263 CPPUNIT_ASSERT_THROW(connect("self", "int", "c3", "conj"), mbe_already_connected);
265 int nc = nconnections();
266 disconnect("self", "int", "c3", "conj"); // disconnect
267 CPPUNIT_ASSERT_EQUAL(nc-1, nconnections());
269 connect("self", "int", "c3", "conj"); // reconnect
270 CPPUNIT_ASSERT_EQUAL(nc, nconnections());
272 // confirm we're already connected
273 CPPUNIT_ASSERT_THROW(connect("self", "int", "c3", "conj"), mbe_already_connected);
276 connect("c0", "conj", "c5", "data");
277 connect("c4", "norm", "c5", "conj");
278 connect("c4", "conj", "c5", "norm");
281 disconnect_component("c4");
282 CPPUNIT_ASSERT_EQUAL(nc-2, nconnections());
284 disconnect_component("c5");
285 CPPUNIT_ASSERT_EQUAL(nc-3, nconnections());
288 CPPUNIT_ASSERT_EQUAL(0, nconnections());
297 ////////////////////////////////////////////////////////////////
299 class tc_1 : public mb_mblock
303 define_component("c0", mb_mblock_sptr(new tc_norm()));
304 define_component("c1", mb_mblock_sptr(new tc_norm()));
306 connect("c0", "norm", "c1", "conj");
314 ////////////////////////////////////////////////////////////////
317 qa_mblock_prims::test_connect()
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
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
329 mb_runtime_sptr rt = mb_make_runtime();
330 mb_mblock_sptr mb0 = mb_mblock_sptr(new tc_0());