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