switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / PPI / Jack.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Jack unit tests */
30
31 //#include "Jack.test.hh"
32 //#include "Jack.test.ih"
33
34 // Custom includes
35 #include "Jack.hh"
36 #include "DebugModules.hh"
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 namespace {
45
46     class ActiveDummyForward
47         : public senf::ppi::module::Module
48     {
49         SENF_PPI_MODULE(ActiveDummyForward);
50     public:
51         senf::ppi::connector::ActiveInput<> input;
52         senf::ppi::connector::PassiveOutput<> output;
53
54         ActiveDummyForward() : n (0)
55             { route(input, output); output.onRequest(&ActiveDummyForward::request); }
56
57         unsigned n;
58
59     private:
60         void request()
61             { ++n; output(input()); }
62     };
63
64     class PassiveDummyForward
65         : public senf::ppi::module::Module
66     {
67         SENF_PPI_MODULE(PassiveDummyForward);
68     public:
69         senf::ppi::connector::PassiveInput<> input;
70         senf::ppi::connector::ActiveOutput<> output;
71
72         PassiveDummyForward() : n (0)
73             { route(input, output); input.onRequest(&PassiveDummyForward::request); }
74
75         unsigned n;
76
77     private:
78         void request()
79             { ++n; output(input()); }
80     };
81
82     struct ActiveGroup
83     {
84         senf::ppi::connector::ActiveInputJack<> input;
85         senf::ppi::connector::PassiveOutputJack<> output;
86
87         ActiveGroup()
88             : input (forward1.input), output (forward1.output) {}
89
90         void flip()
91         {
92             input.reset(forward2.input);
93             output.reset(forward2.output);
94         }
95
96         ActiveDummyForward forward1;
97         ActiveDummyForward forward2;
98     };
99
100     struct PassiveGroup
101     {
102         senf::ppi::connector::PassiveInputJack<> input;
103         senf::ppi::connector::ActiveOutputJack<> output;
104
105         PassiveGroup()
106             : input (forward1.input), output (forward1.output) {}
107
108         void flip()
109         {
110             input.reset(forward2.input);
111             output.reset(forward2.output);
112         }
113
114         PassiveDummyForward forward1;
115         PassiveDummyForward forward2;
116     };
117
118 }
119
120 SENF_AUTO_UNIT_TEST(jacks)
121 {
122     {
123         ActiveGroup group;
124         senf::ppi::module::debug::PassiveSource source;
125         senf::ppi::module::debug::ActiveSink sink;
126
127         senf::ppi::connect(source, group);
128         senf::ppi::connect(group, sink);
129
130         senf::ppi::init();
131
132         {
133             senf::Packet p (senf::DataPacket::create());
134             source.submit(p);
135
136             BOOST_CHECK(p == sink.request());
137         }
138
139         group.flip();
140         senf::ppi::init();
141
142         {
143             senf::Packet p (senf::DataPacket::create());
144             source.submit(p);
145
146             BOOST_CHECK(p == sink.request());
147         }
148
149         BOOST_CHECK_EQUAL( group.forward1.n, 1u );
150         BOOST_CHECK_EQUAL( group.forward2.n, 1u );
151     }
152
153     {
154         PassiveGroup group;
155         senf::ppi::module::debug::ActiveSource source;
156         senf::ppi::module::debug::PassiveSink sink;
157
158         senf::ppi::connect(source, group);
159         senf::ppi::connect(group, sink);
160
161         senf::ppi::init();
162
163         {
164             senf::Packet p (senf::DataPacket::create());
165             source.submit(p);
166
167             BOOST_CHECK(p == sink.pop_front());
168         }
169
170         group.flip();
171         senf::ppi::init();
172
173         {
174             senf::Packet p (senf::DataPacket::create());
175             source.submit(p);
176
177             BOOST_CHECK(p == sink.pop_front());
178         }
179
180         BOOST_CHECK_EQUAL( group.forward1.n, 1u );
181         BOOST_CHECK_EQUAL( group.forward2.n, 1u );
182     }
183 }
184
185 //-/////////////////////////////////////////////////////////////////////////////////////////////////
186 #undef prefix_
187
188 \f
189 // Local Variables:
190 // mode: c++
191 // fill-column: 100
192 // comment-column: 40
193 // c-file-style: "senf"
194 // indent-tabs-mode: nil
195 // ispell-local-dictionary: "american"
196 // compile-command: "scons -u test"
197 // End: