98336ebcd0efb0afa31c12bce3aa311bfb71f29e
[senf.git] / senf / PPI / Jack.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Jack unit tests */
25
26 //#include "Jack.test.hh"
27 //#include "Jack.test.ih"
28
29 // Custom includes
30 #include "Jack.hh"
31 #include "DebugModules.hh"
32
33 #include <senf/Utils/auto_unit_test.hh>
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 namespace {
40
41     class ActiveDummyForward
42         : public senf::ppi::module::Module
43     {
44         SENF_PPI_MODULE(ActiveDummyForward);
45     public:
46         senf::ppi::connector::ActiveInput<> input;
47         senf::ppi::connector::PassiveOutput<> output;
48
49         ActiveDummyForward() : n (0)
50             { route(input, output); output.onRequest(&ActiveDummyForward::request); }
51
52         unsigned n;
53
54     private:
55         void request()
56             { ++n; output(input()); }
57     };
58
59     class PassiveDummyForward
60         : public senf::ppi::module::Module
61     {
62         SENF_PPI_MODULE(PassiveDummyForward);
63     public:
64         senf::ppi::connector::PassiveInput<> input;
65         senf::ppi::connector::ActiveOutput<> output;
66
67         PassiveDummyForward() : n (0)
68             { route(input, output); input.onRequest(&PassiveDummyForward::request); }
69
70         unsigned n;
71
72     private:
73         void request()
74             { ++n; output(input()); }
75     };
76
77     struct ActiveGroup
78     {
79         senf::ppi::connector::ActiveInputJack<> input;
80         senf::ppi::connector::PassiveOutputJack<> output;
81
82         ActiveGroup()
83             : input (forward1.input), output (forward1.output) {}
84
85         void flip()
86         {
87             input.reset(forward2.input);
88             output.reset(forward2.output);
89         }
90
91         ActiveDummyForward forward1;
92         ActiveDummyForward forward2;
93     };
94
95     struct PassiveGroup
96     {
97         senf::ppi::connector::PassiveInputJack<> input;
98         senf::ppi::connector::ActiveOutputJack<> output;
99
100         PassiveGroup()
101             : input (forward1.input), output (forward1.output) {}
102
103         void flip()
104         {
105             input.reset(forward2.input);
106             output.reset(forward2.output);
107         }
108
109         PassiveDummyForward forward1;
110         PassiveDummyForward forward2;
111     };
112
113 }
114
115 SENF_AUTO_UNIT_TEST(jacks)
116 {
117     {
118         ActiveGroup group;
119         senf::ppi::module::debug::PassiveSource source;
120         senf::ppi::module::debug::ActiveSink sink;
121
122         senf::ppi::connect(source, group);
123         senf::ppi::connect(group, sink);
124
125         senf::ppi::init();
126
127         {
128             senf::Packet p (senf::DataPacket::create());
129             source.submit(p);
130
131             BOOST_CHECK(p == sink.request());
132         }
133
134         group.flip();
135         senf::ppi::init();
136
137         {
138             senf::Packet p (senf::DataPacket::create());
139             source.submit(p);
140
141             BOOST_CHECK(p == sink.request());
142         }
143
144         BOOST_CHECK_EQUAL( group.forward1.n, 1u );
145         BOOST_CHECK_EQUAL( group.forward2.n, 1u );
146     }
147
148     {
149         PassiveGroup group;
150         senf::ppi::module::debug::ActiveSource source;
151         senf::ppi::module::debug::PassiveSink sink;
152
153         senf::ppi::connect(source, group);
154         senf::ppi::connect(group, sink);
155
156         senf::ppi::init();
157
158         {
159             senf::Packet p (senf::DataPacket::create());
160             source.submit(p);
161
162             BOOST_CHECK(p == sink.pop_front());
163         }
164
165         group.flip();
166         senf::ppi::init();
167
168         {
169             senf::Packet p (senf::DataPacket::create());
170             source.submit(p);
171
172             BOOST_CHECK(p == sink.pop_front());
173         }
174
175         BOOST_CHECK_EQUAL( group.forward1.n, 1u );
176         BOOST_CHECK_EQUAL( group.forward2.n, 1u );
177     }
178 }
179
180 //-/////////////////////////////////////////////////////////////////////////////////////////////////
181 #undef prefix_
182
183 \f
184 // Local Variables:
185 // mode: c++
186 // fill-column: 100
187 // comment-column: 40
188 // c-file-style: "senf"
189 // indent-tabs-mode: nil
190 // ispell-local-dictionary: "american"
191 // compile-command: "scons -u test"
192 // End: