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 PacketImpl unit tests */
31 //#include "PacketImpl.test.hh"
32 //#include "PacketImpl.test.ih"
36 #include "main.test.hh"
39 #include <senf/Utils/auto_unit_test.hh>
40 #include <boost/test/test_tools.hpp>
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46 struct VoidPacket : public senf::PacketTypeBase {};
49 // PacketImpl cannot be tested without relying on PacketInterpreterBase. However these unit-tests
50 // only validate PacketInterpreterBase as far as to ensure that a failure of one test is not due to
51 // an error in PacketInterpreterbase
53 SENF_AUTO_UNIT_TEST(packetImpl_mem)
55 senf::detail::PacketImpl * p (new senf::detail::PacketImpl());
56 BOOST_CHECK_EQUAL(p->refcount(), 0);
58 BOOST_CHECK_EQUAL(p->refcount(), 1);
61 senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 1u);
63 // From now on, the object should stay alive since I manually incremented the
68 BOOST_CHECK_EQUAL(p->refcount(), 2);
70 BOOST_CHECK_EQUAL(p->refcount(), 1);
73 senf::PacketInterpreterBase::ptr pi (
74 senf::detail::packet::test::TestDriver::create<VoidPacket>(
75 p,p->begin(),p->end(), senf::PacketInterpreterBase::Append));
76 // Hmm ... this check works as long as sizeof(PacketInterpreterBase> !=
77 // sizeof(PacketImpl) ... !!
80 senf::pool_alloc_mixin< senf::PacketInterpreter<VoidPacket> >::allocCounter(), 1u);
82 senf::PacketInterpreterBase::ptr pi2 (pi);
83 BOOST_CHECK_EQUAL(p->refcount(), 2);
85 BOOST_CHECK_EQUAL(p->refcount(),1);
88 senf::PacketInterpreterBase::ptr pi (p->first());
89 BOOST_CHECK_EQUAL(p->refcount(),2);
90 p->truncateInterpreters(pi.get());
91 BOOST_CHECK_EQUAL(p->refcount(),1);
95 senf::pool_alloc_mixin<senf::PacketInterpreterBase>::allocCounter(), 0u);
97 BOOST_CHECK_EQUAL(p->refcount(),1);
100 // The refcount must be one here (from incrementing the refcount above)
101 // Therefore we can safely delete the object.
102 BOOST_CHECK_EQUAL(p->refcount(), 1);
106 senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 0u);
110 SENF_AUTO_UNIT_TEST(packetImpl_data)
112 senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create());
113 senf::detail::PacketImpl * p (senf::detail::packet::test::TestDriver::impl(pi));
115 senf::detail::PacketImpl::byte data[] =
116 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
118 p->insert(&pi->data(),p->begin(),data, data+sizeof(data));
119 BOOST_CHECK_EQUAL(p->size(), 8u);
120 BOOST_CHECK_EQUAL(p->begin()[0], 0x00u);
121 BOOST_CHECK_EQUAL(p->begin()[7], 0x07u);
122 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
124 p->insert(&pi->data(),p->begin()+2,0xf0u);
125 BOOST_CHECK_EQUAL(p->size(),9u);
126 BOOST_CHECK_EQUAL(p->begin()[8], 0x07u);
127 BOOST_CHECK_EQUAL(p->begin()[2], 0xf0u);
128 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
130 p->insert(&pi->data(),p->begin()+9,8,0xffu);
131 BOOST_CHECK_EQUAL(p->size(),17u);
132 BOOST_CHECK_EQUAL(p->begin()[16], 0xffu);
133 BOOST_CHECK_EQUAL(p->begin()[8], 0x07u);
134 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
136 p->erase(&pi->data(),p->begin());
137 BOOST_CHECK_EQUAL(p->size(),16u);
138 BOOST_CHECK_EQUAL(p->begin()[0], 0x01u);
139 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
141 p->erase(&pi->data(),p->begin()+2, p->begin()+7);
142 BOOST_CHECK_EQUAL(p->size(),11u);
143 BOOST_CHECK_EQUAL(p->begin()[2], 0x07u);
144 BOOST_CHECK_EQUAL(p->begin()[3], 0xffu);
145 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
147 BOOST_REQUIRE_EQUAL(pi->data().size(), p->size());
148 BOOST_REQUIRE(pi->data().begin() == p->begin());
150 p->clear(&pi->data());
151 BOOST_CHECK_EQUAL(p->size(), 0u);
152 BOOST_CHECK_EQUAL(pi->data().size(), 0u);
153 BOOST_CHECK(pi->data().begin() == p->begin());
156 SENF_AUTO_UNIT_TEST(packetImpl_interpreters)
158 senf::detail::PacketImpl * p (new senf::detail::PacketImpl());
162 senf::PacketInterpreterBase::ptr pi3 (
163 senf::detail::packet::test::TestDriver::create<VoidPacket>(
164 p,p->begin(),p->end(),senf::PacketInterpreterBase::Append));
165 senf::PacketInterpreterBase::ptr pi4 (
166 senf::detail::packet::test::TestDriver::create<VoidPacket>(
167 p,p->end(),p->end(),senf::PacketInterpreterBase::Append));
168 senf::PacketInterpreterBase::ptr pi1 (
169 senf::detail::packet::test::TestDriver::create<VoidPacket>(
170 p,p->begin(),p->end(),senf::PacketInterpreterBase::Prepend));
171 senf::PacketInterpreterBase::ptr pi2 (
172 senf::detail::packet::test::TestDriver::create<VoidPacket>(
173 p,p->begin(),p->end(),pi3));
175 BOOST_CHECK_EQUAL(p->first(), pi1.get());
176 BOOST_CHECK_EQUAL(p->next(p->first()), pi2.get());
177 BOOST_CHECK_EQUAL(p->next(p->next(p->first())), pi3.get());
178 BOOST_CHECK_EQUAL(p->next(p->next(p->next(p->first()))), pi4.get());
179 BOOST_CHECK( !p->next(p->next(p->next(p->next(p->first())))) );
181 BOOST_CHECK_EQUAL(p->last(), pi4.get());
182 BOOST_CHECK_EQUAL(p->prev(p->last()), pi3.get());
183 BOOST_CHECK_EQUAL(p->prev(p->prev(p->last())), pi2.get());
184 BOOST_CHECK_EQUAL(p->prev(p->prev(p->prev(p->last()))), pi1.get());
185 BOOST_CHECK( !p->prev(p->prev(p->prev(p->prev(p->last())))) );
187 p->insert(&pi2->data(),p->begin(),10,0x00u);
188 BOOST_CHECK_EQUAL(pi1->data().size(), 10u);
189 BOOST_CHECK_EQUAL(pi2->data().size(), 10u);
190 BOOST_CHECK_EQUAL(pi3->data().size(), 0u);
191 BOOST_CHECK( pi1->data().begin() == p->begin() );
192 BOOST_CHECK( pi2->data().begin() == p->begin() );
193 BOOST_CHECK( pi3->data().begin() == p->end() );
195 p->insert(&pi3->data(),p->end(), 0x00u);
196 BOOST_CHECK_EQUAL(pi1->data().size(), 11u);
197 BOOST_CHECK_EQUAL(pi2->data().size(), 11u);
198 BOOST_CHECK_EQUAL(pi3->data().size(), 1u);
200 p->insert(&pi1->data(),p->end(), 2, 0x00u);
201 BOOST_CHECK_EQUAL(pi1->data().size(), 13u);
202 BOOST_CHECK_EQUAL(pi2->data().size(), 11u);
203 BOOST_CHECK_EQUAL(pi3->data().size(), 1u);
204 BOOST_CHECK( pi1->data().end() == p->begin()+13u );
205 BOOST_CHECK( pi2->data().end() == p->begin()+11u );
206 BOOST_CHECK( pi3->data().end() == p->begin()+11u );
208 p->clear(&pi2->data());
209 BOOST_CHECK_EQUAL(pi1->data().size(), 2u);
210 BOOST_CHECK( ! p->next(p->next(p->first())) );
213 BOOST_CHECK_EQUAL(p->refcount(), 1);
217 senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 0u);
221 //-/////////////////////////////////////////////////////////////////////////////////////////////////
228 // c-file-style: "senf"
229 // indent-tabs-mode: nil
230 // ispell-local-dictionary: "american"
231 // compile-command: "scons -u test"
232 // comment-column: 40