PPI: Checkin of first compiling (yet not working) version
[senf.git] / Packets / ParseListB.test.cc
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief ParseListB.test unit tests */
23
24 //#include "ParseListB.test.hh"
25 //#include "ParseListB.test.ih"
26
27 // Custom includes
28 #include "Packets.hh"
29
30 #include <boost/test/auto_unit_test.hpp>
31 #include <boost/test/test_tools.hpp>
32
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 namespace {
37     struct VoidPacket : public senf::PacketTypeBase
38     {};
39
40     typedef senf::Parse_VectorN<senf::Parse_UInt16,senf::Parse_UInt8>::parser ParseVec;
41     typedef senf::Parse_ListB<ParseVec,senf::Parse_UInt16>::parser ParseList;
42 }
43
44 BOOST_AUTO_UNIT_TEST(parseListB)
45 {
46     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
47                                              ParseList::init_bytes));
48     
49     ParseList p (pi->data().begin(),&pi->data());
50     p.init();
51     BOOST_CHECK_EQUAL( p.size(), 0u );
52     BOOST_CHECK_EQUAL( p.bytes(), 2u );
53     BOOST_CHECK( p.empty() );
54     BOOST_CHECK( p.begin() == p.end() );
55
56     // the mutators are really tested together with the container wrappers since they are based
57     // on the container wrapper. Here we only need one call to make the list larger ...
58
59     p.push_back_space();
60     p = ParseList(pi->data().begin(),&pi->data());
61     BOOST_CHECK_EQUAL( p.bytes(), 3u );
62     BOOST_CHECK_EQUAL( p.size(), 1u );
63     BOOST_CHECK( ! p.empty() );
64     BOOST_CHECK( p.begin() != p.end() );
65 }
66
67 BOOST_AUTO_UNIT_TEST(parseListB_container)
68 {
69     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
70                                              ParseList::init_bytes));
71     
72     {
73         ParseList::container c (ParseList(pi->data().begin(),&pi->data()));
74      
75         BOOST_CHECK_EQUAL( c.size(), 0u );
76         BOOST_CHECK_EQUAL( c.bytes(), 2u );
77         BOOST_CHECK( c.begin() == c.end() );
78         
79         c.shift(c.begin());
80         BOOST_CHECK_EQUAL( c.size(), 1u );
81         BOOST_CHECK_EQUAL( c.bytes(), 3u );
82
83         BOOST_CHECK_EQUAL( c.front().size(), 0u );
84         c.front().push_back(0x1234u);
85         BOOST_CHECK_EQUAL( c.bytes(), 5u );
86
87         {
88             senf::PacketInterpreterBase::ptr pi2 (senf::PacketInterpreter<VoidPacket>::create(
89                                                       ParseList::init_bytes));
90             ParseList::container c2 (ParseList(pi2->data().begin(),&pi2->data()));
91             c2.push_back_space();
92             {
93                 ParseVec::container c2v (c2.front());
94                 c2v.push_back(0x2345u);
95                 c2v.push_back(0x3456u);
96             }
97
98             BOOST_CHECK_EQUAL(c2.size(), 1u);
99             BOOST_CHECK_EQUAL(c2.bytes(), 7u);
100             
101             c.insert(c.end(),c2.back());
102             BOOST_CHECK_EQUAL( c.size(), 2u );
103             BOOST_CHECK_EQUAL( c.bytes(), 10u );
104             BOOST_CHECK_EQUAL( c.back()[0], 0x2345u );
105             BOOST_CHECK_EQUAL( c.back().bytes(), c2.back().bytes() );
106
107             c2.back()[0] << 0x1357u;
108             c.insert(boost::next(c.begin()), 2u, c2.back());
109             BOOST_CHECK_EQUAL( c.size(), 4u );
110             BOOST_CHECK_EQUAL( c.bytes(), 20u );
111             BOOST_CHECK_EQUAL( (*boost::next(c.begin()))[0], 0x1357u ); 
112             BOOST_CHECK_EQUAL( (*boost::next(c.begin(),2))[0], 0x1357u );
113
114             c2.back()[0] << 0x2468u;
115             c.insert(c.begin(),c2.begin(),c2.end());
116             BOOST_CHECK_EQUAL( c.size(), 5u );
117             BOOST_CHECK_EQUAL( c.bytes(), 25u );
118             BOOST_CHECK_EQUAL( c.front()[0], 0x2468u );
119
120             c.erase(c.begin(),2);
121             BOOST_CHECK_EQUAL( c.size(), 3u );
122             BOOST_CHECK_EQUAL( c.bytes(), 17u );
123             BOOST_CHECK_EQUAL( c.front()[0],0x1357u );
124             BOOST_CHECK_EQUAL( c.back()[0], 0x2345u );
125             
126             c.erase((boost::next(c.begin(),2)),c.end());
127             BOOST_CHECK_EQUAL( c.size(), 2u );
128             BOOST_CHECK_EQUAL( c.bytes(), 12u );
129             BOOST_CHECK_EQUAL( c.front()[0],0x1357u );
130             BOOST_CHECK_EQUAL( c.back()[0], 0x1357u );
131
132             c.clear();
133             BOOST_CHECK_EQUAL( c.size(), 0u );
134             BOOST_CHECK_EQUAL( c.bytes(), 2u );
135        }
136     }
137 }
138
139 ///////////////////////////////cc.e////////////////////////////////////////
140 #undef prefix_
141
142 \f
143 // Local Variables:
144 // mode: c++
145 // fill-column: 100
146 // comment-column: 40
147 // c-file-style: "senf"
148 // indent-tabs-mode: nil
149 // ispell-local-dictionary: "american"
150 // compile-command: "scons -u test"
151 // End: