3 * Copyright 2006,2007,2008 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 3, 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.
27 #include <qa_mblock_prims.h>
28 #include <cppunit/TestAssert.h>
29 #include <mblock/mblock.h>
30 #include <mblock/runtime.h>
31 #include <mblock/protocol_class.h>
32 #include <mblock/exception.h>
33 #include <mblock/msg_queue.h>
34 #include <mblock/message.h>
35 #include <mb_mblock_impl.h>
36 #include <mblock/msg_accepter.h>
37 #include <mblock/class_registry.h>
40 static pmt_t s_cs = pmt_intern("cs");
41 static pmt_t s_debug = pmt_intern("debug");
42 static pmt_t s_in = pmt_intern("in");
43 static pmt_t s_out = pmt_intern("out");
46 // ================================================================
48 class dp_1 : public mb_mblock
51 dp_1(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
55 dp_1::dp_1(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
56 : mb_mblock(runtime, instance_name, user_arg)
62 REGISTER_MBLOCK_CLASS(dp_1);
64 // ----------------------------------------------------------------
66 class dp_2 : public mb_mblock
69 dp_2(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
73 dp_2::dp_2(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
74 : mb_mblock(runtime, instance_name, user_arg)
76 define_port("cs", "cs-protocol", false, mb_port::EXTERNAL);
81 REGISTER_MBLOCK_CLASS(dp_2);
83 // ----------------------------------------------------------------
85 class dp_3 : public mb_mblock
88 dp_3(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
92 dp_3::dp_3(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
93 : mb_mblock(runtime, instance_name, user_arg)
95 define_port("cs", "cs-protocol", false, mb_port::EXTERNAL);
96 define_port("cs", "cs-protocol", false, mb_port::EXTERNAL); // duplicate def
101 REGISTER_MBLOCK_CLASS(dp_3);
103 // ----------------------------------------------------------------
106 qa_mblock_prims::test_define_ports()
109 mb_runtime_sptr rts = mb_make_runtime();
110 mb_runtime *rt = rts.get();
113 mb_mblock_sptr mb1 = mb_mblock_sptr(new dp_1(rt, "top", PMT_F));
115 // raises runtime_error because of unknown protocol "cs-protocol"
116 CPPUNIT_ASSERT_THROW(mb_mblock_sptr(new dp_2(rt, "top", PMT_F)),
119 // define the protocol class
120 pmt_t pc = mb_make_protocol_class(pmt_intern("cs-protocol"),
121 pmt_list2(pmt_intern("start"),
125 // std::cout << "pc = " << pc << '\n';
127 mb_mblock_sptr mb2 = mb_mblock_sptr(new dp_2(rt, "top", PMT_F));
129 // raises pmt_exception because of duplicate port definition of "cs"
130 CPPUNIT_ASSERT_THROW(mb_mblock_sptr(new dp_3(rt, "top", PMT_F)),
134 // ================================================================
136 class dc_0 : public mb_mblock
139 dc_0(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
143 dc_0::dc_0(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
144 : mb_mblock(runtime, instance_name, user_arg)
150 REGISTER_MBLOCK_CLASS(dc_0);
152 // ----------------------------------------------------------------
154 class dc_ok : public mb_mblock
157 dc_ok(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
161 dc_ok::dc_ok(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
162 : mb_mblock(runtime, instance_name, user_arg)
164 define_component("c0", "dc_0");
165 define_component("c1", "dc_0");
166 define_component("c2", "dc_0");
171 REGISTER_MBLOCK_CLASS(dc_ok);
173 // ----------------------------------------------------------------
175 class dc_not_ok : public mb_mblock
178 dc_not_ok(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
182 dc_not_ok::dc_not_ok(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
183 : mb_mblock(runtime, instance_name, user_arg)
185 define_component("c0", "dc_0");
186 define_component("c0", "dc_0"); // duplicate name
189 dc_not_ok::~dc_not_ok(){}
191 REGISTER_MBLOCK_CLASS(dc_not_ok);
193 // ----------------------------------------------------------------
196 qa_mblock_prims::test_define_components()
198 mb_runtime_sptr rts = mb_make_runtime();
199 mb_runtime *rt = rts.get();
202 mb_mblock_sptr mb1 = mb_mblock_sptr(new dc_ok(rt, "top", PMT_F));
204 // raises pmt_exception because of duplicate component definition of "c0"
205 CPPUNIT_ASSERT_THROW(mb_mblock_sptr(new dc_not_ok(rt, "top", PMT_F)),
206 mbe_duplicate_component);
209 // ================================================================
211 class tc_norm : public mb_mblock
214 tc_norm(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
215 : mb_mblock(runtime, instance_name, user_arg)
217 define_port("data", "i/o", false, mb_port::EXTERNAL);
218 define_port("norm", "i/o", false, mb_port::EXTERNAL);
219 define_port("conj", "i/o", true, mb_port::EXTERNAL);
220 define_port("int", "i/o", false, mb_port::INTERNAL);
226 tc_norm::~tc_norm(){}
228 REGISTER_MBLOCK_CLASS(tc_norm);
230 ////////////////////////////////////////////////////////////////
232 class tc_0 : public mb_mblock
235 tc_0(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
236 : mb_mblock(runtime, instance_name, user_arg)
238 define_port("norm", "i/o", false, mb_port::EXTERNAL);
239 define_port("conj", "i/o", true, mb_port::EXTERNAL);
240 define_port("int", "i/o", false, mb_port::INTERNAL);
242 define_component("c0", "tc_norm");
243 define_component("c1", "tc_norm");
244 define_component("c2", "tc_norm");
245 define_component("c3", "tc_norm");
246 define_component("c4", "tc_norm");
247 define_component("c5", "tc_norm");
250 connect("c0", "norm", "c1", "conj");
252 // No: No such component name
253 CPPUNIT_ASSERT_THROW(connect("foo", "data", "c1", "norm"), mbe_no_such_component);
255 // No: No such port name
256 CPPUNIT_ASSERT_THROW(connect("c0", "data", "c1", "foo"), mbe_no_such_port);
258 // No: already connected
259 CPPUNIT_ASSERT_THROW(connect("c0", "norm", "c2", "data"), mbe_already_connected);
261 // No: already connected
262 CPPUNIT_ASSERT_THROW(connect("c2", "data", "c0", "norm"), mbe_already_connected);
264 // No: incompatible ports
265 CPPUNIT_ASSERT_THROW(connect("c1", "norm", "c2", "norm"), mbe_incompatible_ports);
268 connect("c1", "norm", "c2", "conj");
270 // No: No such port name
271 CPPUNIT_ASSERT_THROW(connect("c2", "norm", "self", "foo"), mbe_no_such_port);
273 // No: can't connect to child's internal port
274 CPPUNIT_ASSERT_THROW(connect("c0", "conj", "c2", "int"), mbe_no_such_port);
276 // No: can't connect to our own external port
277 CPPUNIT_ASSERT_THROW(connect("self", "norm", "c0", "conj"), mbe_invalid_port_type);
279 // OK: connecting to one of our internal ports
280 connect("self", "int", "c3", "conj");
282 // ===== Now test disconnecting some stuff =====
284 // Confirm we're already connected
285 CPPUNIT_ASSERT_THROW(connect("self", "int", "c3", "conj"), mbe_already_connected);
287 int nc = nconnections();
288 disconnect("self", "int", "c3", "conj"); // disconnect
289 CPPUNIT_ASSERT_EQUAL(nc-1, nconnections());
291 connect("self", "int", "c3", "conj"); // reconnect
292 CPPUNIT_ASSERT_EQUAL(nc, nconnections());
294 // confirm we're already connected
295 CPPUNIT_ASSERT_THROW(connect("self", "int", "c3", "conj"), mbe_already_connected);
298 connect("c0", "conj", "c5", "data");
299 connect("c4", "norm", "c5", "conj");
300 connect("c4", "conj", "c5", "norm");
303 disconnect_component("c4");
304 CPPUNIT_ASSERT_EQUAL(nc-2, nconnections());
306 disconnect_component("c5");
307 CPPUNIT_ASSERT_EQUAL(nc-3, nconnections());
310 CPPUNIT_ASSERT_EQUAL(0, nconnections());
319 REGISTER_MBLOCK_CLASS(tc_0);
321 ////////////////////////////////////////////////////////////////
323 class tc_1 : public mb_mblock
326 tc_1(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
327 : mb_mblock(runtime, instance_name, user_arg)
329 define_component("c0", "tc_norm");
330 define_component("c1", "tc_norm");
332 connect("c0", "norm", "c1", "conj");
340 REGISTER_MBLOCK_CLASS(tc_1);
342 ////////////////////////////////////////////////////////////////
345 qa_mblock_prims::test_connect()
347 // define the protocol class
348 mb_make_protocol_class(pmt_intern("data"), // name of class
349 pmt_list1(pmt_intern("data")), // in
352 mb_make_protocol_class(pmt_intern("i/o"), // name of class
353 pmt_list1(pmt_intern("in")), // in
354 pmt_list1(pmt_intern("out"))); // out
356 mb_runtime_sptr rts = mb_make_runtime();
357 mb_runtime *rt = rts.get();
359 mb_mblock_sptr mb0 = mb_mblock_sptr(new tc_0(rt, "top", PMT_F));
362 ////////////////////////////////////////////////////////////////
365 qa_mblock_prims::test_msg_queue()
369 // check initial state
370 CPPUNIT_ASSERT(q.get_highest_pri_msg_nowait() == 0);
372 CPPUNIT_ASSERT(MB_NPRI >= 5); // sanity check for this test
374 // insert three messages at the same pri and ensure that they come out in order
375 // signal data metadata pri
376 q.insert(mb_make_message(PMT_NIL, pmt_from_long(0), PMT_NIL, MB_PRI_BEST + 2));
377 q.insert(mb_make_message(PMT_NIL, pmt_from_long(1), PMT_NIL, MB_PRI_BEST + 2));
378 q.insert(mb_make_message(PMT_NIL, pmt_from_long(2), PMT_NIL, MB_PRI_BEST + 2));
380 CPPUNIT_ASSERT_EQUAL(0L, pmt_to_long(q.get_highest_pri_msg_nowait()->data()));
381 CPPUNIT_ASSERT_EQUAL(1L, pmt_to_long(q.get_highest_pri_msg_nowait()->data()));
382 CPPUNIT_ASSERT_EQUAL(2L, pmt_to_long(q.get_highest_pri_msg_nowait()->data()));
384 CPPUNIT_ASSERT(q.get_highest_pri_msg_nowait() == 0);
387 // insert messages of different priorities in pseudo-random order
388 // signal data metadata pri
389 q.insert(mb_make_message(PMT_NIL, PMT_NIL, PMT_NIL, MB_PRI_BEST + 3));
390 q.insert(mb_make_message(PMT_NIL, PMT_NIL, PMT_NIL, MB_PRI_BEST + 2));
391 q.insert(mb_make_message(PMT_NIL, PMT_NIL, PMT_NIL, MB_PRI_BEST + 4));
392 q.insert(mb_make_message(PMT_NIL, PMT_NIL, PMT_NIL, MB_PRI_BEST + 0));
393 q.insert(mb_make_message(PMT_NIL, PMT_NIL, PMT_NIL, MB_PRI_BEST + 1));
394 q.insert(mb_make_message(PMT_NIL, PMT_NIL, PMT_NIL, MB_PRI_BEST + 3));
395 q.insert(mb_make_message(PMT_NIL, PMT_NIL, PMT_NIL, MB_PRI_BEST + 2));
396 q.insert(mb_make_message(PMT_NIL, PMT_NIL, PMT_NIL, MB_PRI_BEST + 4));
397 q.insert(mb_make_message(PMT_NIL, PMT_NIL, PMT_NIL, MB_PRI_BEST + 0));
398 q.insert(mb_make_message(PMT_NIL, PMT_NIL, PMT_NIL, MB_PRI_BEST + 1));
400 // confirm that they come out in order
401 CPPUNIT_ASSERT_EQUAL(MB_PRI_BEST + 0, q.get_highest_pri_msg_nowait()->priority());
402 CPPUNIT_ASSERT_EQUAL(MB_PRI_BEST + 0, q.get_highest_pri_msg_nowait()->priority());
403 CPPUNIT_ASSERT_EQUAL(MB_PRI_BEST + 1, q.get_highest_pri_msg_nowait()->priority());
404 CPPUNIT_ASSERT_EQUAL(MB_PRI_BEST + 1, q.get_highest_pri_msg_nowait()->priority());
405 CPPUNIT_ASSERT_EQUAL(MB_PRI_BEST + 2, q.get_highest_pri_msg_nowait()->priority());
406 CPPUNIT_ASSERT_EQUAL(MB_PRI_BEST + 2, q.get_highest_pri_msg_nowait()->priority());
407 CPPUNIT_ASSERT_EQUAL(MB_PRI_BEST + 3, q.get_highest_pri_msg_nowait()->priority());
408 CPPUNIT_ASSERT_EQUAL(MB_PRI_BEST + 3, q.get_highest_pri_msg_nowait()->priority());
409 CPPUNIT_ASSERT_EQUAL(MB_PRI_BEST + 4, q.get_highest_pri_msg_nowait()->priority());
410 CPPUNIT_ASSERT_EQUAL(MB_PRI_BEST + 4, q.get_highest_pri_msg_nowait()->priority());
413 CPPUNIT_ASSERT(q.get_highest_pri_msg_nowait() == 0);
416 ////////////////////////////////////////////////////////////////
419 qa_mblock_prims::test_make_accepter()
421 mb_runtime_sptr rts = mb_make_runtime();
422 mb_runtime *rt = rts.get();
425 mb_mblock_sptr mb = mb_mblock_sptr(new dp_2(rt, "top", PMT_F));
427 // use "internal use only" method...
428 mb_msg_accepter_sptr accepter = mb->impl()->make_accepter(pmt_intern("cs"));
430 // Now push a few messages into it...
431 // signal data metadata pri
432 (*accepter)(PMT_NIL, pmt_from_long(0), PMT_NIL, MB_PRI_BEST + 2);
433 (*accepter)(PMT_NIL, pmt_from_long(1), PMT_NIL, MB_PRI_BEST + 2);
434 (*accepter)(PMT_NIL, pmt_from_long(2), PMT_NIL, MB_PRI_BEST + 2);
436 // try to pull them out
438 pmt_t cs = pmt_intern("cs");
440 mb_message_sptr msg = mb->impl()->msgq().get_highest_pri_msg_nowait();
441 CPPUNIT_ASSERT(pmt_eq(cs, msg->port_id())); // confirm that port_id is set
442 CPPUNIT_ASSERT_EQUAL(0L, pmt_to_long(msg->data())); // and that data is correct
444 CPPUNIT_ASSERT_EQUAL(1L, pmt_to_long(mb->impl()->msgq().get_highest_pri_msg_nowait()->data()));
445 CPPUNIT_ASSERT_EQUAL(2L, pmt_to_long(mb->impl()->msgq().get_highest_pri_msg_nowait()->data()));