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 ListBParser unit tests */
26 //#include "ListBParser.test.hh"
27 //#include "ListBParser.test.ih"
32 #include <senf/Utils/auto_unit_test.hh>
33 #include <boost/test/test_tools.hpp>
36 ///////////////////////////////cc.p////////////////////////////////////////
39 struct VoidPacket : public senf::PacketTypeBase
42 struct VectorParser : public senf::PacketParserBase
44 # include SENF_PARSER()
46 SENF_PARSER_PRIVATE_FIELD( size, senf::UInt8Parser );
47 SENF_PARSER_VECTOR( vec, size, senf::UInt16Parser );
49 SENF_PARSER_FINALIZE(VectorParser);
52 typedef senf::ListParser<
53 senf::detail::ListBParser_Policy<
55 senf::detail::PrefixAuxParserPolicy<senf::UInt16Parser> > > MyListBParser;
58 SENF_AUTO_UNIT_TEST(ListBParser)
60 senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
61 MyListBParser::init_bytes));
63 MyListBParser p (pi->data().begin(),&pi->data());
65 BOOST_CHECK_EQUAL( p.size(), 0u );
66 BOOST_CHECK_EQUAL( p.bytes(), 2u );
67 BOOST_CHECK( p.empty() );
69 // the mutators are really tested together with the container wrappers since they are based
70 // on the container wrapper. Here we only need one call to make the list larger ...
73 p = MyListBParser(pi->data().begin(),&pi->data());
74 BOOST_CHECK_EQUAL( p.bytes(), 3u );
75 BOOST_CHECK_EQUAL( p.size(), 1u );
76 BOOST_CHECK( ! p.empty() );
79 SENF_AUTO_UNIT_TEST(ListBParser_container)
81 senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
82 MyListBParser::init_bytes));
85 MyListBParser::container c (MyListBParser(pi->data().begin(),&pi->data()));
87 BOOST_CHECK_EQUAL( c.size(), 0u );
88 BOOST_CHECK_EQUAL( c.bytes(), 2u );
89 BOOST_CHECK( c.begin() == c.end() );
92 BOOST_CHECK_EQUAL( c.size(), 1u );
93 BOOST_CHECK_EQUAL( c.bytes(), 3u );
95 BOOST_CHECK_EQUAL( c.front().vec().size(), 0u );
96 c.front().vec().push_back(0x1234u);
97 BOOST_CHECK_EQUAL( c.bytes(), 5u );
100 senf::PacketInterpreterBase::ptr pi2 (senf::PacketInterpreter<VoidPacket>::create(
101 MyListBParser::init_bytes));
102 MyListBParser::container c2 (MyListBParser(pi2->data().begin(),&pi2->data()));
104 VectorParser::vec_t::container c2v (c2.push_back_space().vec());
105 c2v.push_back(0x2345u);
106 c2v.push_back(0x3456u);
109 BOOST_CHECK_EQUAL(c2.size(), 1u);
110 BOOST_CHECK_EQUAL(c2.bytes(), 7u);
112 c.insert(c.end(),c2.back());
113 BOOST_CHECK_EQUAL( c.size(), 2u );
114 BOOST_CHECK_EQUAL( c.bytes(), 10u );
115 BOOST_CHECK_EQUAL( c.back().vec()[0], 0x2345u );
116 BOOST_CHECK_EQUAL( c.back().bytes(), c2.back().bytes() );
118 c2.back().vec()[0] << 0x1357u;
119 c.insert(boost::next(c.begin()), 2u, c2.back());
120 BOOST_CHECK_EQUAL( c.size(), 4u );
121 BOOST_CHECK_EQUAL( c.bytes(), 20u );
122 BOOST_CHECK_EQUAL( (*boost::next(c.begin())).vec()[0], 0x1357u );
123 BOOST_CHECK_EQUAL( (*boost::next(c.begin(),2)).vec()[0], 0x1357u );
125 c2.back().vec()[0] << 0x2468u;
126 c.insert(c.begin(),c2.begin(),c2.end());
127 BOOST_CHECK_EQUAL( c.size(), 5u );
128 BOOST_CHECK_EQUAL( c.bytes(), 25u );
129 BOOST_CHECK_EQUAL( c.front().vec()[0], 0x2468u );
131 c.erase(c.begin(),2);
132 BOOST_CHECK_EQUAL( c.size(), 3u );
133 BOOST_CHECK_EQUAL( c.bytes(), 17u );
134 BOOST_CHECK_EQUAL( c.front().vec()[0],0x1357u );
135 BOOST_CHECK_EQUAL( c.back().vec()[0], 0x2345u );
137 c.erase((boost::next(c.begin(),2)),c.end());
138 BOOST_CHECK_EQUAL( c.size(), 2u );
139 BOOST_CHECK_EQUAL( c.bytes(), 12u );
140 BOOST_CHECK_EQUAL( c.front().vec()[0],0x1357u );
141 BOOST_CHECK_EQUAL( c.back().vec()[0], 0x1357u );
144 BOOST_CHECK_EQUAL( c.size(), 0u );
145 BOOST_CHECK_EQUAL( c.bytes(), 2u );
154 typedef unsigned value_type;
155 static unsigned get(unsigned v) { return v/2; }
156 static unsigned set(unsigned v) { return 2*v; }
159 struct TestListParser
160 : public senf::PacketParserBase
162 # include SENF_PARSER()
164 SENF_PARSER_PRIVATE_FIELD ( size1 , senf::UInt8Parser );
165 SENF_PARSER_PRIVATE_FIELD ( size2 , senf::UInt8Parser );
166 SENF_PARSER_FIELD ( dummy , senf::UInt32Parser );
167 SENF_PARSER_LIST ( list1 , bytes(size1) , VectorParser );
168 SENF_PARSER_LIST ( list2 , transform(TestTransform, bytes(size2)) ,
171 SENF_PARSER_FINALIZE(TestListParser);
176 SENF_AUTO_UNIT_TEST(listBytesMacro)
178 unsigned char data[] = { 8, // size1
180 0x01, 0x02, 0x03, 0x04, // dummy
181 0x01, // list1()[0].size()
182 0x05, 0x06, // list1().vec()[0]
183 0x02, // list1()[1].size()
184 0x07, 0x08, // list1()[1].vec()[0]
185 0x09, 0x0A, // list1()[1].vec()[1]
186 0x00, // list2()[0].size()
187 0x02, // list2()[1].size()
188 0x0B, 0x0C, // list2()[1].vec()[0]
189 0x0D, 0x0E, // list2()[1].vec()[1]
190 0x01, // list2()[2].size()
191 0x0F, 0x10 }; // list2()[2].vec()[0]
193 senf::DataPacket p (senf::DataPacket::create(data));
194 TestListParser parser (p.data().begin(), &p.data());
196 BOOST_CHECK_EQUAL( parser.list1().size(), 2u );
197 BOOST_CHECK_EQUAL( parser.list2().size(), 3u );
198 BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
200 TestListParser::list2_t::container list2 (parser.list2());
203 TestListParser::list1_t::container list (parser.list1());
204 BOOST_CHECK_EQUAL( list.size(), 2u );
206 TestListParser::list1_t::container::iterator i (list.begin());
207 BOOST_CHECK_EQUAL( i->vec().size(), 1u );
208 BOOST_CHECK_EQUAL( i->vec()[0], 0x0506u );
211 BOOST_CHECK_EQUAL( i->vec().size(), 2u );
212 BOOST_CHECK_EQUAL( i->vec()[0], 0x0708u );
213 BOOST_CHECK_EQUAL( i->vec()[1], 0x090Au );
216 BOOST_CHECK( i == list.end() );
220 TestListParser::list2_t::container list (parser.list2());
221 BOOST_CHECK_EQUAL( list.size(), 3u );
223 TestListParser::list2_t::container::iterator i (list.begin());
224 BOOST_CHECK_EQUAL( i->vec().size(), 0u );
227 BOOST_CHECK_EQUAL( i->vec().size(), 2u );
228 BOOST_CHECK_EQUAL( i->vec()[0], 0x0B0Cu );
229 BOOST_CHECK_EQUAL( i->vec()[1], 0x0D0Eu );
232 BOOST_CHECK_EQUAL( i->vec().size(), 1u );
233 BOOST_CHECK_EQUAL( i->vec()[0], 0x0F10u );
236 BOOST_CHECK( i == list.end() );
243 struct TestPacketSizeList
244 : public senf::PacketParserBase
246 # include SENF_PARSER()
248 SENF_PARSER_LIST ( list, packetSize(), VectorParser );
250 SENF_PARSER_FINALIZE(TestPacketSizeList);
256 SENF_AUTO_UNIT_TEST(listBytesParser_packetSize)
258 unsigned char data[] = { 0x01, // list()[0].vec().size()
259 0x05, 0x06, // list()[0].vec()[0]
260 0x02, // list()[1].vec().size()
261 0x07, 0x08, // list()[1].vec()[0]
262 0x09, 0x0A, // list()[1].vec()[1]
263 0x00, // list()[2].vec().size()
264 0x02, // list()[3].vec().size()
265 0x0B, 0x0C, // list()[3].vec()[0]
266 0x0D, 0x0E, // list()[3].vec()[1]
267 0x01, // list()[4].vec().size()
268 0x0F, 0x10 }; // list()[4].vec()[0]
270 senf::DataPacket p (senf::DataPacket::create(data));
273 TestPacketSizeList l (p.data().begin(), &p.data());
274 BOOST_CHECK_EQUAL( l.list().size(), 5u );
276 TestPacketSizeList::list_t::container c (l.list());
277 TestPacketSizeList::list_t::container::iterator i (c.begin());
279 senf::UInt16Parser::value_type vec0[] = { 0x0506 };
280 senf::UInt16Parser::value_type vec1[] = { 0x0708, 0x090A };
281 senf::UInt16Parser::value_type vec2[] = {};
282 senf::UInt16Parser::value_type vec3[] = { 0x0B0C, 0x0D0E };
283 senf::UInt16Parser::value_type vec4[] = { 0x0F10 };
285 BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
286 vec0, vec0+sizeof(vec0)/sizeof(vec0[0]) );
288 BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
289 vec1, vec1+sizeof(vec1)/sizeof(vec1[0]) );
291 BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
292 vec2, vec2+sizeof(vec2)/sizeof(vec2[0]) );
294 BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
295 vec3, vec3+sizeof(vec3)/sizeof(vec3[0]) );
298 BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
299 vec4, vec4+sizeof(vec4)/sizeof(vec4[0]) );
302 BOOST_CHECK( i == c.end() );
306 TestPacketSizeList::list_t::value_type::vec_t::container v (i->vec());
311 TestPacketSizeList l (p.data().begin(), &p.data());
312 BOOST_CHECK_EQUAL( l.list().size(), 5u );
313 BOOST_CHECK_EQUAL( l.list().bytes(), p.data().size() );
315 TestPacketSizeList::list_t::container c (l.list());
316 TestPacketSizeList::list_t::container::iterator i (c.begin());
318 senf::UInt16Parser::value_type vec0[] = { 0x0506 };
319 senf::UInt16Parser::value_type vec1[] = { 0x0708, 0x090A, 0xFEFF };
320 senf::UInt16Parser::value_type vec2[] = {};
321 senf::UInt16Parser::value_type vec3[] = { 0x0B0C, 0x0D0E };
322 senf::UInt16Parser::value_type vec4[] = { 0x0F10 };
324 BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
325 vec0, vec0+sizeof(vec0)/sizeof(vec0[0]) );
327 BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
328 vec1, vec1+sizeof(vec1)/sizeof(vec1[0]) );
330 BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
331 vec2, vec2+sizeof(vec2)/sizeof(vec2[0]) );
333 BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
334 vec3, vec3+sizeof(vec3)/sizeof(vec3[0]) );
337 BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
338 vec4, vec4+sizeof(vec4)/sizeof(vec4[0]) );
340 BOOST_CHECK( i == c.end() );
344 ///////////////////////////////cc.e////////////////////////////////////////
351 // comment-column: 40
352 // c-file-style: "senf"
353 // indent-tabs-mode: nil
354 // ispell-local-dictionary: "american"
355 // compile-command: "scons -u test"