switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / PPI / Joins.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
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 Joins unit tests */
30
31 //#include "Joins.test.hh"
32 //#include "Joins.test.ih"
33
34 // Custom includes
35 #include "Joins.hh"
36 #include "DebugModules.hh"
37 #include "Setup.hh"
38 #include <senf/Packets/Packets.hh>
39
40 #include <senf/Utils/auto_unit_test.hh>
41 #include <boost/test/test_tools.hpp>
42
43 #define prefix_
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45
46 namespace ppi = senf::ppi;
47 namespace connector = ppi::connector;
48 namespace module = ppi::module;
49 namespace debug = module::debug;
50
51 namespace {
52
53     struct PassiveJoin : public module::PassiveJoin
54     {
55         using module::PassiveJoin::connectors;
56     };
57
58 }
59
60 SENF_AUTO_UNIT_TEST(passiveJoin)
61 {
62     debug::ActiveSource source1;
63     debug::ActiveSource source2;
64     PassiveJoin join;
65     debug::PassiveSink sink;
66
67     ppi::connect(source1, join);
68     ppi::connect(source2, join);
69     ppi::connect(join, sink);
70     ppi::init();
71
72     senf::Packet p (senf::DataPacket::create());
73
74     source1.submit(p);
75     BOOST_CHECK_EQUAL( sink.size(), 1u );
76     source2.submit(p);
77     BOOST_CHECK_EQUAL( sink.size(), 2u );
78
79     sink.input.throttle();
80     BOOST_CHECK( ! source1 );
81     BOOST_CHECK( ! source2 );
82
83     source1.submit(p);
84     source2.submit(p);
85     BOOST_CHECK_EQUAL( sink.size(), 2u );
86     sink.input.unthrottle();
87     BOOST_CHECK_EQUAL( sink.size(), 4u );
88
89     BOOST_CHECK_EQUAL( join.connectors().size(), 2u);
90     source1.output.disconnect();
91     BOOST_CHECK_EQUAL( join.connectors().size(), 1u);
92     ppi::connect(source1, join);
93     ppi::init();
94     BOOST_CHECK_EQUAL( join.connectors().size(), 2u);
95 }
96
97 SENF_AUTO_UNIT_TEST(priorityJoin)
98 {
99     debug::PassiveSource source1;
100     debug::PassiveSource source2;
101     module::PriorityJoin join;
102     debug::ActiveSink sink;
103
104     ppi::connect(source1, join);
105     ppi::connect(source2, join);
106     ppi::connect(join,sink);
107     ppi::init();
108
109     BOOST_CHECK( ! sink );
110
111     senf::Packet p1 (senf::DataPacket::create());
112     senf::Packet p2 (senf::DataPacket::create());
113
114     source1.submit(p1);
115     BOOST_CHECK( sink );
116     source2.submit(p2);
117     BOOST_CHECK( sink );
118     BOOST_CHECK( sink.request() == p1 );
119     BOOST_CHECK( sink );
120     BOOST_CHECK( sink.request() == p2 );
121     BOOST_CHECK( ! sink );
122
123     source1.submit(p1);
124     source2.submit(p2);
125     source1.submit(p1);
126     BOOST_CHECK( sink.request() == p1 );
127     BOOST_CHECK( sink.request() == p1 );
128     BOOST_CHECK( sink.request() == p2 );
129     BOOST_CHECK( ! sink );
130
131     debug::PassiveSource source3;
132     debug::PassiveSource source4;
133     ppi::connect(source3, join, 0);
134     ppi::connect(source4, join, -2);
135     // Ordering now: source3, source1, source4, source2
136
137     senf::Packet p3 (senf::DataPacket::create());
138     senf::Packet p4 (senf::DataPacket::create());
139
140     source4.submit(p4);
141     source3.submit(p3);
142     source2.submit(p2);
143     source1.submit(p1);
144     BOOST_CHECK( sink.request() == p3 );
145     BOOST_CHECK( sink.request() == p1 );
146     BOOST_CHECK( sink.request() == p4 );
147     BOOST_CHECK( sink.request() == p2 );
148     BOOST_CHECK( ! sink );
149 }
150
151 namespace {
152     struct ActiveJackSource
153     {
154         senf::ppi::connector::ActiveOutputJack<> output;
155
156         debug::ActiveSource source1;
157         debug::ActiveSource source2;
158
159         ActiveJackSource()
160             : output (source1.output) {}
161
162         void flip() {
163             output.reset( source2.output);
164         }
165     };
166 }
167
168 //SENF_AUTO_UNIT_TEST(jack_passiveJoin)
169 //{
170 //    ActiveJackSource jackSource;
171 //    PassiveJoin join;
172 //    debug::PassiveSink sink;
173 //
174 //    ppi::connect(jackSource, join);
175 //    ppi::connect(join, sink);
176 //    ppi::init();
177 //
178 //    senf::Packet p1 (senf::DataPacket::create());
179 //    senf::Packet p2 (senf::DataPacket::create());
180 //
181 //    jackSource.source1.submit( p1);
182 //    BOOST_CHECK_EQUAL( sink.pop_front(), p1);
183 //
184 //    jackSource.flip();
185 //
186 //    jackSource.source2.submit( p2);
187 //    BOOST_CHECK_EQUAL( sink.pop_front(), p2);
188 //}
189
190 //-/////////////////////////////////////////////////////////////////////////////////////////////////
191 #undef prefix_
192
193 \f
194 // Local Variables:
195 // mode: c++
196 // fill-column: 100
197 // comment-column: 40
198 // c-file-style: "senf"
199 // indent-tabs-mode: nil
200 // ispell-local-dictionary: "american"
201 // compile-command: "scons -u test"
202 // End: