2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 // Stefan Bund <g0dil@berlios.de>
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 \brief PacketImpl.test unit tests */
24 //#include "PacketImpl.test.hh"
25 //#include "PacketImpl.test.ih"
29 #include "main.test.hh"
31 #include "../Utils/auto_unit_test.hh"
32 #include <boost/test/test_tools.hpp>
35 ///////////////////////////////cc.p////////////////////////////////////////
38 struct VoidPacket : public senf::PacketTypeBase {};
41 // PacketImpl cannot be tested without relying on PacketInterpreterBase. However these unit-tests
42 // only validate PacketInterpreterBase as far as to ensure that a failure of one test is not due to
43 // an error in PacketInterpreterbase
45 BOOST_AUTO_UNIT_TEST(packetImpl_mem)
47 senf::detail::PacketImpl * p (new senf::detail::PacketImpl());
48 BOOST_CHECK_EQUAL(p->refcount(), 0);
50 BOOST_CHECK_EQUAL(p->refcount(), 1);
53 senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 1u);
55 // From now on, the object should stay alive since I manually incremented the
60 BOOST_CHECK_EQUAL(p->refcount(), 3);
62 BOOST_CHECK_EQUAL(p->refcount(), 1);
65 senf::PacketInterpreterBase::ptr pi (
66 senf::detail::packet::test::TestDriver::create<VoidPacket>(
67 p,p->begin(),p->end(), senf::PacketInterpreterBase::Append));
68 // Hmm ... this check works as long as sizeof(PacketInterpreterBase> !=
69 // sizeof(PacketImpl) ... !!
72 senf::pool_alloc_mixin< senf::PacketInterpreter<VoidPacket> >::allocCounter(), 1u);
74 senf::PacketInterpreterBase::ptr pi2 (pi);
75 BOOST_CHECK_EQUAL(p->refcount(), 3);
77 BOOST_CHECK_EQUAL(p->refcount(),1);
80 senf::PacketInterpreterBase::ptr pi (p->first());
81 BOOST_CHECK_EQUAL(p->refcount(),2);
82 p->truncateInterpreters(pi.get());
83 BOOST_CHECK_EQUAL(p->refcount(),1);
87 senf::pool_alloc_mixin<senf::PacketInterpreterBase>::allocCounter(), 0u);
89 BOOST_CHECK_EQUAL(p->refcount(),1);
92 // The refcount must be one here (from incrementing the refcount above)
93 // Therefore we can safely delete the object.
94 BOOST_CHECK_EQUAL(p->refcount(), 1);
98 senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 0u);
102 BOOST_AUTO_UNIT_TEST(packetImpl_data)
104 senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create());
105 senf::detail::PacketImpl * p (senf::detail::packet::test::TestDriver::impl(pi));
107 senf::detail::PacketImpl::byte data[] =
108 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
110 p->insert(&pi->data(),p->begin(),data, data+sizeof(data));
111 BOOST_CHECK_EQUAL(p->size(), 8u);
112 BOOST_CHECK_EQUAL(p->begin()[0], 0x00u);
113 BOOST_CHECK_EQUAL(p->begin()[7], 0x07u);
114 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
116 p->insert(&pi->data(),p->begin()+2,0xf0u);
117 BOOST_CHECK_EQUAL(p->size(),9u);
118 BOOST_CHECK_EQUAL(p->begin()[8], 0x07u);
119 BOOST_CHECK_EQUAL(p->begin()[2], 0xf0u);
120 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
122 p->insert(&pi->data(),p->begin()+9,8,0xffu);
123 BOOST_CHECK_EQUAL(p->size(),17u);
124 BOOST_CHECK_EQUAL(p->begin()[16], 0xffu);
125 BOOST_CHECK_EQUAL(p->begin()[8], 0x07u);
126 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
128 p->erase(&pi->data(),p->begin());
129 BOOST_CHECK_EQUAL(p->size(),16u);
130 BOOST_CHECK_EQUAL(p->begin()[0], 0x01u);
131 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
133 p->erase(&pi->data(),p->begin()+2, p->begin()+7);
134 BOOST_CHECK_EQUAL(p->size(),11u);
135 BOOST_CHECK_EQUAL(p->begin()[2], 0x07u);
136 BOOST_CHECK_EQUAL(p->begin()[3], 0xffu);
137 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
139 BOOST_REQUIRE_EQUAL(pi->data().size(), p->size());
140 BOOST_REQUIRE(pi->data().begin() == p->begin());
142 p->clear(&pi->data());
143 BOOST_CHECK_EQUAL(p->size(), 0u);
144 BOOST_CHECK_EQUAL(pi->data().size(), 0u);
145 BOOST_CHECK(pi->data().begin() == p->begin());
148 BOOST_AUTO_UNIT_TEST(packetImpl_interpreters)
150 senf::detail::PacketImpl * p (new senf::detail::PacketImpl());
154 senf::PacketInterpreterBase::ptr pi2 (
155 senf::detail::packet::test::TestDriver::create<VoidPacket>(
156 p,p->begin(),p->end(),senf::PacketInterpreterBase::Append));
157 senf::PacketInterpreterBase::ptr pi3 (
158 senf::detail::packet::test::TestDriver::create<VoidPacket>(
159 p,p->end(),p->end(),senf::PacketInterpreterBase::Append));
160 senf::PacketInterpreterBase::ptr pi1 (
161 senf::detail::packet::test::TestDriver::create<VoidPacket>(
162 p,p->begin(),p->end(),senf::PacketInterpreterBase::Prepend));
164 BOOST_CHECK_EQUAL(p->first(), pi1.get());
165 BOOST_CHECK_EQUAL(p->next(p->first()), pi2.get());
166 BOOST_CHECK_EQUAL(p->next(p->next(p->first())), pi3.get());
167 BOOST_CHECK( !p->next(p->next(p->next(p->first()))) );
169 BOOST_CHECK_EQUAL(p->last(), pi3.get());
170 BOOST_CHECK_EQUAL(p->prev(p->last()), pi2.get());
171 BOOST_CHECK_EQUAL(p->prev(p->prev(p->last())), pi1.get());
172 BOOST_CHECK( !p->prev(p->prev(p->prev(p->last()))) );
174 p->insert(&pi2->data(),p->begin(),10,0x00u);
175 BOOST_CHECK_EQUAL(pi1->data().size(), 10u);
176 BOOST_CHECK_EQUAL(pi2->data().size(), 10u);
177 BOOST_CHECK_EQUAL(pi3->data().size(), 0u);
178 BOOST_CHECK( pi1->data().begin() == p->begin() );
179 BOOST_CHECK( pi2->data().begin() == p->begin() );
180 BOOST_CHECK( pi3->data().begin() == p->end() );
182 p->insert(&pi3->data(),p->end(), 0x00u);
183 BOOST_CHECK_EQUAL(pi1->data().size(), 11u);
184 BOOST_CHECK_EQUAL(pi2->data().size(), 11u);
185 BOOST_CHECK_EQUAL(pi3->data().size(), 1u);
187 p->insert(&pi1->data(),p->end(), 2, 0x00u);
188 BOOST_CHECK_EQUAL(pi1->data().size(), 13u);
189 BOOST_CHECK_EQUAL(pi2->data().size(), 11u);
190 BOOST_CHECK_EQUAL(pi3->data().size(), 1u);
191 BOOST_CHECK( pi1->data().end() == p->begin()+13u );
192 BOOST_CHECK( pi2->data().end() == p->begin()+11u );
193 BOOST_CHECK( pi3->data().end() == p->begin()+11u );
195 p->clear(&pi2->data());
196 BOOST_CHECK_EQUAL(pi1->data().size(), 2u);
197 BOOST_CHECK( ! p->next(p->next(p->first())) );
200 BOOST_CHECK_EQUAL(p->refcount(), 1);
204 senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 0u);
208 ///////////////////////////////cc.e////////////////////////////////////////
215 // c-file-style: "senf"
216 // indent-tabs-mode: nil
217 // ispell-local-dictionary: "american"
218 // compile-command: "scons -u test"
219 // comment-column: 40