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