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 VectorParser unit tests */
26 //#include "VectorParser.test.hh"
27 //#include "VectorParser.test.ih"
32 #include <senf/Utils/auto_unit_test.hh>
33 #include <boost/test/test_tools.hpp>
34 #include <boost/assign.hpp>
35 #include <boost/foreach.hpp>
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41 struct VoidPacket : public senf::PacketTypeBase
45 SENF_AUTO_UNIT_TEST(VectorParser)
47 unsigned char data[] = { 0x03, // size
48 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, // data
49 0x20, 0x21, 0x22, 0x23, 0x24, 0x25 };
50 senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));
51 typedef senf::VectorParser<
53 senf::detail::FixedAuxParserPolicy<senf::UInt8Parser, 1u>
57 UInt16VectorParser v (boost::next(p->data().begin(), 1), &p->data());
59 BOOST_CHECK_EQUAL( v[0], 0x1011 );
60 BOOST_CHECK_EQUAL( v[2], 0x1415 );
61 BOOST_CHECK_EQUAL( v.size(), 3u );
62 BOOST_CHECK_EQUAL( v.bytes(), 6u );
63 BOOST_CHECK( ! v.empty() );
65 BOOST_CHECK_EQUAL( v.size(), 6u );
66 BOOST_CHECK_EQUAL( v.bytes(), 12u );
68 UInt16VectorParser::iterator b (v.begin());
69 UInt16VectorParser::iterator e (v.end());
70 BOOST_CHECK_EQUAL(std::distance(b,e), UInt16VectorParser::difference_type(v.size()));
73 // Warning: Each of the following operations invalidate the parser -> we need to recreate it at
76 // And since all these members directly call the corresponding members in the container wrapper,
77 // we don't need to check them again below ...
80 # define v UInt16VectorParser(boost::next(p->data().begin(),1),&p->data())
82 v.push_back(0xf0f1u,2);
83 BOOST_CHECK_EQUAL( v.size(), 8u );
84 BOOST_CHECK_EQUAL( v[7], 0xf0f1u );
86 v.push_back_space() = 37u;
87 BOOST_CHECK_EQUAL( v.size(), 9u );
88 BOOST_CHECK_EQUAL( v[8], 37u );
90 v.push_front(0xf3f4u);
91 BOOST_CHECK_EQUAL( v.size(), 10u );
92 BOOST_CHECK_EQUAL( v[0], 0xf3f4u );
93 BOOST_CHECK_EQUAL( v[1], 0x1011u );
95 v.push_front_space(2);
96 BOOST_CHECK_EQUAL( v.size(), 12u );
97 BOOST_CHECK_EQUAL( v[0], 0u );
98 BOOST_CHECK_EQUAL( v[1], 0u );
99 BOOST_CHECK_EQUAL( v[2], 0xf3f4u );
100 BOOST_CHECK_EQUAL( p->data().size(), 25u );
103 BOOST_CHECK_EQUAL( v.size(), 4u );
104 BOOST_CHECK_EQUAL( p->data().size(), 9u );
105 BOOST_CHECK_EQUAL( v[3], 0x1011u );
108 BOOST_CHECK_EQUAL( v.size(), 6u );
109 BOOST_CHECK_EQUAL( v[5], 0u );
112 BOOST_CHECK_EQUAL( v.size(), 8u );
113 BOOST_CHECK_EQUAL( p->data().size(), 17u );
114 BOOST_CHECK_EQUAL( v[6], 0xffffu );
120 SENF_AUTO_UNIT_TEST(VectorParser_wrapper)
122 unsigned char data[] = { 0x03, // size
123 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, // data
124 0x20, 0x21, 0x22, 0x23, 0x24, 0x25 };
125 senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));
126 typedef senf::VectorParser<
128 senf::detail::FixedAuxParserPolicy<senf::UInt8Parser, 1u>
129 > UInt16VectorParser;
130 UInt16VectorParser v (boost::next(p->data().begin(),1), &p->data());
131 UInt16VectorParser::container w (v);
133 BOOST_CHECK_EQUAL( w[0], 0x1011 );
134 BOOST_CHECK_EQUAL( w[2], 0x1415 );
135 BOOST_CHECK_EQUAL( w.size(), 3u );
137 BOOST_CHECK_EQUAL( w.size(), 6u );
138 BOOST_CHECK_EQUAL( std::distance(w.begin(),w.end()),
139 UInt16VectorParser::difference_type(w.size()) );
141 w.shift(w.begin()+1);
142 BOOST_CHECK_EQUAL( w.size(), 7u );
143 BOOST_CHECK_EQUAL( w[0], 0x1011 );
144 BOOST_CHECK_EQUAL( w[1], 0 );
145 BOOST_CHECK_EQUAL( w[2], 0x1213 );
147 w.insert(w.begin()+3, 2u, 0xfffe);
148 BOOST_CHECK_EQUAL( w.size(), 9u );
149 BOOST_CHECK_EQUAL( w[2], 0x1213 );
150 BOOST_CHECK_EQUAL( w[3], 0xfffe );
151 BOOST_CHECK_EQUAL( w[4], 0xfffe );
152 BOOST_CHECK_EQUAL( w[5], 0x1415 );
154 w.erase(w.begin()+3, w.begin()+5);
155 BOOST_CHECK_EQUAL( w.size(), 7u );
157 w.erase(w.begin()+1);
158 BOOST_CHECK_EQUAL( w.size(), 6u );
161 senf::PacketData::iterator i (p->data().begin()+1);
162 UInt16VectorParser::iterator j (w.begin());
163 UInt16VectorParser::iterator e (w.end());
164 for (;j!=e;++j, i+=2)
165 BOOST_CHECK_EQUAL( senf::UInt16Parser(i,&p->data()), *j );
166 BOOST_CHECK_EQUAL(p->data().end()-i, 0);
170 BOOST_CHECK_EQUAL( w.size(), 0u );
171 BOOST_CHECK( w.begin() == w.end() );
172 BOOST_CHECK_EQUAL( p->data().size(), 1u );
174 BOOST_CHECK_EQUAL( w.parser().size(), 0u );
177 SENF_AUTO_UNIT_TEST(dynamicPolicyVector)
179 unsigned char data[] = { 0x03, // size
180 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, // data
181 0x20, 0x21, 0x22, 0x23, 0x24, 0x25 };
182 senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));
184 typedef senf::VectorParser<
186 senf::detail::DynamicAuxParserPolicy<senf::UInt8Parser>
187 > UInt16VectorParser;
189 UInt16VectorParser v (senf::UInt8Parser(p->data().begin(), &p->data()),
190 boost::next(p->data().begin(),1), &p->data());
191 UInt16VectorParser::container w (v);
193 BOOST_CHECK_EQUAL( v.size(), 3u );
194 BOOST_CHECK_EQUAL( w.size(), 3u );
196 BOOST_CHECK_EQUAL( v[0], 0x1011 );
197 BOOST_CHECK_EQUAL( v[2], 0x1415 );
199 BOOST_CHECK_EQUAL( w[0], 0x1011 );
200 BOOST_CHECK_EQUAL( w[2], 0x1415 );
207 typedef unsigned value_type;
208 static unsigned get(unsigned v) { return v-2; }
209 static unsigned set(unsigned v) { return v+2; }
212 struct TestVectorParser
213 : public senf::PacketParserBase
215 # include SENF_PARSER()
217 SENF_PARSER_PRIVATE_FIELD ( size1 , senf::UInt8Parser );
218 SENF_PARSER_PRIVATE_FIELD ( size2 , senf::UInt8Parser );
219 SENF_PARSER_FIELD ( dummy , senf::UInt32Parser );
220 SENF_PARSER_VECTOR ( vec1 , transform(TestTransform, size1) , senf::UInt16Parser );
221 SENF_PARSER_VECTOR ( vec2 , bytes(size2) , senf::UInt16Parser );
223 SENF_PARSER_FINALIZE( TestVectorParser );
226 struct TestVectorPacketType
227 : public senf::PacketTypeBase,
228 public senf::PacketTypeMixin<TestVectorPacketType>
230 typedef senf::PacketTypeMixin<TestVectorPacketType> mixin;
231 typedef TestVectorParser parser;
233 using mixin::nextPacketRange;
235 using mixin::initSize;
237 typedef senf::ConcretePacket<TestVectorPacketType> TestVectorPacket;
241 SENF_AUTO_UNIT_TEST(vectorMacro_parse)
243 unsigned char data[] = { 0x05, // size1
245 0x01, 0x02, 0x03, 0x04, // dummy
246 0x05, 0x06, // vec1[0]
247 0x07, 0x08, // vec1[1]
248 0x09, 0x0A, // vec1[2]
249 0x0B, 0x0C, // vec2[0]
250 0x0D, 0x0E }; // vec2[1]
252 senf::DataPacket p (senf::DataPacket::create(data));
253 TestVectorParser parser (p.data().begin(), &p.data());
255 BOOST_CHECK_EQUAL( parser.vec1().size(), 3u );
256 BOOST_CHECK_EQUAL( parser.vec2().size(), 2u );
257 BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
258 BOOST_CHECK_EQUAL( parser.vec1()[0], 0x0506u );
259 BOOST_CHECK_EQUAL( parser.vec1()[1], 0x0708u );
260 BOOST_CHECK_EQUAL( parser.vec1()[2], 0x090Au );
261 BOOST_CHECK_EQUAL( parser.vec2()[0], 0x0B0Cu );
262 BOOST_CHECK_EQUAL( parser.vec2()[1], 0x0D0Eu );
266 SENF_AUTO_UNIT_TEST(vectorMacro_create)
268 TestVectorPacket p (TestVectorPacket::create());
269 p->dummy() = 0x01020304u;
270 p->vec1().push_back( 0x0506u);
271 p->vec1().push_back( 0x0708u);
272 p->vec1().push_back( 0x090Au);
273 p->vec2().push_back( 0x0B0Cu);
274 p->vec2().push_back( 0x0D0Eu);
277 unsigned char data[] = {
280 0x01, 0x02, 0x03, 0x04, // dummy
281 0x05, 0x06, // vec1[0]
282 0x07, 0x08, // vec1[1]
283 0x09, 0x0A, // vec1[2]
284 0x0B, 0x0C, // vec2[0]
285 0x0D, 0x0E // vec2[1]
287 SENF_CHECK_EQUAL_COLLECTIONS(
288 data, data+sizeof(data), p.data().begin(), p.data().end() );
293 struct TestVectorBaseParser
294 : public senf::PacketParserBase
296 # include SENF_PARSER()
298 SENF_PARSER_PRIVATE_FIELD ( size1 , senf::UInt8Parser );
299 SENF_PARSER_FIELD_RO ( size2 , senf::UInt8Parser );
300 SENF_PARSER_FIELD ( dummy , senf::UInt32Parser );\
302 SENF_PARSER_FINALIZE( TestVectorBaseParser );
305 struct TestVectorDerivedParser
306 : public TestVectorBaseParser
308 # include SENF_PARSER()
310 SENF_PARSER_INHERIT(TestVectorBaseParser);
312 SENF_PARSER_VECTOR ( vec1 , transform(TestTransform, size1) , senf::UInt8Parser );
313 SENF_PARSER_VECTOR ( vec2 , bytes(size2) , senf::UInt16Parser );
315 SENF_PARSER_FINALIZE( TestVectorDerivedParser );
320 SENF_AUTO_UNIT_TEST(vectorMacro_inherit)
322 unsigned char data[] = { 0x08, // size1
324 0x01, 0x02, 0x03, 0x04, // dummy
325 0x05, 0x06, // vec1[0], vec1[1]
326 0x07, 0x08, // vec1[2], vec1[3]
327 0x09, 0x0A, // vec1[4], vec1[5]
328 0x0B, 0x0C, // vec2[0]
329 0x0D, 0x0E }; // vec2[1]
331 senf::DataPacket p (senf::DataPacket::create(data));
332 TestVectorDerivedParser parser (p.data().begin(), &p.data());
334 BOOST_CHECK_EQUAL( parser.vec1().size(), 6u );
335 BOOST_CHECK_EQUAL( parser.vec2().size(), 2u );
336 BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
337 BOOST_CHECK_EQUAL( parser.vec2()[0], 0x0B0Cu );
338 BOOST_CHECK_EQUAL( parser.vec2()[1], 0x0D0Eu );
339 boost::uint8_t c = 0x05;
340 BOOST_FOREACH( boost::uint8_t v, parser.vec1() ) {
341 BOOST_CHECK_EQUAL( v, c++ );
347 struct TestPacketSizeVectorParser
348 : public senf::PacketParserBase
350 # include SENF_PARSER()
352 SENF_PARSER_VECTOR ( vec , packetSize() , senf::UInt16Parser );
354 SENF_PARSER_FINALIZE( TestPacketSizeVectorParser );
359 SENF_AUTO_UNIT_TEST(vectorMacro_packetSize)
361 unsigned char data[] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
362 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
364 senf::DataPacket p (senf::DataPacket::create(data));
365 TestPacketSizeVectorParser parser (p.data().begin(), &p.data());
368 BOOST_CHECK_EQUAL( parser.vec().size(), 6u );
369 BOOST_CHECK_EQUAL( parser.vec()[0], 0x1112u );
370 BOOST_CHECK_EQUAL( parser.vec()[1], 0x1314u );
371 BOOST_CHECK_EQUAL( parser.vec()[5], 0x2526u );
374 // The real functionality is already tested in AuxPolixy.test.cc ...
377 //-/////////////////////////////////////////////////////////////////////////////////////////////////
384 // c-file-style: "senf"
385 // indent-tabs-mode: nil
386 // ispell-local-dictionary: "american"
387 // compile-command: "scons -u test"
388 // comment-column: 40