4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief IntParser unit tests */
26 //#include "IntParser.test.hh"
27 //#include "IntParser.test.ih"
32 #include <senf/Utils/auto_unit_test.hh>
33 #include <boost/test/test_tools.hpp>
36 ///////////////////////////////cc.p////////////////////////////////////////
41 struct VoidPacket : public PacketTypeBase
45 BOOST_AUTO_UNIT_TEST(parseInt_fixedSizes)
47 PacketInterpreterBase::byte data[] = { 0x8e, 0x2f, 0x57, 0x12, 0xd1 };
48 PacketInterpreterBase::ptr p (PacketInterpreter<VoidPacket>::create(data));
50 BOOST_CHECK_EQUAL(Int8Parser(p->data().begin(),&p->data()).value(), -114);
51 BOOST_CHECK_EQUAL(Int8Parser(p->data().begin()+1,&p->data()).value(), 47);
52 BOOST_CHECK_EQUAL(UInt8Parser(p->data().begin(),&p->data()).value(), 142u);
54 BOOST_CHECK_EQUAL(Int16Parser(p->data().begin(),&p->data()).value(), -29137);
55 BOOST_CHECK_EQUAL(Int16Parser(p->data().begin()+1,&p->data()).value(), 12119);
56 BOOST_CHECK_EQUAL(UInt16Parser(p->data().begin(),&p->data()).value(), 36399u);
58 BOOST_CHECK_EQUAL(Int24Parser(p->data().begin(),&p->data()).value(), -7458985);
59 BOOST_CHECK_EQUAL(Int24Parser(p->data().begin()+1,&p->data()).value(), 3102482);
60 BOOST_CHECK_EQUAL(UInt24Parser(p->data().begin(),&p->data()).value(), 9318231u);
62 BOOST_CHECK_EQUAL(Int32Parser(p->data().begin(),&p->data()).value(), -1909500142);
63 BOOST_CHECK_EQUAL(Int32Parser(p->data().begin()+1,&p->data()).value(), 794235601);
64 BOOST_CHECK_EQUAL(UInt32Parser(p->data().begin(),&p->data()).value(), 2385467154u);
67 BOOST_AUTO_UNIT_TEST(parseInt_bits)
70 // 012345678901234567890123456789012345678901234567
71 // -------- -------- --------
72 // 011000111101011101011010001100011010010001000110
73 PacketInterpreterBase::byte data[] = { 0x63, 0xd7, 0x5a, 0x31, 0xa4, 0x46 };
74 PacketInterpreterBase::ptr p (PacketInterpreter<VoidPacket>::create(data));
77 BOOST_CHECK_EQUAL((UIntFieldParser<2,7>(p->data().begin(),&p->data()).value()), 17u);
78 BOOST_CHECK_EQUAL((IntFieldParser<2,7>(p->data().begin(),&p->data()).value()), -15);
79 BOOST_CHECK_EQUAL((UIntFieldParser<3,7>(p->data().begin(),&p->data()).value()), 1u);
80 BOOST_CHECK_EQUAL((IntFieldParser<3,7>(p->data().begin(),&p->data()).value()), 1);
81 BOOST_CHECK_EQUAL((UIntFieldParser<0,8>(p->data().begin(),&p->data()).value()), 99u);
84 BOOST_CHECK_EQUAL((UIntFieldParser<5,12>(p->data().begin(),&p->data()).value()), 61u);
85 BOOST_CHECK_EQUAL((UIntFieldParser<0,12>(p->data().begin(),&p->data()).value()), 1597u);
86 BOOST_CHECK_EQUAL((UIntFieldParser<8,13>(p->data().begin(),&p->data()).value()), 26u);
87 BOOST_CHECK_EQUAL((UIntFieldParser<8,16>(p->data().begin(),&p->data()).value()), 215u);
88 BOOST_CHECK_EQUAL((UIntFieldParser<0,16>(p->data().begin(),&p->data()).value()), 25559u);
91 BOOST_CHECK_EQUAL((UIntFieldParser<6,20>(p->data().begin(),&p->data()).value()), 15733u);
92 BOOST_CHECK_EQUAL((IntFieldParser<6,20>(p->data().begin(),&p->data()).value()), -651);
93 BOOST_CHECK_EQUAL((UIntFieldParser<13,22>(p->data().begin(),&p->data()).value()), 470u);
96 BOOST_CHECK_EQUAL((UIntFieldParser<3,28>(p->data().begin(),&p->data()).value()), 4027811u);
97 BOOST_CHECK_EQUAL((UIntFieldParser<13,38>(p->data().begin(),&p->data()).value()), 30837865u);
98 BOOST_CHECK_EQUAL((UIntFieldParser<8,40>(p->data().begin(),&p->data()).value()), 3613012388u);
99 BOOST_CHECK_EQUAL((IntFieldParser<8,40>(p->data().begin(),&p->data()).value()), -681954908);
102 BOOST_CHECK_EQUAL((UIntFieldParser<3,34>(p->data().begin(),&p->data()).value()), 257779910u);
103 BOOST_CHECK_EQUAL((IntFieldParser<13,41>(p->data().begin(),&p->data()).value()), -21732536);
106 BOOST_CHECK_EQUAL((FlagParser<32>(p->data().begin(),&p->data()).value()), true);
107 BOOST_CHECK_EQUAL((FlagParser<12>(p->data().begin(),&p->data()).value()), false);
110 BOOST_AUTO_UNIT_TEST(parseInt_assign)
112 PacketInterpreterBase::byte data[] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
113 PacketInterpreterBase::ptr p (PacketInterpreter<VoidPacket>::create(data));
115 Int8Parser(p->data().begin(),&p->data()).value(0x2f);
116 BOOST_CHECK_EQUAL( p->data()[0], 0x2f );
118 Int16Parser(p->data().begin(),&p->data()).value(0xa341);
119 BOOST_CHECK_EQUAL( p->data()[0], 0xa3 );
120 BOOST_CHECK_EQUAL( p->data()[1], 0x41 );
122 Int24Parser(p->data().begin(),&p->data()).value(0x234567);
123 BOOST_CHECK_EQUAL( p->data()[0], 0x23 );
124 BOOST_CHECK_EQUAL( p->data()[1], 0x45 );
125 BOOST_CHECK_EQUAL( p->data()[2], 0x67 );
127 Int32Parser(p->data().begin(),&p->data()).value(0xfedcba98);
128 BOOST_CHECK_EQUAL( p->data()[0], 0xfe );
129 BOOST_CHECK_EQUAL( p->data()[1], 0xdc );
130 BOOST_CHECK_EQUAL( p->data()[2], 0xba );
131 BOOST_CHECK_EQUAL( p->data()[3], 0x98 );
133 IntFieldParser<2,6>(p->data().begin(),&p->data()).value(0x3);
134 BOOST_CHECK_EQUAL( p->data()[0], 0xce );
135 BOOST_CHECK_EQUAL( p->data()[1], 0xdc );
137 IntFieldParser<6,9>(p->data().begin(),&p->data()).value(0x2);
138 BOOST_CHECK_EQUAL( p->data()[0], 0xcd );
139 BOOST_CHECK_EQUAL( p->data()[1], 0x5c );
140 BOOST_CHECK_EQUAL( p->data()[2], 0xba );
142 IntFieldParser<2,21>(p->data().begin(),&p->data()).value(0x13d75);
143 BOOST_CHECK_EQUAL( p->data()[0], 0xc9 );
144 BOOST_CHECK_EQUAL( p->data()[1], 0xeb );
145 BOOST_CHECK_EQUAL( p->data()[2], 0xaa );
146 BOOST_CHECK_EQUAL( p->data()[3], 0x98 );
148 UIntFieldParser<4,34>(p->data().begin(),&p->data()).value(0x268ad497u);
149 BOOST_CHECK_EQUAL( (UIntFieldParser<4,34>(p->data().begin(),&p->data()).value()), 0x268ad497u );
152 BOOST_AUTO_UNIT_TEST(parseInt_operators)
154 PacketInterpreterBase::byte data[] = { 0x63, 0xd7, 0x5a, 0x31, 0xa4, 0x46 };
155 PacketInterpreterBase::ptr p (PacketInterpreter<VoidPacket>::create(data));
157 UInt24Parser p1(p->data().begin(),&p->data());
158 UInt16Parser p2(p->data().begin()+3,&p->data());
160 BOOST_CHECK_EQUAL( ~p1, 4288424101u );
161 BOOST_CHECK ( !!p1 );
162 BOOST_CHECK_EQUAL( -p1, -6543194u );
165 BOOST_CHECK_EQUAL( p1, 6543210u );
167 BOOST_CHECK_EQUAL( p1, 6543194u );
170 BOOST_CHECK_EQUAL( p1, 6555902u );
172 // Here some idiotic automatic promotion from unsigned short ->
173 // int happens in the first macro parameter ... hrmpf ...
174 BOOST_CHECK_EQUAL( p2, 15010 );
176 BOOST_CHECK_EQUAL( ++p1, 6555903u );
177 BOOST_CHECK_EQUAL( p1++, 6555903u );
178 BOOST_CHECK_EQUAL( p1, 6555904u );
179 BOOST_CHECK_EQUAL( --p1, 6555903u );
180 BOOST_CHECK_EQUAL( p1--, 6555903u );
181 BOOST_CHECK_EQUAL( p1, 6555902u );
184 BOOST_CHECK_EQUAL( p->data()[0], 0x12 );
185 BOOST_CHECK_EQUAL( p->data()[1], 0x34 );
186 BOOST_CHECK_EQUAL( p->data()[2], 0x56 );
187 BOOST_CHECK_EQUAL( p->data()[3], 0x3a );
189 // I stop here ... this is absolutely identical for all other
190 // operators and all other integer types. If really some error pops
191 // up, I'll add a check here ...
194 ///////////////////////////////cc.e////////////////////////////////////////
201 // c-file-style: "senf"
202 // indent-tabs-mode: nil
203 // ispell-local-dictionary: "american"
204 // compile-command: "scons -u test"
205 // comment-column: 40