139dfdb8a53d81e11526fbde22ee221599e85efb
[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
32 #include <boost/test/auto_unit_test.hpp>
33 #include <boost/test/test_tools.hpp>
34 #include <boost/assign.hpp>
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 using namespace senf;
40
41 BOOST_AUTO_UNIT_TEST(parseVec_test)
42 {
43     unsigned char data[] = { 0x03,                                   // size
44                              0x10, 0x11,  0x12, 0x13,  0x14, 0x15,   // data
45                              0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
46     typedef unsigned char * iterator;
47     typedef Parse_Vector<Parse_UInt16<>,Parse_UInt8<>,iterator> Parse_UInt16Vec;
48
49     Parse_UInt8<iterator> sizeParser (data);
50     Parse_UInt16Vec v (sizeParser, data+1);
51     
52     BOOST_CHECK_EQUAL( v[0], 0x1011 );
53     BOOST_CHECK_EQUAL( v[2], 0x1415 );
54     BOOST_CHECK_EQUAL( v.size(), 3u );
55     BOOST_CHECK_EQUAL( v.bytes(), 6u );
56     data[0] = 0x06;
57     BOOST_CHECK_EQUAL( v.size(), 6u );
58     BOOST_CHECK_EQUAL( v.bytes(), 12u );
59     
60     iterator i (data+1);
61     Parse_UInt16Vec::iterator j (v.begin());
62     Parse_UInt16Vec::iterator e (v.end());
63     for (;j!=e;++j, i+=2)
64         BOOST_CHECK_EQUAL( Parse_UInt16<iterator>(i), *j );
65     BOOST_CHECK_EQUAL(i, data+13);
66 }
67
68 BOOST_AUTO_UNIT_TEST(parseVec_wrapper)
69 {
70     typedef std::vector<unsigned char> Container;
71     typedef Container::iterator iterator;
72     typedef Parse_UInt8<iterator> Parse_Size;
73     typedef Parse_Vector<Parse_UInt16<>,Parse_Size,iterator> Parse_UInt16Vec;
74     typedef Parse_UInt16Vec::wrapper<Container>::t Parse_UInt16VecWrap;
75
76     using namespace boost::assign;
77     
78     Container data;
79     data += 
80         0x03,                                   // size
81         0x10, 0x11,  0x12, 0x13,  0x14, 0x15,   // data
82         0x20, 0x21,  0x22, 0x23,  0x24, 0x25;
83
84     Parse_Size sizeParser (data.begin());
85     Parse_UInt16Vec v (sizeParser, data.begin()+1);
86     Parse_UInt16VecWrap w (v,data);
87
88     BOOST_CHECK_EQUAL( w[0], 0x1011 );
89     BOOST_CHECK_EQUAL( w[2], 0x1415 );
90     BOOST_CHECK_EQUAL( w.size(), 3u );
91     data[0] = 0x06;
92     BOOST_CHECK_EQUAL( w.size(), 6u );
93     
94     {
95         iterator i (data.begin()+1);
96         Parse_UInt16VecWrap::iterator j (w.begin());
97         Parse_UInt16VecWrap::iterator e (w.end());
98         for (;j!=e;++j, i+=2)
99             BOOST_CHECK_EQUAL( Parse_UInt16<iterator>(i), *j );
100         BOOST_CHECK_EQUAL(data.end()-i, 0);
101     }
102
103     w.shift(w.begin()+1);
104     BOOST_CHECK_EQUAL( w.size(), 7u );
105     BOOST_CHECK_EQUAL( w[0], 0x1011 );
106     BOOST_CHECK_EQUAL( w[1], 0 );
107     BOOST_CHECK_EQUAL( w[2], 0x1213 );
108     
109     w.insert(w.begin()+3, 2u, 0xfffe);
110     BOOST_CHECK_EQUAL( w.size(), 9u );
111     BOOST_CHECK_EQUAL( w[2], 0x1213 );
112     BOOST_CHECK_EQUAL( w[3], 0xfffe );
113     BOOST_CHECK_EQUAL( w[4], 0xfffe );
114     BOOST_CHECK_EQUAL( w[5], 0x1415 );
115
116     w.erase(w.begin()+3, w.begin()+5);
117     BOOST_CHECK_EQUAL( w.size(), 7u );
118     
119     w.erase(w.begin()+1);
120     BOOST_CHECK_EQUAL( w.size(), 6u );
121
122     {
123         iterator i (data.begin()+1);
124         Parse_UInt16VecWrap::iterator j (w.begin());
125         Parse_UInt16VecWrap::iterator e (w.end());
126         for (;j!=e;++j, i+=2)
127             BOOST_CHECK_EQUAL( Parse_UInt16<iterator>(i), *j );
128         BOOST_CHECK_EQUAL(data.end()-i, 0);
129     }
130
131     w.clear();
132     BOOST_CHECK_EQUAL( w.size(), 0u );
133     BOOST_CHECK( w.begin() == w.end() );
134     BOOST_CHECK_EQUAL( data.size(), 1u );
135 }
136
137 // This really belongs into ParserBase.test.cc but it's simpler here
138 BOOST_AUTO_UNIT_TEST(parserTraits_test)
139 {
140     // Really, this could be checked by BOOST_STATIC_ASSERT since 
141     // it's compile-time ...
142     BOOST_CHECK( Parser_traits< Parse_UInt32<> >::fixed_size );
143     BOOST_CHECK( (! Parser_traits< Parse_Vector< Parse_UInt16<>,Parse_UInt16<> > >::fixed_size) );
144 }
145
146 ///////////////////////////////cc.e////////////////////////////////////////
147 #undef prefix_
148
149 \f
150 // Local Variables:
151 // mode: c++
152 // c-file-style: "senf"
153 // End: