4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
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 PacketImpl.test unit tests */
26 //#include "PacketImpl.test.hh"
27 //#include "PacketImpl.test.ih"
31 #include "main.test.hh"
33 #include "../Utils/auto_unit_test.hh"
34 #include <boost/test/test_tools.hpp>
37 ///////////////////////////////cc.p////////////////////////////////////////
40 struct VoidPacket : public senf::PacketTypeBase {};
43 // PacketImpl cannot be tested without relying on PacketInterpreterBase. However these unit-tests
44 // only validate PacketInterpreterBase as far as to ensure that a failure of one test is not due to
45 // an error in PacketInterpreterbase
47 BOOST_AUTO_UNIT_TEST(packetImpl_mem)
49 senf::detail::PacketImpl * p (new senf::detail::PacketImpl());
50 BOOST_CHECK_EQUAL(p->refcount(), 0);
52 BOOST_CHECK_EQUAL(p->refcount(), 1);
55 senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 1u);
57 // From now on, the object should stay alive since I manually incremented the
62 BOOST_CHECK_EQUAL(p->refcount(), 3);
64 BOOST_CHECK_EQUAL(p->refcount(), 1);
67 senf::PacketInterpreterBase::ptr pi (
68 senf::detail::packet::test::TestDriver::create<VoidPacket>(
69 p,p->begin(),p->end(), senf::PacketInterpreterBase::Append));
70 // Hmm ... this check works as long as sizeof(PacketInterpreterBase> !=
71 // sizeof(PacketImpl) ... !!
74 senf::pool_alloc_mixin< senf::PacketInterpreter<VoidPacket> >::allocCounter(), 1u);
76 senf::PacketInterpreterBase::ptr pi2 (pi);
77 BOOST_CHECK_EQUAL(p->refcount(), 3);
79 BOOST_CHECK_EQUAL(p->refcount(),1);
82 senf::PacketInterpreterBase::ptr pi (p->first());
83 BOOST_CHECK_EQUAL(p->refcount(),2);
84 p->truncateInterpreters(pi.get());
85 BOOST_CHECK_EQUAL(p->refcount(),1);
89 senf::pool_alloc_mixin<senf::PacketInterpreterBase>::allocCounter(), 0u);
91 BOOST_CHECK_EQUAL(p->refcount(),1);
94 // The refcount must be one here (from incrementing the refcount above)
95 // Therefore we can safely delete the object.
96 BOOST_CHECK_EQUAL(p->refcount(), 1);
100 senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 0u);
104 BOOST_AUTO_UNIT_TEST(packetImpl_data)
106 senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create());
107 senf::detail::PacketImpl * p (senf::detail::packet::test::TestDriver::impl(pi));
109 senf::detail::PacketImpl::byte data[] =
110 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
112 p->insert(&pi->data(),p->begin(),data, data+sizeof(data));
113 BOOST_CHECK_EQUAL(p->size(), 8u);
114 BOOST_CHECK_EQUAL(p->begin()[0], 0x00u);
115 BOOST_CHECK_EQUAL(p->begin()[7], 0x07u);
116 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
118 p->insert(&pi->data(),p->begin()+2,0xf0u);
119 BOOST_CHECK_EQUAL(p->size(),9u);
120 BOOST_CHECK_EQUAL(p->begin()[8], 0x07u);
121 BOOST_CHECK_EQUAL(p->begin()[2], 0xf0u);
122 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
124 p->insert(&pi->data(),p->begin()+9,8,0xffu);
125 BOOST_CHECK_EQUAL(p->size(),17u);
126 BOOST_CHECK_EQUAL(p->begin()[16], 0xffu);
127 BOOST_CHECK_EQUAL(p->begin()[8], 0x07u);
128 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
130 p->erase(&pi->data(),p->begin());
131 BOOST_CHECK_EQUAL(p->size(),16u);
132 BOOST_CHECK_EQUAL(p->begin()[0], 0x01u);
133 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
135 p->erase(&pi->data(),p->begin()+2, p->begin()+7);
136 BOOST_CHECK_EQUAL(p->size(),11u);
137 BOOST_CHECK_EQUAL(p->begin()[2], 0x07u);
138 BOOST_CHECK_EQUAL(p->begin()[3], 0xffu);
139 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
141 BOOST_REQUIRE_EQUAL(pi->data().size(), p->size());
142 BOOST_REQUIRE(pi->data().begin() == p->begin());
144 p->clear(&pi->data());
145 BOOST_CHECK_EQUAL(p->size(), 0u);
146 BOOST_CHECK_EQUAL(pi->data().size(), 0u);
147 BOOST_CHECK(pi->data().begin() == p->begin());
150 BOOST_AUTO_UNIT_TEST(packetImpl_interpreters)
152 senf::detail::PacketImpl * p (new senf::detail::PacketImpl());
156 senf::PacketInterpreterBase::ptr pi2 (
157 senf::detail::packet::test::TestDriver::create<VoidPacket>(
158 p,p->begin(),p->end(),senf::PacketInterpreterBase::Append));
159 senf::PacketInterpreterBase::ptr pi3 (
160 senf::detail::packet::test::TestDriver::create<VoidPacket>(
161 p,p->end(),p->end(),senf::PacketInterpreterBase::Append));
162 senf::PacketInterpreterBase::ptr pi1 (
163 senf::detail::packet::test::TestDriver::create<VoidPacket>(
164 p,p->begin(),p->end(),senf::PacketInterpreterBase::Prepend));
166 BOOST_CHECK_EQUAL(p->first(), pi1.get());
167 BOOST_CHECK_EQUAL(p->next(p->first()), pi2.get());
168 BOOST_CHECK_EQUAL(p->next(p->next(p->first())), pi3.get());
169 BOOST_CHECK( !p->next(p->next(p->next(p->first()))) );
171 BOOST_CHECK_EQUAL(p->last(), pi3.get());
172 BOOST_CHECK_EQUAL(p->prev(p->last()), pi2.get());
173 BOOST_CHECK_EQUAL(p->prev(p->prev(p->last())), pi1.get());
174 BOOST_CHECK( !p->prev(p->prev(p->prev(p->last()))) );
176 p->insert(&pi2->data(),p->begin(),10,0x00u);
177 BOOST_CHECK_EQUAL(pi1->data().size(), 10u);
178 BOOST_CHECK_EQUAL(pi2->data().size(), 10u);
179 BOOST_CHECK_EQUAL(pi3->data().size(), 0u);
180 BOOST_CHECK( pi1->data().begin() == p->begin() );
181 BOOST_CHECK( pi2->data().begin() == p->begin() );
182 BOOST_CHECK( pi3->data().begin() == p->end() );
184 p->insert(&pi3->data(),p->end(), 0x00u);
185 BOOST_CHECK_EQUAL(pi1->data().size(), 11u);
186 BOOST_CHECK_EQUAL(pi2->data().size(), 11u);
187 BOOST_CHECK_EQUAL(pi3->data().size(), 1u);
189 p->insert(&pi1->data(),p->end(), 2, 0x00u);
190 BOOST_CHECK_EQUAL(pi1->data().size(), 13u);
191 BOOST_CHECK_EQUAL(pi2->data().size(), 11u);
192 BOOST_CHECK_EQUAL(pi3->data().size(), 1u);
193 BOOST_CHECK( pi1->data().end() == p->begin()+13u );
194 BOOST_CHECK( pi2->data().end() == p->begin()+11u );
195 BOOST_CHECK( pi3->data().end() == p->begin()+11u );
197 p->clear(&pi2->data());
198 BOOST_CHECK_EQUAL(pi1->data().size(), 2u);
199 BOOST_CHECK( ! p->next(p->next(p->first())) );
202 BOOST_CHECK_EQUAL(p->refcount(), 1);
206 senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 0u);
210 ///////////////////////////////cc.e////////////////////////////////////////
217 // c-file-style: "senf"
218 // indent-tabs-mode: nil
219 // ispell-local-dictionary: "american"
220 // compile-command: "scons -u test"
221 // comment-column: 40