Move sourcecode into 'senf/' directory
[senf.git] / senf / Packets / ListBParser.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@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 ListBParser unit tests */
25
26 //#include "ListBParser.test.hh"
27 //#include "ListBParser.test.ih"
28
29 // Custom includes
30 #include "Packets.hh"
31
32 #include "../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 namespace {
39     struct VoidPacket : public senf::PacketTypeBase
40     {};
41
42     struct VectorParser : public senf::PacketParserBase
43     {
44 #       include SENF_PARSER()
45
46         SENF_PARSER_PRIVATE_FIELD( size, senf::UInt8Parser );
47         SENF_PARSER_VECTOR( vec, size, senf::UInt16Parser );
48
49         SENF_PARSER_FINALIZE(VectorParser);
50     };
51
52     typedef senf::ListParser<
53         senf::detail::ListBParser_Policy<
54             VectorParser,
55             senf::detail::PrefixAuxParserPolicy<senf::UInt16Parser> > > MyListBParser;
56 }
57
58 BOOST_AUTO_UNIT_TEST(ListBParser)
59 {
60     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
61             MyListBParser::init_bytes));
62     
63     MyListBParser p (pi->data().begin(),&pi->data());
64     p.init();
65     BOOST_CHECK_EQUAL( p.size(), 0u );
66     BOOST_CHECK_EQUAL( p.bytes(), 2u );
67     BOOST_CHECK( p.empty() );
68
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 ...
71
72     p.push_back_space();
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() );
77 }
78
79 BOOST_AUTO_UNIT_TEST(ListBParser_container)
80 {
81     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
82             MyListBParser::init_bytes));
83     
84     {
85         MyListBParser::container c (MyListBParser(pi->data().begin(),&pi->data()));
86      
87         BOOST_CHECK_EQUAL( c.size(), 0u );
88         BOOST_CHECK_EQUAL( c.bytes(), 2u );
89         BOOST_CHECK( c.begin() == c.end() );
90         
91         c.shift(c.begin());
92         BOOST_CHECK_EQUAL( c.size(), 1u );
93         BOOST_CHECK_EQUAL( c.bytes(), 3u );
94
95         BOOST_CHECK_EQUAL( c.front().vec().size(), 0u );
96         c.front().vec().push_back(0x1234u);
97         BOOST_CHECK_EQUAL( c.bytes(), 5u );
98
99         {
100             senf::PacketInterpreterBase::ptr pi2 (senf::PacketInterpreter<VoidPacket>::create(
101                     MyListBParser::init_bytes));
102             MyListBParser::container c2 (MyListBParser(pi2->data().begin(),&pi2->data()));
103             c2.push_back_space();
104             {
105                 VectorParser::vec_t::container c2v (c2.front().vec());
106                 c2v.push_back(0x2345u);
107                 c2v.push_back(0x3456u);
108             }
109
110             BOOST_CHECK_EQUAL(c2.size(), 1u);
111             BOOST_CHECK_EQUAL(c2.bytes(), 7u);
112             
113             c.insert(c.end(),c2.back());
114             BOOST_CHECK_EQUAL( c.size(), 2u );
115             BOOST_CHECK_EQUAL( c.bytes(), 10u );
116             BOOST_CHECK_EQUAL( c.back().vec()[0], 0x2345u );
117             BOOST_CHECK_EQUAL( c.back().bytes(), c2.back().bytes() );
118
119             c2.back().vec()[0] << 0x1357u;
120             c.insert(boost::next(c.begin()), 2u, c2.back());
121             BOOST_CHECK_EQUAL( c.size(), 4u );
122             BOOST_CHECK_EQUAL( c.bytes(), 20u );
123             BOOST_CHECK_EQUAL( (*boost::next(c.begin())).vec()[0], 0x1357u ); 
124             BOOST_CHECK_EQUAL( (*boost::next(c.begin(),2)).vec()[0], 0x1357u );
125
126             c2.back().vec()[0] << 0x2468u;
127             c.insert(c.begin(),c2.begin(),c2.end());
128             BOOST_CHECK_EQUAL( c.size(), 5u );
129             BOOST_CHECK_EQUAL( c.bytes(), 25u );
130             BOOST_CHECK_EQUAL( c.front().vec()[0], 0x2468u );
131
132             c.erase(c.begin(),2);
133             BOOST_CHECK_EQUAL( c.size(), 3u );
134             BOOST_CHECK_EQUAL( c.bytes(), 17u );
135             BOOST_CHECK_EQUAL( c.front().vec()[0],0x1357u );
136             BOOST_CHECK_EQUAL( c.back().vec()[0], 0x2345u );
137             
138             c.erase((boost::next(c.begin(),2)),c.end());
139             BOOST_CHECK_EQUAL( c.size(), 2u );
140             BOOST_CHECK_EQUAL( c.bytes(), 12u );
141             BOOST_CHECK_EQUAL( c.front().vec()[0],0x1357u );
142             BOOST_CHECK_EQUAL( c.back().vec()[0], 0x1357u );
143
144             c.clear();
145             BOOST_CHECK_EQUAL( c.size(), 0u );
146             BOOST_CHECK_EQUAL( c.bytes(), 2u );
147        }
148     }
149 }
150
151 namespace {
152     
153     struct TestTransform
154     {
155         typedef unsigned value_type;
156         static unsigned get(unsigned v) { return v/2; }
157         static unsigned set(unsigned v) { return 2*v; }
158     };
159
160     struct TestListParser
161         : public senf::PacketParserBase
162     {
163 #       include SENF_PARSER()
164
165         SENF_PARSER_PRIVATE_FIELD ( size1 , senf::UInt8Parser );
166         SENF_PARSER_PRIVATE_FIELD ( size2 , senf::UInt8Parser );
167         SENF_PARSER_FIELD         ( dummy , senf::UInt32Parser );
168         SENF_PARSER_LIST          ( list1  , bytes(size1) , VectorParser );
169         SENF_PARSER_LIST          ( list2  , transform(TestTransform, bytes(size2)) , 
170                                              VectorParser );
171
172         SENF_PARSER_FINALIZE(TestListParser);
173     };
174
175 }
176
177 BOOST_AUTO_UNIT_TEST(listBytesMacro)
178 {
179     unsigned char data[] = {    8,                   // size1
180                                18,                   // size2
181                              0x01, 0x02, 0x03, 0x04, // dummy
182                              0x01,                   // list1()[0].size()
183                              0x05, 0x06,             // list1().vec()[0]
184                              0x02,                   // list1()[1].size()
185                              0x07, 0x08,             // list1()[1].vec()[0]
186                              0x09, 0x0A,             // list1()[1].vec()[1]
187                              0x00,                   // list2()[0].size()
188                              0x02,                   // list2()[1].size()
189                              0x0B, 0x0C,             // list2()[1].vec()[0]
190                              0x0D, 0x0E,             // list2()[1].vec()[1]
191                              0x01,                   // list2()[2].size()
192                              0x0F, 0x10 };           // list2()[2].vec()[0]
193     
194     senf::DataPacket p (senf::DataPacket::create(data));
195     TestListParser parser (p.data().begin(), &p.data());
196     
197     BOOST_CHECK_EQUAL( parser.list1().size(), 2u );
198     BOOST_CHECK_EQUAL( parser.list2().size(), 3u );
199     BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
200
201     TestListParser::list2_t::container list2 (parser.list2());
202
203     {
204         TestListParser::list1_t::container list (parser.list1());
205         BOOST_CHECK_EQUAL( list.size(), 2u );
206
207         TestListParser::list1_t::container::iterator i (list.begin());
208         BOOST_CHECK_EQUAL( i->vec().size(), 1u );
209         BOOST_CHECK_EQUAL( i->vec()[0], 0x0506u );
210
211         ++i;
212         BOOST_CHECK_EQUAL( i->vec().size(), 2u );
213         BOOST_CHECK_EQUAL( i->vec()[0], 0x0708u );
214         BOOST_CHECK_EQUAL( i->vec()[1], 0x090Au );
215         
216         ++i;
217         BOOST_CHECK( i == list.end() );
218     }
219
220     {
221         TestListParser::list2_t::container list (parser.list2());
222         BOOST_CHECK_EQUAL( list.size(), 3u );
223
224         TestListParser::list2_t::container::iterator i (list.begin());
225         BOOST_CHECK_EQUAL( i->vec().size(), 0u );
226
227         ++i;
228         BOOST_CHECK_EQUAL( i->vec().size(), 2u );
229         BOOST_CHECK_EQUAL( i->vec()[0], 0x0B0Cu );
230         BOOST_CHECK_EQUAL( i->vec()[1], 0x0D0Eu );
231         
232         ++i;
233         BOOST_CHECK_EQUAL( i->vec().size(), 1u );
234         BOOST_CHECK_EQUAL( i->vec()[0], 0x0F10u );
235         
236         ++i;
237         BOOST_CHECK( i == list.end() );
238     }
239
240 }
241
242 namespace {
243
244     struct TestPacketSizeList
245         : public senf::PacketParserBase
246     {
247 #       include SENF_PARSER()
248
249         SENF_PARSER_LIST ( list, packetSize(), VectorParser );
250
251         SENF_PARSER_FINALIZE(TestPacketSizeList);
252     };
253
254
255 }
256
257 BOOST_AUTO_UNIT_TEST(listBytesParser_packetSize)
258 {
259     unsigned char data[] = { 0x01,                   // list()[0].vec().size()
260                              0x05, 0x06,             // list()[0].vec()[0]
261                              0x02,                   // list()[1].vec().size()
262                              0x07, 0x08,             // list()[1].vec()[0]
263                              0x09, 0x0A,             // list()[1].vec()[1]
264                              0x00,                   // list()[2].vec().size()
265                              0x02,                   // list()[3].vec().size()
266                              0x0B, 0x0C,             // list()[3].vec()[0]
267                              0x0D, 0x0E,             // list()[3].vec()[1]
268                              0x01,                   // list()[4].vec().size()
269                              0x0F, 0x10 };           // list()[4].vec()[0]
270     
271     senf::DataPacket p (senf::DataPacket::create(data));
272
273     {
274         TestPacketSizeList l (p.data().begin(), &p.data());
275         BOOST_CHECK_EQUAL( l.list().size(), 5u );
276
277         TestPacketSizeList::list_t::container c (l.list());
278         TestPacketSizeList::list_t::container::iterator i (c.begin());
279
280         senf::UInt16Parser::value_type vec0[] = { 0x0506 };
281         senf::UInt16Parser::value_type vec1[] = { 0x0708, 0x090A };
282         senf::UInt16Parser::value_type vec2[] = {};
283         senf::UInt16Parser::value_type vec3[] = { 0x0B0C, 0x0D0E };
284         senf::UInt16Parser::value_type vec4[] = { 0x0F10 };
285
286         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
287                                        vec0, vec0+sizeof(vec0)/sizeof(vec0[0]) );
288         ++ i;
289         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
290                                        vec1, vec1+sizeof(vec1)/sizeof(vec1[0]) );
291         ++ i;
292         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
293                                        vec2, vec2+sizeof(vec2)/sizeof(vec2[0]) );
294         ++ i;
295         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
296                                        vec3, vec3+sizeof(vec3)/sizeof(vec3[0]) );
297
298         ++ i;
299         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
300                                        vec4, vec4+sizeof(vec4)/sizeof(vec4[0]) );
301
302         ++ i;
303         BOOST_CHECK( i == c.end() );
304
305         i = c.begin();
306         ++i;
307         TestPacketSizeList::list_t::value_type::vec_t::container v (i->vec());
308         v.push_back(0xFEFF);
309     }
310
311     {
312         TestPacketSizeList l (p.data().begin(), &p.data());
313         BOOST_CHECK_EQUAL( l.list().size(), 5u );
314         BOOST_CHECK_EQUAL( l.list().bytes(), p.data().size() );
315
316         TestPacketSizeList::list_t::container c (l.list());
317         TestPacketSizeList::list_t::container::iterator i (c.begin());
318
319         senf::UInt16Parser::value_type vec0[] = { 0x0506 };
320         senf::UInt16Parser::value_type vec1[] = { 0x0708, 0x090A, 0xFEFF };
321         senf::UInt16Parser::value_type vec2[] = {};
322         senf::UInt16Parser::value_type vec3[] = { 0x0B0C, 0x0D0E };
323         senf::UInt16Parser::value_type vec4[] = { 0x0F10 };
324
325         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
326                                        vec0, vec0+sizeof(vec0)/sizeof(vec0[0]) );
327         ++ i;
328         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
329                                        vec1, vec1+sizeof(vec1)/sizeof(vec1[0]) );
330         ++ i;
331         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
332                                        vec2, vec2+sizeof(vec2)/sizeof(vec2[0]) );
333         ++ i;
334         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
335                                        vec3, vec3+sizeof(vec3)/sizeof(vec3[0]) );
336
337         ++ i;
338         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
339                                        vec4, vec4+sizeof(vec4)/sizeof(vec4[0]) );
340         ++ i;
341         BOOST_CHECK( i == c.end() );
342     }
343 }
344
345 ///////////////////////////////cc.e////////////////////////////////////////
346 #undef prefix_
347
348 \f
349 // Local Variables:
350 // mode: c++
351 // fill-column: 100
352 // comment-column: 40
353 // c-file-style: "senf"
354 // indent-tabs-mode: nil
355 // ispell-local-dictionary: "american"
356 // compile-command: "scons -u test"
357 // End: