Merged revisions 262,264-265,267-282,284-298,300-311 via svnmerge from
[senf.git] / Packets / ParseVec.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 // Unit tests
24
25 //#include "ParseVec.test.hh"
26 //#include "ParseVec.test.ih"
27
28 // Custom includes
29 #include "ParseVec.hh"
30 #include "ParseInt.hh"
31 #include "PacketType.hh"
32
33 #include <boost/test/auto_unit_test.hpp>
34 #include <boost/test/test_tools.hpp>
35 #include <boost/assign.hpp>
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 namespace {
41     struct VoidPacket : public senf::PacketTypeBase
42     {};
43 }
44
45 BOOST_AUTO_UNIT_TEST(parseVec)
46 {
47     unsigned char data[] = { 0x03,                                   // size
48                              0x10, 0x11,  0x12, 0x13,  0x14, 0x15,   // data
49                              0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
50     senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));
51     typedef senf::Parse_Vector<
52         senf::Parse_UInt16,
53         senf::detail::Parse_VectorN_Sizer<senf::Parse_UInt8>
54         > Parse_UInt16Vec;
55
56     {
57         Parse_UInt16Vec v (p->data().begin(), &p->data());
58         
59         BOOST_CHECK_EQUAL( v[0], 0x1011 );
60         BOOST_CHECK_EQUAL( v[2], 0x1415 );
61         BOOST_CHECK_EQUAL( v.size(), 3u );
62         BOOST_CHECK_EQUAL( v.bytes(), 7u );
63         BOOST_CHECK( ! v.empty() );
64         p->data()[0] = 0x06;
65         BOOST_CHECK_EQUAL( v.size(), 6u );
66         BOOST_CHECK_EQUAL( v.bytes(), 13u );
67         
68         Parse_UInt16Vec::iterator b (v.begin());
69         Parse_UInt16Vec::iterator e (v.end());
70         BOOST_CHECK_EQUAL(std::distance(b,e), Parse_UInt16Vec::difference_type(v.size()));
71     }
72
73     // Warning: Each of the followingoperations invalidate the parser -> we need to recreate it at
74     // each step
75
76     // And since all these members directly call the corresponding members in the container wrapper,
77     // we don't need to check them again below ...
78
79     {
80 #       define v Parse_UInt16Vec(p->data().begin(),&p->data())
81
82         v.push_back(0xf0f1u,2);
83         BOOST_CHECK_EQUAL( v.size(), 8u );
84         BOOST_CHECK_EQUAL( v[7], 0xf0f1u );
85
86         v.push_back_space();
87         BOOST_CHECK_EQUAL( v.size(), 9u );
88         BOOST_CHECK_EQUAL( v[8], 0u );
89         
90         v.push_front(0xf3f4u);
91         BOOST_CHECK_EQUAL( v.size(), 10u );
92         BOOST_CHECK_EQUAL( v[0], 0xf3f4u );
93         BOOST_CHECK_EQUAL( v[1], 0x1011u );
94
95         v.push_front_space(2);
96         BOOST_CHECK_EQUAL( v.size(), 12u );
97         BOOST_CHECK_EQUAL( v[0], 0u );
98         BOOST_CHECK_EQUAL( v[1], 0u );
99         BOOST_CHECK_EQUAL( v[2], 0xf3f4u );
100         BOOST_CHECK_EQUAL( p->data().size(), 25u );
101
102         v.resize(4);
103         BOOST_CHECK_EQUAL( v.size(), 4u );
104         BOOST_CHECK_EQUAL( p->data().size(), 9u );
105         BOOST_CHECK_EQUAL( v[3], 0x1011u );
106
107         v.resize(6);
108         BOOST_CHECK_EQUAL( v.size(), 6u );
109         BOOST_CHECK_EQUAL( v[5], 0u );
110
111         v.resize(8,0xffff);
112         BOOST_CHECK_EQUAL( v.size(), 8u );
113         BOOST_CHECK_EQUAL( p->data().size(), 17u );
114         BOOST_CHECK_EQUAL( v[6], 0xffffu );
115
116 #       undef v
117     }
118 }
119
120 BOOST_AUTO_UNIT_TEST(parseVec_wrapper)
121 {
122     unsigned char data[] = { 0x03,                                   // size
123                              0x10, 0x11,  0x12, 0x13,  0x14, 0x15,   // data
124                              0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
125     senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));
126     typedef senf::Parse_Vector<
127         senf::Parse_UInt16,
128         senf::detail::Parse_VectorN_Sizer<senf::Parse_UInt8>
129         > Parse_UInt16Vec;
130     Parse_UInt16Vec v (p->data().begin(), &p->data());
131     Parse_UInt16Vec::container w (v);
132
133     BOOST_CHECK_EQUAL( w[0], 0x1011 );
134     BOOST_CHECK_EQUAL( w[2], 0x1415 );
135     BOOST_CHECK_EQUAL( w.size(), 3u );
136     p->data()[0] = 0x06;
137     BOOST_CHECK_EQUAL( w.size(), 6u );
138     BOOST_CHECK_EQUAL( std::distance(w.begin(),w.end()), 
139                        Parse_UInt16Vec::difference_type(w.size()) );
140
141     w.shift(w.begin()+1);
142     BOOST_CHECK_EQUAL( w.size(), 7u );
143     BOOST_CHECK_EQUAL( w[0], 0x1011 );
144     BOOST_CHECK_EQUAL( w[1], 0 );
145     BOOST_CHECK_EQUAL( w[2], 0x1213 );
146
147     w.insert(w.begin()+3, 2u, 0xfffe);
148     BOOST_CHECK_EQUAL( w.size(), 9u );
149     BOOST_CHECK_EQUAL( w[2], 0x1213 );
150     BOOST_CHECK_EQUAL( w[3], 0xfffe );
151     BOOST_CHECK_EQUAL( w[4], 0xfffe );
152     BOOST_CHECK_EQUAL( w[5], 0x1415 );
153
154     w.erase(w.begin()+3, w.begin()+5);
155     BOOST_CHECK_EQUAL( w.size(), 7u );
156
157     w.erase(w.begin()+1);
158     BOOST_CHECK_EQUAL( w.size(), 6u );
159
160     {
161         senf::PacketData::iterator i (p->data().begin()+1);
162         Parse_UInt16Vec::iterator j (w.begin());
163         Parse_UInt16Vec::iterator e (w.end());
164         for (;j!=e;++j, i+=2)
165             BOOST_CHECK_EQUAL( senf::Parse_UInt16(i,&p->data()), *j );
166         BOOST_CHECK_EQUAL(p->data().end()-i, 0);
167     }
168
169     w.clear();
170     BOOST_CHECK_EQUAL( w.size(), 0u );
171     BOOST_CHECK( w.begin() == w.end() );
172     BOOST_CHECK_EQUAL( p->data().size(), 1u );
173
174     BOOST_CHECK_EQUAL( w.parser().size(), 0u );
175 }
176
177 ///////////////////////////////cc.e////////////////////////////////////////
178 #undef prefix_
179
180 \f
181 // Local Variables:
182 // mode: c++
183 // fill-column: 100
184 // c-file-style: "senf"
185 // indent-tabs-mode: nil
186 // ispell-local-dictionary: "american"
187 // compile-command: "scons -u test"
188 // End: