4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief ListNParser.test unit tests */
26 //#include "ListNParser.test.hh"
27 //#include "ListNParser.test.ih"
32 #include "../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
36 ///////////////////////////////cc.p////////////////////////////////////////
39 struct VoidPacket_Type : public senf::PacketTypeBase
41 typedef senf::ConcretePacket<VoidPacket_Type> VoidPacket;
43 struct MyVec : public senf::PacketParserBase
45 # include SENF_PARSER()
47 SENF_PARSER_PRIVATE_FIELD( size, senf::UInt8Parser );
48 SENF_PARSER_VECTOR( vec, size, senf::UInt16Parser );
50 SENF_PARSER_FINALIZE(MyVec);
53 typedef senf::ListParser<
54 senf::detail::ListNParser_Policy<
56 senf::detail::PrefixAuxParserPolicy<
57 senf::UInt16Parser> > > MyListNParser;
60 BOOST_AUTO_UNIT_TEST(ListNParser_container)
62 VoidPacket vp (VoidPacket::create(MyListNParser::init_bytes));
63 MyListNParser(vp.data().begin(),&vp.data()).init();
66 MyListNParser::container c (MyListNParser(vp.data().begin(),&vp.data()));
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 );
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() );
82 BOOST_CHECK_EQUAL( std::distance(c.data().begin(), i->i()), 3 );
83 BOOST_CHECK( i != c.end() );
85 BOOST_CHECK( i == c.end() );
89 BOOST_AUTO_UNIT_TEST(ListNParser)
91 VoidPacket vp (VoidPacket::create(MyListNParser::init_bytes));
94 MyListNParser p (vp.data().begin(),&vp.data());
96 BOOST_CHECK_EQUAL( p.size(), 0u );
97 BOOST_CHECK_EQUAL( p.bytes(), 2u );
98 BOOST_CHECK( p.empty() );
102 # define p MyListNParser(vp.data().begin(),&vp.data())
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 );
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 );
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 );
124 BOOST_CHECK_EQUAL( p.size(), 2u );
125 BOOST_CHECK_EQUAL( p.bytes(), 8u );
126 BOOST_CHECK_EQUAL( p.back().vec().size(), 0u );
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 );
132 p.push_front_space(2u);
133 BOOST_CHECK_EQUAL( p.size(), 4u );
134 BOOST_CHECK_EQUAL( p.front().vec().size(), 0u);
137 BOOST_CHECK_EQUAL( p.size(), 3u );
138 BOOST_CHECK_EQUAL( p.back().vec()[0], 0x1234u );
139 BOOST_CHECK_EQUAL( p.bytes(), 9u );
149 typedef unsigned value_type;
150 static unsigned get(unsigned v) { return v/2; }
151 static unsigned set(unsigned v) { return 2*v; }
154 struct TestListParser
155 : public senf::PacketParserBase
157 # include SENF_PARSER()
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 );
165 SENF_PARSER_FINALIZE(TestListParser);
170 BOOST_AUTO_UNIT_TEST(listMacro)
172 unsigned char data[] = { 0x04, // size1
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]
187 senf::DataPacket p (senf::DataPacket::create(data));
188 TestListParser parser (p.data().begin(), &p.data());
190 BOOST_CHECK_EQUAL( parser.list1().size(), 2u );
191 BOOST_CHECK_EQUAL( parser.list2().size(), 3u );
192 BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
194 TestListParser::list2_t::container list2 (parser.list2());
197 TestListParser::list1_t::container list (parser.list1());
198 BOOST_CHECK_EQUAL( list.size(), 2u );
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 );
205 BOOST_CHECK_EQUAL( i->vec().size(), 2u );
206 BOOST_CHECK_EQUAL( i->vec()[0], 0x0708u );
207 BOOST_CHECK_EQUAL( i->vec()[1], 0x090Au );
210 BOOST_CHECK( i == list.end() );
214 TestListParser::list2_t::container list (parser.list2());
215 BOOST_CHECK_EQUAL( list.size(), 3u );
217 TestListParser::list2_t::container::iterator i (list.begin());
218 BOOST_CHECK_EQUAL( i->vec().size(), 0u );
221 BOOST_CHECK_EQUAL( i->vec().size(), 2u );
222 BOOST_CHECK_EQUAL( i->vec()[0], 0x0B0Cu );
223 BOOST_CHECK_EQUAL( i->vec()[1], 0x0D0Eu );
226 BOOST_CHECK_EQUAL( i->vec().size(), 1u );
227 BOOST_CHECK_EQUAL( i->vec()[0], 0x0F10u );
230 BOOST_CHECK( i == list.end() );
235 ///////////////////////////////cc.e////////////////////////////////////////
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"