switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / AuxParser.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief AuxParser unit tests */
30
31 //#include "AuxParser.test.hh"
32 //#include "AuxParser.test.ih"
33
34 // Custom includes
35 #include "Packets.hh"
36
37 #include <senf/Utils/auto_unit_test.hh>
38 #include <boost/test/test_tools.hpp>
39
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 // The other aux policies are tested with the corresponding container classes
44
45 SENF_AUTO_UNIT_TEST(vectorPacketSizeAuxPolicy)
46 {
47     unsigned char data[] = { 0x10, 0x11,  0x12, 0x13,  0x14, 0x15,
48                              0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
49     senf::Packet p (senf::DataPacket::create(data));
50
51     typedef senf::VectorParser<
52         senf::UInt8Parser,
53         senf::detail::PacketSizeAuxParserPolicy
54         > UInt8VectorParser;
55
56     {
57         UInt8VectorParser v (p.data().begin(), &p.data());
58
59         BOOST_CHECK_EQUAL( v.size(), p.data().size() );
60         BOOST_CHECK_EQUAL_COLLECTIONS( v.begin(), v.end(), data, data+sizeof(data) );
61     }
62
63     {
64         UInt8VectorParser v (p.data().begin(), &p.data());
65         UInt8VectorParser::container w (v);
66
67         BOOST_CHECK_EQUAL_COLLECTIONS( w.begin(), w.end(), data, data+sizeof(data) );
68
69         w.shift(w.begin()+1);
70         BOOST_CHECK_EQUAL( w.size(), p.data().size() );
71
72         w.erase(w.begin()+3, w.begin()+5);
73         BOOST_CHECK_EQUAL( w.size(), p.data().size() );
74
75         unsigned char data2[] = { 0x10, 0x00, 0x11, 0x14, 0x15,
76                                   0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
77
78         BOOST_CHECK_EQUAL_COLLECTIONS( w.begin(), w.end(), data2, data2+sizeof(data2) );
79     }
80 }
81
82 SENF_AUTO_UNIT_TEST(vectorPacketSizeAuxPolicy_transformed)
83 {
84     unsigned char data[] = { 0x10, 0x11,  0x12, 0x13,  0x14, 0x15,
85                              0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
86     senf::Packet p (senf::DataPacket::create(data));
87
88     typedef senf::VectorParser<
89         senf::UInt16Parser,
90         senf::detail::TransformAuxParserPolicy<
91             senf::detail::PacketSizeAuxParserPolicy,
92             senf::detail::VectorParserBytesTransform<senf::UInt16Parser::fixed_bytes> >
93         > UInt16VectorParser;
94
95     {
96         UInt16VectorParser v (p.data().begin(), &p.data());
97
98         BOOST_REQUIRE_EQUAL( v.size(), p.data().size()/2 );
99         BOOST_CHECK_EQUAL( v[0], 0x1011u );
100         BOOST_CHECK_EQUAL( v[1], 0x1213u );
101         BOOST_CHECK_EQUAL( v[5], 0x2425u );
102     }
103
104     {
105         UInt16VectorParser v (p.data().begin(), &p.data());
106         UInt16VectorParser::container w (v);
107
108         BOOST_REQUIRE_EQUAL( w.size(), p.data().size()/2 );
109         BOOST_CHECK_EQUAL( w[0], 0x1011u );
110         BOOST_CHECK_EQUAL( w[1], 0x1213u );
111         BOOST_CHECK_EQUAL( w[5], 0x2425u );
112
113         w.shift(w.begin()+1);
114         BOOST_CHECK_EQUAL( w.size(), p.data().size()/2 );
115
116         w.erase(w.begin()+3, w.begin()+5);
117         BOOST_CHECK_EQUAL( w.size(), p.data().size()/2 );
118
119         senf::UInt16Parser::value_type  data2[] =
120             { 0x1011u, 0x0000u, 0x1213u, 0x2223u, 0x2425u };
121
122         BOOST_CHECK_EQUAL_COLLECTIONS( w.begin(), w.end(),
123                                        data2, data2+sizeof(data2)/sizeof(data2[0]) );
124     }
125 }
126
127 SENF_AUTO_UNIT_TEST(listPacketSizeAuxPolicy)
128 {
129     unsigned char data[] = { 0x10, 0x11,  0x12, 0x13,  0x14, 0x15,
130                              0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
131     senf::Packet p (senf::DataPacket::create(data));
132
133     typedef senf::ListParser<
134         senf::detail::ListBParser_Policy<
135             senf::UInt16Parser,
136             senf::detail::PacketSizeAuxParserPolicy>
137         > ListParser;
138
139     {
140         ListParser l (p.data().begin(), &p.data());
141
142         BOOST_REQUIRE_EQUAL( l.size(), p.data().size()/2 );
143         BOOST_CHECK_EQUAL( l.front(), 0x1011u );
144         BOOST_CHECK_EQUAL( l.back(), 0x2425u );
145     }
146
147     {
148         ListParser l (p.data().begin(), &p.data());
149         ListParser::container w (l);
150
151         BOOST_REQUIRE_EQUAL( w.size(), p.data().size()/2 );
152         BOOST_CHECK_EQUAL( *w.begin(), 0x1011u );
153         BOOST_CHECK_EQUAL( *boost::next(w.begin()), 0x1213u );
154         BOOST_CHECK_EQUAL( *boost::next(w.begin(),5), 0x2425u );
155
156         w.shift(boost::next(w.begin()));
157         BOOST_CHECK_EQUAL( w.size(), p.data().size()/2 );
158
159         w.erase(boost::next(w.begin(),3), boost::next(w.begin(),5));
160         BOOST_CHECK_EQUAL( w.size(), p.data().size()/2 );
161
162         senf::UInt16Parser::value_type  data2[] =
163             { 0x1011u, 0x0000u, 0x1213u, 0x2223u, 0x2425u };
164
165         BOOST_CHECK_EQUAL_COLLECTIONS( w.begin(), w.end(),
166                                        data2, data2+sizeof(data2)/sizeof(data2[0]) );
167     }
168 }
169
170 //-/////////////////////////////////////////////////////////////////////////////////////////////////
171 #undef prefix_
172
173 \f
174 // Local Variables:
175 // mode: c++
176 // fill-column: 100
177 // comment-column: 40
178 // c-file-style: "senf"
179 // indent-tabs-mode: nil
180 // ispell-local-dictionary: "american"
181 // compile-command: "scons -u test"
182 // End: