Packets: Update AuxParser interface
[senf.git] / 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.test unit tests */
25
26 //#include "ListNParser.test.hh"
27 //#include "ListNParser.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_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_VEC_N( vec, size, senf::UInt16Parser );
49         
50         SENF_PARSER_FINALIZE(MyVec);
51     };
52 }
53
54 BOOST_AUTO_UNIT_TEST(ListNParser_container)
55 {
56     typedef senf::ListNParser<MyVec,senf::UInt16Parser>::parser MyListNParser;
57     
58     VoidPacket vp (VoidPacket::create(MyListNParser::init_bytes));
59     MyListNParser(vp.data().begin(),&vp.data()).init();
60
61     {
62         MyListNParser::container c (MyListNParser(vp.data().begin(),&vp.data()));
63
64         c.push_back_space(2u);
65         BOOST_CHECK_EQUAL( std::distance(c.begin(), c.end()), 2 );
66         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.begin().raw()), 2 );
67         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.front().vec().begin().raw()), 3 );
68         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.front().vec().end().raw()), 3 );
69         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.back().vec().begin().raw()), 4 );
70         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.back().vec().end().raw()), 4 );
71         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), c.end().raw()), 4 );
72         BOOST_CHECK_EQUAL( c.bytes(), 4u );
73
74         MyListNParser::container::iterator i (c.begin());
75         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), i->i()), 2 );
76         BOOST_CHECK( i != c.end() );
77         ++i;
78         BOOST_CHECK_EQUAL( std::distance(c.data().begin(), i->i()), 3 );
79         BOOST_CHECK( i != c.end() );
80         ++i;
81         BOOST_CHECK( i == c.end() );
82     }
83 }
84
85 BOOST_AUTO_UNIT_TEST(ListNParser)
86 {
87     typedef senf::ListNParser<MyVec,senf::UInt16Parser>::parser MyListNParser;
88     
89     VoidPacket vp (VoidPacket::create(MyListNParser::init_bytes));
90
91     {
92         MyListNParser p (vp.data().begin(),&vp.data());
93         p.init();
94         BOOST_CHECK_EQUAL( p.size(), 0u );
95         BOOST_CHECK_EQUAL( p.bytes(), 2u );
96         BOOST_CHECK( p.empty() );
97     }
98
99     {
100 #       define p MyListNParser(vp.data().begin(),&vp.data())
101
102         p.push_back_space();
103         BOOST_CHECK_EQUAL( p.bytes(), 3u );
104         BOOST_CHECK_EQUAL( p.size(), 1u );
105         BOOST_CHECK_EQUAL( p.front().bytes(), 1u );
106         BOOST_CHECK_EQUAL( p.front().vec().size(), 0u );
107         BOOST_CHECK_EQUAL( vp.data()[1], 0x01u );
108
109         p.front().vec().push_back(0x1234u);
110         BOOST_CHECK_EQUAL( p.front().vec().size(), 1u );
111         BOOST_CHECK_EQUAL( p.front().bytes(), 3u );
112         BOOST_CHECK_EQUAL( p.front().vec()[0], 0x1234u );
113         BOOST_CHECK_EQUAL( p.size(), 1u );
114         BOOST_CHECK_EQUAL( p.bytes(), 5u );
115
116         p.front().vec().push_back(0x2345u);
117         BOOST_CHECK_EQUAL( p.front().vec().back(), 0x2345u );
118         BOOST_CHECK_EQUAL( p.front().vec().size(), 2u );
119         BOOST_CHECK_EQUAL( p.bytes(), 7u );
120
121         p.push_back_space();
122         BOOST_CHECK_EQUAL( p.size(), 2u );
123         BOOST_CHECK_EQUAL( p.bytes(), 8u );
124         BOOST_CHECK_EQUAL( p.back().vec().size(), 0u );
125         
126         p.back().vec().push_front(0x0123u);
127         BOOST_CHECK_EQUAL( p.front().vec().size(), 2u );
128         BOOST_CHECK_EQUAL( p.back().vec().size(), 1u );
129
130         p.push_front_space(2u);
131         BOOST_CHECK_EQUAL( p.size(), 4u );
132         BOOST_CHECK_EQUAL( p.front().vec().size(), 0u);
133         
134         p.resize(3u);
135         BOOST_CHECK_EQUAL( p.size(), 3u );
136         BOOST_CHECK_EQUAL( p.back().vec()[0], 0x1234u );
137         BOOST_CHECK_EQUAL( p.bytes(), 9u );
138
139 #       undef p
140     }
141 }
142
143 namespace {
144     
145     struct TestTransform
146     {
147         typedef unsigned value_type;
148         static unsigned get(unsigned v) { return v/2; }
149         static unsigned set(unsigned v) { return 2*v; }
150     };
151
152     struct TestListParser
153         : public senf::PacketParserBase
154     {
155 #       include SENF_PARSER()
156
157         SENF_PARSER_PRIVATE_FIELD ( size1 , senf::UInt8Parser );
158         SENF_PARSER_PRIVATE_FIELD ( size2 , senf::UInt8Parser );
159         SENF_PARSER_FIELD         ( dummy , senf::UInt32Parser );
160         SENF_PARSER_LIST          ( list1  , transform(TestTransform, size1) , MyVec );
161         SENF_PARSER_LIST          ( list2  , size2 , MyVec );
162
163         SENF_PARSER_FINALIZE(TestListParser);
164     };
165
166 }
167
168 BOOST_AUTO_UNIT_TEST(listMacro)
169 {
170     unsigned char data[] = { 0x04,                   // size1
171                              0x03,                   // size2
172                              0x01, 0x02, 0x03, 0x04, // dummy
173                              0x01,                   // list1()[0].size()
174                              0x05, 0x06,             // list1().vec()[0]
175                              0x02,                   // list1()[1].size()
176                              0x07, 0x08,             // list1()[1].vec()[0]
177                              0x09, 0x0A,             // list1()[1].vec()[1]
178                              0x00,                   // list2()[0].size()
179                              0x02,                   // list2()[1].size()
180                              0x0B, 0x0C,             // list2()[1].vec()[0]
181                              0x0D, 0x0E,             // list2()[1].vec()[1]
182                              0x01,                   // list2()[2].size()
183                              0x0F, 0x10 };           // list2()[2].vec()[0]
184     
185     senf::DataPacket p (senf::DataPacket::create(data));
186     TestListParser parser (p.data().begin(), &p.data());
187     
188     BOOST_CHECK_EQUAL( parser.list1().size(), 2u );
189     BOOST_CHECK_EQUAL( parser.list2().size(), 3u );
190     BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
191
192     TestListParser::list2_t::container list2 (parser.list2());
193
194     {
195         TestListParser::list1_t::container list (parser.list1());
196         BOOST_CHECK_EQUAL( list.size(), 2u );
197
198         TestListParser::list1_t::container::iterator i (list.begin());
199         BOOST_CHECK_EQUAL( i->vec().size(), 1u );
200         BOOST_CHECK_EQUAL( i->vec()[0], 0x0506u );
201
202         ++i;
203         BOOST_CHECK_EQUAL( i->vec().size(), 2u );
204         BOOST_CHECK_EQUAL( i->vec()[0], 0x0708u );
205         BOOST_CHECK_EQUAL( i->vec()[1], 0x090Au );
206         
207         ++i;
208         BOOST_CHECK( i == list.end() );
209     }
210
211     {
212         TestListParser::list2_t::container list (parser.list2());
213         BOOST_CHECK_EQUAL( list.size(), 3u );
214
215         TestListParser::list2_t::container::iterator i (list.begin());
216         BOOST_CHECK_EQUAL( i->vec().size(), 0u );
217
218         ++i;
219         BOOST_CHECK_EQUAL( i->vec().size(), 2u );
220         BOOST_CHECK_EQUAL( i->vec()[0], 0x0B0Cu );
221         BOOST_CHECK_EQUAL( i->vec()[1], 0x0D0Eu );
222         
223         ++i;
224         BOOST_CHECK_EQUAL( i->vec().size(), 1u );
225         BOOST_CHECK_EQUAL( i->vec()[0], 0x0F10u );
226         
227         ++i;
228         BOOST_CHECK( i == list.end() );
229     }
230
231 }
232
233 ///////////////////////////////cc.e////////////////////////////////////////
234 #undef prefix_
235
236 \f
237 // Local Variables:
238 // mode: c++
239 // fill-column: 100
240 // comment-column: 40
241 // c-file-style: "senf"
242 // indent-tabs-mode: nil
243 // ispell-local-dictionary: "american"
244 // compile-command: "scons -u test"
245 // End: