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 ListBParser.test unit tests */
26 //#include "ListBParser.test.hh"
27 //#include "ListBParser.test.ih"
32 #include "../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
36 ///////////////////////////////cc.p////////////////////////////////////////
39 struct VoidPacket : public senf::PacketTypeBase
42 struct VectorParser : public senf::PacketParserBase
44 # include SENF_PARSER()
46 SENF_PARSER_PRIVATE_FIELD( size, senf::UInt8Parser );
47 SENF_PARSER_VEC_N( vec, size, senf::UInt16Parser );
49 SENF_PARSER_FINALIZE(VectorParser);
52 typedef senf::ListBParser<VectorParser,senf::UInt16Parser>::parser MyListBParser;
55 BOOST_AUTO_UNIT_TEST(ListBParser)
57 senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
58 MyListBParser::init_bytes));
60 MyListBParser p (pi->data().begin(),&pi->data());
62 BOOST_CHECK_EQUAL( p.size(), 0u );
63 BOOST_CHECK_EQUAL( p.bytes(), 2u );
64 BOOST_CHECK( p.empty() );
66 // the mutators are really tested together with the container wrappers since they are based
67 // on the container wrapper. Here we only need one call to make the list larger ...
70 p = MyListBParser(pi->data().begin(),&pi->data());
71 BOOST_CHECK_EQUAL( p.bytes(), 3u );
72 BOOST_CHECK_EQUAL( p.size(), 1u );
73 BOOST_CHECK( ! p.empty() );
76 BOOST_AUTO_UNIT_TEST(ListBParser_container)
78 senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
79 MyListBParser::init_bytes));
82 MyListBParser::container c (MyListBParser(pi->data().begin(),&pi->data()));
84 BOOST_CHECK_EQUAL( c.size(), 0u );
85 BOOST_CHECK_EQUAL( c.bytes(), 2u );
86 BOOST_CHECK( c.begin() == c.end() );
89 BOOST_CHECK_EQUAL( c.size(), 1u );
90 BOOST_CHECK_EQUAL( c.bytes(), 3u );
92 BOOST_CHECK_EQUAL( c.front().vec().size(), 0u );
93 c.front().vec().push_back(0x1234u);
94 BOOST_CHECK_EQUAL( c.bytes(), 5u );
97 senf::PacketInterpreterBase::ptr pi2 (senf::PacketInterpreter<VoidPacket>::create(
98 MyListBParser::init_bytes));
99 MyListBParser::container c2 (MyListBParser(pi2->data().begin(),&pi2->data()));
100 c2.push_back_space();
102 VectorParser::vec_t::container c2v (c2.front().vec());
103 c2v.push_back(0x2345u);
104 c2v.push_back(0x3456u);
107 BOOST_CHECK_EQUAL(c2.size(), 1u);
108 BOOST_CHECK_EQUAL(c2.bytes(), 7u);
110 c.insert(c.end(),c2.back());
111 BOOST_CHECK_EQUAL( c.size(), 2u );
112 BOOST_CHECK_EQUAL( c.bytes(), 10u );
113 BOOST_CHECK_EQUAL( c.back().vec()[0], 0x2345u );
114 BOOST_CHECK_EQUAL( c.back().bytes(), c2.back().bytes() );
116 c2.back().vec()[0] << 0x1357u;
117 c.insert(boost::next(c.begin()), 2u, c2.back());
118 BOOST_CHECK_EQUAL( c.size(), 4u );
119 BOOST_CHECK_EQUAL( c.bytes(), 20u );
120 BOOST_CHECK_EQUAL( (*boost::next(c.begin())).vec()[0], 0x1357u );
121 BOOST_CHECK_EQUAL( (*boost::next(c.begin(),2)).vec()[0], 0x1357u );
123 c2.back().vec()[0] << 0x2468u;
124 c.insert(c.begin(),c2.begin(),c2.end());
125 BOOST_CHECK_EQUAL( c.size(), 5u );
126 BOOST_CHECK_EQUAL( c.bytes(), 25u );
127 BOOST_CHECK_EQUAL( c.front().vec()[0], 0x2468u );
129 c.erase(c.begin(),2);
130 BOOST_CHECK_EQUAL( c.size(), 3u );
131 BOOST_CHECK_EQUAL( c.bytes(), 17u );
132 BOOST_CHECK_EQUAL( c.front().vec()[0],0x1357u );
133 BOOST_CHECK_EQUAL( c.back().vec()[0], 0x2345u );
135 c.erase((boost::next(c.begin(),2)),c.end());
136 BOOST_CHECK_EQUAL( c.size(), 2u );
137 BOOST_CHECK_EQUAL( c.bytes(), 12u );
138 BOOST_CHECK_EQUAL( c.front().vec()[0],0x1357u );
139 BOOST_CHECK_EQUAL( c.back().vec()[0], 0x1357u );
142 BOOST_CHECK_EQUAL( c.size(), 0u );
143 BOOST_CHECK_EQUAL( c.bytes(), 2u );
152 typedef unsigned value_type;
153 static unsigned get(unsigned v) { return v/2; }
154 static unsigned set(unsigned v) { return 2*v; }
157 struct TestListParser
158 : public senf::PacketParserBase
160 # include SENF_PARSER()
162 SENF_PARSER_PRIVATE_FIELD ( size1 , senf::UInt8Parser );
163 SENF_PARSER_PRIVATE_FIELD ( size2 , senf::UInt8Parser );
164 SENF_PARSER_FIELD ( dummy , senf::UInt32Parser );
165 SENF_PARSER_LIST ( list1 , bytes(size1) , VectorParser );
166 SENF_PARSER_LIST ( list2 , transform(TestTransform, bytes(size2)) ,
169 SENF_PARSER_FINALIZE(TestListParser);
174 BOOST_AUTO_UNIT_TEST(listBytesMacro)
176 unsigned char data[] = { 8, // size1
178 0x01, 0x02, 0x03, 0x04, // dummy
179 0x01, // list1()[0].size()
180 0x05, 0x06, // list1().vec()[0]
181 0x02, // list1()[1].size()
182 0x07, 0x08, // list1()[1].vec()[0]
183 0x09, 0x0A, // list1()[1].vec()[1]
184 0x00, // list2()[0].size()
185 0x02, // list2()[1].size()
186 0x0B, 0x0C, // list2()[1].vec()[0]
187 0x0D, 0x0E, // list2()[1].vec()[1]
188 0x01, // list2()[2].size()
189 0x0F, 0x10 }; // list2()[2].vec()[0]
191 senf::DataPacket p (senf::DataPacket::create(data));
192 TestListParser parser (p.data().begin(), &p.data());
194 BOOST_CHECK_EQUAL( parser.list1().size(), 2u );
195 BOOST_CHECK_EQUAL( parser.list2().size(), 3u );
196 BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
198 TestListParser::list2_t::container list2 (parser.list2());
201 TestListParser::list1_t::container list (parser.list1());
202 BOOST_CHECK_EQUAL( list.size(), 2u );
204 TestListParser::list1_t::container::iterator i (list.begin());
205 BOOST_CHECK_EQUAL( i->vec().size(), 1u );
206 BOOST_CHECK_EQUAL( i->vec()[0], 0x0506u );
209 BOOST_CHECK_EQUAL( i->vec().size(), 2u );
210 BOOST_CHECK_EQUAL( i->vec()[0], 0x0708u );
211 BOOST_CHECK_EQUAL( i->vec()[1], 0x090Au );
214 BOOST_CHECK( i == list.end() );
218 TestListParser::list2_t::container list (parser.list2());
219 BOOST_CHECK_EQUAL( list.size(), 3u );
221 TestListParser::list2_t::container::iterator i (list.begin());
222 BOOST_CHECK_EQUAL( i->vec().size(), 0u );
225 BOOST_CHECK_EQUAL( i->vec().size(), 2u );
226 BOOST_CHECK_EQUAL( i->vec()[0], 0x0B0Cu );
227 BOOST_CHECK_EQUAL( i->vec()[1], 0x0D0Eu );
230 BOOST_CHECK_EQUAL( i->vec().size(), 1u );
231 BOOST_CHECK_EQUAL( i->vec()[0], 0x0F10u );
234 BOOST_CHECK( i == list.end() );
240 ///////////////////////////////cc.e////////////////////////////////////////
247 // comment-column: 40
248 // c-file-style: "senf"
249 // indent-tabs-mode: nil
250 // ispell-local-dictionary: "american"
251 // compile-command: "scons -u test"