// Custom includes
#include <sstream>
#include <boost/static_assert.hpp>
+#include <boost/cstdint.hpp>
#include "Packets.hh"
#include <senf/Utils/auto_unit_test.hh>
typedef unsigned key_t;
};
- struct FooPacketType
+ struct FooPacketType
: public senf::PacketTypeBase,
public senf::PacketTypeMixin<FooPacketType>
{
// mode. Otherwise, mixin::nextPacketRange() would query the parser for it's size to find
// the header size. Since the parser is VoidPacketParser, the header size would therefore be
// 0
- static size_type initHeadSize()
+ static size_type initHeadSize()
{ return initSize(); }
};
typedef senf::ConcretePacket<FooPacketType> FooPacket;
struct BarPacketParser : public senf::PacketParserBase
{
# include SENF_FIXED_PARSER()
-
+
SENF_PARSER_FIELD( type, senf::UInt16Parser );
SENF_PARSER_FIELD( length, senf::Int32Parser );
SENF_PARSER_FIELD( reserved, senf::UInt16Parser );
SENF_PARSER_FINALIZE(BarPacketParser);
};
- struct BarPacketType
+ struct BarPacketType
: public senf::PacketTypeBase,
public senf::PacketTypeMixin<BarPacketType,RegTag>
{
using mixin::init;
static void dump(packet p, std::ostream & os) {
os << "BarPacket:\n"
- << " type: " << p->type() << "\n"
- << " length: " << p->length() << "\n";
+ << senf::fieldName("type") << p->type() << "\n"
+ << senf::fieldName("length") << p->length() << "\n";
}
static void finalize(packet p) {
if (p.next(senf::nothrow))
typedef BarPacketType::packet BarPacket;
namespace reg {
- senf::PacketRegistry<RegTag>::RegistrationProxy<FooPacket> registerFoo(1u);
- senf::PacketRegistry<RegTag>::RegistrationProxy<BarPacket> registerBar(2u);
+ senf::PacketRegistry<RegTag>::ScopedRegistrationProxy<FooPacket> registerFoo(1u);
+ senf::PacketRegistry<RegTag>::ScopedRegistrationProxy<BarPacket> registerBar(2u);
}
struct IntAnnotation {
- unsigned value;
+ boost::uint32_t value;
};
std::ostream & operator<<(std::ostream & os, IntAnnotation const & v)
{ os << v.value; return os; }
-
+
struct LargeAnnotation {
char value[32];
};
struct ComplexAnnotation : senf::ComplexAnnotation
{
- ComplexAnnotation() : s(), i() {}
+ ComplexAnnotation() : s("empty"), i(-1) {}
std::string s;
- int i;
+ boost::int32_t i;
+ // padding so the size does not depend on the platform ...
+ struct _ {std::string s;boost::int32_t i;};
+ char __ [32-sizeof(_)];
};
std::ostream & operator<<(std::ostream & os, ComplexAnnotation const & v)
SENF_AUTO_UNIT_TEST(packet)
{
- senf::Packet packet (FooPacket::create());
+ BOOST_CHECK(! senf::Packet().is<BarPacket>() );
+ senf::Packet packet (FooPacket::create());
BarPacket::createAfter(packet);
BOOST_REQUIRE( packet );
BOOST_CHECK( packet.next() );
BOOST_CHECK( ! packet.next().next(senf::nothrow) );
+ BOOST_CHECK( ! packet.next().next(senf::nothrow).is<BarPacket>() );
BOOST_CHECK( ! packet.prev(senf::nothrow) );
BOOST_CHECK( packet.next().prev() == packet );
SENF_CHECK_NOT_EQUAL( packet.next(), packet );
BOOST_CHECK( packet.next().is<BarPacket>() );
BOOST_CHECK( packet.first() == packet );
BOOST_CHECK( packet.last() == packet.next() );
-
+
senf::Packet p2 (packet.next());
BOOST_CHECK( p2 );
packet.parseNextAs<FooPacket>();
BOOST_CHECK( packet.next().is<FooPacket>() );
BOOST_CHECK( ! p2 );
BOOST_CHECK( packet.next().as<FooPacket>() );
-
+
p2 = packet.next().clone();
BOOST_REQUIRE( p2 );
packet.next().append( p2 );
BOOST_CHECK_EQUAL( senf::PacketRegistry<RegTag>::key(packet), 1u );
packet.next().parseNextAs( senf::PacketRegistry<RegTag>::lookup(2u).factory() );
BOOST_CHECK( packet.next().next().is<BarPacket>() );
-
+
std::stringstream s;
packet.dump(s);
- BOOST_CHECK_EQUAL( s.str(),
+ BOOST_CHECK_EQUAL( s.str(),
"Annotations:\n"
- " (anonymous namespace)::ComplexAnnotation: no value\n"
- " (anonymous namespace)::IntAnnotation: 0\n"
+ " (anonymous namespace)::IntAnnotation : 0\n"
"BarPacket:\n"
- " type: 0\n"
- " length: 0\n" );
-
+ " type : 0\n"
+ " length : 0\n" );
+
packet.finalizeAll();
- BOOST_CHECK_EQUAL( packet.last().as<BarPacket>()->type(),
+ BOOST_CHECK_EQUAL( packet.last().as<BarPacket>()->type(),
BarPacket::Parser::type_t::value_type(-1) );
packet.last().append(FooPacket::create());
packet.finalizeThis();
BOOST_CHECK( packet.last().rfind<FooPacket>() == packet.last() );
BOOST_CHECK( packet.next<BarPacket>() == packet.next() );
BOOST_CHECK( packet.last().prev().prev<FooPacket>() == packet );
-
+
senf::DataPacket::createAfter(packet);
BOOST_CHECK_THROW( packet.next().next().next().parseNextAs<BarPacket>(),
senf::InvalidPacketChainException );
// No 'u' suffix here to check, that the disable_if works ...
BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,10).size(), 10u );
BOOST_CHECK_EQUAL( packet.size(), 14u );
-
+
BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,2u,senf::noinit).size(), 2u );
BOOST_CHECK_EQUAL( packet.size(), 6u );
-
+
BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,data).size(), 6u );
BOOST_CHECK_EQUAL( packet.size(), 10u );
-
+
BOOST_CHECK_EQUAL( FooPacket::createBefore(packet).size(), 14u );
BOOST_CHECK_EQUAL( packet.size(), 10u );
bar2->type() << 0x2A2Bu;
bar1.parser() << bar2;
-
+
BOOST_CHECK_EQUAL( bar1->type(), 0x2A2Bu );
}
SENF_AUTO_UNIT_TEST(packetAnnotation)
{
+ typedef senf::detail::AnnotationRegistry Reg;
+
senf::Packet packet (FooPacket::create());
BarPacket::createAfter(packet);
ComplexAnnotation & ca (packet.annotation<ComplexAnnotation>());
+
+ BOOST_CHECK_EQUAL( ca.s, "empty" );
+ BOOST_CHECK_EQUAL( ca.i, -1 );
+
ca.s = "dead beef";
ca.i = 0x12345678;
SENF_CHECK_NO_THROW( packet.annotation<IntAnnotation>().value = 0xDEADBEEF );
BOOST_CHECK_EQUAL( p2.annotation<ComplexAnnotation>().s, "dead beef" );
BOOST_CHECK_EQUAL( p2.annotation<ComplexAnnotation>().i, 0x12345678 );
- BOOST_CHECK( senf::detail::AnnotationIndexer<IntAnnotation>::Small );
- BOOST_CHECK( ! senf::detail::AnnotationIndexer<LargeAnnotation>::Small );
- BOOST_CHECK( ! senf::detail::AnnotationIndexer<ComplexAnnotation>::Small );
- BOOST_CHECK( ! senf::detail::AnnotationIndexer<ComplexEmptyAnnotation>::Small );
+ BOOST_CHECK( Reg::lookup<IntAnnotation>() >= 0 );
+ BOOST_CHECK( Reg::lookup<LargeAnnotation>() < 0 );
+ BOOST_CHECK( Reg::lookup<ComplexAnnotation>() < 0 );
+ BOOST_CHECK( Reg::lookup<ComplexEmptyAnnotation>() < 0 );
+
+ std::stringstream ss;
+ senf::dumpPacketAnnotationRegistry(ss);
+ BOOST_CHECK_EQUAL(
+ ss.str(),
+ "SENF_PACKET_ANNOTATION_SLOTS = 8\n"
+ "SENF_PACKET_ANNOTATION_SLOTSIZE = 16\n"
+ "TYPE FAST COMPLEX SIZE\n"
+ "(anonymous namespace)::ComplexAnnotation no yes 32\n"
+ "(anonymous namespace)::ComplexEmptyAnnotation no yes 1\n"
+ "(anonymous namespace)::IntAnnotation yes no 4\n"
+ "(anonymous namespace)::LargeAnnotation no no 32\n" );
}
#ifdef COMPILE_CHECK