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