4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief IntParser unit tests */
31 //#include "IntParser.test.hh"
32 //#include "IntParser.test.ih"
37 #include <senf/Utils/auto_unit_test.hh>
38 #include <boost/test/test_tools.hpp>
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46 struct VoidPacket : public PacketTypeBase
50 SENF_AUTO_UNIT_TEST(parseInt_fixedSizes)
52 PacketInterpreterBase::byte data[] = { 0x8e, 0x2f, 0x57, 0x12, 0xd1 };
53 PacketInterpreterBase::ptr p (PacketInterpreter<VoidPacket>::create(data));
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);
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);
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);
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);
72 SENF_AUTO_UNIT_TEST(parseInt_bits)
75 // 012345678901234567890123456789012345678901234567
76 // -------- -------- --------
77 // 011000111101011101011010001100011010010001000110
78 PacketInterpreterBase::byte data[] = { 0x63, 0xd7, 0x5a, 0x31, 0xa4, 0x46 };
79 PacketInterpreterBase::ptr p (PacketInterpreter<VoidPacket>::create(data));
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);
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);
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);
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);
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);
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);
115 SENF_AUTO_UNIT_TEST(parseInt_assign)
117 PacketInterpreterBase::byte data[] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
118 PacketInterpreterBase::ptr p (PacketInterpreter<VoidPacket>::create(data));
120 Int8Parser(p->data().begin(),&p->data()).value(0x2f);
121 BOOST_CHECK_EQUAL( p->data()[0], 0x2f );
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 );
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 );
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 );
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 );
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 );
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 );
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 );
157 SENF_AUTO_UNIT_TEST(parseInt_operators)
159 PacketInterpreterBase::byte data[] = { 0x63, 0xd7, 0x5a, 0x31, 0xa4, 0x46 };
160 PacketInterpreterBase::ptr p (PacketInterpreter<VoidPacket>::create(data));
162 UInt24Parser p1(p->data().begin(),&p->data());
163 UInt16Parser p2(p->data().begin()+3,&p->data());
165 BOOST_CHECK_EQUAL( ~p1, 4288424101u );
166 BOOST_CHECK ( !!p1 );
167 BOOST_CHECK_EQUAL( -p1, -6543194u );
170 BOOST_CHECK_EQUAL( p1, 6543210u );
172 BOOST_CHECK_EQUAL( p1, 6543194u );
175 BOOST_CHECK_EQUAL( p1, 6555902u );
177 // Here some idiotic automatic promotion from unsigned short ->
178 // int happens in the first macro parameter ... hrmpf ...
179 BOOST_CHECK_EQUAL( p2, 15010 );
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 );
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 );
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 ...
199 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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