f909cecbbe2ab335791360c5cc9f4b803eb391cf
[senf.git] / senf / Packets / ListNParser.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 ListNParser unit tests */
25
26 //#include "ListNParser.test.hh"
27 //#include "ListNParser.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 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 namespace {
39     struct VoidPacket_Type : public senf::PacketTypeBase
40     {};
41     typedef senf::ConcretePacket<VoidPacket_Type> VoidPacket;
42
43     struct MyVec : public senf::PacketParserBase
44     {
45 #       include SENF_PARSER()
46
47         SENF_PARSER_PRIVATE_FIELD( size, senf::UInt8Parser );
48         SENF_PARSER_VECTOR( vec, size, senf::UInt16Parser );
49
50         SENF_PARSER_FINALIZE(MyVec);
51
52         typedef std::vector<boost::uint16_t> value_type;
53
54         value_type value() const {
55             value_type v (vec().begin(), vec().end());
56             return v;
57         }
58         void value(value_type const & v) {
59             vec_t::container container (vec());
60             container.clear();
61             for (value_type::const_iterator i=v.begin(); i!=v.end(); ++i)
62                 container.push_back( *i);
63         }
64         operator value_type() const { return value(); }
65         MyVec const & operator= (value_type const & other) { value(other); return *this; }
66     };
67
68     typedef senf::ListParser<
69         senf::detail::ListNParser_Policy<
70             MyVec,
71             senf::detail::PrefixAuxParserPolicy<
72                 senf::UInt16Parser> > > MyListNParser;
73 }
74
75 SENF_AUTO_UNIT_TEST(ListNParser_container)
76 {
77     VoidPacket vp (VoidPacket::create(MyListNParser::init_bytes));
78     MyListNParser(vp.data().begin(),&vp.data()).init();
79
80     {
81         MyListNParser::container c (MyListNParser(vp.data().begin(),&vp.data()));
82
83         c.push_back_space(2u);
84         BOOST_CHECK_EQUAL( std::distance(c.begin(), c.end()), 2 );
85         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.begin().raw()), 2 );
86         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.front().vec().begin().raw()), 3 );
87         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.front().vec().end().raw()), 3 );
88         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.back().vec().begin().raw()), 4 );
89         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.back().vec().end().raw()), 4 );
90         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.end().raw()), 4 );
91         BOOST_CHECK_EQUAL( c.bytes(), 4u );
92
93         MyListNParser::container::iterator i (c.begin());
94         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), i->i()), 2 );
95         BOOST_CHECK( i != c.end() );
96         ++i;
97         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), i->i()), 3 );
98         BOOST_CHECK( i != c.end() );
99         ++i;
100         BOOST_CHECK( i == c.end() );
101     }
102 }
103
104 SENF_AUTO_UNIT_TEST(ListNParser)
105 {
106     VoidPacket vp (VoidPacket::create(MyListNParser::init_bytes));
107
108     {
109         MyListNParser p (vp.data().begin(),&vp.data());
110         p.init();
111         BOOST_CHECK_EQUAL( p.size(), 0u );
112         BOOST_CHECK_EQUAL( p.bytes(), 2u );
113         BOOST_CHECK( p.empty() );
114     }
115
116     {
117 #       define p MyListNParser(vp.data().begin(),&vp.data())
118
119         p.push_back_space();
120         BOOST_CHECK_EQUAL( p.bytes(), 3u );
121         BOOST_CHECK_EQUAL( p.size(), 1u );
122         BOOST_CHECK_EQUAL( p.front().bytes(), 1u );
123         BOOST_CHECK_EQUAL( p.front().vec().size(), 0u );
124         BOOST_CHECK_EQUAL( vp.data()[1], 0x01u );
125
126         p.front().vec().push_back(0x1234u);
127         BOOST_CHECK_EQUAL( p.front().vec().size(), 1u );
128         BOOST_CHECK_EQUAL( p.front().bytes(), 3u );
129         BOOST_CHECK_EQUAL( p.front().vec()[0], 0x1234u );
130         BOOST_CHECK_EQUAL( p.size(), 1u );
131         BOOST_CHECK_EQUAL( p.bytes(), 5u );
132
133         p.front().vec().push_back(0x2345u);
134         BOOST_CHECK_EQUAL( p.front().vec().back(), 0x2345u );
135         BOOST_CHECK_EQUAL( p.front().vec().size(), 2u );
136         BOOST_CHECK_EQUAL( p.bytes(), 7u );
137
138         p.push_back_space();
139         BOOST_CHECK_EQUAL( p.size(), 2u );
140         BOOST_CHECK_EQUAL( p.bytes(), 8u );
141         BOOST_CHECK_EQUAL( p.back().vec().size(), 0u );
142
143         p.back().vec().push_front(0x0123u);
144         BOOST_CHECK_EQUAL( p.front().vec().size(), 2u );
145         BOOST_CHECK_EQUAL( p.back().vec().size(), 1u );
146
147         p.push_front_space(2u);
148         BOOST_CHECK_EQUAL( p.size(), 4u );
149         BOOST_CHECK_EQUAL( p.front().vec().size(), 0u);
150
151         p.resize(3u);
152         BOOST_CHECK_EQUAL( p.size(), 3u );
153         BOOST_CHECK_EQUAL( p.back().vec()[0], 0x1234u );
154         BOOST_CHECK_EQUAL( p.bytes(), 9u );
155
156 #       undef p
157     }
158 }
159
160 namespace {
161
162     struct TestTransform
163     {
164         typedef unsigned value_type;
165         static unsigned get(unsigned v) { return v/2; }
166         static unsigned set(unsigned v) { return 2*v; }
167     };
168
169     struct TestListParserBase
170         : public senf::PacketParserBase
171     {
172 #       include SENF_PARSER()
173
174         SENF_PARSER_FIELD_RO ( size1 , senf::UInt8Parser );
175         SENF_PARSER_FIELD_RO ( size2 , senf::UInt8Parser );
176
177         SENF_PARSER_FINALIZE(TestListParserBase);
178     };
179
180     struct TestListParser
181         : public TestListParserBase
182     {
183 #       include SENF_PARSER()
184         SENF_PARSER_INHERIT ( TestListParserBase );
185
186         SENF_PARSER_FIELD   ( dummy , senf::UInt32Parser );
187         SENF_PARSER_LIST    ( list1  , transform(TestTransform, size1) , MyVec );
188         SENF_PARSER_LIST    ( list2  , size2 , MyVec );
189
190         SENF_PARSER_FINALIZE(TestListParser);
191     };
192
193     struct TestListPacketType
194         : public senf::PacketTypeBase,
195           public senf::PacketTypeMixin<TestListPacketType>
196     {
197         typedef senf::PacketTypeMixin<TestListPacketType> mixin;
198         typedef senf::ConcretePacket<TestListPacketType> packet;
199         typedef TestListParser parser;
200
201         using mixin::nextPacketRange;
202         using mixin::initSize;
203         using mixin::init;
204     };
205     typedef senf::ConcretePacket<TestListPacketType> TestListPacket;
206
207 }
208
209 SENF_AUTO_UNIT_TEST(listMacro)
210 {
211     unsigned char data[] = { 0x04,                   // size1
212                              0x03,                   // size2
213                              0x01, 0x02, 0x03, 0x04, // dummy
214                              0x01,                   // list1()[0].size()
215                              0x05, 0x06,             // list1().vec()[0]
216                              0x02,                   // list1()[1].size()
217                              0x07, 0x08,             // list1()[1].vec()[0]
218                              0x09, 0x0A,             // list1()[1].vec()[1]
219                              0x00,                   // list2()[0].size()
220                              0x02,                   // list2()[1].size()
221                              0x0B, 0x0C,             // list2()[1].vec()[0]
222                              0x0D, 0x0E,             // list2()[1].vec()[1]
223                              0x01,                   // list2()[2].size()
224                              0x0F, 0x10 };           // list2()[2].vec()[0]
225
226     senf::DataPacket p (senf::DataPacket::create(data));
227     TestListParser parser (p.data().begin(), &p.data());
228
229     BOOST_CHECK_EQUAL( parser.list1().size(), 2u );
230     BOOST_CHECK_EQUAL( parser.list2().size(), 3u );
231     BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
232
233     TestListParser::list2_t::container list2 (parser.list2());
234
235     {
236         TestListParser::list1_t::container list (parser.list1());
237         BOOST_CHECK_EQUAL( list.size(), 2u );
238
239         TestListParser::list1_t::container::iterator i (list.begin());
240         BOOST_CHECK_EQUAL( i->vec().size(), 1u );
241         BOOST_CHECK_EQUAL( i->vec()[0], 0x0506u );
242
243         ++i;
244         BOOST_CHECK_EQUAL( i->vec().size(), 2u );
245         BOOST_CHECK_EQUAL( i->vec()[0], 0x0708u );
246         BOOST_CHECK_EQUAL( i->vec()[1], 0x090Au );
247
248         ++i;
249         BOOST_CHECK( i == list.end() );
250     }
251
252     {
253         TestListParser::list2_t::container list (parser.list2());
254         BOOST_CHECK_EQUAL( list.size(), 3u );
255
256         TestListParser::list2_t::container::iterator i (list.begin());
257         BOOST_CHECK_EQUAL( i->vec().size(), 0u );
258
259         ++i;
260         BOOST_CHECK_EQUAL( i->vec().size(), 2u );
261         BOOST_CHECK_EQUAL( i->vec()[0], 0x0B0Cu );
262         BOOST_CHECK_EQUAL( i->vec()[1], 0x0D0Eu );
263
264         ++i;
265         BOOST_CHECK_EQUAL( i->vec().size(), 1u );
266         BOOST_CHECK_EQUAL( i->vec()[0], 0x0F10u );
267
268         ++i;
269         BOOST_CHECK( i == list.end() );
270     }
271 }
272
273 SENF_AUTO_UNIT_TEST(listMacro_stress)
274 {
275     TestListPacket testListPacket (TestListPacket::create());
276     for (unsigned i=0; i<42; ++i) {
277         MyVec::value_type vec( 4, 42);
278         testListPacket->list2().push_back( vec);
279     }
280     BOOST_CHECK_EQUAL( testListPacket->list2().size(), 42u );
281
282 }
283
284 //-/////////////////////////////////////////////////////////////////////////////////////////////////
285 #undef prefix_
286
287 \f
288 // Local Variables:
289 // mode: c++
290 // fill-column: 100
291 // comment-column: 40
292 // c-file-style: "senf"
293 // indent-tabs-mode: nil
294 // ispell-local-dictionary: "american"
295 // compile-command: "scons -u test"
296 // End: