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_sys.h>
28 #include <cppunit/TestAssert.h>
29 #include <mblock/mblock.h>
30 #include <mblock/runtime.h>
31 #include <mb_runtime_nop.h> // QA only
32 #include <mblock/protocol_class.h>
33 #include <mblock/exception.h>
34 #include <mblock/msg_queue.h>
35 #include <mblock/message.h>
36 #include <mb_mblock_impl.h>
37 #include <mblock/msg_accepter.h>
38 #include <mblock/class_registry.h>
44 static pmt_t s_data = pmt_intern("data");
45 static pmt_t s_status = pmt_intern("status");
46 static pmt_t s_control = pmt_intern("control");
47 static pmt_t s_p0 = pmt_intern("p0");
48 static pmt_t s_p1 = pmt_intern("p1");
49 static pmt_t s_p2 = pmt_intern("p2");
50 static pmt_t s_p3 = pmt_intern("p3");
51 static pmt_t s_e1 = pmt_intern("e1");
52 static pmt_t s_r1 = pmt_intern("r1");
55 define_protocol_classes()
57 mb_make_protocol_class(s_data, // name
58 pmt_list1(s_data), // incoming
59 pmt_list1(s_data)); // outgoing
63 // ================================================================
65 // ================================================================
67 class sys_1 : public mb_mblock
73 sys_1(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
75 void initial_transition();
78 sys_1::sys_1(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
79 : mb_mblock(runtime, instance_name, user_arg),
82 d_data = define_port("data", "data", true, mb_port::EXTERNAL);
88 sys_1::initial_transition()
90 shutdown_all(d_user_arg);
93 REGISTER_MBLOCK_CLASS(sys_1);
96 qa_mblock_sys::test_sys_1()
98 define_protocol_classes();
101 pmt_t n1 = pmt_from_long(1);
102 pmt_t n2 = pmt_from_long(2);
104 mb_runtime_sptr rt1 = mb_make_runtime();
108 rt1->run("top-1", "sys_1", n1, &result);
110 catch (omni_thread_fatal e){
111 std::cerr << "caught omni_thread_fatal: error = " << e.error
112 << ": " << strerror(e.error) << std::endl;
114 catch (omni_thread_invalid){
115 std::cerr << "caught omni_thread_invalid\n";
118 rt1->run("top-1", "sys_1", n1, &result);
120 CPPUNIT_ASSERT(pmt_equal(n1, result));
122 // Execute run a second time, with the same rt, to ensure sanity.
123 rt1->run("top-2", "sys_1", n2, &result);
124 CPPUNIT_ASSERT(pmt_equal(n2, result));
127 // ================================================================
129 // ================================================================
131 class squarer : public mb_mblock
136 squarer(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
138 void handle_message(mb_message_sptr msg);
141 squarer::squarer(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
142 : mb_mblock(runtime, instance_name, user_arg)
144 d_data = define_port("data", "data", true, mb_port::EXTERNAL);
148 squarer::handle_message(mb_message_sptr msg)
150 if (!pmt_eq(msg->signal(), s_data)) // we only handle the "data" message
153 // long x -> (long x . long (x * x))
155 pmt_t x_pmt = msg->data();
156 long x = pmt_to_long(x_pmt);
157 d_data->send(s_data, pmt_cons(x_pmt, pmt_from_long(x * x)));
160 REGISTER_MBLOCK_CLASS(squarer);
162 // ----------------------------------------------------------------
164 class sys_2 : public mb_mblock
169 sys_2(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
170 void initial_transition();
171 void handle_message(mb_message_sptr msg);
174 sys_2::sys_2(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
175 : mb_mblock(runtime, instance_name, user_arg)
177 d_data = define_port("data", "data", true, mb_port::INTERNAL);
178 define_component("squarer", "squarer");
179 connect("self", "data", "squarer", "data");
183 sys_2::initial_transition()
185 // FIXME start timer to detect general failure
187 d_data->send(s_data, pmt_from_long(0)); // send initial message
191 sys_2::handle_message(mb_message_sptr msg)
193 if (!pmt_eq(msg->signal(), s_data)) // we only handle the "data" message
196 // first check correctness of message
198 long x = pmt_to_long(pmt_car(msg->data()));
199 long y = pmt_to_long(pmt_cdr(msg->data()));
201 // std::cout << msg->data() << std::endl;
204 std::cerr << "sys_2::handle_message: Expected y == x * x. Got y = "
205 << y << " for x = " << x << std::endl;
207 shutdown_all(PMT_F); // failed
211 shutdown_all(PMT_T); // done, OK
213 d_data->send(s_data, pmt_from_long(x + 1)); // send next request
216 REGISTER_MBLOCK_CLASS(sys_2);
218 // ----------------------------------------------------------------
221 qa_mblock_sys::test_sys_2()
223 mb_runtime_sptr rt = mb_make_runtime();
224 pmt_t result = PMT_NIL;
226 // std::cerr << "qa_mblock_sys::test_sys_2 (enter)\n";
228 rt->run("top-sys-2", "sys_2", PMT_F, &result);
229 CPPUNIT_ASSERT(pmt_equal(PMT_T, result));
232 // ================================================================
234 // ================================================================
237 qa_mblock_sys::test_bitset_1()
239 mb_runtime_sptr rt = mb_make_runtime();
240 pmt_t result = PMT_NIL;
245 pmt_t arg = pmt_list2(pmt_from_long(nmsgs), // # of messages to send through pipe
246 pmt_from_long(batch_size));
248 rt->run("top", "qa_bitset_top", arg, &result);
250 CPPUNIT_ASSERT(pmt_equal(PMT_T, result));
253 // ================================================================
255 // ================================================================
258 qa_mblock_sys::test_disconnect()
260 mb_runtime_sptr rt = mb_make_runtime();
261 pmt_t result = PMT_NIL;
265 pmt_t arg = pmt_list1(pmt_from_long(nmsgs)); // # of messages to send through pipe
268 rt->run("top", "qa_disconnect_top", arg, &result);
270 CPPUNIT_ASSERT(pmt_equal(PMT_T, result));