1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 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 3, 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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <gnuradio/blocks/head.h>
28 #include <gnuradio/blocks/nop.h>
29 #include <gnuradio/blocks/null_sink.h>
30 #include <gnuradio/blocks/null_source.h>
31 #include <gnuradio/top_block.h>
32 #include <boost/test/unit_test.hpp>
33 #include <iostream>
34 
35 #define VERBOSE 0
36 
BOOST_AUTO_TEST_CASE(t0)37 BOOST_AUTO_TEST_CASE(t0)
38 {
39     if (VERBOSE)
40         std::cout << "qa_top_block::t0()\n";
41 
42     gr::top_block_sptr tb = gr::make_top_block("top");
43 
44     BOOST_REQUIRE(tb);
45 }
46 
BOOST_AUTO_TEST_CASE(t1_run)47 BOOST_AUTO_TEST_CASE(t1_run)
48 {
49     if (VERBOSE)
50         std::cout << "qa_top_block::t1()\n";
51 
52     gr::top_block_sptr tb = gr::make_top_block("top");
53 
54     gr::block_sptr src = gr::blocks::null_source::make(sizeof(int));
55     gr::block_sptr head = gr::blocks::head::make(sizeof(int), 100000);
56     gr::block_sptr dst = gr::blocks::null_sink::make(sizeof(int));
57 
58     tb->connect(src, 0, head, 0);
59     tb->connect(head, 0, dst, 0);
60     tb->run();
61 }
62 
BOOST_AUTO_TEST_CASE(t2_start_stop_wait)63 BOOST_AUTO_TEST_CASE(t2_start_stop_wait)
64 {
65     if (VERBOSE)
66         std::cout << "qa_top_block::t2()\n";
67 
68     gr::top_block_sptr tb = gr::make_top_block("top");
69 
70     gr::block_sptr src = gr::blocks::null_source::make(sizeof(int));
71     gr::block_sptr head = gr::blocks::head::make(sizeof(int), 100000);
72     gr::block_sptr dst = gr::blocks::null_sink::make(sizeof(int));
73 
74     tb->connect(src, 0, head, 0);
75     tb->connect(head, 0, dst, 0);
76 
77     tb->start();
78     tb->stop();
79     tb->wait();
80 }
81 
BOOST_AUTO_TEST_CASE(t3_lock_unlock)82 BOOST_AUTO_TEST_CASE(t3_lock_unlock)
83 {
84     if (VERBOSE)
85         std::cout << "qa_top_block::t3()\n";
86 
87     gr::top_block_sptr tb = gr::make_top_block("top");
88 
89     gr::block_sptr src = gr::blocks::null_source::make(sizeof(int));
90     gr::block_sptr dst = gr::blocks::null_sink::make(sizeof(int));
91 
92     tb->connect(src, 0, dst, 0);
93 
94     tb->start();
95 
96     tb->lock();
97     tb->unlock();
98 
99     tb->stop();
100     tb->wait();
101 }
102 
BOOST_AUTO_TEST_CASE(t4_reconfigure)103 BOOST_AUTO_TEST_CASE(t4_reconfigure)
104 {
105     if (VERBOSE)
106         std::cout << "qa_top_block::t4()\n";
107 
108     gr::top_block_sptr tb = gr::make_top_block("top");
109 
110     gr::block_sptr src = gr::blocks::null_source::make(sizeof(int));
111     gr::block_sptr head = gr::blocks::head::make(sizeof(int), 100000);
112     gr::block_sptr dst = gr::blocks::null_sink::make(sizeof(int));
113 
114     // Start infinite flowgraph
115     tb->connect(src, 0, dst, 0);
116     tb->start();
117 
118     // Reconfigure with gr_head in the middle
119     tb->lock();
120     tb->disconnect(src, 0, dst, 0);
121     tb->connect(src, 0, head, 0);
122     tb->connect(head, 0, dst, 0);
123     tb->unlock();
124 
125     // Wait for flowgraph to end on its own
126     tb->wait();
127 }
128 
129 
BOOST_AUTO_TEST_CASE(t5_max_noutputs)130 BOOST_AUTO_TEST_CASE(t5_max_noutputs)
131 {
132     if (VERBOSE)
133         std::cout << "qa_top_block::t5()\n";
134 
135     gr::top_block_sptr tb = gr::make_top_block("top");
136 
137     gr::block_sptr src = gr::blocks::null_source::make(sizeof(int));
138     gr::block_sptr head = gr::blocks::head::make(sizeof(int), 100000);
139     gr::block_sptr dst = gr::blocks::null_sink::make(sizeof(int));
140 
141     // Start infinite flowgraph
142     tb->connect(src, 0, head, 0);
143     tb->connect(head, 0, dst, 0);
144     tb->start(100);
145     tb->wait();
146 }
147 
BOOST_AUTO_TEST_CASE(t6_reconfig_max_noutputs)148 BOOST_AUTO_TEST_CASE(t6_reconfig_max_noutputs)
149 {
150     if (VERBOSE)
151         std::cout << "qa_top_block::t6()\n";
152 
153     gr::top_block_sptr tb = gr::make_top_block("top");
154 
155     gr::block_sptr src = gr::blocks::null_source::make(sizeof(int));
156     gr::block_sptr head = gr::blocks::head::make(sizeof(int), 100000);
157     gr::block_sptr dst = gr::blocks::null_sink::make(sizeof(int));
158 
159     // Start infinite flowgraph
160     tb->connect(src, 0, dst, 0);
161     tb->start(100);
162 
163     // Reconfigure with gr_head in the middle
164     tb->lock();
165     tb->disconnect(src, 0, dst, 0);
166     tb->connect(src, 0, head, 0);
167     tb->connect(head, 0, dst, 0);
168     tb->set_max_noutput_items(1000);
169     head->set_max_noutput_items(500);
170     tb->unlock();
171 
172     // Wait for flowgraph to end on its own
173     tb->wait();
174 }
175 
BOOST_AUTO_TEST_CASE(t7_max_noutputs_per_block)176 BOOST_AUTO_TEST_CASE(t7_max_noutputs_per_block)
177 {
178     if (VERBOSE)
179         std::cout << "qa_top_block::t7()\n";
180 
181     gr::top_block_sptr tb = gr::make_top_block("top");
182 
183     gr::block_sptr src = gr::blocks::null_source::make(sizeof(int));
184     gr::block_sptr head = gr::blocks::head::make(sizeof(int), 100000);
185     gr::block_sptr dst = gr::blocks::null_sink::make(sizeof(int));
186 
187     head->set_max_noutput_items(100);
188 
189     // Start infinite flowgraph
190     tb->connect(src, 0, head, 0);
191     tb->connect(head, 0, dst, 0);
192     tb->start();
193     tb->wait();
194 }
195 
BOOST_AUTO_TEST_CASE(t8_reconfig_max_noutputs_per_block)196 BOOST_AUTO_TEST_CASE(t8_reconfig_max_noutputs_per_block)
197 {
198     if (VERBOSE)
199         std::cout << "qa_top_block::t8()\n";
200 
201     gr::top_block_sptr tb = gr::make_top_block("top");
202 
203     gr::block_sptr src = gr::blocks::null_source::make(sizeof(int));
204     gr::block_sptr head = gr::blocks::head::make(sizeof(int), 100000);
205     gr::block_sptr dst = gr::blocks::null_sink::make(sizeof(int));
206 
207     head->set_max_noutput_items(99);
208 
209     // Start infinite flowgraph
210     tb->connect(src, 0, dst, 0);
211     tb->start(201);
212 
213     // Reconfigure with gr_head in the middle
214     tb->lock();
215     tb->disconnect(src, 0, dst, 0);
216     tb->connect(src, 0, head, 0);
217     tb->connect(head, 0, dst, 0);
218     tb->set_max_noutput_items(1023);
219     head->set_max_noutput_items(513);
220     tb->unlock();
221 
222     // Wait for flowgraph to end on its own
223     tb->wait();
224 }
225 
BOOST_AUTO_TEST_CASE(t9_max_output_buffer)226 BOOST_AUTO_TEST_CASE(t9_max_output_buffer)
227 {
228     if (VERBOSE)
229         std::cout << "qa_top_block::t9()\n";
230 
231     gr::top_block_sptr tb = gr::make_top_block("top");
232 
233     gr::block_sptr src = gr::blocks::null_source::make(sizeof(int));
234     gr::block_sptr head = gr::blocks::head::make(sizeof(int), 100000);
235     gr::block_sptr dst = gr::blocks::null_sink::make(sizeof(int));
236 
237     head->set_max_output_buffer(1024);
238 
239     // Start infinite flowgraph
240     tb->connect(src, 0, head, 0);
241     tb->connect(head, 0, dst, 0);
242     tb->start();
243     tb->wait();
244 }
245 
BOOST_AUTO_TEST_CASE(t10_reconfig_max_output_buffer)246 BOOST_AUTO_TEST_CASE(t10_reconfig_max_output_buffer)
247 {
248     if (VERBOSE)
249         std::cout << "qa_top_block::t10()\n";
250 
251     gr::top_block_sptr tb = gr::make_top_block("top");
252 
253     gr::block_sptr src = gr::blocks::null_source::make(sizeof(int));
254     gr::block_sptr head = gr::blocks::head::make(sizeof(int), 100000);
255     gr::block_sptr dst = gr::blocks::null_sink::make(sizeof(int));
256 
257     head->set_max_output_buffer(1000);
258 
259     // Start infinite flowgraph
260     tb->connect(src, 0, dst, 0);
261     tb->start(201);
262 
263     // Reconfigure with gr_head in the middle
264     tb->lock();
265     gr::block_sptr nop = gr::blocks::nop::make(sizeof(int));
266     nop->set_max_output_buffer(4000);
267     tb->disconnect(src, 0, dst, 0);
268     tb->connect(src, 0, head, 0);
269     tb->connect(head, 0, nop, 0);
270     tb->connect(nop, 0, dst, 0);
271     tb->unlock();
272 
273     // Wait for flowgraph to end on its own
274     tb->wait();
275 }
276 
BOOST_AUTO_TEST_CASE(t11_set_block_affinity)277 BOOST_AUTO_TEST_CASE(t11_set_block_affinity)
278 {
279     gr::top_block_sptr tb = gr::make_top_block("top");
280     gr::block_sptr src(gr::blocks::null_source::make(sizeof(float)));
281     gr::block_sptr snk(gr::blocks::null_sink::make(sizeof(float)));
282 
283     std::vector<int> set(1, 0), ret;
284     src->set_processor_affinity(set);
285 
286     tb->connect(src, 0, snk, 0);
287     tb->start();
288     tb->stop();
289     tb->wait();
290 
291     ret = src->processor_affinity();
292 
293     // We only set the core affinity to 0 because we always know at
294     // least one thread core exists to use.
295     BOOST_CHECK_EQUAL(set[0], ret[0]);
296 }
297 
BOOST_AUTO_TEST_CASE(t12_release_shared_pointers)298 BOOST_AUTO_TEST_CASE(t12_release_shared_pointers)
299 {
300     if (VERBOSE)
301         std::cout << "qa_top_block::t12()\n";
302 
303     gr::top_block_sptr tb = gr::make_top_block("top");
304 
305     gr::block_sptr src = gr::blocks::null_source::make(sizeof(int));
306     gr::block_sptr head = gr::blocks::head::make(sizeof(int), 100000);
307     gr::block_sptr dst = gr::blocks::null_sink::make(sizeof(int));
308 
309     tb->connect(src, 0, head, 0);
310     tb->connect(head, 0, dst, 0);
311 
312     tb->start();
313     tb->stop();
314     tb->wait();
315 
316     BOOST_CHECK(src.use_count() > 1);
317     BOOST_CHECK(head.use_count() > 1);
318     BOOST_CHECK(dst.use_count() > 1);
319 
320     tb->disconnect(src, 0, head, 0);
321     tb->disconnect(head, 0, dst, 0);
322 
323     BOOST_CHECK_EQUAL(1, src.use_count());
324     BOOST_CHECK_EQUAL(1, head.use_count());
325     BOOST_CHECK_EQUAL(1, dst.use_count());
326 }
327