Whitespce cleanup: Remove whitespace at end-on-line, remove tabs, wrap
[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 <senf/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 SENF_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 SENF_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             {
104                 VectorParser::vec_t::container c2v (c2.push_back_space().vec());
105                 c2v.push_back(0x2345u);
106                 c2v.push_back(0x3456u);
107             }
108
109             BOOST_CHECK_EQUAL(c2.size(), 1u);
110             BOOST_CHECK_EQUAL(c2.bytes(), 7u);
111
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() );
117
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 );
124
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 );
130
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 );
136
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 );
142
143             c.clear();
144             BOOST_CHECK_EQUAL( c.size(), 0u );
145             BOOST_CHECK_EQUAL( c.bytes(), 2u );
146        }
147     }
148 }
149
150 namespace {
151
152     struct TestTransform
153     {
154         typedef unsigned value_type;
155         static unsigned get(unsigned v) { return v/2; }
156         static unsigned set(unsigned v) { return 2*v; }
157     };
158
159     struct TestListParser
160         : public senf::PacketParserBase
161     {
162 #       include SENF_PARSER()
163
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)) ,
169                                              VectorParser );
170
171         SENF_PARSER_FINALIZE(TestListParser);
172     };
173
174 }
175
176 SENF_AUTO_UNIT_TEST(listBytesMacro)
177 {
178     unsigned char data[] = {    8,                   // size1
179                                18,                   // size2
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]
192
193     senf::DataPacket p (senf::DataPacket::create(data));
194     TestListParser parser (p.data().begin(), &p.data());
195
196     BOOST_CHECK_EQUAL( parser.list1().size(), 2u );
197     BOOST_CHECK_EQUAL( parser.list2().size(), 3u );
198     BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
199
200     TestListParser::list2_t::container list2 (parser.list2());
201
202     {
203         TestListParser::list1_t::container list (parser.list1());
204         BOOST_CHECK_EQUAL( list.size(), 2u );
205
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 );
209
210         ++i;
211         BOOST_CHECK_EQUAL( i->vec().size(), 2u );
212         BOOST_CHECK_EQUAL( i->vec()[0], 0x0708u );
213         BOOST_CHECK_EQUAL( i->vec()[1], 0x090Au );
214
215         ++i;
216         BOOST_CHECK( i == list.end() );
217     }
218
219     {
220         TestListParser::list2_t::container list (parser.list2());
221         BOOST_CHECK_EQUAL( list.size(), 3u );
222
223         TestListParser::list2_t::container::iterator i (list.begin());
224         BOOST_CHECK_EQUAL( i->vec().size(), 0u );
225
226         ++i;
227         BOOST_CHECK_EQUAL( i->vec().size(), 2u );
228         BOOST_CHECK_EQUAL( i->vec()[0], 0x0B0Cu );
229         BOOST_CHECK_EQUAL( i->vec()[1], 0x0D0Eu );
230
231         ++i;
232         BOOST_CHECK_EQUAL( i->vec().size(), 1u );
233         BOOST_CHECK_EQUAL( i->vec()[0], 0x0F10u );
234
235         ++i;
236         BOOST_CHECK( i == list.end() );
237     }
238
239 }
240
241 namespace {
242
243     struct TestPacketSizeList
244         : public senf::PacketParserBase
245     {
246 #       include SENF_PARSER()
247
248         SENF_PARSER_LIST ( list, packetSize(), VectorParser );
249
250         SENF_PARSER_FINALIZE(TestPacketSizeList);
251     };
252
253
254 }
255
256 SENF_AUTO_UNIT_TEST(listBytesParser_packetSize)
257 {
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]
269
270     senf::DataPacket p (senf::DataPacket::create(data));
271
272     {
273         TestPacketSizeList l (p.data().begin(), &p.data());
274         BOOST_CHECK_EQUAL( l.list().size(), 5u );
275
276         TestPacketSizeList::list_t::container c (l.list());
277         TestPacketSizeList::list_t::container::iterator i (c.begin());
278
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 };
284
285         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
286                                        vec0, vec0+sizeof(vec0)/sizeof(vec0[0]) );
287         ++ i;
288         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
289                                        vec1, vec1+sizeof(vec1)/sizeof(vec1[0]) );
290         ++ i;
291         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
292                                        vec2, vec2+sizeof(vec2)/sizeof(vec2[0]) );
293         ++ i;
294         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
295                                        vec3, vec3+sizeof(vec3)/sizeof(vec3[0]) );
296
297         ++ i;
298         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
299                                        vec4, vec4+sizeof(vec4)/sizeof(vec4[0]) );
300
301         ++ i;
302         BOOST_CHECK( i == c.end() );
303
304         i = c.begin();
305         ++i;
306         TestPacketSizeList::list_t::value_type::vec_t::container v (i->vec());
307         v.push_back(0xFEFF);
308     }
309
310     {
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() );
314
315         TestPacketSizeList::list_t::container c (l.list());
316         TestPacketSizeList::list_t::container::iterator i (c.begin());
317
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 };
323
324         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
325                                        vec0, vec0+sizeof(vec0)/sizeof(vec0[0]) );
326         ++ i;
327         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
328                                        vec1, vec1+sizeof(vec1)/sizeof(vec1[0]) );
329         ++ i;
330         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
331                                        vec2, vec2+sizeof(vec2)/sizeof(vec2[0]) );
332         ++ i;
333         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
334                                        vec3, vec3+sizeof(vec3)/sizeof(vec3[0]) );
335
336         ++ i;
337         BOOST_CHECK_EQUAL_COLLECTIONS( i->vec().begin(), i->vec().end(),
338                                        vec4, vec4+sizeof(vec4)/sizeof(vec4[0]) );
339         ++ i;
340         BOOST_CHECK( i == c.end() );
341     }
342 }
343
344 ///////////////////////////////cc.e////////////////////////////////////////
345 #undef prefix_
346
347 \f
348 // Local Variables:
349 // mode: c++
350 // fill-column: 100
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"
356 // End: