3be4be6ccc2605e8b1b47af2306a3deb9aa7267c
[senf.git] / Packets / ListBParser.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 ListBParser.test unit tests */
25
26 //#include "ListBParser.test.hh"
27 //#include "ListBParser.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 : public senf::PacketTypeBase
40     {};
41
42     struct VectorParser : public senf::PacketParserBase
43     {
44 #       include SENF_PARSER()
45
46         SENF_PARSER_PRIVATE_FIELD( size, senf::UInt8Parser );
47         SENF_PARSER_VEC_N( vec, size, senf::UInt16Parser );
48
49         SENF_PARSER_FINALIZE(VectorParser);
50     };
51
52     typedef senf::ListBParser<VectorParser,senf::UInt16Parser>::parser MyListBParser;
53 }
54
55 BOOST_AUTO_UNIT_TEST(ListBParser)
56 {
57     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
58             MyListBParser::init_bytes));
59     
60     MyListBParser p (pi->data().begin(),&pi->data());
61     p.init();
62     BOOST_CHECK_EQUAL( p.size(), 0u );
63     BOOST_CHECK_EQUAL( p.bytes(), 2u );
64     BOOST_CHECK( p.empty() );
65     BOOST_CHECK( p.begin() == p.end() );
66
67     // the mutators are really tested together with the container wrappers since they are based
68     // on the container wrapper. Here we only need one call to make the list larger ...
69
70     p.push_back_space();
71     p = MyListBParser(pi->data().begin(),&pi->data());
72     BOOST_CHECK_EQUAL( p.bytes(), 3u );
73     BOOST_CHECK_EQUAL( p.size(), 1u );
74     BOOST_CHECK( ! p.empty() );
75     BOOST_CHECK( p.begin() != p.end() );
76 }
77
78 BOOST_AUTO_UNIT_TEST(ListBParser_container)
79 {
80     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
81             MyListBParser::init_bytes));
82     
83     {
84         MyListBParser::container c (MyListBParser(pi->data().begin(),&pi->data()));
85      
86         BOOST_CHECK_EQUAL( c.size(), 0u );
87         BOOST_CHECK_EQUAL( c.bytes(), 2u );
88         BOOST_CHECK( c.begin() == c.end() );
89         
90         c.shift(c.begin());
91         BOOST_CHECK_EQUAL( c.size(), 1u );
92         BOOST_CHECK_EQUAL( c.bytes(), 3u );
93
94         BOOST_CHECK_EQUAL( c.front().vec().size(), 0u );
95         c.front().vec().push_back(0x1234u);
96         BOOST_CHECK_EQUAL( c.bytes(), 5u );
97
98         {
99             senf::PacketInterpreterBase::ptr pi2 (senf::PacketInterpreter<VoidPacket>::create(
100                     MyListBParser::init_bytes));
101             MyListBParser::container c2 (MyListBParser(pi2->data().begin(),&pi2->data()));
102             c2.push_back_space();
103             {
104                 VectorParser::vec_t::container c2v (c2.front().vec());
105                 c2v.push_back(0x2345u);
106                 c2v.push_back(0x3456u);
107             }
108
109             BOOST_CHECK_EQUAL(c2.size(), 1u);
110             BOOST_CHECK_EQUAL(c2.bytes(), 7u);
111             
112             c.insert(c.end(),c2.back());
113             BOOST_CHECK_EQUAL( c.size(), 2u );
114             BOOST_CHECK_EQUAL( c.bytes(), 10u );
115             BOOST_CHECK_EQUAL( c.back().vec()[0], 0x2345u );
116             BOOST_CHECK_EQUAL( c.back().bytes(), c2.back().bytes() );
117
118             c2.back().vec()[0] << 0x1357u;
119             c.insert(boost::next(c.begin()), 2u, c2.back());
120             BOOST_CHECK_EQUAL( c.size(), 4u );
121             BOOST_CHECK_EQUAL( c.bytes(), 20u );
122             BOOST_CHECK_EQUAL( (*boost::next(c.begin())).vec()[0], 0x1357u ); 
123             BOOST_CHECK_EQUAL( (*boost::next(c.begin(),2)).vec()[0], 0x1357u );
124
125             c2.back().vec()[0] << 0x2468u;
126             c.insert(c.begin(),c2.begin(),c2.end());
127             BOOST_CHECK_EQUAL( c.size(), 5u );
128             BOOST_CHECK_EQUAL( c.bytes(), 25u );
129             BOOST_CHECK_EQUAL( c.front().vec()[0], 0x2468u );
130
131             c.erase(c.begin(),2);
132             BOOST_CHECK_EQUAL( c.size(), 3u );
133             BOOST_CHECK_EQUAL( c.bytes(), 17u );
134             BOOST_CHECK_EQUAL( c.front().vec()[0],0x1357u );
135             BOOST_CHECK_EQUAL( c.back().vec()[0], 0x2345u );
136             
137             c.erase((boost::next(c.begin(),2)),c.end());
138             BOOST_CHECK_EQUAL( c.size(), 2u );
139             BOOST_CHECK_EQUAL( c.bytes(), 12u );
140             BOOST_CHECK_EQUAL( c.front().vec()[0],0x1357u );
141             BOOST_CHECK_EQUAL( c.back().vec()[0], 0x1357u );
142
143             c.clear();
144             BOOST_CHECK_EQUAL( c.size(), 0u );
145             BOOST_CHECK_EQUAL( c.bytes(), 2u );
146        }
147     }
148 }
149
150 ///////////////////////////////cc.e////////////////////////////////////////
151 #undef prefix_
152
153 \f
154 // Local Variables:
155 // mode: c++
156 // fill-column: 100
157 // comment-column: 40
158 // c-file-style: "senf"
159 // indent-tabs-mode: nil
160 // ispell-local-dictionary: "american"
161 // compile-command: "scons -u test"
162 // End: