4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief ListNParser unit tests */
26 //#include "ListNParser.test.hh"
27 //#include "ListNParser.test.ih"
32 #include <senf/Utils/auto_unit_test.hh>
33 #include <boost/test/test_tools.hpp>
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39 struct VoidPacket_Type : public senf::PacketTypeBase
41 typedef senf::ConcretePacket<VoidPacket_Type> VoidPacket;
43 struct MyVec : public senf::PacketParserBase
45 # include SENF_PARSER()
47 SENF_PARSER_PRIVATE_FIELD( size, senf::UInt8Parser );
48 SENF_PARSER_VECTOR( vec, size, senf::UInt16Parser );
50 SENF_PARSER_FINALIZE(MyVec);
52 typedef std::vector<boost::uint16_t> value_type;
54 value_type value() const {
55 value_type v (vec().begin(), vec().end());
58 void value(value_type const & v) {
59 vec_t::container container (vec());
61 for (value_type::const_iterator i=v.begin(); i!=v.end(); ++i)
62 container.push_back( *i);
64 operator value_type() const { return value(); }
65 MyVec const & operator= (value_type const & other) { value(other); return *this; }
68 typedef senf::ListParser<
69 senf::detail::ListNParser_Policy<
71 senf::detail::PrefixAuxParserPolicy<
72 senf::UInt16Parser> > > MyListNParser;
75 SENF_AUTO_UNIT_TEST(ListNParser_container)
77 VoidPacket vp (VoidPacket::create(MyListNParser::init_bytes));
78 MyListNParser(vp.data().begin(),&vp.data()).init();
81 MyListNParser::container c (MyListNParser(vp.data().begin(),&vp.data()));
83 c.push_back_space(2u);
84 BOOST_CHECK_EQUAL( std::distance(c.begin(), c.end()), 2 );
85 BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.begin().raw()), 2 );
86 BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.front().vec().begin().raw()), 3 );
87 BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.front().vec().end().raw()), 3 );
88 BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.back().vec().begin().raw()), 4 );
89 BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.back().vec().end().raw()), 4 );
90 BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.end().raw()), 4 );
91 BOOST_CHECK_EQUAL( c.bytes(), 4u );
93 MyListNParser::container::iterator i (c.begin());
94 BOOST_CHECK_EQUAL( std::distance(c.data().begin(), i->i()), 2 );
95 BOOST_CHECK( i != c.end() );
97 BOOST_CHECK_EQUAL( std::distance(c.data().begin(), i->i()), 3 );
98 BOOST_CHECK( i != c.end() );
100 BOOST_CHECK( i == c.end() );
104 SENF_AUTO_UNIT_TEST(ListNParser)
106 VoidPacket vp (VoidPacket::create(MyListNParser::init_bytes));
109 MyListNParser p (vp.data().begin(),&vp.data());
111 BOOST_CHECK_EQUAL( p.size(), 0u );
112 BOOST_CHECK_EQUAL( p.bytes(), 2u );
113 BOOST_CHECK( p.empty() );
117 # define p MyListNParser(vp.data().begin(),&vp.data())
120 BOOST_CHECK_EQUAL( p.bytes(), 3u );
121 BOOST_CHECK_EQUAL( p.size(), 1u );
122 BOOST_CHECK_EQUAL( p.front().bytes(), 1u );
123 BOOST_CHECK_EQUAL( p.front().vec().size(), 0u );
124 BOOST_CHECK_EQUAL( vp.data()[1], 0x01u );
126 p.front().vec().push_back(0x1234u);
127 BOOST_CHECK_EQUAL( p.front().vec().size(), 1u );
128 BOOST_CHECK_EQUAL( p.front().bytes(), 3u );
129 BOOST_CHECK_EQUAL( p.front().vec()[0], 0x1234u );
130 BOOST_CHECK_EQUAL( p.size(), 1u );
131 BOOST_CHECK_EQUAL( p.bytes(), 5u );
133 p.front().vec().push_back(0x2345u);
134 BOOST_CHECK_EQUAL( p.front().vec().back(), 0x2345u );
135 BOOST_CHECK_EQUAL( p.front().vec().size(), 2u );
136 BOOST_CHECK_EQUAL( p.bytes(), 7u );
139 BOOST_CHECK_EQUAL( p.size(), 2u );
140 BOOST_CHECK_EQUAL( p.bytes(), 8u );
141 BOOST_CHECK_EQUAL( p.back().vec().size(), 0u );
143 p.back().vec().push_front(0x0123u);
144 BOOST_CHECK_EQUAL( p.front().vec().size(), 2u );
145 BOOST_CHECK_EQUAL( p.back().vec().size(), 1u );
147 p.push_front_space(2u);
148 BOOST_CHECK_EQUAL( p.size(), 4u );
149 BOOST_CHECK_EQUAL( p.front().vec().size(), 0u);
152 BOOST_CHECK_EQUAL( p.size(), 3u );
153 BOOST_CHECK_EQUAL( p.back().vec()[0], 0x1234u );
154 BOOST_CHECK_EQUAL( p.bytes(), 9u );
164 typedef unsigned value_type;
165 static unsigned get(unsigned v) { return v/2; }
166 static unsigned set(unsigned v) { return 2*v; }
169 struct TestListParserBase
170 : public senf::PacketParserBase
172 # include SENF_PARSER()
174 SENF_PARSER_FIELD_RO ( size1 , senf::UInt8Parser );
175 SENF_PARSER_FIELD_RO ( size2 , senf::UInt8Parser );
177 SENF_PARSER_FINALIZE(TestListParserBase);
180 struct TestListParser
181 : public TestListParserBase
183 # include SENF_PARSER()
184 SENF_PARSER_INHERIT ( TestListParserBase );
186 SENF_PARSER_FIELD ( dummy , senf::UInt32Parser );
187 SENF_PARSER_LIST ( list1 , transform(TestTransform, size1) , MyVec );
188 SENF_PARSER_LIST ( list2 , size2 , MyVec );
190 SENF_PARSER_FINALIZE(TestListParser);
193 struct TestListPacketType
194 : public senf::PacketTypeBase,
195 public senf::PacketTypeMixin<TestListPacketType>
197 typedef senf::PacketTypeMixin<TestListPacketType> mixin;
198 typedef senf::ConcretePacket<TestListPacketType> packet;
199 typedef TestListParser parser;
201 using mixin::nextPacketRange;
202 using mixin::initSize;
205 typedef senf::ConcretePacket<TestListPacketType> TestListPacket;
209 SENF_AUTO_UNIT_TEST(listMacro)
211 unsigned char data[] = { 0x04, // size1
213 0x01, 0x02, 0x03, 0x04, // dummy
214 0x01, // list1()[0].size()
215 0x05, 0x06, // list1().vec()[0]
216 0x02, // list1()[1].size()
217 0x07, 0x08, // list1()[1].vec()[0]
218 0x09, 0x0A, // list1()[1].vec()[1]
219 0x00, // list2()[0].size()
220 0x02, // list2()[1].size()
221 0x0B, 0x0C, // list2()[1].vec()[0]
222 0x0D, 0x0E, // list2()[1].vec()[1]
223 0x01, // list2()[2].size()
224 0x0F, 0x10 }; // list2()[2].vec()[0]
226 senf::DataPacket p (senf::DataPacket::create(data));
227 TestListParser parser (p.data().begin(), &p.data());
229 BOOST_CHECK_EQUAL( parser.list1().size(), 2u );
230 BOOST_CHECK_EQUAL( parser.list2().size(), 3u );
231 BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
233 TestListParser::list2_t::container list2 (parser.list2());
236 TestListParser::list1_t::container list (parser.list1());
237 BOOST_CHECK_EQUAL( list.size(), 2u );
239 TestListParser::list1_t::container::iterator i (list.begin());
240 BOOST_CHECK_EQUAL( i->vec().size(), 1u );
241 BOOST_CHECK_EQUAL( i->vec()[0], 0x0506u );
244 BOOST_CHECK_EQUAL( i->vec().size(), 2u );
245 BOOST_CHECK_EQUAL( i->vec()[0], 0x0708u );
246 BOOST_CHECK_EQUAL( i->vec()[1], 0x090Au );
249 BOOST_CHECK( i == list.end() );
253 TestListParser::list2_t::container list (parser.list2());
254 BOOST_CHECK_EQUAL( list.size(), 3u );
256 TestListParser::list2_t::container::iterator i (list.begin());
257 BOOST_CHECK_EQUAL( i->vec().size(), 0u );
260 BOOST_CHECK_EQUAL( i->vec().size(), 2u );
261 BOOST_CHECK_EQUAL( i->vec()[0], 0x0B0Cu );
262 BOOST_CHECK_EQUAL( i->vec()[1], 0x0D0Eu );
265 BOOST_CHECK_EQUAL( i->vec().size(), 1u );
266 BOOST_CHECK_EQUAL( i->vec()[0], 0x0F10u );
269 BOOST_CHECK( i == list.end() );
273 SENF_AUTO_UNIT_TEST(listMacro_stress)
275 TestListPacket testListPacket (TestListPacket::create());
276 for (unsigned i=0; i<42; ++i) {
277 MyVec::value_type vec( 4, 42);
278 testListPacket->list2().push_back( vec);
280 BOOST_CHECK_EQUAL( testListPacket->list2().size(), 42u );
284 //-/////////////////////////////////////////////////////////////////////////////////////////////////
291 // comment-column: 40
292 // c-file-style: "senf"
293 // indent-tabs-mode: nil
294 // ispell-local-dictionary: "american"
295 // compile-command: "scons -u test"