4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief VectorParser unit tests */
31 //#include "VectorParser.test.hh"
32 //#include "VectorParser.test.ih"
37 #include <senf/Utils/auto_unit_test.hh>
38 #include <boost/test/test_tools.hpp>
39 #include <boost/assign.hpp>
40 #include <boost/foreach.hpp>
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46 struct VoidPacket : public senf::PacketTypeBase
50 SENF_AUTO_UNIT_TEST(VectorParser)
52 unsigned char data[] = { 0x03, // size
53 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, // data
54 0x20, 0x21, 0x22, 0x23, 0x24, 0x25 };
55 senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));
56 typedef senf::VectorParser<
58 senf::detail::FixedAuxParserPolicy<senf::UInt8Parser, 1u>
62 UInt16VectorParser v (boost::next(p->data().begin(), 1), &p->data());
64 BOOST_CHECK_EQUAL( v[0], 0x1011 );
65 BOOST_CHECK_EQUAL( v[2], 0x1415 );
66 BOOST_CHECK_EQUAL( v.size(), 3u );
67 BOOST_CHECK_EQUAL( v.bytes(), 6u );
68 BOOST_CHECK( ! v.empty() );
70 BOOST_CHECK_EQUAL( v.size(), 6u );
71 BOOST_CHECK_EQUAL( v.bytes(), 12u );
73 UInt16VectorParser::iterator b (v.begin());
74 UInt16VectorParser::iterator e (v.end());
75 BOOST_CHECK_EQUAL(std::distance(b,e), UInt16VectorParser::difference_type(v.size()));
78 // Warning: Each of the following operations invalidate the parser -> we need to recreate it at
81 // And since all these members directly call the corresponding members in the container wrapper,
82 // we don't need to check them again below ...
85 # define v UInt16VectorParser(boost::next(p->data().begin(),1),&p->data())
87 v.push_back(0xf0f1u,2);
88 BOOST_CHECK_EQUAL( v.size(), 8u );
89 BOOST_CHECK_EQUAL( v[7], 0xf0f1u );
91 v.push_back_space() = 37u;
92 BOOST_CHECK_EQUAL( v.size(), 9u );
93 BOOST_CHECK_EQUAL( v[8], 37u );
95 v.push_front(0xf3f4u);
96 BOOST_CHECK_EQUAL( v.size(), 10u );
97 BOOST_CHECK_EQUAL( v[0], 0xf3f4u );
98 BOOST_CHECK_EQUAL( v[1], 0x1011u );
100 v.push_front_space(2);
101 BOOST_CHECK_EQUAL( v.size(), 12u );
102 BOOST_CHECK_EQUAL( v[0], 0u );
103 BOOST_CHECK_EQUAL( v[1], 0u );
104 BOOST_CHECK_EQUAL( v[2], 0xf3f4u );
105 BOOST_CHECK_EQUAL( p->data().size(), 25u );
108 BOOST_CHECK_EQUAL( v.size(), 4u );
109 BOOST_CHECK_EQUAL( p->data().size(), 9u );
110 BOOST_CHECK_EQUAL( v[3], 0x1011u );
113 BOOST_CHECK_EQUAL( v.size(), 6u );
114 BOOST_CHECK_EQUAL( v[5], 0u );
117 BOOST_CHECK_EQUAL( v.size(), 8u );
118 BOOST_CHECK_EQUAL( p->data().size(), 17u );
119 BOOST_CHECK_EQUAL( v[6], 0xffffu );
125 SENF_AUTO_UNIT_TEST(VectorParser_wrapper)
127 unsigned char data[] = { 0x03, // size
128 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, // data
129 0x20, 0x21, 0x22, 0x23, 0x24, 0x25 };
130 senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));
131 typedef senf::VectorParser<
133 senf::detail::FixedAuxParserPolicy<senf::UInt8Parser, 1u>
134 > UInt16VectorParser;
135 UInt16VectorParser v (boost::next(p->data().begin(),1), &p->data());
136 UInt16VectorParser::container w (v);
138 BOOST_CHECK_EQUAL( w[0], 0x1011 );
139 BOOST_CHECK_EQUAL( w[2], 0x1415 );
140 BOOST_CHECK_EQUAL( w.size(), 3u );
142 BOOST_CHECK_EQUAL( w.size(), 6u );
143 BOOST_CHECK_EQUAL( std::distance(w.begin(),w.end()),
144 UInt16VectorParser::difference_type(w.size()) );
146 w.shift(w.begin()+1);
147 BOOST_CHECK_EQUAL( w.size(), 7u );
148 BOOST_CHECK_EQUAL( w[0], 0x1011 );
149 BOOST_CHECK_EQUAL( w[1], 0 );
150 BOOST_CHECK_EQUAL( w[2], 0x1213 );
152 w.insert(w.begin()+3, 2u, 0xfffe);
153 BOOST_CHECK_EQUAL( w.size(), 9u );
154 BOOST_CHECK_EQUAL( w[2], 0x1213 );
155 BOOST_CHECK_EQUAL( w[3], 0xfffe );
156 BOOST_CHECK_EQUAL( w[4], 0xfffe );
157 BOOST_CHECK_EQUAL( w[5], 0x1415 );
159 w.erase(w.begin()+3, w.begin()+5);
160 BOOST_CHECK_EQUAL( w.size(), 7u );
162 w.erase(w.begin()+1);
163 BOOST_CHECK_EQUAL( w.size(), 6u );
166 senf::PacketData::iterator i (p->data().begin()+1);
167 UInt16VectorParser::iterator j (w.begin());
168 UInt16VectorParser::iterator e (w.end());
169 for (;j!=e;++j, i+=2)
170 BOOST_CHECK_EQUAL( senf::UInt16Parser(i,&p->data()), *j );
171 BOOST_CHECK_EQUAL(p->data().end()-i, 0);
175 BOOST_CHECK_EQUAL( w.size(), 0u );
176 BOOST_CHECK( w.begin() == w.end() );
177 BOOST_CHECK_EQUAL( p->data().size(), 1u );
179 BOOST_CHECK_EQUAL( w.parser().size(), 0u );
182 SENF_AUTO_UNIT_TEST(dynamicPolicyVector)
184 unsigned char data[] = { 0x03, // size
185 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, // data
186 0x20, 0x21, 0x22, 0x23, 0x24, 0x25 };
187 senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));
189 typedef senf::VectorParser<
191 senf::detail::DynamicAuxParserPolicy<senf::UInt8Parser>
192 > UInt16VectorParser;
194 UInt16VectorParser v (senf::UInt8Parser(p->data().begin(), &p->data()),
195 boost::next(p->data().begin(),1), &p->data());
196 UInt16VectorParser::container w (v);
198 BOOST_CHECK_EQUAL( v.size(), 3u );
199 BOOST_CHECK_EQUAL( w.size(), 3u );
201 BOOST_CHECK_EQUAL( v[0], 0x1011 );
202 BOOST_CHECK_EQUAL( v[2], 0x1415 );
204 BOOST_CHECK_EQUAL( w[0], 0x1011 );
205 BOOST_CHECK_EQUAL( w[2], 0x1415 );
212 typedef unsigned value_type;
213 static unsigned get(unsigned v) { return v-2; }
214 static unsigned set(unsigned v) { return v+2; }
217 struct TestVectorParser
218 : public senf::PacketParserBase
220 # include SENF_PARSER()
222 SENF_PARSER_PRIVATE_FIELD ( size1 , senf::UInt8Parser );
223 SENF_PARSER_PRIVATE_FIELD ( size2 , senf::UInt8Parser );
224 SENF_PARSER_FIELD ( dummy , senf::UInt32Parser );
225 SENF_PARSER_VECTOR ( vec1 , transform(TestTransform, size1) , senf::UInt16Parser );
226 SENF_PARSER_VECTOR ( vec2 , bytes(size2) , senf::UInt16Parser );
228 SENF_PARSER_FINALIZE( TestVectorParser );
231 struct TestVectorPacketType
232 : public senf::PacketTypeBase,
233 public senf::PacketTypeMixin<TestVectorPacketType>
235 typedef senf::PacketTypeMixin<TestVectorPacketType> mixin;
236 typedef TestVectorParser parser;
238 using mixin::nextPacketRange;
240 using mixin::initSize;
242 typedef senf::ConcretePacket<TestVectorPacketType> TestVectorPacket;
246 SENF_AUTO_UNIT_TEST(vectorMacro_parse)
248 unsigned char data[] = { 0x05, // size1
250 0x01, 0x02, 0x03, 0x04, // dummy
251 0x05, 0x06, // vec1[0]
252 0x07, 0x08, // vec1[1]
253 0x09, 0x0A, // vec1[2]
254 0x0B, 0x0C, // vec2[0]
255 0x0D, 0x0E }; // vec2[1]
257 senf::DataPacket p (senf::DataPacket::create(data));
258 TestVectorParser parser (p.data().begin(), &p.data());
260 BOOST_CHECK_EQUAL( parser.vec1().size(), 3u );
261 BOOST_CHECK_EQUAL( parser.vec2().size(), 2u );
262 BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
263 BOOST_CHECK_EQUAL( parser.vec1()[0], 0x0506u );
264 BOOST_CHECK_EQUAL( parser.vec1()[1], 0x0708u );
265 BOOST_CHECK_EQUAL( parser.vec1()[2], 0x090Au );
266 BOOST_CHECK_EQUAL( parser.vec2()[0], 0x0B0Cu );
267 BOOST_CHECK_EQUAL( parser.vec2()[1], 0x0D0Eu );
271 SENF_AUTO_UNIT_TEST(vectorMacro_create)
273 TestVectorPacket p (TestVectorPacket::create());
274 p->dummy() = 0x01020304u;
275 p->vec1().push_back( 0x0506u);
276 p->vec1().push_back( 0x0708u);
277 p->vec1().push_back( 0x090Au);
278 p->vec2().push_back( 0x0B0Cu);
279 p->vec2().push_back( 0x0D0Eu);
282 unsigned char data[] = {
285 0x01, 0x02, 0x03, 0x04, // dummy
286 0x05, 0x06, // vec1[0]
287 0x07, 0x08, // vec1[1]
288 0x09, 0x0A, // vec1[2]
289 0x0B, 0x0C, // vec2[0]
290 0x0D, 0x0E // vec2[1]
292 SENF_CHECK_EQUAL_COLLECTIONS(
293 data, data+sizeof(data), p.data().begin(), p.data().end() );
298 struct TestVectorBaseParser
299 : public senf::PacketParserBase
301 # include SENF_PARSER()
303 SENF_PARSER_PRIVATE_FIELD ( size1 , senf::UInt8Parser );
304 SENF_PARSER_FIELD_RO ( size2 , senf::UInt8Parser );
305 SENF_PARSER_FIELD ( dummy , senf::UInt32Parser );\
307 SENF_PARSER_FINALIZE( TestVectorBaseParser );
310 struct TestVectorDerivedParser
311 : public TestVectorBaseParser
313 # include SENF_PARSER()
315 SENF_PARSER_INHERIT(TestVectorBaseParser);
317 SENF_PARSER_VECTOR ( vec1 , transform(TestTransform, size1) , senf::UInt8Parser );
318 SENF_PARSER_VECTOR ( vec2 , bytes(size2) , senf::UInt16Parser );
320 SENF_PARSER_FINALIZE( TestVectorDerivedParser );
325 SENF_AUTO_UNIT_TEST(vectorMacro_inherit)
327 unsigned char data[] = { 0x08, // size1
329 0x01, 0x02, 0x03, 0x04, // dummy
330 0x05, 0x06, // vec1[0], vec1[1]
331 0x07, 0x08, // vec1[2], vec1[3]
332 0x09, 0x0A, // vec1[4], vec1[5]
333 0x0B, 0x0C, // vec2[0]
334 0x0D, 0x0E }; // vec2[1]
336 senf::DataPacket p (senf::DataPacket::create(data));
337 TestVectorDerivedParser parser (p.data().begin(), &p.data());
339 BOOST_CHECK_EQUAL( parser.vec1().size(), 6u );
340 BOOST_CHECK_EQUAL( parser.vec2().size(), 2u );
341 BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
342 BOOST_CHECK_EQUAL( parser.vec2()[0], 0x0B0Cu );
343 BOOST_CHECK_EQUAL( parser.vec2()[1], 0x0D0Eu );
344 boost::uint8_t c = 0x05;
345 BOOST_FOREACH( boost::uint8_t v, parser.vec1() ) {
346 BOOST_CHECK_EQUAL( v, c++ );
352 struct TestPacketSizeVectorParser
353 : public senf::PacketParserBase
355 # include SENF_PARSER()
357 SENF_PARSER_VECTOR ( vec , packetSize() , senf::UInt16Parser );
359 SENF_PARSER_FINALIZE( TestPacketSizeVectorParser );
364 SENF_AUTO_UNIT_TEST(vectorMacro_packetSize)
366 unsigned char data[] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
367 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
369 senf::DataPacket p (senf::DataPacket::create(data));
370 TestPacketSizeVectorParser parser (p.data().begin(), &p.data());
373 BOOST_CHECK_EQUAL( parser.vec().size(), 6u );
374 BOOST_CHECK_EQUAL( parser.vec()[0], 0x1112u );
375 BOOST_CHECK_EQUAL( parser.vec()[1], 0x1314u );
376 BOOST_CHECK_EQUAL( parser.vec()[5], 0x2526u );
379 // The real functionality is already tested in AuxPolixy.test.cc ...
382 //-/////////////////////////////////////////////////////////////////////////////////////////////////
389 // c-file-style: "senf"
390 // indent-tabs-mode: nil
391 // ispell-local-dictionary: "american"
392 // compile-command: "scons -u test"
393 // comment-column: 40