switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / IntParser.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
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 IntParser unit tests */
30
31 //#include "IntParser.test.hh"
32 //#include "IntParser.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 using namespace senf;
44
45 namespace {
46     struct VoidPacket : public PacketTypeBase
47     {};
48 }
49
50 SENF_AUTO_UNIT_TEST(parseInt_fixedSizes)
51 {
52     PacketInterpreterBase::byte data[] = { 0x8e, 0x2f, 0x57, 0x12, 0xd1 };
53     PacketInterpreterBase::ptr p (PacketInterpreter<VoidPacket>::create(data));
54
55     BOOST_CHECK_EQUAL(Int8Parser(p->data().begin(),&p->data()).value(), -114);
56     BOOST_CHECK_EQUAL(Int8Parser(p->data().begin()+1,&p->data()).value(), 47);
57     BOOST_CHECK_EQUAL(UInt8Parser(p->data().begin(),&p->data()).value(), 142u);
58
59     BOOST_CHECK_EQUAL(Int16Parser(p->data().begin(),&p->data()).value(), -29137);
60     BOOST_CHECK_EQUAL(Int16Parser(p->data().begin()+1,&p->data()).value(), 12119);
61     BOOST_CHECK_EQUAL(UInt16Parser(p->data().begin(),&p->data()).value(), 36399u);
62
63     BOOST_CHECK_EQUAL(Int24Parser(p->data().begin(),&p->data()).value(), -7458985);
64     BOOST_CHECK_EQUAL(Int24Parser(p->data().begin()+1,&p->data()).value(), 3102482);
65     BOOST_CHECK_EQUAL(UInt24Parser(p->data().begin(),&p->data()).value(), 9318231u);
66
67     BOOST_CHECK_EQUAL(Int32Parser(p->data().begin(),&p->data()).value(), -1909500142);
68     BOOST_CHECK_EQUAL(Int32Parser(p->data().begin()+1,&p->data()).value(), 794235601);
69     BOOST_CHECK_EQUAL(UInt32Parser(p->data().begin(),&p->data()).value(), 2385467154u);
70 }
71
72 SENF_AUTO_UNIT_TEST(parseInt_bits)
73 {
74     //                       0         1         2         3         4
75     //                       012345678901234567890123456789012345678901234567
76     //                       --------        --------        --------
77     //                       011000111101011101011010001100011010010001000110
78     PacketInterpreterBase::byte data[] = { 0x63,   0xd7,   0x5a,   0x31,   0xa4,   0x46 };
79     PacketInterpreterBase::ptr p (PacketInterpreter<VoidPacket>::create(data));
80
81     // 1 byte
82     BOOST_CHECK_EQUAL((UIntFieldParser<2,7>(p->data().begin(),&p->data()).value()), 17u);
83     BOOST_CHECK_EQUAL((IntFieldParser<2,7>(p->data().begin(),&p->data()).value()), -15);
84     BOOST_CHECK_EQUAL((UIntFieldParser<3,7>(p->data().begin(),&p->data()).value()), 1u);
85     BOOST_CHECK_EQUAL((IntFieldParser<3,7>(p->data().begin(),&p->data()).value()), 1);
86     BOOST_CHECK_EQUAL((UIntFieldParser<0,8>(p->data().begin(),&p->data()).value()), 99u);
87
88     // 2 byte
89     BOOST_CHECK_EQUAL((UIntFieldParser<5,12>(p->data().begin(),&p->data()).value()), 61u);
90     BOOST_CHECK_EQUAL((UIntFieldParser<0,12>(p->data().begin(),&p->data()).value()), 1597u);
91     BOOST_CHECK_EQUAL((UIntFieldParser<8,13>(p->data().begin(),&p->data()).value()), 26u);
92     BOOST_CHECK_EQUAL((UIntFieldParser<8,16>(p->data().begin(),&p->data()).value()), 215u);
93     BOOST_CHECK_EQUAL((UIntFieldParser<0,16>(p->data().begin(),&p->data()).value()), 25559u);
94
95     // 3 byte
96     BOOST_CHECK_EQUAL((UIntFieldParser<6,20>(p->data().begin(),&p->data()).value()), 15733u);
97     BOOST_CHECK_EQUAL((IntFieldParser<6,20>(p->data().begin(),&p->data()).value()), -651);
98     BOOST_CHECK_EQUAL((UIntFieldParser<13,22>(p->data().begin(),&p->data()).value()), 470u);
99
100     // 4 byte
101     BOOST_CHECK_EQUAL((UIntFieldParser<3,28>(p->data().begin(),&p->data()).value()), 4027811u);
102     BOOST_CHECK_EQUAL((UIntFieldParser<13,38>(p->data().begin(),&p->data()).value()), 30837865u);
103     BOOST_CHECK_EQUAL((UIntFieldParser<8,40>(p->data().begin(),&p->data()).value()), 3613012388u);
104     BOOST_CHECK_EQUAL((IntFieldParser<8,40>(p->data().begin(),&p->data()).value()), -681954908);
105
106     // 5 byte
107     BOOST_CHECK_EQUAL((UIntFieldParser<3,34>(p->data().begin(),&p->data()).value()), 257779910u);
108     BOOST_CHECK_EQUAL((IntFieldParser<13,41>(p->data().begin(),&p->data()).value()), -21732536);
109
110     // single bit
111     BOOST_CHECK_EQUAL((FlagParser<32>(p->data().begin(),&p->data()).value()), true);
112     BOOST_CHECK_EQUAL((FlagParser<12>(p->data().begin(),&p->data()).value()), false);
113 }
114
115 SENF_AUTO_UNIT_TEST(parseInt_assign)
116 {
117     PacketInterpreterBase::byte data[] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
118     PacketInterpreterBase::ptr p (PacketInterpreter<VoidPacket>::create(data));
119
120     Int8Parser(p->data().begin(),&p->data()).value(0x2f);
121     BOOST_CHECK_EQUAL( p->data()[0], 0x2f );
122
123     Int16Parser(p->data().begin(),&p->data()).value(0xa341);
124     BOOST_CHECK_EQUAL( p->data()[0], 0xa3 );
125     BOOST_CHECK_EQUAL( p->data()[1], 0x41 );
126
127     Int24Parser(p->data().begin(),&p->data()).value(0x234567);
128     BOOST_CHECK_EQUAL( p->data()[0], 0x23 );
129     BOOST_CHECK_EQUAL( p->data()[1], 0x45 );
130     BOOST_CHECK_EQUAL( p->data()[2], 0x67 );
131
132     Int32Parser(p->data().begin(),&p->data()).value(0xfedcba98);
133     BOOST_CHECK_EQUAL( p->data()[0], 0xfe );
134     BOOST_CHECK_EQUAL( p->data()[1], 0xdc );
135     BOOST_CHECK_EQUAL( p->data()[2], 0xba );
136     BOOST_CHECK_EQUAL( p->data()[3], 0x98 );
137
138     IntFieldParser<2,6>(p->data().begin(),&p->data()).value(0x3);
139     BOOST_CHECK_EQUAL( p->data()[0], 0xce );
140     BOOST_CHECK_EQUAL( p->data()[1], 0xdc );
141
142     IntFieldParser<6,9>(p->data().begin(),&p->data()).value(0x2);
143     BOOST_CHECK_EQUAL( p->data()[0], 0xcd );
144     BOOST_CHECK_EQUAL( p->data()[1], 0x5c );
145     BOOST_CHECK_EQUAL( p->data()[2], 0xba );
146
147     IntFieldParser<2,21>(p->data().begin(),&p->data()).value(0x13d75);
148     BOOST_CHECK_EQUAL( p->data()[0], 0xc9 );
149     BOOST_CHECK_EQUAL( p->data()[1], 0xeb );
150     BOOST_CHECK_EQUAL( p->data()[2], 0xaa );
151     BOOST_CHECK_EQUAL( p->data()[3], 0x98 );
152
153     UIntFieldParser<4,34>(p->data().begin(),&p->data()).value(0x268ad497u);
154     BOOST_CHECK_EQUAL( (UIntFieldParser<4,34>(p->data().begin(),&p->data()).value()), 0x268ad497u );
155 }
156
157 SENF_AUTO_UNIT_TEST(parseInt_operators)
158 {
159     PacketInterpreterBase::byte data[] = { 0x63, 0xd7, 0x5a, 0x31, 0xa4, 0x46 };
160     PacketInterpreterBase::ptr p (PacketInterpreter<VoidPacket>::create(data));
161
162     UInt24Parser p1(p->data().begin(),&p->data());
163     UInt16Parser p2(p->data().begin()+3,&p->data());
164
165     BOOST_CHECK_EQUAL( ~p1, 4288424101u );
166     BOOST_CHECK ( !!p1 );
167     BOOST_CHECK_EQUAL( -p1, -6543194u );
168
169     p1 += 0x10;
170     BOOST_CHECK_EQUAL( p1, 6543210u );
171     p1 -= 0x10;
172     BOOST_CHECK_EQUAL( p1, 6543194u );
173
174     p1 += p2;
175     BOOST_CHECK_EQUAL( p1, 6555902u );
176     p2 += p1;
177     // Here some idiotic automatic promotion from unsigned short ->
178     // int happens in the first macro parameter ... hrmpf ...
179     BOOST_CHECK_EQUAL( p2, 15010 );
180
181     BOOST_CHECK_EQUAL( ++p1, 6555903u );
182     BOOST_CHECK_EQUAL( p1++, 6555903u );
183     BOOST_CHECK_EQUAL( p1, 6555904u );
184     BOOST_CHECK_EQUAL( --p1, 6555903u );
185     BOOST_CHECK_EQUAL( p1--, 6555903u );
186     BOOST_CHECK_EQUAL( p1, 6555902u );
187
188     p1 = 0x123456u;
189     BOOST_CHECK_EQUAL( p->data()[0], 0x12 );
190     BOOST_CHECK_EQUAL( p->data()[1], 0x34 );
191     BOOST_CHECK_EQUAL( p->data()[2], 0x56 );
192     BOOST_CHECK_EQUAL( p->data()[3], 0x3a );
193
194     // I stop here ... this is absolutely identical for all other
195     // operators and all other integer types. If really some error pops
196     // up, I'll add a check here ...
197 }
198
199 //-/////////////////////////////////////////////////////////////////////////////////////////////////
200 #undef prefix_
201
202 \f
203 // Local Variables:
204 // mode: c++
205 // fill-column: 100
206 // c-file-style: "senf"
207 // indent-tabs-mode: nil
208 // ispell-local-dictionary: "american"
209 // compile-command: "scons -u test"
210 // comment-column: 40
211 // End: