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 <boost/test/auto_unit_test.hpp>
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);
52 senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 1u);
53 // From now on, the object should stay alive since I manually incremented the
58 BOOST_CHECK_EQUAL(p->refcount(), 3);
60 BOOST_CHECK_EQUAL(p->refcount(), 1);
63 senf::PacketInterpreterBase::ptr pi (
64 senf::detail::packet::test::TestDriver::create<VoidPacket>(
65 p,p->begin(),p->end(), senf::PacketInterpreterBase::Append));
66 // Hmm ... this check works as long as sizeof(PacketInterpreterBase> !=
67 // sizeof(PacketImpl) ... !!
69 senf::pool_alloc_mixin< senf::PacketInterpreter<VoidPacket> >::allocCounter(), 1u);
70 senf::PacketInterpreterBase::ptr pi2 (pi);
71 BOOST_CHECK_EQUAL(p->refcount(), 3);
73 BOOST_CHECK_EQUAL(p->refcount(),1);
76 senf::PacketInterpreterBase::ptr pi (p->first());
77 BOOST_CHECK_EQUAL(p->refcount(),2);
78 p->truncateInterpreters(pi.get());
79 BOOST_CHECK_EQUAL(p->refcount(),1);
82 senf::pool_alloc_mixin<senf::PacketInterpreterBase>::allocCounter(), 0u);
83 BOOST_CHECK_EQUAL(p->refcount(),1);
86 // The refcount must be one here (from incrementing the refcount above)
87 // Therefore we can safely delete the object.
88 BOOST_CHECK_EQUAL(p->refcount(), 1);
91 senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 0u);
94 BOOST_AUTO_UNIT_TEST(packetImpl_data)
96 senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create());
97 senf::detail::PacketImpl * p (senf::detail::packet::test::TestDriver::impl(pi));
99 senf::detail::PacketImpl::byte data[] =
100 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
102 p->insert(&pi->data(),p->begin(),data, data+sizeof(data));
103 BOOST_CHECK_EQUAL(p->size(), 8u);
104 BOOST_CHECK_EQUAL(p->begin()[0], 0x00u);
105 BOOST_CHECK_EQUAL(p->begin()[7], 0x07u);
106 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
108 p->insert(&pi->data(),p->begin()+2,0xf0u);
109 BOOST_CHECK_EQUAL(p->size(),9u);
110 BOOST_CHECK_EQUAL(p->begin()[8], 0x07u);
111 BOOST_CHECK_EQUAL(p->begin()[2], 0xf0u);
112 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
114 p->insert(&pi->data(),p->begin()+9,8,0xffu);
115 BOOST_CHECK_EQUAL(p->size(),17u);
116 BOOST_CHECK_EQUAL(p->begin()[16], 0xffu);
117 BOOST_CHECK_EQUAL(p->begin()[8], 0x07u);
118 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
120 p->erase(&pi->data(),p->begin());
121 BOOST_CHECK_EQUAL(p->size(),16u);
122 BOOST_CHECK_EQUAL(p->begin()[0], 0x01u);
123 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
125 p->erase(&pi->data(),p->begin()+2, p->begin()+7);
126 BOOST_CHECK_EQUAL(p->size(),11u);
127 BOOST_CHECK_EQUAL(p->begin()[2], 0x07u);
128 BOOST_CHECK_EQUAL(p->begin()[3], 0xffu);
129 BOOST_CHECK_EQUAL(pi->data().size(), p->size());
131 BOOST_REQUIRE_EQUAL(pi->data().size(), p->size());
132 BOOST_REQUIRE(pi->data().begin() == p->begin());
134 p->clear(&pi->data());
135 BOOST_CHECK_EQUAL(p->size(), 0u);
136 BOOST_CHECK_EQUAL(pi->data().size(), 0u);
137 BOOST_CHECK(pi->data().begin() == p->begin());
140 BOOST_AUTO_UNIT_TEST(packetImpl_interpreters)
142 senf::detail::PacketImpl * p (new senf::detail::PacketImpl());
146 senf::PacketInterpreterBase::ptr pi2 (
147 senf::detail::packet::test::TestDriver::create<VoidPacket>(
148 p,p->begin(),p->end(),senf::PacketInterpreterBase::Append));
149 senf::PacketInterpreterBase::ptr pi3 (
150 senf::detail::packet::test::TestDriver::create<VoidPacket>(
151 p,p->end(),p->end(),senf::PacketInterpreterBase::Append));
152 senf::PacketInterpreterBase::ptr pi1 (
153 senf::detail::packet::test::TestDriver::create<VoidPacket>(
154 p,p->begin(),p->end(),senf::PacketInterpreterBase::Prepend));
156 BOOST_CHECK_EQUAL(p->first(), pi1.get());
157 BOOST_CHECK_EQUAL(p->next(p->first()), pi2.get());
158 BOOST_CHECK_EQUAL(p->next(p->next(p->first())), pi3.get());
159 BOOST_CHECK( !p->next(p->next(p->next(p->first()))) );
161 BOOST_CHECK_EQUAL(p->last(), pi3.get());
162 BOOST_CHECK_EQUAL(p->prev(p->last()), pi2.get());
163 BOOST_CHECK_EQUAL(p->prev(p->prev(p->last())), pi1.get());
164 BOOST_CHECK( !p->prev(p->prev(p->prev(p->last()))) );
166 p->insert(&pi2->data(),p->begin(),10,0x00u);
167 BOOST_CHECK_EQUAL(pi1->data().size(), 10u);
168 BOOST_CHECK_EQUAL(pi2->data().size(), 10u);
169 BOOST_CHECK_EQUAL(pi3->data().size(), 0u);
170 BOOST_CHECK( pi1->data().begin() == p->begin() );
171 BOOST_CHECK( pi2->data().begin() == p->begin() );
172 BOOST_CHECK( pi3->data().begin() == p->end() );
174 p->insert(&pi3->data(),p->end(), 0x00u);
175 BOOST_CHECK_EQUAL(pi1->data().size(), 11u);
176 BOOST_CHECK_EQUAL(pi2->data().size(), 11u);
177 BOOST_CHECK_EQUAL(pi3->data().size(), 1u);
179 p->insert(&pi1->data(),p->end(), 2, 0x00u);
180 BOOST_CHECK_EQUAL(pi1->data().size(), 13u);
181 BOOST_CHECK_EQUAL(pi2->data().size(), 11u);
182 BOOST_CHECK_EQUAL(pi3->data().size(), 1u);
183 BOOST_CHECK( pi1->data().end() == p->begin()+13u );
184 BOOST_CHECK( pi2->data().end() == p->begin()+11u );
185 BOOST_CHECK( pi3->data().end() == p->begin()+11u );
187 p->clear(&pi2->data());
188 BOOST_CHECK_EQUAL(pi1->data().size(), 2u);
189 BOOST_CHECK( ! p->next(p->next(p->first())) );
192 BOOST_CHECK_EQUAL(p->refcount(), 1);
195 senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 0u);
198 ///////////////////////////////cc.e////////////////////////////////////////
205 // c-file-style: "senf"
206 // indent-tabs-mode: nil
207 // ispell-local-dictionary: "american"
208 // compile-command: "scons -u test"
209 // comment-column: 40