9b8fabedd59070cf0e70ded042b1d9549a38641c
[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 <senf/Utils/auto_unit_test.hh>
34 #include <boost/test/test_tools.hpp>
35 #include <boost/lambda/lambda.hpp>
36
37 #define prefix_
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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     if (pusi)
50         BOOST_CHECK_EQUAL( tsPacket->pointer_field(),         0x0u                                   );
51
52 }
53
54 template<class InputIterator, class T>
55 bool equal_elements(InputIterator first, InputIterator last, const T& value)
56 {
57     return std::find_if( first, last, boost::lambda::_1 != value) == last;
58 }
59
60
61 SENF_AUTO_UNIT_TEST(one_section_to_one_transportpacket)
62 {
63     senf::ppi::module::debug::ActiveSource source;
64     senf::ppi::module::debug::PassiveSink sink;
65     unsigned PID = 42;
66     Psi2TsModule psi2ts (PID);
67
68     senf::ppi::connect( source, psi2ts);
69     senf::ppi::connect( psi2ts, sink);
70     senf::ppi::init();
71
72     std::string sec_data ( "psi2ts_test: one_section_to_one_transportpacket");
73     senf::Packet sec_packet (senf::DataPacket::create(sec_data));
74     sec_packet.finalize();
75
76     source.submit(sec_packet);
77     BOOST_CHECK_EQUAL( sink.size(), 1u);
78
79     senf::TransportPacket ts_packet = sink.pop_front().as<senf::TransportPacket>();
80     check_transportpacket_header( ts_packet, true, PID, 1);
81     senf::PacketData & ts_payload_data = ts_packet.next().data();
82     BOOST_CHECK_EQUAL_COLLECTIONS(
83             ts_payload_data.begin(),
84             boost::next( ts_payload_data.begin(), sec_data.size()),
85             sec_data.begin(),
86             sec_data.end());
87     BOOST_CHECK( equal_elements(
88             boost::next( ts_payload_data.begin(), ts_payload_data.size()),
89             ts_payload_data.end(),
90             0xffu));
91 }
92
93 SENF_AUTO_UNIT_TEST(one_section_to_two_transportpackets)
94 {
95     senf::ppi::module::debug::ActiveSource source;
96     senf::ppi::module::debug::PassiveSink sink;
97     unsigned PID = 42;
98     Psi2TsModule psi2ts (PID);
99
100     senf::ppi::connect( source, psi2ts);
101     senf::ppi::connect( psi2ts, sink);
102     senf::ppi::init();
103
104     std::string sec_data ( 183, 0x42);
105     std::string sec_data2 ( "psi2ts_test: one_section_to_two_transportpackets");
106     sec_data.append( sec_data2);
107     senf::Packet sec_packet (senf::DataPacket::create(sec_data));
108     sec_packet.finalize();
109
110     source.submit( sec_packet);
111     BOOST_CHECK_EQUAL( sink.size(), 2u);
112
113     senf::TransportPacket ts_packet = sink.pop_front().as<senf::TransportPacket>();
114     check_transportpacket_header( ts_packet, true, PID, 1);
115     senf::PacketData & ts_payload_data1 = ts_packet.next().data();
116
117     BOOST_CHECK( equal_elements( ts_payload_data1.begin(), ts_payload_data1.end(), 0x42));
118
119     ts_packet = sink.pop_front().as<senf::TransportPacket>();
120     check_transportpacket_header( ts_packet, false, PID, 2);
121     senf::PacketData & ts_payload_data2 = ts_packet.next().data();
122     BOOST_CHECK_EQUAL_COLLECTIONS(
123             ts_payload_data2.begin(),
124             boost::next( ts_payload_data2.begin(), sec_data2.size()),
125             sec_data2.begin(),
126             sec_data2.end());
127     BOOST_CHECK( equal_elements(
128             boost::next( ts_payload_data2.begin(), sec_data2.size()),
129             ts_payload_data2.end(),
130             0xffu));
131 }
132
133 SENF_AUTO_UNIT_TEST(many_sections_to_many_transportpackets)
134 {
135     senf::ppi::module::debug::ActiveSource source;
136     senf::ppi::module::debug::PassiveSink sink;
137     unsigned PID = 42;
138     Psi2TsModule psi2ts (PID);
139
140     senf::ppi::connect( source, psi2ts);
141     senf::ppi::connect( psi2ts, sink);
142     senf::ppi::init();
143
144     std::string sec_data ( "many_sections_to_many_transportpackets");
145     senf::Packet sec_packet (senf::DataPacket::create(sec_data));
146     sec_packet.finalize();
147
148     unsigned NUMBER_OF_SECTIONS = 42u;
149     for (unsigned i=1; i<=NUMBER_OF_SECTIONS; i++) {
150         source.submit( sec_packet);
151     }
152     BOOST_CHECK_EQUAL( sink.size(), NUMBER_OF_SECTIONS);
153
154     for (unsigned i=1; i<=NUMBER_OF_SECTIONS; i++) {
155         senf::TransportPacket ts_packet = sink.pop_front().as<senf::TransportPacket>();
156         check_transportpacket_header( ts_packet, true, PID, i%16);
157     }
158 }
159
160 //-/////////////////////////////////////////////////////////////////////////////////////////////////
161 #undef prefix_
162
163 \f
164 // Local Variables:
165 // mode: c++
166 // fill-column: 100
167 // comment-column: 40
168 // c-file-style: "senf"
169 // indent-tabs-mode: nil
170 // ispell-local-dictionary: "american"
171 // compile-command: "scons -u test"
172 // End: