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