switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / Examples / psi2tsModule / psi2ts.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
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 //   Thorsten Horstmann <tho@berlios.de>
27
28 /** \file
29     \brief psi2ts unit tests */
30
31 // Custom includes
32 #include <senf/PPI/DebugModules.hh>
33 #include <senf/PPI/Setup.hh>
34 #include <senf/Packets/MPEGDVBBundle/MPESection.hh>
35 #include <senf/Packets/MPEGDVBBundle/TransportPacket.hh>
36 #include "psi2ts.hh"
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40 #include <boost/lambda/lambda.hpp>
41
42 #define prefix_
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44
45 void check_transportpacket_header(senf::TransportPacket tsPacket, bool pusi, unsigned pid, unsigned counter)
46 {
47     BOOST_CHECK_EQUAL( tsPacket->sync_byte(),                 senf::TransportPacketType::SYNC_BYTE+0 );
48     BOOST_CHECK_EQUAL( tsPacket->transport_error_indicator(), false                                  );
49     BOOST_CHECK_EQUAL( tsPacket->pusi(),                      pusi                                   );
50     BOOST_CHECK_EQUAL( tsPacket->transport_priority(),        false                                  );
51     BOOST_CHECK_EQUAL( tsPacket->transport_scrmbl_ctrl(),     0x0u                                   );
52     BOOST_CHECK_EQUAL( tsPacket->adaptation_field_ctrl(),     0x1u                                   );
53     BOOST_CHECK_EQUAL( tsPacket->continuity_counter(),        counter                                );
54     if (pusi)
55         BOOST_CHECK_EQUAL( tsPacket->pointer_field(),         0x0u                                   );
56
57 }
58
59 template<class InputIterator, class T>
60 bool equal_elements(InputIterator first, InputIterator last, const T& value)
61 {
62     return std::find_if( first, last, boost::lambda::_1 != value) == last;
63 }
64
65
66 SENF_AUTO_UNIT_TEST(one_section_to_one_transportpacket)
67 {
68     senf::ppi::module::debug::ActiveSource source;
69     senf::ppi::module::debug::PassiveSink sink;
70     unsigned PID = 42;
71     Psi2TsModule psi2ts (PID);
72
73     senf::ppi::connect( source, psi2ts);
74     senf::ppi::connect( psi2ts, sink);
75     senf::ppi::init();
76
77     std::string sec_data ( "psi2ts_test: one_section_to_one_transportpacket");
78     senf::Packet sec_packet (senf::DataPacket::create(sec_data));
79     sec_packet.finalize();
80
81     source.submit(sec_packet);
82     BOOST_CHECK_EQUAL( sink.size(), 1u);
83
84     senf::TransportPacket ts_packet = sink.pop_front().as<senf::TransportPacket>();
85     check_transportpacket_header( ts_packet, true, PID, 1);
86     senf::PacketData & ts_payload_data = ts_packet.next().data();
87     BOOST_CHECK_EQUAL_COLLECTIONS(
88             ts_payload_data.begin(),
89             boost::next( ts_payload_data.begin(), sec_data.size()),
90             sec_data.begin(),
91             sec_data.end());
92     BOOST_CHECK( equal_elements(
93             boost::next( ts_payload_data.begin(), ts_payload_data.size()),
94             ts_payload_data.end(),
95             0xffu));
96 }
97
98 SENF_AUTO_UNIT_TEST(one_section_to_two_transportpackets)
99 {
100     senf::ppi::module::debug::ActiveSource source;
101     senf::ppi::module::debug::PassiveSink sink;
102     unsigned PID = 42;
103     Psi2TsModule psi2ts (PID);
104
105     senf::ppi::connect( source, psi2ts);
106     senf::ppi::connect( psi2ts, sink);
107     senf::ppi::init();
108
109     std::string sec_data ( 183, 0x42);
110     std::string sec_data2 ( "psi2ts_test: one_section_to_two_transportpackets");
111     sec_data.append( sec_data2);
112     senf::Packet sec_packet (senf::DataPacket::create(sec_data));
113     sec_packet.finalize();
114
115     source.submit( sec_packet);
116     BOOST_CHECK_EQUAL( sink.size(), 2u);
117
118     senf::TransportPacket ts_packet = sink.pop_front().as<senf::TransportPacket>();
119     check_transportpacket_header( ts_packet, true, PID, 1);
120     senf::PacketData & ts_payload_data1 = ts_packet.next().data();
121
122     BOOST_CHECK( equal_elements( ts_payload_data1.begin(), ts_payload_data1.end(), 0x42));
123
124     ts_packet = sink.pop_front().as<senf::TransportPacket>();
125     check_transportpacket_header( ts_packet, false, PID, 2);
126     senf::PacketData & ts_payload_data2 = ts_packet.next().data();
127     BOOST_CHECK_EQUAL_COLLECTIONS(
128             ts_payload_data2.begin(),
129             boost::next( ts_payload_data2.begin(), sec_data2.size()),
130             sec_data2.begin(),
131             sec_data2.end());
132     BOOST_CHECK( equal_elements(
133             boost::next( ts_payload_data2.begin(), sec_data2.size()),
134             ts_payload_data2.end(),
135             0xffu));
136 }
137
138 SENF_AUTO_UNIT_TEST(many_sections_to_many_transportpackets)
139 {
140     senf::ppi::module::debug::ActiveSource source;
141     senf::ppi::module::debug::PassiveSink sink;
142     unsigned PID = 42;
143     Psi2TsModule psi2ts (PID);
144
145     senf::ppi::connect( source, psi2ts);
146     senf::ppi::connect( psi2ts, sink);
147     senf::ppi::init();
148
149     std::string sec_data ( "many_sections_to_many_transportpackets");
150     senf::Packet sec_packet (senf::DataPacket::create(sec_data));
151     sec_packet.finalize();
152
153     unsigned NUMBER_OF_SECTIONS = 42u;
154     for (unsigned i=1; i<=NUMBER_OF_SECTIONS; i++) {
155         source.submit( sec_packet);
156     }
157     BOOST_CHECK_EQUAL( sink.size(), NUMBER_OF_SECTIONS);
158
159     for (unsigned i=1; i<=NUMBER_OF_SECTIONS; i++) {
160         senf::TransportPacket ts_packet = sink.pop_front().as<senf::TransportPacket>();
161         check_transportpacket_header( ts_packet, true, PID, i%16);
162     }
163 }
164
165 //-/////////////////////////////////////////////////////////////////////////////////////////////////
166 #undef prefix_
167
168 \f
169 // Local Variables:
170 // mode: c++
171 // fill-column: 100
172 // comment-column: 40
173 // c-file-style: "senf"
174 // indent-tabs-mode: nil
175 // ispell-local-dictionary: "american"
176 // compile-command: "scons -u test"
177 // End: