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